Fiona は、GeoPackage や Shapefile などの GIS 形式との間で単純なフィーチャ データをストリーミングします。
Fiona は、多層 GIS 形式、圧縮されたメモリ内仮想ファイル システムを使用して、ハード ドライブまたはクラウド ストレージ内のファイルから現実世界のデータを読み書きできます。このプロジェクトには、Python モジュールとコマンド ライン インターフェイス (CLI) が含まれています。
Fiona は GDAL に依存しますが、GDAL 独自のバインディングとは異なります。 Fiona は、生産性が高く、読みやすいコードを簡単に作成できるように設計されています。
Fiona には、libgdal にリンクする拡張モジュールがいくつかあります。これにより、インストールが複雑になります。 libgdal とその独自の依存関係を含むバイナリ ディストリビューション (ホイール) は Python Package Index から入手でき、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 を使用して 1 つのデータ ファイルからいくつかのフィーチャを読み取り、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()``.
「fio」という名前の Fiona のコマンド ライン インターフェイスは、docs/cli.rst に文書化されています。 CLI にはさまざまなコマンドがあります。そのfio cat
コマンドは、任意のデータセットから GeoJSON 特徴をストリーミングします。
$ fio cat --compact tests/data/coutwildrnp.shp | jq -c ' . '
{"geometry":{"coordinates":[[[-111.73527526855469,41.995094299316406],...]]}}
...
このプロジェクトの詳細については、以下を参照してください。