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

 

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值