温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python中shapefile转换geojson的示例

发布时间:2020-09-02 15:49:16 来源:脚本之家 阅读:242 作者:staHuri 栏目:开发技术

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

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对亿速云的支持。如果你想了解更多相关内容请查看下面相关链接

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI