当前位置: 首页 > news >正文

python(ogr)处理geojson为本地shp文件

 前言


        本次所利用的geojson数据来自https://geo.datav.aliyun.com/areas_v3/bound/410000_full.json

,如果觉得下方代码看起来不方便,可以来GitHub上来看,在这上面还有一些辅助内容便于理解

GISpjd/GIS-union-Python (github.com)icon-default.png?t=N7T8https://github.com/GISpjd/GIS-union-Python

 

一.展示


二.环境


        我是在Anaconda下的jupyter notebook完成代码的编写,下面是我对应的版本号,我建议大家在这个环境下编写,因为在下载gdal等包的时候会更方便。

三.参考网站 

osgeo.osr module — GDAL documentation

osgeo.ogr module — GDAL documentation

 不过对应API像字典一样,对新手不太友好,可以结合网上博客和AI来学习,而且随着时间的变化,相应API可能也会变化,发现实现不了的时候及时查阅。

对于ogr的矢量结构,可以阅读:OGR矢量结构 — headfirst gdal 0.1 documentation (headfirst-gdal.readthedocs.io)

四. 代码


from osgeo import ogr, osr, gdal
import requests
import json# 设置Shapefile的编码为UTF-8,这有助于确保中文或其他非ASCII字符能够正确保存和显示。
gdal.SetConfigOption('SHAPE_ENCODING', 'UTF-8')# 获取geojson
url = 'https://geo.datav.aliyun.com/areas_v3/bound/410000_full.json'
geojson = requests.get(url)
data = json.loads(geojson.content)# 准备shp数据源
driver = ogr.GetDriverByName('ESRI Shapefile')
shp_path = r'C:\python爬虫\henan.shp' #换成自己想保存的位置
data_source = driver.CreateDataSource(shp_path)# 定义坐标系
srs = osr.SpatialReference()
srs.ImportFromEPSG(4326)# 创建坐标系
layer = data_source.CreateLayer('province',srs,ogr.wkbMultiPolygon)feature_def = layer.GetLayerDefn() #获取图层定义
properties = data['features'][0]['properties']# 字段名重命名映射表
rename_map = {'adcode': 'adcode','name': 'name','center':'center','childrenNum': 'childNum',  # 将'childrenNum'简化为'childNum''level': 'level','parent': 'parent','subFeatureIndex': 'subIdx',  # 将'subFeatureIndex'简化为'subIdx''acroutes':'acroutes','geometry': 'geometry'
}# 为图层创建字段,基于GeoJSON数据的属性。
for prop_name in properties.keys():#dict.get(key,default)short_name = rename_map.get(prop_name, prop_name[:10]) # 使用重命名映射表或截断过长的字段名。field = ogr.FieldDefn(short_name,ogr.OFTString)# 创建新的字段定义。layer.CreateField(field)# 在图层中添加该字段。# 遍历GeoJSON数据中的每一个特征(Feature),将它们转换为Shapefile格式并添加到图层中。
for feature in data['features']:geom = ogr.CreateGeometryFromJson(json.dumps(feature['geometry'])) #创建几何对象shp_feature = ogr.Feature(feature_def) #生成新的特征(Feature),以便将其添加到layer中#为特征设置属性值for prop_name,prop_value in feature['properties'].items():# 根据rename_map获取映射后的字段名short_name = rename_map.get(prop_name, prop_name[:10])prop_value = str(prop_value) if prop_value is not None else ''shp_feature.SetField(short_name, prop_value)# 设置特征的属性。shp_feature.SetGeometry(geom) # 将几何对象与特征关联。layer.CreateFeature(shp_feature)# 将特征添加到图层中。# 销毁要素,释放内存shp_feature = None
# 关闭数据源
data_source = None

 

http://www.lryc.cn/news/316711.html

相关文章:

  • Docker容器化技术(使用Dockerfile制作镜像)
  • C++ struct 结构体类型
  • 什么是VR虚拟现实体验店|VR主题馆加盟|元宇宙文化旅游
  • 【智能家居入门1之环境信息监测】(STM32、ONENET云平台、微信小程序、HTTP协议)
  • AIGC安全研究简述(附资料下载)
  • 初识Spring MVC
  • 云原生之容器编排实践-ruoyi-cloud项目部署到K8S:Nginx1.25.3
  • PHP立体安全攻击向量:保护应用程序的关键挑战
  • 【功能大全】手机短信验证码一键注册登录流程
  • 【Python】【Matplotlib】深入解析plt.grid()---原理、应用与注意事项
  • 数据库规范化设计案例解析
  • 服务器段的连接端口和监听端口编程实现
  • 用“定时执行专家”武装你的电脑,做时间管理大师!
  • css3实现3D立方体旋转特效源码
  • 计算器系统基础知识-校验码
  • springboot换日志框架后爆SLF4J: Class path contains multiple SLF4J bindings的解决办法
  • k8s+zabbix
  • k8s-生产级的k8s高可用(2) 25
  • ubuntu20.04 创建ros环境、创建rospackage
  • QT进阶---------pro项目文件中的常用命令 (第三天)
  • php常用设计模式应用场景及示例
  • 浏览器与服务器通信过程(HTTP协议)
  • Pytorch搭建AlexNet 预测实现
  • 笔记:使用parfile进行的数据导入导出
  • 基于YOLOv8/YOLOv7/YOLOv6/YOLOv5的行人跌倒检测系统(深度学习+UI界面+完整训练数据集)
  • Ubuntu 14.04:PaddleOCR基于PaddleServing的在线服务化部署(失败)
  • Java JUC 笔记(2)
  • webpack5高级--02_提升打包构建速度
  • MAC M芯片 Anaconda安装
  • 【JS】自动下拉网页刷新,当出现指定关键字,就打印出来