Fiona 在 GeoPackage 和 Shapefile 等 GIS 格式之间传输简单的要素数据。
Fiona 可以使用多层 GIS 格式、压缩和内存虚拟文件系统,从硬盘驱动器或云存储中的文件读取和写入真实世界的数据。该项目包括 Python 模块和命令行界面 (CLI)。
Fiona 依赖于 GDAL,但与 GDAL 自己的绑定不同。 Fiona 旨在提高工作效率,并轻松编写易于阅读的代码。
Fiona 有几个链接到 libgdal 的扩展模块。这使安装变得复杂。包含 libgdal 及其自身依赖项的二进制发行版(轮子)可从 Python 包索引中获取,并且可以使用 pip 安装。
pip install fiona
这些轮子主要是为了方便简单应用的安装,而不是为了生产。它们没有经过与所有其他二进制轮、conda 包或 QGIS 的兼容性测试,并且省略了许多 GDAL 的可选格式驱动程序。例如,如果您需要 GML 支持,您将需要从源发行版构建并安装 Fiona。可以使用 pip(版本 >= 22.3)和 --no-binary 选项从源安装 Fiona。可以通过设置 GDAL_CONFIG 环境变量来选择特定的 GDAL 安装。
pip install -U pip
pip install --no-binary fiona fiona
许多用户发现 Anaconda 和 conda-forge 是安装 Fiona 并访问更多可选格式驱动程序(如 GML)的好方法。
Fiona 1.10 需要 Python 3.8 或更高版本以及 GDAL 3.4 或更高版本。
从fiona.open()
函数返回的类似文件的Collection
对象中读取和写入特征。要素是基于 GeoJSON 格式建模的数据类。它们没有自己的任何空间方法,所以如果你想转换它们,你将需要 Shapely 或类似的东西。下面是一个使用 Fiona 从一个数据文件中读取某些要素、使用 Shapely 更改其几何属性并将其写入新数据文件的示例。
import fiona
from fiona import Feature , Geometry
from shapely . geometry import mapping , shape
# Open a file for reading. We'll call this the source.
with fiona . open (
"zip+https://github.com/Toblerity/Fiona/files/11151652/coutwildrnp.zip"
) as src :
# The file we'll write to must be initialized with a coordinate
# system, a format driver name, and a record schema. We can get
# initial values from the open source's profile property and then
# modify them as we need.
profile = src . profile
profile [ "schema" ][ "geometry" ] = "Point"
profile [ "driver" ] = "GPKG"
# Open an output file, using the same format driver and coordinate
# reference system as the source. The profile mapping fills in the
# keyword parameters of fiona.open.
with fiona . open ( "centroids.gpkg" , "w" , ** profile ) as dst :
# Process only the feature records intersecting a box.
for feat in src . filter ( bbox = ( - 107.0 , 37.0 , - 105.0 , 39.0 )):
# Get the feature's centroid.
centroid_shp = shape ( feat . geometry ). centroid
new_geom = Geometry . from_dict ( centroid_shp )
# Write the feature out.
dst . write (
Feature ( geometry = new_geom , properties = f . properties )
)
# The destination's contents are flushed to disk and the file is
# closed when its with block ends. This effectively
# executes ``dst.flush(); dst.close()``.
Fiona 的命令行界面名为“fio”,记录在 docs/cli.rst 中。 CLI 有许多不同的命令。它的fio cat
命令从任何数据集中传输 GeoJSON 特征。
$ fio cat --compact tests/data/coutwildrnp.shp | jq -c ' . '
{"geometry":{"coordinates":[[[-111.73527526855469,41.995094299316406],...]]}}
...
有关此项目的更多详细信息,请参阅: