我正在尝试将 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
由于我是新手,所以我尝试了在网上找到的解决方案。有什么方法可以使这项工作正常进行吗?
最佳答案
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
不是写入文件的合适方式。
关于python - 使用 Python 将 Geojson 转为 shapefile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44049679/