我基本上想加载一个geojson文件,就像在QGIS或slaple中一样,但是在matplotlib之类的文件中。我希望这样做,这样我就可以加载一吨(确切地说是6000个geojson文件),更改一些样式,然后将它们导出为图像。在
我已经访问了其他链接,如this one…但是当我试图在这个方法中设置'bounds'时,会遇到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来做是最容易的。在
在伪代码中,我想做的就是。。。在
^{pr2}$我的完整代码在这里…(这几乎就是另一个堆栈问题中的代码)
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')
这并不是这个问题的完整答案,但我最终只能在QGIS中用PyQGIS编写脚本来做到这一点。这是非常缓慢的,在那里完成工作,但如果有人知道不使用QGIS的方法,请回答!在
相关问题 更多 >
编程相关推荐