网络编程
位置:首页>> 网络编程>> Python编程>> Python中shapefile转换geojson的示例

Python中shapefile转换geojson的示例

作者:staHuri  发布时间:2023-08-03 19:05:04 

标签:python,shapefile,geojson,转换

shapefile转换geojson


import shapefile
import codecs
from json import dumps
# read the shapefile
def shp2geo(file="line出产.shp"):
 reader = shapefile.Reader(file)
 fields = reader.fields[1:]
 field_names = [field[0] for field in fields]
 buffer = []
 for sr in reader.shapeRecords():
   record = sr.record
   record = [r.decode('gb2312', 'ignore') if isinstance(r, bytes)
        else r for r in record]
   atr = dict(zip(field_names, record))
   geom = sr.shape.__geo_interface__
   buffer.append(dict(type="Feature", geometry=geom, properties=atr))
   # write the GeoJSON file
 geojson = codecs.open(file.split('.')[0] + "-geo.json", "w", encoding="gb2312")
 geojson.write(dumps({"type": "FeatureCollection", "features": buffer}, indent=2) + "\n")
 geojson.close()
if __name__ == '__main__':
 # import os
 # for z,x,c in os.walk('.'):
 #   for zz in c:
 #     if zz.endswith(".shp"):
 #       shp2geo(zz)
 # shp2geo(file='D.shp')
 shp2geo(file='ttttttttttt.shp')

来源:https://blog.csdn.net/staHuri/article/details/80838226

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com