使用Python将Geojson转换为shapefile

Gus*_*Gus 4 python shapefile geojson

我正在尝试将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"
Run Code Online (Sandbox Code Playgroud)

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

 File "<stdin>", line 1
    ogr2ogr -f "ESRI Shapefile" destination_data.shp "data.geojson"
                              ^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

因为我还很新,所以尝试了在网上找到的解决方案。有什么办法可以使这项工作吗?

干杯,

G

aso*_*uin 8

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)
Run Code Online (Sandbox Code Playgroud)

编辑:在回应评论-是的,pickle是不恰当的方式去了解在这种情况下写入文件。