使用Fiona创建Shapefile矢量数据
作者:阿振 邮箱:tanzhenyugis@163.com
博客:https://blog.csdn.net/theonegis/article/details/80089375
修改时间:2018-06-10
声明:本文为博主原创文章,转载请注明原文出处
基本思路
使用Fiona写入Shapefile数据,主要是构建一个Schema,然后将空间对象转为GeoJSON的形式进行写入。
这个Schema是一个字典结构,定义了Geometry的类型,属性字段的名称及其类型。
代码实现
这里我们举两个例子进行说明:第一是将GeoJSON数据转为Shapefile,第二个是新建一个Shapefile,然后再里面写入自定义的空间几何数据。
因为从GeoJSON中读入的数据本身就是JSON格式,所以我们可以直接写入。GeoJSON的格式定义,参见:创建Shapefile文件并写入数据。
import fiona
import json
with open('China.json') as f:
data = json.load(f)
# schema是一个字典结构,指定了geometry及其它属性结构
schema = {'geometry': 'Polygon',
'properties': {'id': 'int', 'name': 'str'}}
# 使用fiona.open方法打开文件,写入数据
with fiona.open('Provinces.shp', mode='w', driver='ESRI Shapefile',
schema=schema, crs='EPSG:4326', encoding='utf-8') as layer:
# 依次遍历GeoJSON中的空间对象
for feature in data['features']:
# 从GeoJSON中读取JSON格式的geometry和properties的记录
element = {'geometry': feature['geometry'],
'properties': {'id': feature['properties']['id'],
'name': feature['properties']['name']}}
# 写入文件
layer.write(element)第二种方法使用shapely包创建Geometry对象,然后利用mapping方法将创建的对象转为GeoJSON格式进行写入。
Shapely包提供了对空间几何体的定义,操作等功能。
Last updated