Fiona (Spatial Data I/O)
Fiona is a Python library for reading and writing spatial data files, providing a high-level API built on GDAL/OGR. It enables seamless interaction with various vector data formats like Shapefile, GeoJSON, and KML. The current version is 1.10.1, with an active development cycle that includes regular bug fixes, feature additions, and pre-releases leading up to major versions.
Warnings
- breaking The `fiona.path` module and its members (e.g., `fiona.fspath`, `fiona.BytesIOResource`) were removed in Fiona 1.10.0. Code relying on these for path manipulation or in-memory resources will break.
- deprecated Mutable item access to `Feature`, `Geometry`, and `Properties` instances is deprecated. Instances of these classes will become immutable in a future major version (2.0), leading to `TypeError` if modification is attempted.
- gotcha Fiona relies on underlying C libraries (GDAL, PROJ, GEOS). While `pip install fiona` typically provides wheels bundling these for common platforms, issues can arise on specific systems, custom environments, or when mixing with system-installed GDAL versions, leading to import errors or runtime crashes.
- deprecated Attributes like `fiona.schema.FIELD_TYPES`, `FIELD_TYPES_MAP`, and `FIELD_TYPES_MAP_REV` are deprecated and will be removed in Fiona 2.0.
Install
-
pip install fiona
Imports
- fiona
import fiona
- open
import fiona # ... fiona.open(...)
- CRS
from fiona.crs import CRS
- path
import os.path
Quickstart
import fiona
import os
# Create a dummy GeoJSON file for demonstration
geoj_feature = {
'type': 'Feature',
'geometry': {'type': 'Point', 'coordinates': (10, 20)},
'properties': {'name': 'test_point'}
}
# Define the schema for the output file
schema = {
'geometry': 'Point',
'properties': {'name': 'str'},
}
# Define the Coordinate Reference System (CRS)
crs = 'EPSG:4326'
# Define the driver (file format)
driver = 'GeoJSON'
output_file = 'temp_fiona_test.geojson'
# Write data to a new GeoJSON file
with fiona.open(
output_file,
'w',
driver=driver,
crs=crs,
schema=schema
) as collection:
collection.write(geoj_feature)
print(f"Successfully wrote data to {output_file}")
# Read data from the created file
with fiona.open(output_file, 'r') as collection:
print(f"\nDriver: {collection.driver}")
print(f"CRS: {collection.crs}")
print(f"Schema: {collection.schema}")
for feature in collection:
print(f"Read Feature: {feature}")
# Clean up the dummy file
os.remove(output_file)
print(f"Cleaned up {output_file}")