簡體   English   中英

使用 Python 將 Geojson 轉換為 shapefile

[英]Geojson to shapefile using 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

由於我很新,我嘗試了我在網上找到的解決方案。 有沒有辦法使這項工作?

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不是寫入文件的合適方式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM