GeoPy
  • 前言
  • Python基础
    • Python环境搭建及基本数据类型
    • 运算符及字符串
    • 序列与字典
    • 流程控制语句
    • 函数
    • 面向对象编程初识
    • 面向对象编程高级
    • Python科学计算
    • 空间数据处理环境搭建
  • 空间数据基础
    • 空间参考系统
    • 地图投影
    • 空间数据
  • GDAL空间数据处理
    • GDAL简介
    • GDAL数据基本操作
      • 打开栅格数据的正确方式
      • 栅格数据格式转换
      • 栅格数据创建与保存
      • 读取HDF或者NetCDF格式的栅格数据
      • 栅格数据投影转换
      • 栅格数据裁剪
      • 打开Shapefile文件的正确方式
      • 创建Shapefile文件并写入数据
      • 矢量数据投影转换
    • Fiona矢量数据处理
      • Fiona简介及Shapefile数据读取
      • 使用Fiona创建Shapefile矢量数据
    • Rasterio栅格数据处理
      • 使用Rasterio读取栅格数据
      • 使用Rasterio创建栅格数据
      • 使用Rasterio做投影变换
Powered by GitBook
On this page
  • 基本思路
  • 代码实现
  1. GDAL空间数据处理
  2. Fiona矢量数据处理

使用Fiona创建Shapefile矢量数据

PreviousFiona简介及Shapefile数据读取NextRasterio栅格数据处理

Last updated 6 years ago

作者:阿振 邮箱:tanzhenyugis@163.com

博客:

修改时间:2018-06-10

声明:本文为博主原创文章,转载请注明原文出处

基本思路

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

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

代码实现

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

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

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包提供了对空间几何体的定义,操作等功能。

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)
https://blog.csdn.net/theonegis/article/details/80089375
创建Shapefile文件并写入数据