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],...]]}}
...
有關此項目的更多詳細信息,請參閱: