【发布时间】:2017-12-09 12:04:51
【问题描述】:
我基本上想加载一个 geojson 文件,就像你在 QGIS 或 Leaflet 中一样,但在 matplotlib 之类的东西中。我想这样做,这样我就可以加载大量(确切地说是 6000 个 geojson 文件),更改一些样式,然后将它们导出为图像。
我已经关注了其他链接,例如 this one...但是当我尝试在此方法中设置“边界”时遇到 KeyErrors。
def setPlotExtent(ax, data):
# get feature extents (a property of the cloudmade geojson)
# note this was previously bbox
minx = data['bounds'][0][0]
maxx = data['bounds'][1][0]
miny = data['bounds'][0][1]
maxy = data['bounds'][1][1]
# set the graph axes to the feature extents
ax.set_xlim(minx, maxx)
ax.set_ylim(miny, maxy)
我已经尝试在 QGIS 中执行此操作,但没有太大成功,我曾考虑尝试下载 ArcGIS 试用版并执行此操作,但使用 matplot lib 执行此操作将是最简单的。
在伪代码中,我想做的只是......
for geoJsonFile in geoJsonDir
loadIntoMatPlotLib(geoJsonFile)
changeSomeFeatures()
exportAsPNG()
我的完整代码在这里...(这几乎就是其他堆栈问题中的内容)
from matplotlib import pyplot
from descartes import PolygonPatch
import simplejson
def configurePlot():
# set up the mapplotlib
fig = pyplot.figure(1, figsize=(10, 4), dpi=180)
ax = fig.add_subplot(121)
return fig, ax
def setPlotExtent(ax, data):
# get feature extents (a property of the cloudmade geojson)
# note this was previously bbox
minx = data['bounds'][0][0]
maxx = data['bounds'][1][0]
miny = data['bounds'][0][1]
maxy = data['bounds'][1][1]
# set the graph axes to the feature extents
ax.set_xlim(minx, maxx)
ax.set_ylim(miny, maxy)
def plotFeature(coordinates, myplot):
poly = {"type": "Polygon", "coordinates": coordinates}
patch = PolygonPatch(poly, fc='#6699cc', ec='#6699cc', alpha=0.5, zorder=2)
# plot it on the graph
myplot.add_patch(patch)
# turn the geojson into a python object
with open(r"Geo_AOI_1_RIO_img163.geojson") as f:
pydata = simplejson.load(f)
print(pydata)
fig, myplot = configurePlot()
setPlotExtent(myplot, pydata)
plotFeature(pydata['coordinates'], myplot)
# save the plot as an image
pyplot.show()
fig.savefig('myplot.png')
【问题讨论】:
标签: python matplotlib gis geojson qgis