Python中shapefile转换geojson的示例
作者:staHuri 时间:2023-08-03 19:05:04
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')
来源:https://blog.csdn.net/staHuri/article/details/80838226
标签:python,shapefile,geojson,转换
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python实现监控linux性能及进程消耗性能的方法
2021-10-20 03:27:56
sqlserver 巧妙的自关联运用
2012-07-21 14:55:12
js动态创建、删除表格示例代码
2024-04-19 09:54:37
Python尾递归优化实现代码及原理详解
2023-11-08 15:35:28
pytorch 实现删除tensor中的指定行列
2023-05-22 14:26:26
RHEL7.5下mysql 8.0.11安装教程
2024-01-27 13:43:49
go语言 bool类型的使用操作
2024-04-26 17:35:03
Centos 6.4 安装Python 2.7 python-pip的详细步骤
2023-12-01 10:21:03
Python生成随机数组的方法小结
2023-10-09 08:19:41
![](https://img.aspxhome.com/file/2023/1/114501_0s.jpg)
Python3简单实现串口通信的方法
2022-03-10 03:39:53
微信公众号H5支付接口调用方法
2023-07-02 05:19:04
PHP基于迭代实现文件夹复制、删除、查看大小等操作的方法
2023-09-03 22:21:42
Python操作redis和mongoDB的方法
2023-01-01 17:06:34
python中的字典详细介绍
2021-06-08 23:10:41
python机器学习理论与实战(四)逻辑回归
2021-07-19 21:05:12
![](https://img.aspxhome.com/file/2023/3/101313_0s.jpg)
Python参数传递机制传值和传引用原理详解
2023-07-28 11:16:02
![](https://img.aspxhome.com/file/2023/6/61466_0s.png)
php使用Cookie实现和用户会话的方法
2023-07-08 15:30:49
Go语言入门exec的基本使用示例
2024-04-25 13:16:33
mysqldumpslow用法示例(慢查询)
2024-01-15 22:42:03
![](https://img.aspxhome.com/file/2023/3/78093_0s.png)
对pandas的层次索引与取值的新方法详解
2023-01-20 00:39:55
![](https://img.aspxhome.com/file/2023/8/105608_0s.jpg)