使用Python将Geojson转换为shapefile

使用Python将Geojson转换为shapefile

本文介绍了使用Python将Geojson转换为shapefile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将geojson文件转换为shapefile.我正在尝试这种方式(我是Python的新手,所以可能不正确.)

I'm trying to convert a geojson file into a shapefile.I'm trying this way (I'm very new to Python so it might be incorrect).

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时,出现此错误:

So I'm getting the data from an url, put it in a file and when I try to convert it into a shapefile I got this error:

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

由于我是个新手,所以尝试了在网上找到的解决方案.有什么办法可以使这项工作完成吗?

As I'm quite new I tried the solutions that I found on the web. Is there any way of making this work?

推荐答案

ogr2ogr似乎是一个命令行程序-要使用此程序,您可能需要研究类似 subprocess.Popen() :

ogr2ogr appears to be a command line program - to use this you might want to look into something like 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 不是写文件的适当方法.

In response to comments - yes, pickle is not the appropriate way to go about writing to the file in this case.

这篇关于使用Python将Geojson转换为shapefile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 05:51