Shapefile 到 geojson 转换 python 3
Shapefile into geojson conversion python 3
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}}**,**
请查看以下图书馆: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')
对于 shapefile 和 geojson 之间的转换,我肯定会使用 geopandas:
import geopandas
myshpfile = geopandas.read_file('myshpfile.shp')
myshpfile.to_file('myJson.geojson', driver='GeoJSON')
添加到@alexisdevarennes。
您现在可以在 1 或两行中使用 PyShp 转换为 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'
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}}**,**
请查看以下图书馆: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')
对于 shapefile 和 geojson 之间的转换,我肯定会使用 geopandas:
import geopandas
myshpfile = geopandas.read_file('myshpfile.shp')
myshpfile.to_file('myJson.geojson', driver='GeoJSON')
添加到@alexisdevarennes。
您现在可以在 1 或两行中使用 PyShp 转换为 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'