如何将要显示的geoJson文件加载到Python上的matplotlib中进行显示?

2024-09-27 09:31:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我基本上想加载一个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')

最后,这就是我试图从GeoJSON文件生成的图像。我在QGIS里手工做的。 enter image description here


Tags: 文件thedatadefgeojsonfigaxfeature

热门问题