扣丁学堂Python培训简述Python中shapefile如何转换geojson

本篇文章扣丁学堂Python培训小编给读者们分享一下Python中shapefile如何转换geojson的,对Python开发技术感兴趣或者是想要参加Python培训学习Python开发技术的小伙伴下面就随小面来看一下Python中shapefile转换geojson的示例吧。

扣丁学堂Python培训简述Python中shapefile如何转换geojson

Python培训

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) + "")

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')

最后想要了解更多关于Python方面内容的小伙伴,请关注扣丁学堂Python培训官网、微信等平台,扣丁学堂IT职业在线学习教育平台为您提供权威的Python开发环境搭建视频,Python培训后的前景无限,行业薪资和未来的发展会越来越好的,扣丁学堂老师精心推出的Python视频直播课定能让你快速掌握Python从入门到精通开发实战技能。

相关推荐