【问题标题】:Geojson to shapefile using PythonGeojson 到 shapefile 使用 Python
【发布时间】:2017-05-18 13:52:22
【问题描述】:

我正在尝试将 geojson 文件转换为 shapefile。 我正在尝试这种方式(我对 Python 很陌生,所以可能不正确)。

import urllib, geojson, gdal
url= ' http://ig3is.grid.unep.ch/istsos/wa/istsos/services/ghg/procedures/operations/geojson?epsg=4326'
response = urllib.urlopen(url)
data = geojson.loads(response.read())

file = open ('data.geojson', 'w')
pickle.dump(data,file)
file.close()
ogr2ogr -f "ESRI Shapefile" destination_data.shp "data.geojson"

所以我从 url 获取数据,将其放入文件中,当我尝试将其转换为 shapefile 时出现此错误:

 File "<stdin>", line 1
    ogr2ogr -f "ESRI Shapefile" destination_data.shp "data.geojson"
                              ^
SyntaxError: invalid syntax

由于我是新手,我尝试了在网上找到的解决方案。有什么办法可以使这项工作?

【问题讨论】:

    标签: python geojson shapefile


    【解决方案1】:

    ogr2ogr 似乎是一个命令行程序 - 要使用它,您可能需要查看类似 subprocess.Popen():

    import urllib, geojson, gdal, subprocess
    url= ' http://ig3is.grid.unep.ch/istsos/wa/istsos/services/ghg/procedures/operations/geojson?epsg=4326'
    response = urllib.urlopen(url)
    data = geojson.loads(response.read())
    
    with open('data.geojson', 'w') as f:
        geojson.dump(data, f)
    
    args = ['ogr2ogr', '-f', 'ESRI Shapefile', 'destination_data.shp', 'data.geojson']
    subprocess.Popen(args)
    

    编辑:响应 cmets - 是的,pickle 在这种情况下不是写入文件的适当方式。

    【讨论】:

    • 当我这样做时,我得到另一个错误 >> 错误 4:无法读取 GeoJSON 数据失败:无法使用以下驱动程序打开数据源“data.geojson”。 -> ESRI Shapefile 老实说,我不太确定我获取geojson 的方式。 file = open ('data.geojson', 'w') pickle.dump(data,file) file.close()
    • @Gus 你写的 - 我已经编辑了我的回复。这似乎确实将geojson数据写入文件,但不幸的是我无法测试ogr2ogr部分。
    • 谢谢,现在可以使用了。泡菜确实不是正确的方法。
    猜你喜欢
    • 2017-08-24
    • 1970-01-01
    • 2018-11-29
    • 2019-09-24
    • 1970-01-01
    • 2020-12-10
    • 2020-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多