使用Python

时间:2017-05-18 13:52:23

标签: python geojson shapefile

我正在尝试将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"

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

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

由于我很新,我尝试了我在网络上找到的解决方案。有没有办法使这项工作?

干杯,

1 个答案:

答案 0 :(得分:6)

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)

编辑:在回复评论时 - 是的,pickle不适合在这种情况下写入文件。