当前位置: 移动技术网 > IT编程>脚本编程>Python > Python中shapefile转换geojson的示例

Python中shapefile转换geojson的示例

2019年01月07日  | 移动技术网IT编程  | 我要评论

旷世奇材,chengrendianshi,天天向上达贝妮

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

总结

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网