Fiona Vector Data — Core Concepts

Fiona provides a Pythonic interface to the OGR vector-data library (part of GDAL). It reads and writes vector geospatial files — Shapefiles, GeoJSON, GeoPackage, KML, and about 80 other formats — using a consistent, record-oriented API.

Opening and reading

import fiona

with fiona.open("cities.geojson") as src:
    print(src.driver)       # "GeoJSON"
    print(src.crs)          # CRS dictionary
    print(src.schema)       # {'geometry': 'Point', 'properties': {'name': 'str', ...}}
    print(len(src))         # number of features

    for feature in src:
        print(feature["properties"]["name"])
        print(feature["geometry"]["type"])       # "Point"
        print(feature["geometry"]["coordinates"])  # [lon, lat]

Each feature is a plain Python dictionary following the GeoJSON structure, regardless of the source format. This uniformity makes Fiona easy to integrate with any Python code.

The schema

Every collection has a schema describing geometry type and property fields:

schema = {
    "geometry": "Polygon",
    "properties": {
        "name": "str",
        "population": "int",
        "area_km2": "float",
    },
}

When writing, Fiona uses the schema to validate records and create the correct field types in the output file.

Writing data

import fiona
from fiona.crs import from_epsg

schema = {
    "geometry": "Point",
    "properties": {"name": "str", "rating": "float"},
}

with fiona.open(
    "cafes.gpkg",
    "w",
    driver="GPKG",
    crs=from_epsg(4326),
    schema=schema,
) as dst:
    dst.write({
        "geometry": {"type": "Point", "coordinates": [-73.985, 40.748]},
        "properties": {"name": "Café Luna", "rating": 4.5},
    })

Fiona supports append mode ("a") for adding records to existing files without rewriting the whole dataset.

Format drivers

Fiona exposes GDAL/OGR format drivers. List them with:

print(sorted(fiona.supported_drivers.keys()))

Common drivers include ESRI Shapefile, GeoJSON, GPKG (GeoPackage), KML, FlatGeobuf, and CSV. Each driver has different capabilities — some are read-only, some support transactions.

Filtering with bounding box

Instead of reading every feature, filter by geographic extent:

with fiona.open("buildings.gpkg") as src:
    # Only features whose bounding box intersects the given window
    for feature in src.filter(bbox=(-73.99, 40.70, -73.95, 40.75)):
        process(feature)

This is especially useful for large datasets where you only need a small area.

CRS handling

from fiona.crs import from_epsg, to_string

crs = from_epsg(4326)
print(to_string(crs))  # PROJ string

Fiona represents CRS as dictionaries or PROJ strings. For transformations between coordinate systems, use pyproj — Fiona itself does not reproject geometries.

How it works

Fiona is a Python binding to OGR. When you open a file, OGR identifies the driver, parses the header, and sets up a feature iterator. Each .next() call reads one feature from disk, converts it to a Python dict, and advances the cursor. Writing reverses the process — dict to OGR feature to bytes on disk.

Common misconception

Fiona is not a spatial-analysis library. It does not compute intersections, buffers, or distances. Its sole purpose is reliable I/O. For spatial operations, pair it with Shapely (geometry math) or GeoPandas (which uses Fiona under the hood for file I/O).

The one thing to remember: Fiona is the I/O layer for vector geospatial data — it reads and writes dozens of formats into simple Python dictionaries, letting you focus on analysis instead of file-format gymnastics.

pythonfionageospatialvector-data

See Also

  • Python Adaptive Learning Systems How Python builds learning apps that adjust to each student like a personal tutor who knows exactly what you need next.
  • Python Airflow Learn Airflow as a timetable manager that makes sure data tasks run in the right order every day.
  • Python Altair Learn Altair through the idea of drawing charts by describing rules, not by hand-placing every visual element.
  • Python Automated Grading How Python grades homework and exams automatically, from simple answer keys to understanding written essays.
  • Python Batch Vs Stream Processing Batch processing is like doing laundry once a week; stream processing is like a self-cleaning shirt that cleans itself constantly.