前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用Fiona创建Shapefile矢量数据

使用Fiona创建Shapefile矢量数据

作者头像
卡尔曼和玻尔兹曼谁曼
发布2019-01-22 09:45:27
1.6K0
发布2019-01-22 09:45:27
举报
文章被收录于专栏:给永远比拿愉快

基本思路

使用Fiona写入Shapefile数据,主要是构建一个Schema,然后将空间对象转为GeoJSON的形式进行写入。

这个Schema是一个字典结构,定义了Geometry的类型,属性字段的名称及其类型。

代码实现

这里我们举两个例子进行说明:第一是将GeoJSON数据转为Shapefile,第二个是新建一个Shapefile,然后再里面写入自定义的空间几何数据。

因为从GeoJSON中读入的数据本身就是JSON格式,所以我们可以直接写入。GeoJSON的格式定义,参见:创建Shapefile文件并写入数据

代码语言:javascript
复制
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包提供了对空间几何体的定义,操作等功能。

代码语言:javascript
复制
import fiona
from shapely.geometry import Polygon, mapping

# schema是一个字典结构,指定了geometry及其它属性结构
schema = {'geometry': 'Polygon',
          'properties': {'id': 'int', 'name': 'str'}}

# 使用fiona.open方法打开文件,写入数据
with fiona.open('Beijing.shp', mode='w', driver='ESRI Shapefile',
                schema=schema, crs='EPSG:4326', encoding='utf-8') as layer:
    # 使用shapely创建空间几何对象
    coordinates = [[117.4219, 40.21], [117.334, 40.1221], [117.2461, 40.0781], [116.8066, 39.9902], [116.8945, 39.8145],
                   [116.8945, 39.6826], [116.8066, 39.5947], [116.543, 39.5947], [116.3672, 39.4629],
                   [116.1914, 39.5947], [115.752, 39.5068], [115.4883, 39.6387], [115.4004, 39.9463],
                   [115.9277, 40.2539], [115.752, 40.5615], [116.1035, 40.6055], [116.1914, 40.7813],
                   [116.4551, 40.7813], [116.3672, 40.9131], [116.6309, 41.0449], [116.9824, 40.6934],
                   [117.4219, 40.6494], [117.2461, 40.5176], [117.4219, 40.21]]
    polygon = Polygon(coordinates)  # 使用地理坐标定义Polygon对象
    polygon = mapping(polygon)  # 将Polygon对象转为GeoJSON格式
    feature = {'geometry': polygon,
               'properties': {'id': 1, 'name': '北京市'}}
    # 写入文件
    layer.write(feature)
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018年06月12日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 基本思路
  • 代码实现
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档