【问题标题】:How do I load a geoJson file to display into matplotlib on Python to display?如何将 geoJson 文件加载到 Python 上的 matplotlib 中显示?
【发布时间】: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')

最终,这是我试图从 GeoJSON 文件生成的图像。我是在 QGIS 中手动完成的。

【问题讨论】:

    标签: python matplotlib gis geojson qgis


    【解决方案1】:

    这不是这个问题的完整答案,但我最终只能在 QGIS 中执行此操作,并在 PyQGIS 中编写脚本。它在那里很慢并且可以完成工作,但是如果有人知道不使用 QGIS 的方法,请回答!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-25
      • 1970-01-01
      • 2015-02-18
      • 2019-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多