python - 使用 Python 将 Geojson 转为 shapefile

标签 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"

所以我从 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/

相关文章:

python - 将 nltk 绘制的解析树保存到图像文件

python - 我如何在 pymongo 中填充?

javascript - 点击功能的传单

javascript - 在 JavaScript 中使用 osmtogeojson 处理来自 Overpass API 的数据

r - 在 R 中提取形状文件对象的质心?

python - 如何调整多 channel tiff 图像的大小?

python - 将字符串转换为 bash 转义形式

javascript - 使用 d3 通过投影映射 geojson 分布数据

python - 平滑 shapefile 输出 - basemap python

mysql - shp2mysql : Shapefile import to mysql