Explore Kartoza

Try searching for: QGIS, Training, GeoNode, PostGIS

Reading and Writing Shapefiles in Python with Fiona
Back to Blog
Python Fiona
June 10, 2024 Zulfikar Akbar Muzakki

Reading and Writing Shapefiles in Python with Fiona

Fiona is a FOSS Python library that simplifies reading and writing GIS formats like shapefiles and geopackages.

Reading and Writing Shapefiles in Python with Fiona

Python

What is Fiona?

Fiona is described as “a FOSS Python library that can be used to read and write GIS formats like shape file and geopackage.” The tool simplifies scenarios requiring GIS data import/export operations, offering straightforward functionality.

Installation

Install via pip:

pip install fiona

Reading Shapefiles

Import required modules:

import fiona
from fiona.model import to_dict

Open files using context manager syntax:

with fiona.open('path/to/shapefile.shp', 'r') as shapefile:
    print(shapefile.crs)

Access CRS information and iterate through records. Convert to dictionary format using the to_dict() function.

Writing Shapefiles

Import necessary components:

import fiona
from fiona.crs import from_epsg

Define schema specifying geometry type and attribute properties with data types and constraints. Create output file with CRS, driver, and schema specifications:

with fiona.open('path/to/shapefile.shp', 'w', crs=from_epsg(4326),
                driver='ESRI Shapefile', schema=schema) as output:

Build geometry in GeoJSON format and write features containing both geometry and property data.

Want to Learn More?

Explore our training courses or get in touch to discuss how we can help your organization.