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设计】的支持。如果你想了解更多相关内容请查看下面相关链接

相关文章

python3实现点餐系统

本文实例为大家分享了python3实现点餐系统的具体代码,供大家参考,具体内容如下 题目:     某餐厅外卖每天更新菜品,但是搭配价格是不变的,具体如下: &nbs...

Python使用Matplotlib模块时坐标轴标题中文及各种特殊符号显示方法

Python使用Matplotlib模块时坐标轴标题中文及各种特殊符号显示方法

本文实例讲述了Python使用Matplotlib模块时坐标轴标题中文及各种特殊符号显示方法。分享给大家供大家参考,具体如下: Matplotlib中文显示问题——用例子说明问题 #...

Python 第一步 hello world

首先下载最新版本的python。www.python.org,目前版本为3.1。 接下来是安装,在windows下python的安装与其他应用程序一样,不多复述。 在windows下,即...

python3.7 使用pymssql往sqlserver插入数据的方法

python3.7 使用pymssql往sqlserver插入数据 import pymssql conn = pymssql.connect(host='szs',server='...

Python中的filter()函数的用法

Python内建的filter()函数用于过滤序列。 和map()类似,filter()也接收一个函数和一个序列。和map()不同的时,filter()把传入的函数依次作用于每个元素,然...