Fiona (Spatial Data I/O)

1.10.1 · active · verified Thu Apr 09

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

Install

Imports

Quickstart

This quickstart demonstrates how to create a simple GeoJSON file using Fiona, write a feature to it, and then read the data back. It covers defining schema, CRS, and using the `fiona.open` context manager for file operations.

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}")

view raw JSON →