以编程方式将 GeoJSON 渲染为图像文件

Programmatically render GeoJSON to image file

我有一系列 GeoJSON 对象,我希望以编程方式在地图上呈现这些对象。

我可以使用 http://geojson.io 并上传我的 GeoJSON,但我如何以编程方式执行此操作并导出 PNG 或其他图像文件?

https://github.com/mapbox/geojson.io 看起来很棒,但它公开发布到 geojson.io 网站。

您需要获取 Python 的 MapBox SDK:pip install mapbox

https://github.com/mapbox/mapbox-sdk-py

然后您可以使用如下服务:Static Maps V4(或者 Static Styles V1 看起来也很有趣)

https://www.mapbox.com/api-documentation/pages/static_classic.html

这是他们示例中的代码:https://github.com/mapbox/mapbox-sdk-py/blob/master/docs/static.md#static-maps

main.py

from mapbox import Static

service = Static()

portland = {
    'type': 'Feature',
    'properties': {'name': 'Portland, OR'},
    'geometry': {
        'type': 'Point',
        'coordinates': [-122.7282, 45.5801]}}
response = service.image('mapbox.satellite', features=[portland])

# add to a file
with open('./output_file.png', 'wb') as output:
    _ = output.write(response.content)

运行: export MAPBOX_ACCESS_TOKEN="YOUR_MAP_BOX_TOKEN" && python main.py

上面的代码对我有用,它创建了所提供数据周围区域的 png,如下所示。 features 属性应该接受您的 geojson 对象。

如果您想使用自定义 MapBox 样式,则需要使用 Static Styles V1

https://www.mapbox.com/api-documentation/?language=Python#static

main.py

from mapbox import StaticStyle

service = StaticStyle()

portland = {
    'type': 'Feature',
    'properties': {'name': 'Portland, OR'},
    'geometry': {
        'type': 'Point',
        'coordinates': [-122.7282, 45.5801]}}
response = service.image('YOUR_USERNAME', 'YOUR_STYLE_ID', features=[portland])


# add to a file
with open('./output_file.png', 'wb') as output:
    _ = output.write(response.content)

我还在 GitHub 上创建了一个带有示例函数的存储库: https://github.com/sarcoma/MapBox-Static-Style-Python-Script

geojson-renderer 是一个命令行工具,可在地图图块上呈现 geojson 内容。它可以生成 SVG 或 PNG 作为输出。地图图块的来源是可配置的。要从 Python 使用它,您可以 shell 出来。