Python中shapefile转换geojson的示例

yipeiwu_com6年前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 字段拆分详解

按照固定的字符,拆分已有的字符串 split(sep, n, expand = False) :sep:用于分割的字符串 n:分割为多少列 expand:是否展开为数据框,默认值为Fal...

django-初始配置(纯手写)详解

我们通过django-admin startproject zhuyu命令创建好项目后,在pycharm中打开 我们需要在在该项目中,配置一些相关操作。 1、template(存放模板的...

python中for循环变量作用域及用法详解

在讲这个话题前,首先我们来看一道题: 代码1: def foo(): return [lambda x: x**i for i in range(1,5,2)] print([f...

python进行文件对比的方法

python进行文件对比的方法

文件对比是否一致,我们一般采用md5值对比,假如一样,代表文件一致,不一样说明不一致 假如想要详细的对比信息内容,difflib库提供了文件对比的详细信息和结果 1、首先我们查看下md5...

对django后台admin下拉框进行过滤的实例

使用django admin 自带后台 admin后台下拉显示的时候需要添加过滤条件, 因为表是自己关联自己,同时还需要过滤掉自己, 需要获取当前对象的id,需要获取obj_id f...