Python中shapefile转换geojson的示例

yipeiwu_com5年前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) + "\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实现植物大战僵尸游戏实例代码

python实现植物大战僵尸游戏实例代码

开发思路 完整项目地址:https://github.com/371854496/... 觉得还OK的话,点下Star,作者不易,thank you! 实现方法 1.引入需要的模...

浅谈python为什么不需要三目运算符和switch

对于三目运算符(ternary operator),python可以用conditional expressions来替代 如对于x<5?1:0可以用下面的方式来实现...

举例详解Python中yield生成器的用法

yield是生成的意思,但是在python中则是作为生成器理解,生成器的用处主要可以迭代,这样简化了很多运算模型(还不是很了解是如何简化的)。 yield是一个表达式,是有返回值的. 当...

python常用函数与用法示例

本文实例讲述了python常用函数与用法。分享给大家供大家参考,具体如下: 自定义函数实例 # 定义一个函数 def printme( str ): "打印任何传入的字符串"...

django 在原有表格添加或删除字段的实例

一、如果models.py文件为时: timestamp = models.DateTimeField('保存日期') 会提示: (env8) D:\Desktop\env8\...