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

2019-03-01 13:34:44 1081浏览

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



扣丁学堂Python简述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')


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




扣丁学堂微信公众号 


关注微信公众号获取更多学习资料 


 

查看更多关于"Python开发资讯"的相关文章>



标签: Python培训 Python视频教程 Python在线视频 Python学习视频 Python培训班

热门专区

暂无热门资讯

课程推荐

微信
微博
15311698296

全国免费咨询热线

邮箱:codingke@1000phone.com

官方群:148715490

北京千锋互联科技有限公司版权所有   北京市海淀区宝盛北里西区28号中关村智诚科创大厦4层
京ICP备12003911号-6   Copyright © 2013 - 2019

京公网安备 11010802030908号