创建Shapefile文件并写入数据的例子
作者:TheOneGIS 时间:2023-06-21 23:27:12
基本思路
使用GDAL创建Shapefile数据的基本步骤如下:
使用osgeo.ogr.Driver的CreateDataSource()方法创建osgeo.ogr.DataSource矢量数据集
使用osgeo.ogr.DataSource的CreateLayer()方法创建一个图层
使用osgeo.ogr.FieldDefn()定义Shapefile文件的属性字段
创建osgeo.ogr.Feature对象,设置每个属性字段的值,使用Feature对象的SetGeometry()定义几何属性
创建Feature对象以后,使用osgeo.ogr.Layer的CreateFeature()添加Feature对象到当前图层
重复步骤4和5依次添加所有的Feature到当前图层即可
代码实现
下面的例子中,我们读取GeoJSON表示的中国省区数据,然后其转为Shapefile格式。
GeoJSON编码片段如下:
可以看到每个Feature都有一个properties字段和geometry字段,我们需要根据properties字段的信息创建Shapefile数据的属性表,根据geometry字段创建Shapefile中的几何数据。
from osgeo import ogr
from osgeo import osr
import json
import os
os.environ['SHAPE_ENCODING'] = "utf-8"
with open('China.json') as f:
china = json.load(f)
# 创建DataSource
driver = ogr.GetDriverByName('ESRI Shapefile')
ds = driver.CreateDataSource('China.shp')
# 创建WGS84空间参考
srs = osr.SpatialReference()
srs.ImportFromEPSG(4326)
# 创建图层
layer = ds.CreateLayer('province', srs, ogr.wkbPolygon)
# 添加属性定义
fname = ogr.FieldDefn('Name', ogr.OFTString)
fname.SetWidth(24)
layer.CreateField(fname)
fcx = ogr.FieldDefn('CenterX', ogr.OFTReal)
layer.CreateField(fcx)
fcy = ogr.FieldDefn('CenterY', ogr.OFTReal)
layer.CreateField(fcy)
# 变量GeoJSON中的features
for f in china['features']:
# 新建Feature并且给其属性赋值
feature = ogr.Feature(layer.GetLayerDefn())
feature.SetField('Name', f['properties']['name'])
feature.SetField('CenterX', f['properties']['cp'][0])
feature.SetField('CenterY', f['properties']['cp'][1])
# 设置Feature的几何属性Geometry
polygon = ogr.CreateGeometryFromJson(str(f['geometry']))
feature.SetGeometry(polygon)
# 创建Feature
layer.CreateFeature(feature)
del feature
ds.FlushCache()
del ds
来源:https://blog.csdn.net/theonegis/article/details/80554993
标签:Shapefile,写入数据
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
go语言中的return语句
2024-05-28 15:22:09
python机器学习理论与实战(五)支持向量机
2021-11-27 11:36:30
![](https://img.aspxhome.com/file/2023/2/75942_0s.jpg)
一个asp版XMLDOM操作类
2011-04-19 10:50:00
数据仓库基本报表制作过程中的SQL写法
2008-11-25 12:17:00
使用Python的PEAK来适配协议的教程
2021-08-01 04:12:04
Python os和os.path模块详情
2022-12-08 12:53:45
![](https://img.aspxhome.com/file/2023/2/89332_0s.png)
Python实现的读取/更改/写入xml文件操作示例
2023-06-05 05:16:42
python读取json文件并将数据插入到mongodb的方法
2021-03-22 20:30:22
Pycharm 2020最新永久激活码(附最新激活码和插件)
2023-06-14 14:21:21
![](https://img.aspxhome.com/file/2023/2/59132_0s.jpg)
如何使用Python优雅的合并两个字典Dict
2023-10-12 22:50:36
详解GaussDB for MySQL性能优化
2024-01-25 15:10:18
![](https://img.aspxhome.com/file/2023/9/129889_0s.jpg)
Python调用C语言的方法【基于ctypes模块】
2021-03-08 20:31:24
10分钟学会Google Map API (一)
2009-06-07 18:17:00
解决Vue+SpringBoot+Shiro跨域问题
2024-05-05 09:06:03
![](https://img.aspxhome.com/file/2023/6/130856_0s.png)
Python中生成一个指定长度的随机字符串实现示例
2023-02-16 21:46:07
python画图把时间作为横坐标的方法
2021-06-17 08:13:28
![](https://img.aspxhome.com/file/2023/0/89340_0s.jpg)
Python线程条件变量Condition原理解析
2022-07-23 02:52:06
GO的锁和原子操作的示例详解
2024-05-05 09:30:57
![](https://img.aspxhome.com/file/2023/0/128650_0s.png)
mysql的in会不会让索引失效?
2024-01-27 11:50:44
SQL Server中的执行引擎入门 图解
2012-06-06 20:08:26