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程序中,用if语句实现: age = 20 if a...

详解python中的文件与目录操作

详解python中的文件与目录操作 一 获得当前路径 1、代码1 >>>import os >>>print('Current directo...

python 经典数字滤波实例

python 经典数字滤波实例

数字滤波分为 IIR 滤波,和FIR 滤波。 FIR 滤波: import scipy.signal as signal import numpy as np import pyla...

Python中偏函数用法示例

本文实例讲述了Python中偏函数用法。分享给大家供大家参考,具体如下: python中偏函数 当一个函数有很多参数时,调用者就需要提供多个参数。如果减少参数个数,就可以简化调用者的负...

pandas将多个dataframe以多个sheet的形式保存到一个excel文件中

要实现这个功能,可能有多种方法,我在这里记录下一个比较方便的方法: import pandas as pd writer = pd.ExcelWriter('test.xlsx')...