使用Python将Geojson转换为shapefile

2024-04-19 08:45:31 发布

您现在位置:Python中文网/ 问答频道 /正文

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

作为一个新手,我尝试了我在网上找到的解决方案。有什么办法可以让这工作吗?

干杯


Tags: 文件urldataresponsegeojsonurllibdestinationfile
1条回答
网友
1楼 · 发布于 2024-04-19 08:45:31

ogr2ogr似乎是一个命令行程序-要使用它,您可能需要查看^{}

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)

编辑:对于comments-yes,pickle不是在这种情况下写入文件的适当方式。

相关问题 更多 >