将Shapefile转换为geojson的Python 3代码

25
output_buffer = []
for features in range(0,layer.GetFeatureCount()):
    feat = layer.GetNextFeature()
    geom = feat.GetGeometryRef()
    result = feat.ExportToJson()
    output_buffer.append(result)

当我转换为GeoJSON时,我得到了输出结果,但只有一个要素被格式化为JSON。

我得到的输出结果如下:

{"geometry": {"coordinates": [488081.726322771, 2360837.62927308], "type": "Point"}, "type": "Feature", "id": 0, "properties": {"EntityHand": null, "Layer": "pipe", "Linetype": null, "Text": "BB_D2", "ExtendedEn": null, "SubClasses": null}}{"geometry": {"coordinates": [487523.119248441, 2361228.95273474], "type": "Point"}, "type": "Feature", "id": 1, "properties": {"EntityHand": null, "Layer": "pipe", "Linetype": null, "Text": "Mil_D2", "ExtendedEn": null, "SubClasses": null}}..................
我想要得到这样的输出:
{"geometry": {"coordinates": [488081.726322771, 2360837.62927308], "type": "Point"}, "type": "Feature", "id": 0, "properties": {"EntityHand": null, "Layer": "pipe", "Linetype": null, "Text": "BB_D2", "ExtendedEn": null, "SubClasses": null}}**,**    
{"geometry": {"coordinates": [487523.119248441, 2361228.95273474], "type": "Point"}, "type": "Feature", "id": 1, "properties": {"EntityHand": null, "Layer": "pipe", "Linetype": null, "Text": "Mil_D2", "ExtendedEn": null, "SubClasses": null}}**,**

请将您的问题格式化为可读的内容... - Mad Physicist
1
如果您提供一个可行的示例,那会非常有帮助。如果无法复制您的问题,那就无法回答它。 - Rutger Kassies
@RutgerKassies。在这种情况下,正确的程序是进行适当的关闭投票。 - Mad Physicist
你正在使用哪些库? - chrki
嗨,Srinuvas Bathula:这个答案已经有很多浏览量了,已经快3年了。由于这是唯一的答案,并且问题和答案都得到了相当多的积极反馈,您介意将其标记为正确吗? :) - alexisdevarennes
是的,这几乎正确!!帮助我解决了我的问题。 - Vas
3个回答

36
请查看以下库:https://pypi.python.org/pypi/pyshp/1.1.7
import shapefile
from json import dumps

# read the shapefile
reader = shapefile.Reader("my.shp")
fields = reader.fields[1:]
field_names = [field[0] for field in fields]
buffer = []
for sr in reader.shapeRecords():
    atr = dict(zip(field_names, sr.record))
    geom = sr.shape.__geo_interface__
    buffer.append(dict(type="Feature", \
    geometry=geom, properties=atr)) 
   
    # write the GeoJSON file
   
geojson = open("pyshp-demo.json", "w")
geojson.write(dumps({"type": "FeatureCollection", "features": buffer}, indent=2) + "\n")
geojson.close()

正如其他答案所述,您可以使用geopandas:

import geopandas

shp_file = geopandas.read_file('myshpfile.shp')
shp_file.to_file('myshpfile.geojson', driver='GeoJSON')

35

对于shapefile和geojson之间的转换,我肯定会使用geopandas:

    import geopandas
    myshpfile = geopandas.read_file('myshpfile.shp')
    myshpfile.to_file('myJson.geojson', driver='GeoJSON')

6

补充@alexisdevarennes的内容。

现在你可以使用PyShp在1或2行中将其转换为geojson:

import shapefile

with shapefile.Reader("shapefile.shp") as shp:
    geojson_data = shp.__geo_interface__

或者
geojson_data = shapefile.Reader("shapefile.shp").__geo_interface__

使用示例:

>>> geojson_data["type"]

'MultiPolygon'

这个方法很好用,但是对于我所做的.shp文件并不适用;相反需要使用.dbf文件。我正在使用人口普查提供的文件:https://www.census.gov/geographies/mapping-files/time-series/geo/carto-boundary-file.2018.html#list-tab-1556094155 - Declan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接