Fiona
Fiona
Fiona
Release 1.8.6
Sean Gillies
1 Fiona 1
1.1 Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Fiona CLI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.4 Development and testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.5 Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.6 Credits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3 fiona 45
3.1 fiona package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
i
Bibliography 73
ii
CHAPTER 1
Fiona
Fiona is designed to be simple and dependable. It focuses on reading and writing data in standard Python IO style and
relies upon familiar Python types and protocols such as files, dictionaries, mappings, and iterators instead of classes
specific to OGR. Fiona can read and write real-world data using multi-layered GIS formats and zipped virtual file
systems and integrates readily with other Python GIS packages such as pyproj, Rtree, and Shapely. Fiona is supported
only on CPython versions 2.7 and 3.4+.
For more details, see:
• Fiona home page
• Docs and manual
• Examples
• Main user discussion group
• Developers discussion group
1.1 Usage
1.1.1 Collections
Records are read from and written to file-like Collection objects returned from the fiona.open() function.
Records are mappings modeled on the GeoJSON format. They don’t have any spatial methods of their own, so if you
want to do anything fancy with them you will probably need Shapely or something like it. Here is an example of using
Fiona to read some records from one data file, change their geometry attributes, and write them to a new data file.
1
Fiona Documentation, Release 1.8.6
import fiona
meta = src.meta
meta['schema']['geometry'] = 'Point'
f['geometry'] = {
'type': 'Point',
'coordinates': f['geometry']['coordinates'][0][0]}
dst.write(f)
Collections can also be made from single layers within multilayer files or directories of data. The target layer is
specified by name or by its integer index within the file or directory. The fiona.listlayers() function provides
an index ordered list of layer names.
# Output:
# (u'coutwildrnp', 67)
Layer can also be specified by index. In this case, layer=0 and layer='test_uk' specify the same layer in the
data file or directory.
2 Chapter 1. Fiona
Fiona Documentation, Release 1.8.6
# Output:
# (0, u'coutwildrnp', 67)
Multilayer data can be written as well. Layers must be specified by name when writing.
with open('tests/data/cowildrnp.shp') as src:
meta = src.meta
f = next(src)
print(fiona.listlayers('/tmp/foo'))
# Output:
# [u'bar']
# 1
# Polygon
# OrderedDict([(u'PERIMETER', 1.22107), (u'FEATURE2', None), (u'NAME', u'Mount
˓→Naomi Wilderness'), (u'FEATURE1', u'Wilderness'), (u'URL', u'http://www.wilderness.
˓→')])
A view of the /tmp/foo directory will confirm the creation of the new files.
$ ls /tmp/foo
bar.cpg bar.dbf bar.prj bar.shp bar.shx
Zip and Tar archives can be treated as virtual filesystems and Collections can be made from paths and layers within
them. In other words, Fiona lets you read and write zipped Shapefiles.
for i, layername in enumerate(
fiona.listlayers('zip://tests/data/coutwildrnp.zip'):
with fiona.open('zip://tests/data/coutwildrnp.zip', layer=i) as src:
print(i, layername, len(src))
# Output:
# (0, u'coutwildrnp', 67)
Fiona can also read from more exotic file systems. For instance, a zipped shape file in S3 can be accessed like so:
1.1. Usage 3
Fiona Documentation, Release 1.8.6
# Output:
# 67
Fiona’s command line interface, named “fio”, is documented at docs/cli.rst. Its fio info pretty prints information
about a data file.
1.3 Installation
Fiona requires Python 2.7 or 3.4+ and GDAL/OGR 1.8+. To build from a source distribution you will need a C
compiler and GDAL and Python development headers and libraries (libgdal1-dev for Debian/Ubuntu, gdal-dev for
CentOS/Fedora).
To build from a repository copy, you will also need Cython to build C sources from the project’s .pyx files. See the
project’s requirements-dev.txt file for guidance.
The Kyngchaos GDAL frameworks will satisfy the GDAL/OGR dependency for OS X, as will Homebrew’s GDAL
Formula (brew install gdal).
4 Chapter 1. Fiona
Fiona Documentation, Release 1.8.6
Fiona depends on the modules enum34, six, cligj, munch, argparse, and ordereddict (the two latter
modules are standard in Python 2.7+). Pip will fetch these requirements for you, but users installing Fiona from a
Windows installer must get them separately.
Assuming you’re using a virtualenv (if not, skip to the 4th command) and GDAL/OGR libraries, headers, and gdal-
config program are installed to well known locations on your system via your system’s package manager (brew
install gdal using Homebrew on OS X), installation is this simple.
$ mkdir fiona_env
$ virtualenv fiona_env
$ source fiona_env/bin/activate
(fiona_env)$ pip install fiona
If gdal-config is not available or if GDAL/OGR headers and libs aren’t installed to a well known location, you must set
include dirs, library dirs, and libraries options via the setup.cfg file or setup command line as shown below (using git).
You must also specify the version of the GDAL API on the command line using the --gdalversion argument (see
example below) or with the GDAL_VERSION environment variable (e.g. export GDAL_VERSION=2.1).
Or specify that build options and GDAL API version should be provided by a particular gdal-config program.
1.3.3 Windows
$ python setup.py build_ext -I<path to gdal include files> -lgdal_i -L<path to gdal
˓→library> install --gdalversion 2.1
Note: The GDAL DLL (gdal111.dll or similar) and gdal-data directory need to be in your Windows PATH
otherwise Fiona will fail to work.
The [Appveyor CI build](https://ci.appveyor.com/project/sgillies/fiona/history) uses the GISInternals GDAL binaries
to build Fiona. This produces a binary wheel for successful builds, which includes GDAL and other dependencies,
for users wanting to try an unstable development version. The [Appveyor configuration file](appveyor.yml) may be a
useful example for users building from source on Windows.
1.3. Installation 5
Fiona Documentation, Release 1.8.6
Building from the source requires Cython. Tests require pytest. If the GDAL/OGR libraries, headers, and gdal-config
program are installed to well known locations on your system (via your system’s package manager), you can do this:
If you have a non-standard environment, you’ll need to specify the include and lib dirs and GDAL library on the
command line:
(fiona_env)$ py.test
1.5 Changes
• The advertisement for JSON driver enablement in 1.8.5 was false (#176), but in this release they are ready for
use.
• GDAL seems to work best if GDAL_DATA is set as early as possible. Ideally it is set when building the library
or in the environment before importing Fiona, but for wheels we patch GDAL_DATA into os.environ when
fiona.env is imported. This resolves #731.
• A combination of bugs which allowed .cpg files to be overlooked has been fixed (#726).
• On entering a collection context (Collection.__enter__) a new anonymous GDAL environment is created if
needed and entered. This makes with fiona.open(. . . ) as collection: roughly equivalent to with fiona.open(. . . )
as collection, Env():. This helps prevent bugs when Collections are created and then used later or in different
scopes.
• Missing GDAL support for TopoJSON, GeoJSONSeq, and ESRIJSON has been enabled (#721).
• A regression in handling of polygons with M values (#724) has been fixed.
• Per-feature debug logging calls in OGRFeatureBuilder methods have been eliminated to improve feature writing
performance (#718).
• Native support for datasets in Google Cloud Storage identified by “gs” resource names has been added (#709).
6 Chapter 1. Fiona
Fiona Documentation, Release 1.8.6
• Support has been added for triangle, polyhedral surface, and TIN geometry types (#679).
• Notes about using the MemoryFile and ZipMemoryFile classes has been added to the manual (#674).
• The RASTERIO_ENV config environment marker this project picked up from Rasterio has been renamed to
FIONA_ENV (#665).
• Options –gdal-data and –proj-data have been added to the fio-env command so that users of Rasterio wheels can
get paths to set GDAL_DATA and PROJ_LIB environment variables.
• The unsuccessful attempt to make GDAL and PROJ support file discovery and configuration automatic within
collection’s crs and crs_wkt properties has been reverted. Users must execute such code inside a with Env()
block or set the GDAL_DATA and PROJ_LIB environment variables needed by GDAL.
Bug fixes:
• Raise FionaValueError when an iterator’s __next__ is called and the session is found to be missing or inactive
instead of passing a null pointer to OGR_L_GetNextFeature (#687).
Bug fixes:
• Add checks around OSRGetAuthorityName and OSRGetAuthorityCode calls that will log problems with look-
ing up these items.
• Opened data sources are now released before we raise exceptions in WritingSession.start (#676). This fixes an
issue with locked files on Windows.
• We now ensure that an Env instance exists when getting the crs or crs_wkt properties of a Collection (#673,
#690). Otherwise, required GDAL and PROJ data files included in Fiona wheels can not be found.
• GDAL and PROJ data search has been refactored to improve testability (#678).
• In the project’s Cython code, void* pointers have been replaced with proper GDAL types (#672).
• Pervasive warning level log messages about ENCODING creation options (#668) have been eliminated.
1.5. Changes 7
Fiona Documentation, Release 1.8.6
There are no changes in 1.8rc1 other than more test standardization and the introduction of a temporary
test_collection_legacy.py module to support the build of fully tested Python 2.7 macosx wheels on Travis-CI.
Bug fixes:
• The ensure_env_with_credentials decorator will no longer clobber credentials of the outer environment. This
fixes a bug reported to the Rasterio project and which also existed in Fiona.
• An unused import of the packaging module and the dependency have been removed (#653).
• The Env class logged to the ‘rasterio’ hierarchy instead of ‘fiona’. This mistake has been corrected (#646).
• The Mapping abstract base class is imported from collections.abc when possible (#647).
Refactoring:
• Standardization of the tests on pytest functions and fixtures continues and is nearing completion (#648, #649,
#650, #651, #652).
Deprecations:
• Collection slicing has been deprecated and will be prohibited in a future version.
Bug fixes:
• Rasterio CRS objects passed to transform module methods will be converted to dicts as needed (#590).
• Implicitly convert curve geometries to their linear approximations rather than failing (#617).
• Migrated unittest test cases in test_collection.py and test_layer.py to the use of the standard data_dir and
path_coutwildrnp_shp fixtures (#616).
• Root logger configuration has been removed from all test scripts (#615).
• An AWS session is created for the CLI context Env only if explicitly requested, matching the behavior of
Rasterio’s CLI (#635).
• Dependency on attrs is made explicit.
• Other dependencies are pinned to known good versions in requirements files.
• Unused arguments have been removed from the Env constructor (#637).
Refactoring:
8 Chapter 1. Fiona
Fiona Documentation, Release 1.8.6
• A with_context_env decorator has been added and used to set up the GDAL environment for CLI commands.
The command functions themselves are now simplified.
Deprecations:
• The fiona.drivers() context manager is officially deprecated. All users should switch to fiona.Env(),
which registers format drivers and manages GDAL configuration in a reversible manner.
Bug fixes:
• The Collection class now filters log messages about skipped fields to a maximum of one warning message per
field (#627).
• The boto3 module is only imported when needed (#507, #629).
• Compatibility with Click 7.0 is achieved (#633).
• Use of %r instead of %s in a debug() call prevents UnicodeDecodeErrors (#620).
New features:
• 64-bit integers are the now the default for int type fields (#562, #564).
• ‘http’, ‘s3’, ‘zip+http’, and ‘zip+s3’ URI schemes for datasets are now supported (#425, #426).
• We’ve added a MemoryFile class which supports formatted in-memory feature collections (#501).
• Added support for GDAL 2.x boolean field sub-type (#531).
• A new fio rm command makes it possible to cleanly remove multi-file datasets (#538).
• The geometry type in a feature collection is more flexible. We can now specify not only a single geometry type,
but a sequence of permissible types, or “Any” to permit any geometry type (#539).
• Support for GDAL 2.2+ null fields has been added (#554).
• The new gdal_open_vector() function of our internal API provides much improved error handling (#557).
Bug fixes:
• The bug involving OrderedDict import on Python 2.7 has been fixed (#533).
• An AttributeError raised when the --bbox option of fio-cat is used with more than one input file has
been fixed (#543, #544).
• Obsolete and derelict fiona.tool module has been removed.
• Revert the change in 0a2bc7c that discards Z in geometry types when a collection’s schema is reported (#541).
• Require six version 1.7 or higher (#550).
• A regression related to “zip+s3” URIs has been fixed.
• Debian’s GDAL data locations are now searched by default (#583).
1.5. Changes 9
Fiona Documentation, Release 1.8.6
New features:
• Each call of writerecords() involves one or more transactions of up to 20,000 features each. This improves
performance when writing GeoPackage files as the previous transaction size was only 200 features (#476, #491).
Packaging:
• Fiona’s Cython source files have been refactored so that there are no longer separate extension modules for
GDAL 1.x and GDAL 2.x. Instead there is a base extension module based on GDAL 2.x and shim modules for
installations that use GDAL 1.x.
• This post-release adds missing expat (and thereby GPX format) support to the included GDAL library (still
version 2.2.2).
• The encoding keyword argument for fiona.open(), which is intended to allow a caller to override a data
source’s own and possibly erroneous encoding, has not been working (#510, #512). The problem is that we
weren’t always setting GDAL open or config options before opening the data sources. This bug is resolved by a
number of commits in the maint-1.7 branch and the fix is demonstrated in tests/test_encoding.py.
• An --encoding option has been added to fio-load to enable creation of encoded shapefiles with an accompa-
nying .cpg file (#499, #517).
• A post-release has been made to fix a problem with macosx wheels uploaded to PyPI.
Bug fixes:
• An extraneous printed line from the rio cat --layers validator has been removed (#478).
Packaging:
• Official OS X and Manylinux1 wheels (on PyPI) for this release will be compatible with Shapely 1.6.2 and
Rasterio 1.0a10 wheels.
This release introduces no changes in the Fiona package. It upgrades GDAL from 2.2.0 to 2.2.1 in wheels that we
publish to the Python Package Index.
10 Chapter 1. Fiona
Fiona Documentation, Release 1.8.6
Bug fixes:
• Acquire the GIL for GDAL error callback functions to prevent crashes when GDAL errors occur when the GIL
has been released by user code.
• Sync and flush layers when closing even when the number of features is not precisely known (#467).
Bug fixes:
• Provide all arguments needed by CPLError based exceptions (#456).
Bug fixes:
• Switch logger warn() (deprecated) calls to warning().
• Replace all relative imports and cimports in Cython modules with absolute imports (#450).
• Avoid setting PROJ_LIB to a non-existent directory (#439).
Bug fixes:
• Fall back to share/proj for PROJ_LIB (#440).
• Replace every call to OSRDestroySpatialReference() with OSRRelease(), fixing the GPKG driver crasher re-
ported in #441 (#443).
• Add a DriverIOError derived from IOError to use for driver-specific errors such as the GeoJSON driver’s refusal
to overwrite existing files. Also we now ensure that when this error is raised by fiona.open() any created read
or write session is deleted, this eliminates spurious exceptions on teardown of broken Collection objects (#437,
#444).
Bug fixes:
• Opening a data file in read (the default) mode with fiona.open() using the the driver or drivers keyword argu-
ments (to specify certain format drivers) would sometimes cause a crash on Windows due to improperly termi-
nated lists of strings (#428). The fix: Fiona’s buggy string_list() has been replaced by GDAL’s CSLAddString().
Bug fixes:
• OGR’s EsriJSON detection fails when certain keys aren’t found in the first 6000 bytes of data passed to BytesCol-
lection (#422). A .json file extension is now explicitly given to the in-memory file behind BytesCollection when
the driver=’GeoJSON’ keyword argument is given (#423).
1.5. Changes 11
Fiona Documentation, Release 1.8.6
Roses are red. Tan is a pug. Software regression’s the most embarrassing bug.
Bug fixes:
• Use __stdcall for GDAL error handling callback on Windows as in Rasterio.
• Turn on latent support for zip:// URLs in rio-cat and rio-info (#421).
• The 1.7.2 release broke support for zip files with absolute paths (#418). This regression has been fixed with tests
to confirm.
Future Deprecation:
• Collection.__next__() is buggy in that it can lead to duplication of features when used in combination with Col-
lection.filter() or Collection.__iter__(). It will be removed in Fiona 2.0. Please check for usage of this deprecated
feature by running your tests or programs with PYTHONWARNINGS=”always:::fiona” or -W”always:::fiona”
and switch from next(collection) to next(iter(collection)) (#301).
Bug fix:
• Zipped streams of bytes can be accessed by BytesCollection (#318).
Bug Fixes:
• Prevent Fiona from stumbling over ‘Z’, ‘M’, and ‘ZM’ geometry types introduced in GDAL 2.1 (#384). Fiona
1.7.1 doesn’t add explicit support for these types, they are coerced to geometry types 1-7 (‘Point’, ‘LineString’,
etc.)
• Raise an UnsupportedGeometryTypeError when a bogus or unsupported geometry type is encountered in a new
collection’s schema or elsewhere (#340).
• Enable –precision 0 for fio-cat (#370).
• Prevent datetime exceptions from unnecessarily stopping collection iteration by yielding None (#385)
• Replace log.warn calls with log.warning calls (#379).
• Print an error message if neither gdal-config or –gdalversion indicate a GDAL C API version when running
setup.py (#364).
• Let dict-like subclasses through CRS type checks (#367).
Packaging: define extension modules for ‘clean’ and ‘config’ targets (#363).
12 Chapter 1. Fiona
Fiona Documentation, Release 1.8.6
Packaging: No files are copied for the ‘clean’ setup target (#361, #362).
The C extension modules in this library can now be built and used with either a 1.x or 2.x release of the GDAL library.
Big thanks to René Buffat for leading this effort.
Refactoring:
• The ogrext1.pyx and ogrext2.pyx files now use separate C APIs defined in ogrext1.pxd and ogrex2.pxd. The other
extension modules have been refactored so that they do not depend on either of these modules and use subsets
of the GDAL/OGR API compatible with both GDAL 1.x and 2.x (#359).
Packaging:
• Source distributions now contain two different sources for the ogrext extension module. The ogrext1.c file will
be used with GDAL 1.x and the ogrext2.c file will be used with GDAL 2.x.
• New feature: enhancement of the –layer option for fio-cat and fio-dump to allow separate layers of one or more
multi-layer input files to be selected (#349).
• Raise ImportError if the active GDAL library version is >= 2.0 instead of failing unpredictably (#338, #341).
Support for GDAL>=2.0 is coming in Fiona 1.7.
• No changes to the library in this post-release version, but there is a significant change to the distributions
on PyPI: to help make Fiona more compatible with Shapely on OS X, the GDAL shared library included
in the macosx (only) binary wheels now statically links the GEOS library. See https://github.com/sgillies/
frs-wheel-builds/issues/5.
1.5. Changes 13
Fiona Documentation, Release 1.8.6
• Daytime has been decreasing in the Northern Hemisphere, but is now increasing again as it should.
• Non-UTF strings were being passed into OGR functions in some situations and on Windows this would some-
times crash a Python process (#303). Fiona now raises errors derived from UnicodeError when field names or
field values can’t be encoded.
• Providing only PROJ4 representations in the dataset meta property resulted in loss of CRS information when
using the fiona.open(. . . , **src.meta) as dst pattern (#265). This bug has been addressed by adding a crs_wkt
item to the‘ meta property and extending the fiona.open() and the collection constructor to look for and prioritize
this keyword argument.
• Bug fix: Fiona now deserializes JSON-encoded string properties provided by the OGR GeoJSON driver (#244,
#245, #246).
• Bug fix: proj4 data was not copied properly into binary distributions due to a typo (#254).
Special thanks to WFMU DJ Liz Berg for the awesome playlist that’s fueling my release sprint. Check it out at
http://wfmu.org/playlists/shows/62083. You can’t unhear Love Coffin.
14 Chapter 1. Fiona
Fiona Documentation, Release 1.8.6
• –dst-crs and –src-crs options for fio cat and collect (#159).
1.5. Changes 15
Fiona Documentation, Release 1.8.6
16 Chapter 1. Fiona
Fiona Documentation, Release 1.8.6
• Always register all GDAL and OGR drivers when entering the DriverManager context (#80, #92).
• Skip unsupported field types with a warning (#91).
• Allow OGR config options to be passed to fiona.drivers() (#90, #93).
• Add a bounds() function (#100).
• Turn on GPX driver.
• Use a context manager to manage drivers (#78), a backwards compatible but big change. Fiona is now compat-
ible with rasterio and plays better with the osgeo package.
• Allow ordering of written fields and preservation of field order when reading (#57).
1.5. Changes 17
Fiona Documentation, Release 1.8.6
• Python 2/3 compatibility in a single package. Pythons 2.6, 2.7, 3.3 now supported.
18 Chapter 1. Fiona
Fiona Documentation, Release 1.8.6
• Accessing file metadata (crs, schema, bounds) on never inspected closed files returns None without exceptions.
• Add a dict of supported_drivers and their supported modes.
• Raise ValueError for unsupported drivers and modes.
• Remove asserts from ogrext.pyx.
• Add validate_record method to collections.
• Add helpful coordinate system functions to fiona.crs.
• Promote use of fiona.open over fiona.collection.
• Handle Shapefile’s mix of LineString/Polygon and multis (#18).
• Allow users to specify width of shapefile text fields (#20).
• Replaced .opened attribute with .closed (product of collection() is always opened). Also a __del__() which will
close a Collection, but still not to be depended upon.
• Added writerecords method.
1.5. Changes 19
Fiona Documentation, Release 1.8.6
• Initial timezone-naive support for date, time, and datetime fields. Don’t use these field types if you can avoid
them. RFC 3339 datetimes in a string field are much better.
20 Chapter 1. Fiona
Fiona Documentation, Release 1.8.6
1.6 Credits
1.6. Credits 21
Fiona Documentation, Release 1.8.6
• Géraud <galak75@users.noreply.github.com>
• Hannes Gräuler <hgraeule@uos.de>
• Jesse Crocker <jesse@gaiagps.com>
• Juan Luis Cano Rodríguez <Juanlu001@users.noreply.github.com>
• Ludovic Delauné <ludotux@gmail.com>
• Martijn Visser <mgvisser@gmail.com>
• Matthew Perry <perrygeo@users.noreply.github.com>
• Michael Weisman <michael@urbanmapping.com>
• Oliver Tonnhofer <olt@bogosoft.com>
• Stefano Costa <steko@iosa.it>
• Stephane Poss <stephposs@gmail.com>
• dimlev <dimlev@gmail.com>
• wilsaj <wilson.andrew.j+github@gmail.com>
The GeoPandas project (Joris Van den Bossche et al.) has been a major driver for new features in 1.8.0.
Fiona would not be possible without the great work of Frank Warmerdam and other GDAL/OGR developers.
Some portions of this work were supported by a grant (for Pleiades) from the U.S. National Endowment for the
Humanities (http://www.neh.gov).
22 Chapter 1. Fiona
CHAPTER 2
Geographic information systems (GIS) help us plan, react to, and understand changes in our physical, political, eco-
nomic, and cultural landscapes. A generation ago, GIS was something done only by major institutions like nations
and cities, but it’s become ubiquitous today thanks to accurate and inexpensive global positioning systems, commodi-
tization of satellite imagery, and open source software.
The kinds of data in GIS are roughly divided into rasters representing continuous scalar fields (land surface temper-
ature or elevation, for example) and vectors representing discrete entities like roads and administrative boundaries.
Fiona is concerned exclusively with the latter. It is a Python wrapper for vector data access functions from the OGR
library. A very simple wrapper for minimalists. It reads data records from files as GeoJSON-like mappings and writes
the same kind of mappings as records back to files. That’s it. There are no layers, no cursors, no geometric oper-
ations, no transformations between coordinate systems, no remote method calls; all these concerns are left to other
Python packages such as Shapely and pyproj and Python language protocols. Why? To eliminate unnecessary
complication. Fiona aims to be simple to understand and use, with no gotchas.
Please understand this: Fiona is designed to excel in a certain range of tasks and is less optimal in others. Fiona
trades memory and speed for simplicity and reliability. Where OGR’s Python bindings (for example) use C pointers,
Fiona copies vector data from the data source to Python objects. These are simpler and safer to use, but more memory
intensive. Fiona’s performance is relatively more slow if you only need access to a single record field – and of course
if you just want to reproject or filter data files, nothing beats the ogr2ogr program – but Fiona’s performance is much
23
Fiona Documentation, Release 1.8.6
better than OGR’s Python bindings if you want all fields and coordinates of a record. The copying is a constraint, but
it simplifies programs. With Fiona, you don’t have to track references to C objects to avoid crashes, and you can work
with vector data using familiar Python mapping accessors. Less worry, less time spent reading API documentation.
The first example of using Fiona is this: copying records from one file to another, adding two attributes and making
sure that all polygons are facing “up”. Orientation of polygons is significant in some applications, extruded polygons
in Google Earth for one. No other library (like Shapely) is needed here, which keeps it uncomplicated. There’s a
test_uk file in the Fiona repository for use in this and other examples.
import datetime
import logging
import sys
import fiona
logging.basicConfig(stream=sys.stderr, level=logging.INFO)
def signed_area(coords):
"""Return the signed area enclosed by a ring using the linear time
algorithm at http://www.cgafaq.info/wiki/Polygon_Area. A value >= 0
indicates a counter-clockwise oriented ring.
"""
xs, ys = map(list, zip(*coords))
xs.append(xs[1])
ys.append(ys[1])
return sum(xs[i]*(ys[i+1]-ys[i-1]) for i in range(1, len(coords)))/2.0
# Create a sink for processed features with the same format and
# coordinate reference system as the source.
with fiona.open(
'oriented-ccw.shp', 'w',
crs=source.crs,
driver=source.driver,
schema=sink_schema,
) as sink:
for f in source:
try:
sink.write(f)
except Exception, e:
logging.exception("Error processing feature %s:", f['id'])
# The sink file is written to disk and closed when its block ends.
Discrete geographic features are usually represented in geographic information systems by records. The characteristics
of records and their semantic implications are well known [Kent1978]. Among those most significant for geographic
data: records have a single type, all records of that type have the same fields, and a record’s fields concern a single
geographic feature. Different systems model records in different ways, but the various models have enough in common
that programmers have been able to create useful abstract data models. The OGR model is one. Its primary entities
are Data Sources, Layers, and Features. Features have not fields, but attributes and a Geometry. An OGR Layer
contains Features of a single type (“roads” or “wells”, for example). The GeoJSON model is a bit more simple,
keeping Features and substituting Feature Collections for OGR Data Sources and Layers. The term “Feature” is thus
overloaded in GIS modeling, denoting entities in both our conceptual and data models.
Various formats for record files exist. The ESRI Shapefile [ESRI1998] has been, at least in the United States, the most
significant of these up to about 2005 and remains popular today. It is a binary format. The shape fields are stored
in one .shp file and the other fields in another .dbf file. The GeoJSON [GeoJSON] format, from 2008, proposed a
human readable text format in which geometry and other attribute fields are encoded together using Javascript Object
Notation [JSON]. In GeoJSON, there’s a uniformity of data access. Attributes of features are accessed in the same
manner as attributes of a feature collection. Coordinates of a geometry are accessed in the same manner as features of
a collection.
The GeoJSON format turns out to be a good model for a Python API. JSON objects and Python dictionaries are
semantically and syntactically similar. Replacing object-oriented Layer and Feature APIs with interfaces based on
Python mappings provides a uniformity of access to data and reduces the amount of time spent reading documentation.
A Python programmer knows how to use a mapping, so why not treat features as dictionaries? Use of existing Python
idioms is one of Fiona’s major design principles.
TL;DR
Fiona subscribes to the conventional record model of data, but provides GeoJSON-like access to the data via Python
file-like and mapping protocols.
Reading a GIS vector file begins by opening it in mode 'r' using Fiona’s open() function. It returns an opened
Collection object.
API Change
fiona.collection() is deprecated, but aliased to fiona.open() in version 0.9.
>>> next(c)
{'geometry': {'type': 'Polygon', 'coordinates': ...
>>> len(list(c))
48
Note that list() iterates over the entire collection, effectively emptying it as with a Python file.
>>> next(c)
Traceback (most recent call last):
...
StopIteration
>>> len(list(c))
0
Seeking the beginning of the file is not supported. You must reopen the collection to get back to the beginning.
>>> c = fiona.open('docs/data/test_uk.shp')
>>> len(list(c))
48
File Encoding
The format drivers will attempt to detect the encoding of your data, but may fail. In my experience GDAL
1.7.2 (for example) doesn’t detect that the encoding of the Natural Earth dataset is Windows-1252. In this case,
the proper encoding can be specified explicitly by using the encoding keyword parameter of fiona.open():
encoding='Windows-1252'.
New in version 0.9.1.
'type': 'Feature'}
Note that these indices are controlled by GDAL, and do not always follow Python conventions. They can start from 0,
1 (e.g. geopackages), or even other values, and have no guarantee of contiguity. Negative indices will only function
correctly if indices start from 0 and are contiguous.
A Collection involves external resources. There’s no guarantee that these will be released unless you explicitly
close() the object or use a with statement. When a Collection is a context guard, it is closed no matter what
happens within the block.
>>> try:
... with fiona.open('docs/data/test_uk.shp') as c:
... print(len(list(c)))
(continues on next page)
An exception is raised in the with block above, but as you can see from the print statement in the except clause
c.__exit__() (and thereby c.close()) has been called.
Important: Always call close() or use with and you’ll never stumble over tied-up external resources, locked
files, etc.
In addition to attributes like those of file (name, mode, closed), a Collection has a read-only driver
attribute which names the OGR format driver used to open the vector file.
>>> c = fiona.open('docs/data/test_uk.shp')
>>> c.driver
'ESRI Shapefile'
The coordinate reference system (CRS) of the collection’s vector data is accessed via a read-only crs attribute.
>>> c.crs
{'no_defs': True, 'ellps': 'WGS84', 'datum': 'WGS84', 'proj': 'longlat'}
The number of records in the collection’s file can be obtained via Python’s built in len() function.
>>> len(c)
48
The minimum bounding rectangle (MBR) or bounds of the collection’s records is obtained via a read-only bounds
attribute.
>>> c.bounds
(-8.621389, 49.911659, 1.749444, 60.844444)
Finally, the schema of its record type (a vector file has a single type of record, remember) is accessed via a read-only
schema attribute. It has ‘geometry’ and ‘properties’ items. The former is a string and the latter is an ordered dict
with items having the same order as the fields in the data file.
Fiona takes a less is more approach to record types and schemas. Data about record types is structured as closely to
data about records as can be done. Modulo a record’s ‘id’ key, the keys of a schema mapping are the same as the keys
of the collection’s record mappings.
The values of the schema mapping are either additional mappings or field type names like ‘Polygon’, ‘float’, and ‘str’.
The corresponding Python types can be found in a dictionary named fiona.FIELD_TYPES_MAP.
>>> pprint.pprint(fiona.FIELD_TYPES_MAP)
{'date': <class 'fiona.rfc3339.FionaDateType'>,
'datetime': <class 'fiona.rfc3339.FionaDateTimeType'>,
'float': <class 'float'>,
'int': <class 'int'>,
'str': <class 'str'>,
'time': <class 'fiona.rfc3339.FionaTimeType'>}
In a nutshell, the types and their names are as near to what you’d expect in Python (or Javascript) as possible. The ‘str’
vs ‘unicode’ muddle is a fact of life in Python < 3.0. Fiona records have Unicode strings, but their field type name is
‘str’ (looking forward to Python 3).
>>> type(rec['properties']['CNTRY_NAME'])
<class 'str'>
(continues on next page)
String type fields may also indicate their maximum width. A value of ‘str:25’ indicates that all values will be no
longer than 25 characters. If this value is used in the schema of a file opened for writing, values of that property will
be truncated at 25 characters. The default width is 80 chars, which means ‘str’ and ‘str:80’ are more or less equivalent.
Fiona provides a function to get the width of a property.
The example above is for Python 3. With Python 2, the type of ‘str’ properties is ‘unicode’.
>>> prop_type('str:25')
<class 'unicode'>
Fiona supports the geometry types in GeoJSON and their 3D variants. This means that the value of a schema’s
geometry item will be one of the following:
• Point
• LineString
• Polygon
• MultiPoint
• MultiLineString
• MultiPolygon
• GeometryCollection
• 3D Point
• 3D LineString
• 3D Polygon
• 3D MultiPoint
• 3D MultiLineString
• 3D MultiPolygon
• 3D GeometryCollection
The last seven of these, the 3D types, apply only to collection schema. The geometry types of features are always
one of the first seven. A ‘3D Point’ collection, for example, always has features with geometry type ‘Point’. The
coordinates of those geometries will be (x, y, z) tuples.
Note that one of the most common vector data formats, Esri’s Shapefile, has no ‘MultiLineString’ or ‘MultiPoly-
gon’ schema geometries. However, a Shapefile that indicates ‘Polygon’ in its schema may yield either ‘Polygon’ or
‘MultiPolygon’ features.
A record you get from a collection is a Python dict structured exactly like a GeoJSON Feature. Fiona records are
self-describing; the names of its fields are contained within the data structure and the values in the fields are typed
properly for the type of record. Numeric field values are instances of type int and float, for example, not strings.
>>> pprint.pprint(rec)
{'geometry': {'coordinates': [[(-4.663611, 51.158333),
(-4.669168, 51.159439),
(-4.673334, 51.161385),
(-4.674445, 51.165276),
(-4.67139, 51.185272),
(-4.669445, 51.193054),
(-4.665556, 51.195),
(-4.65889, 51.195),
(-4.656389, 51.192215),
(-4.646389, 51.164444),
(-4.646945, 51.160828),
(-4.651668, 51.159439),
(-4.663611, 51.158333)]],
'type': 'Polygon'},
'id': '1',
'properties': {'CAT': 232.0,
'FIPS_CNTRY': 'UK',
'CNTRY_NAME': 'United Kingdom',
'AREA': 244820.0,
'POP_CNTRY': 60270708.0}}
The record data has no references to the Collection from which it originates or to any other external resource. It’s
entirely independent and safe to use in any way. Closing the collection does not affect the record at all.
>>> c.close()
>>> rec['id']
'1'
A record has an id key. As in the GeoJSON specification, its corresponding value is a string unique within the data
file.
>>> c = fiona.open('docs/data/test_uk.shp')
>>> rec = next(c)
(continues on next page)
OGR Details
In the OGR model, feature ids are long integers. Fiona record ids are therefore usually string representations of integer
record indexes.
A record has a properties key. Its corresponding value is a mapping: an ordered dict to be precise. The keys of
the properties mapping are the same as the keys of the properties mapping in the schema of the collection the record
comes from (see above).
>>> pprint.pprint(rec['properties'])
{'CAT': 232.0,
'FIPS_CNTRY': 'UK',
'CNTRY_NAME': 'United Kingdom',
'AREA': 244820.0,
'POP_CNTRY': 60270708.0}
A record has a geometry key. Its corresponding value is a mapping with type and coordinates keys.
>>> pprint.pprint(rec['geometry'])
{'coordinates': [[(0.899167, 51.357216),
(0.885278, 51.35833),
(0.7875, 51.369438),
(0.781111, 51.370552),
(0.766111, 51.375832),
(0.759444, 51.380829),
(0.745278, 51.39444),
(0.740833, 51.400276),
(0.735, 51.408333),
(0.740556, 51.429718),
(0.748889, 51.443604),
(0.760278, 51.444717),
(0.791111, 51.439995),
(0.892222, 51.421387),
(0.904167, 51.418884),
(0.908889, 51.416939),
(0.930555, 51.398888),
(0.936667, 51.393608),
(0.943889, 51.384995),
(0.9475, 51.378609),
(0.947778, 51.374718),
(0.946944, 51.371109),
(0.9425, 51.369164),
(0.904722, 51.358055),
(0.899167, 51.357216)]],
'type': 'Polygon'}
Since the coordinates are just tuples, or lists of tuples, or lists of lists of tuples, the type tells you how to interpret
them.
Type Coordinates
Point A single (x, y) tuple
LineString A list of (x, y) tuple vertices
Polygon A list of rings (each a list of (x, y) tuples)
MultiPoint A list of points (each a single (x, y) tuple)
MultiLineString A list of lines (each a list of (x, y) tuples)
MultiPolygon A list of polygons (see above)
Fiona, like the GeoJSON format, has both Northern Hemisphere “North is up” and Cartesian “X-Y” biases. The values
within a tuple that denoted as (x, y) above are either (longitude E of the prime meridian, latitude N of the equator)
or, for other projected coordinate systems, (easting, northing).
In a proper, well-scrubbed vector data file the geometry mappings explained above are representations of geometric
objects made up of point sets. The following
represents not just two points, but the set of infinitely many points along the line of length 1.0 from (0.0, 0.0) to
(0.0, 1.0). In the application of point set theory commonly called Simple Features Access [SFA] two geometric
objects are equal if their point sets are equal whether they are equal in the Python sense or not. If you have Shapely
(which implements Simple Features Access) installed, you can see this in by verifying the following.
Dirty data
Some files may contain vectors that are invalid from a simple features standpoint due to accident (inadequate quality
control on the producer’s end), intention (“dirty” vectors saved to a file for special treatment) or discrepancies of the
numeric precision models (Fiona can’t handle fixed precision models yet). Fiona doesn’t sniff for or attempt to clean
dirty data, so make sure you’re getting yours from a clean source.
A vector file can be opened for writing in mode 'a' (append) or mode 'w' (write).
Note
The in situ “update” mode of OGR is quite format dependent and is therefore not supported by Fiona.
Let’s start with the simplest if not most common use case, adding new records to an existing file. The file is copied
before modification and a suitable record extracted in the example below.
The coordinate reference system. format, and schema of the file are already defined, so it’s opened with just two
arguments as for reading, but in 'a' mode. The new record is written to the end of the file using the write()
method. Accordingly, the length of the file grows from 48 to 49.
The record you write must match the file’s schema (because a file contains one type of record, remember). You’ll get
a ValueError if it doesn’t.
Now, what about record ids? The id of a record written to a file is ignored and replaced by the next value appropriate
for the file. If you read the file just appended to above,
You’ll see that the id of '-1' which the record had when written is replaced by '48'.
The write() method writes a single record to the collection’s file. Its sibling writerecords() writes a sequence
(or iterator) of records.
Duplication
Fiona’s collections do not guard against duplication. The code above will write 3 duplicate records to the file, and they
will be given unique sequential ids.
Buffering
Fiona’s output is buffered. The records passed to write() and writerecords() are flushed to disk when the
collection is closed. You may also call flush() periodically to write the buffer contents to disk.
Writing a new file is more complex than appending to an existing file because the file CRS, format, and schema
have not yet been defined and must be done so by the programmer. Still, it’s not very complicated. A schema is
just a mapping, as described above. A CRS is also just a mapping, and the possible formats are enumerated in the
fiona.supported_drivers list.
Review the parameters of our demo file.
Because the properties of the source schema are ordered and are passed in the same order to the write-mode collection,
the written file’s fields have the same order as those of the source file.
$ ogrinfo /tmp/foo.shp foo -so
INFO: Open of `/tmp/foo.shp'
using driver `ESRI Shapefile' successful.
The meta attribute makes duplication of a file’s meta properties even easier.
>>> source = fiona.open('docs/data/test_uk.shp')
>>> sink = fiona.open('/tmp/foo.shp', 'w', **source.meta)
To write a new file from scratch we have to define our own specific driver, crs and schema.
To ensure the order of the attribute fields is predictable, in both the schema and the actual manifestation as feature
attributes, we will use ordered dictionaries.
>>> from collections import OrderedDict
Consider the following record, structured in accordance to the Python geo protocol, representing the Eiffel Tower
using a point geometry with UTM coordinates in zone 31N.
>>> eiffel_tower = {
... 'geometry': {
... 'type': 'Point',
... 'coordinates': (448252, 5411935)
(continues on next page)
>>> landmarks_schema = {
... 'geometry': 'Point',
... 'properties': OrderedDict([
... ('name', 'str'),
... ('height', 'float'),
... ('view', 'str'),
... ('year', 'int')
... ])
... }
The coordinate reference system of these landmark coordinates is ETRS89 / UTM zone 31N which is referenced in
the EPSG database as EPSG:25831.
Having specified schema, crs and driver, we are ready to open a file for writing our record:
Beginning with Fiona 1.0.1, the ‘properties’ item of fiona.open()’s ‘schema’ keyword argument may be an or-
dered dict or a list of (key, value) pairs, specifying an ordering that carries into written files. If an ordinary dict is
given, the ordering is determined by the output of that dict’s items() method.
For example, since
a schema of {'properties': {'bar': 'int', 'foo': 'str'}} will produce a shapefile where the
first field is ‘foo’ and the second field is ‘bar’. If you want ‘bar’ to be the first field, you must use a list of property
items
c = fiona.open(
'/tmp/file.shp',
'w',
schema={'properties': [('bar', 'int'), ('foo', 'str')], ...},
... )
or an ordered dict.
c = fiona.open(
'/tmp/file.shp',
'w',
schema={'properties': schema_props, ...},
... )
If you write 3D coordinates, ones having (x, y, z) tuples, to a 2D file (‘Point’ schema geometry, for example) the z
values will be lost.
If you write 2D coordinates, ones having only (x, y) tuples, to a 3D file (‘3D Point’ schema geometry, for example) a
default z value of 0 will be provided.
GDAL/OGR has a large number of features that are controlled by global or thread-local configuration options.
Fiona allows you to configure these options using a context manager, fiona.Env. This class’s constructor takes
GDAL/OGR configuration options as keyword arguments. To see debugging information from GDAL/OGR, for ex-
ample, you may do the following.
import logging
import fiona
(continues on next page)
logging.basicConfig(level=logging.DEBUG)
with fiona.Env(CPL_DEBUG=True):
fiona.open('tests/data/coutwildrnp.shp')
The following extra messages will appear in the Python logger’s output.:
If you call fiona.open() with no surrounding Env environment, one will be created for you.
When your program exits the environent’s with block the configuration reverts to its previous state.
One of the most important uses of fiona.Env is to set credentials for accessing data stored in AWS S3 or another
cloud storage system.
with fiona.Env(
session=AWSSession(
aws_access_key_id="key",
aws_secret_access_key="secret",
)
):
fiona.open("zip+s3://example-bucket/example.zip")
The AWSSession class is currently the only credential session manager in Fiona. The source code has an example
of how classes for other cloud storage providers may be implemented. AWSSession relies upon boto3 and botocore,
which will be installed as extra dependencies of Fiona if you run pip install fiona[s3].
If you call fiona.open() with no surrounding Env and pass a path to an S3 object, a session will be created for
you using code equivalent to the following code.
import boto3
with fiona.Env(session=AWSSession(boto3.Session())):
fiona.open('zip+s3://fiona-testing/coutwildrnp.zip')
With some vector data formats a spatial index accompanies the data file, allowing efficient bounding box searches.
A collection’s items() method returns an iterator over pairs of FIDs and records that intersect a given (minx,
miny, maxx, maxy) bounding box or geometry object. The collection’s own coordinate reference system (see
below) is used to interpret the box’s values. If you want a list of the iterator’s items, pass it to Python’s builtin list()
as shown below.
>>> c = fiona.open('docs/data/test_uk.shp')
>>> hits = list(c.items(bbox=(-5.0, 55.0, 0.0, 60.0)))
>>> len(hits)
7
The iterator method takes the same stop or start, stop[, step] slicing arguments as itertools.
islice(). To get just the first two items from that iterator, pass a stop index.
To get the third through fifth items from that iterator, pass start and stop indexes.
To filter features by property values, use Python’s builtin filter() and lambda or your own filter function that
takes a single feature record and returns True or False.
Up to this point, only simple datasets with one thematic layer or feature type per file have been shown and the venerable
Esri Shapefile has been the primary example. Other GIS data formats can encode multiple layers or feature types within
a single file or directory. Esri’s File Geodatabase is one example of such a format. A more useful example, for the
purpose of this manual, is a directory comprising multiple shapefiles. The following three shell commands will create
just such a two layered data source from the test data distributed with Fiona.
$ mkdir /tmp/data
$ ogr2ogr /tmp/data/ docs/data/test_uk.shp test_uk -nln foo
$ ogr2ogr /tmp/data/ docs/data/test_uk.shp test_uk -nln bar
The layers of a data source can be listed using fiona.listlayers(). In the shapefile format case, layer names
match base names of the files.
>>> fiona.listlayers('/tmp/data')
['bar', 'foo']
Unlike OGR, Fiona has no classes representing layers or data sources. To access the features of a layer, open a
collection using the path to the data source and specify the layer by name using the layer keyword.
>>> import pprint
>>> datasrc_path = '/tmp/data'
>>> for name in fiona.listlayers(datasrc_path):
... with fiona.open(datasrc_path, layer=name) as c:
... pprint.pprint(c.schema)
...
{'geometry': 'Polygon',
'properties': {'CAT': 'float:16',
'FIPS_CNTRY': 'str',
'CNTRY_NAME': 'str',
'AREA': 'float:15.2',
'POP_CNTRY': 'float:15.2'}}
{'geometry': 'Polygon',
'properties': {'CAT': 'float:16',
'FIPS_CNTRY': 'str',
'CNTRY_NAME': 'str',
'AREA': 'float:15.2',
'POP_CNTRY': 'float:15.2'}}
If no layer is specified, fiona.open() returns an open collection using the first layer.
>>> with fiona.open(datasrc_path) as c:
... c.name == fiona.listlayers(datasrc_path)[0]
...
True
The most general way to open a shapefile for reading, using all of the parameters of fiona.open(), is to treat it as
a data source with a named layer.
>>> fiona.open('docs/data/test_uk.shp', 'r', layer='test_uk')
In practice, it is fine to rely on the implicit first layer and default 'r' mode and open a shapefile like this:
>>> fiona.open('docs/data/test_uk.shp')
To write an entirely new layer to a multilayer data source, simply provide a unique name to the layer keyword argu-
ment.
>>> 'wah' not in fiona.listlayers(datasrc_path)
True
>>> with fiona.open(datasrc_path, layer='bar') as c:
... with fiona.open(datasrc_path, 'w', layer='wah', **c.meta) as d:
(continues on next page)
In 'w' mode, existing layers will be overwritten if specified, just as normal files are overwritten by Python’s open()
function.
Zip and Tar archives can be treated as virtual filesystems and collections can be made from paths and layers within
them. In other words, Fiona lets you read zipped shapefiles. For example, make a Zip archive from the shapefile
distributed with Fiona.
The vfs keyword parameter for fiona.listlayers() and fiona.open() may be an Apache Commons VFS
style string beginning with “zip://” or “tar://” and followed by an absolute or relative path to the archive file. When
this parameter is used, the first argument to must be an absolute path within that archive. The layers in that Zip archive
are:
The feature collection in this stream of bytes can be accessed by wrapping it in an instance of ZipMemoryFile.
New in 1.8.0
Fiona installs a script named dumpgj. It converts files to GeoJSON with JSON-LD context as an option and is
intended to be an upgrade to “ogr2ogr -f GeoJSON”.
$ dumpgj --help
usage: dumpgj [-h] [-d] [-n N] [--compact] [--encoding ENC]
[--record-buffered] [--ignore-errors] [--use-ld-context]
[--add-ld-context-item TERM=URI]
infile [outfile]
positional arguments:
infile input file name
outfile output file name, defaults to stdout if omitted
optional arguments:
-h, --help show this help message and exit
-d, --description serialize file's data description (schema) only
-n N, --indent N indentation level in N number of chars
--compact use compact separators (',', ':')
--encoding ENC Specify encoding of the input file
--record-buffered Economical buffering of writes at record, not
collection (default), level
--ignore-errors log errors but do not stop serialization
--use-ld-context add a JSON-LD context to JSON output
--add-ld-context-item TERM=URI
map a term to a URI and add it to the output's JSON LD
context
This manual is a work in progress and will grow and improve with Fiona. Questions and suggestions are very welcome.
Please feel free to use the issue tracker or email the author directly.
Do see the README for installation instructions and information about supported versions of Python and other
software dependencies.
Fiona would not be possible without the contributions of other developers, especially Frank Warmerdam and Even
Rouault, the developers of GDAL/OGR; and Mike Weisman, who saved Fiona from neglect and obscurity.
fiona
3.1.1 Subpackages
fiona.fio package
Submodules
fiona.fio.bounds module
$ fio bounds
fiona.fio.calc module
fiona.fio.cat module
$ fio cat
fiona.fio.collect module
$ fio collect
fiona.fio.distrib module
$ fio distrib
45
Fiona Documentation, Release 1.8.6
fiona.fio.dump module
$ fio dump
fiona.fio.env module
$ fio env
fiona.fio.filter module
$ fio filter
fiona.fio.helpers module
fiona.fio.info module
$ fio info
fiona.fio.insp module
$ fio insp
fiona.fio.load module
$ fio load
fiona.fio.ls module
$ fiona ls
46 Chapter 3. fiona
Fiona Documentation, Release 1.8.6
fiona.fio.main module
Main click group for the CLI. Needs to be isolated for entry-point loading.
fiona.fio.main.configure_logging(verbosity)
fiona.fio.options module
fiona.fio.rm module
Module contents
3.1.2 Submodules
close()
In append or write mode, flushes data to disk, then ends access.
closed
False if data can be accessed, otherwise True.
crs
Returns a Proj4 string.
crs_wkt
Returns a WKT string.
driver
Returns the name of the proper OGR driver.
filter(*args, **kwds)
Returns an iterator over records, but filtered by a test for spatial intersection with the provided bbox, a
(minx, miny, maxx, maxy) tuple or a geometry mask.
Positional arguments stop or start, stop[, step] allows iteration to skip over items or stop at a
specific item.
flush()
Flush the buffer.
get(item)
guard_driver_mode()
items(*args, **kwds)
Returns an iterator over FID, record pairs, optionally filtered by a test for spatial intersection with the
provided bbox, a (minx, miny, maxx, maxy) tuple or a geometry mask.
Positional arguments stop or start, stop[, step] allows iteration to skip over items or stop at a
specific item.
keys(*args, **kwds)
Returns an iterator over FIDs, optionally filtered by a test for spatial intersection with the provided bbox,
a (minx, miny, maxx, maxy) tuple or a geometry mask.
Positional arguments stop or start, stop[, step] allows iteration to skip over items or stop at a
specific item.
meta
Returns a mapping with the driver, schema, crs, and additional properties.
next()
Returns next record from iterator.
profile
Returns a mapping with the driver, schema, crs, and additional properties.
schema
Returns a mapping describing the data schema.
The mapping has ‘geometry’ and ‘properties’ items. The former is a string such as ‘Point’ and the latter is
an ordered mapping that follows the order of fields in the data file.
validate_record(record)
Compares the record to the collection’s schema.
Returns True if the record matches, else False.
48 Chapter 3. fiona
Fiona Documentation, Release 1.8.6
validate_record_geometry(record)
Compares the record’s geometry to the collection’s schema.
Returns True if the record matches, else False.
values(*args, **kwds)
Returns an iterator over records, but filtered by a test for spatial intersection with the provided bbox, a
(minx, miny, maxx, maxy) tuple or a geometry mask.
Positional arguments stop or start, stop[, step] allows iteration to skip over items or stop at a
specific item.
write(record)
Stages a record for writing to disk.
writerecords(records)
Stages multiple records for writing to disk.
fiona.collection.get_filetype(bytesbuf )
Detect compression type of bytesbuf.
ZIP only. TODO: add others relevant to GDAL/OGR.
fiona.compat.strencode(instr, encoding=’utf-8’)
50 Chapter 3. fiona
Fiona Documentation, Release 1.8.6
fiona.env.hascreds()
fiona.env.hasenv()
fiona.env.require_gdal_version(version, param=None, values=None, is_max_version=False,
reason=”)
A decorator that ensures the called function or parameters are supported by the runtime version of GDAL. Raises
GDALVersionError if conditions are not met.
Examples:
@require_gdal_version(‘2.2’) def some_func():
calling some_func with a runtime version of GDAL that is < 2.2 raises a GDALVersionErorr.
@require_gdal_version(‘2.2’, param=’foo’) def some_func(foo=’bar’):
calling some_func with parameter foo of any value on GDAL < 2.2 raises a GDALVersionError.
@require_gdal_version(‘2.2’, param=’foo’, values=(‘bar’,)) def some_func(foo=None):
calling some_func with parameter foo and value bar on GDAL < 2.2 raises a GDALVersionError.
version: tuple, string, or GDALVersion param: string (optional, default: None)
If values are absent, then all use of this parameter with a value other than default value requires at
least GDAL version.
values: tuple, list, or set (optional, default: None) contains values that require at least GDAL version. param
is required for values.
is_max_version: bool (optional, default: False) if True indicates that the version provided is the maximum
version allowed, instead of requiring at least that version.
reason: string (optional: default: ‘’) custom error message presented to user in addition to message about
GDAL version. Use this to provide an explanation of what changed if necessary context to the user.
wrapped function
fiona.env.setenv(**options)
Set options in the existing environment.
exception fiona.errors.CRSError
Bases: fiona.errors.FionaValueError
When a crs mapping has neither init or proj items.
exception fiona.errors.DataIOError
Bases: OSError
IO errors involving driver registration or availability.
exception fiona.errors.DatasetDeleteError
Bases: OSError
Failure to delete a dataset
exception fiona.errors.DriverError
Bases: fiona.errors.FionaValueError
Encapsulates unsupported driver and driver mode errors.
52 Chapter 3. fiona
Fiona Documentation, Release 1.8.6
exception fiona.errors.DriverIOError
Bases: OSError
A format specific driver error.
exception fiona.errors.DriverSupportError
Bases: fiona.errors.DriverIOError
Driver does not support schema
exception fiona.errors.EnvError
Bases: fiona.errors.FionaError
Environment Errors
exception fiona.errors.FieldNameEncodeError
Bases: UnicodeEncodeError
Failure to encode a field name.
exception fiona.errors.FionaDeprecationWarning
Bases: UserWarning
A warning about deprecation of Fiona features
exception fiona.errors.FionaError
Bases: Exception
Base Fiona error
exception fiona.errors.FionaValueError
Bases: ValueError
Fiona-specific value errors
exception fiona.errors.GDALVersionError
Bases: fiona.errors.FionaError
Raised if the runtime version of GDAL does not meet the required version of GDAL.
exception fiona.errors.GeometryTypeValidationError
Bases: fiona.errors.FionaValueError
Tried to write a geometry type not specified in the schema
exception fiona.errors.SchemaError
Bases: fiona.errors.FionaValueError
When a schema mapping has no properties or no geometry.
exception fiona.errors.TransactionError
Bases: RuntimeError
Failure relating to GDAL transactions
exception fiona.errors.UnsupportedGeometryTypeError
Bases: KeyError
When a OGR geometry type isn’t supported by Fiona.
fiona.inspector.main(srcfile)
class fiona.ogrext.FeatureBuilder
Bases: object
Build Fiona features from OGR feature pointers.
54 Chapter 3. fiona
Fiona Documentation, Release 1.8.6
No OGR objects are allocated by this function and the feature argument is not destroyed.
class fiona.ogrext.ItemsIterator
Bases: fiona.ogrext.Iterator
class fiona.ogrext.Iterator
Bases: object
Provides iterated access to feature data.
class fiona.ogrext.KeysIterator
Bases: fiona.ogrext.Iterator
class fiona.ogrext.MemoryFileBase
Bases: object
Base for a BytesIO-like class backed by an in-memory file.
close()
Close MemoryFile and release allocated memory.
exists()
Test if the in-memory file exists.
bool True if the in-memory file exists.
read()
Read size bytes from MemoryFile.
seek()
Seek to position in MemoryFile.
tell()
Tell current position in MemoryFile.
write()
Write data bytes to MemoryFile
class fiona.ogrext.OGRFeatureBuilder
Bases: object
Builds an OGR Feature from a Fiona feature mapping.
Allocates one OGR Feature which should be destroyed by the caller. Borrows a layer definition from the
collection.
class fiona.ogrext.Session
Bases: object
get()
Provides access to feature data by FID.
Supports Collection.__contains__().
get_crs()
Get the layer’s CRS
CRS
get_crs_wkt()
get_driver()
get_extent()
get_feature()
Provides access to feature data by FID.
Supports Collection.__contains__().
get_fileencoding()
DEPRECATED
get_length()
get_schema()
has_feature()
Provides access to feature data by FID.
Supports Collection.__contains__().
isactive()
start()
stop()
class fiona.ogrext.WritingSession
Bases: fiona.ogrext.Session
start()
sync()
Syncs OGR to disk.
writerecs()
Writes buffered records to OGR.
fiona.ogrext.buffer_to_virtual_file()
Maps a bytes buffer to a virtual file.
ext is empty or begins with a period and contains at most one period.
fiona.ogrext.featureRT()
fiona.ogrext.remove_virtual_file()
56 Chapter 3. fiona
Fiona Documentation, Release 1.8.6
is_remote
Test if the path is a remote, network URI
name
The parsed path’s original URI
path
scheme
class fiona.path.Path
Bases: object
Base class for dataset paths
class fiona.path.UnparsedPath(path)
Bases: fiona.path.Path
Encapsulates legacy GDAL filenames
path [str] The legacy GDAL filename.
name
The unparsed path’s original path
path
fiona.path.parse_path(path)
Parse a dataset’s identifier or path into its parts
path [str or path-like object] The path to be parsed.
ParsedPath or UnparsedPath
When legacy GDAL filenames are encountered, they will be returned in a UnparsedPath.
fiona.path.vsi_path(path)
Convert a parsed path to a GDAL VSI path
path [Path] A ParsedPath or UnparsedPath object.
str
class fiona.rfc3339.FionaDateTimeType
Bases: str
Dates and times.
class fiona.rfc3339.FionaDateType
Bases: str
Dates without time.
class fiona.rfc3339.FionaTimeType
Bases: str
Times without dates.
class fiona.rfc3339.group_accessor(m)
Bases: object
group(i)
fiona.rfc3339.parse_date(text)
Given a RFC 3339 date, returns a tz-naive datetime tuple
fiona.rfc3339.parse_datetime(text)
Given a RFC 3339 datetime, returns a tz-naive datetime tuple
fiona.rfc3339.parse_time(text)
Given a RFC 3339 time, returns a tz-naive datetime tuple
fiona.schema.normalize_field_type()
Normalize free form field types to an element of FIELD_TYPES
ftype [str] A type:width format like ‘int:9’ or ‘str:255’
58 Chapter 3. fiona
Fiona Documentation, Release 1.8.6
classmethod hascreds(config)
Determine if the given configuration has proper credentials
cls [class] A Session class.
config [dict] GDAL configuration as a dict.
bool
class fiona.session.Session
Bases: object
Base for classes that configure access to secured resources.
credentials [dict] Keys and values for session credentials.
This class is not intended to be instantiated.
static from_foreign_session(session, cls=None)
Create a session object matching the foreign session.
session [obj] A foreign session object.
cls [Session class, optional] The class to return.
Session
static from_path(path, *args, **kwargs)
Create a session object suited to the data at path.
path [str] A dataset path or identifier.
args [sequence] Positional arguments for the foreign session constructor.
kwargs [dict] Keyword arguments for the foreign session constructor.
Session
get_credential_options()
Get credentials as GDAL configuration options
dict
xp, yp: list of float A pair of transformed coordinate sequences. The elements of xp and yp correspond exactly
to the elements of the xs and ys input parameters.
obj A new GeoJSON-like geometry with transformed coordinates. Note that if the output is at the antimeridian,
it may be cut and of a different geometry type than the input, e.g., a polygon input may result in multi-
polygon output.
>>> transform_geom(
... 'EPSG:4326', 'EPSG:26953',
... {'type': 'Point', 'coordinates': [-105.0, 40.0]})
{'type': 'Point', 'coordinates': (957097.0952383667, 378940.8419189212)}
60 Chapter 3. fiona
Fiona Documentation, Release 1.8.6
How minimal? Fiona can read features as mappings from shapefiles or other GIS vector formats and write mappings
as features to files using the same formats. That’s all. There aren’t any feature or geometry classes. Features and their
geometries are just data.
A Fiona feature is a Python mapping inspired by the GeoJSON format. It has id, ‘geometry‘, and properties keys. The
value of id is a string identifier unique within the feature’s parent collection. The geometry is another mapping with
type and coordinates keys. The properties of a feature is another mapping corresponding to its attribute table. For
example:
{‘id’: ‘1’, ‘geometry’: {‘type’: ‘Point’, ‘coordinates’: (0.0, 0.0)}, ‘properties’: {‘label’: u’Null Island’}
}
is a Fiona feature with a point geometry and one property.
Features are read and written using objects returned by the collection function. These Collection objects are
a lot like Python file objects. A Collection opened in reading mode serves as an iterator over features. One
opened in a writing mode provides a write method.
Usage
Here’s an example of reading a select few polygon features from a shapefile and for each, picking off the first vertex
of the exterior ring of the polygon and using that as the point geometry for a new feature writing to a “points.shp” file.
Because Fiona collections are context managers, they are closed and (in writing modes) flush contents to disk when
their with blocks end.
fiona.bounds(ob)
Returns a (minx, miny, maxx, maxy) bounding box.
The ob may be a feature record or geometry.
fiona.listlayers(path, vfs=None)
Returns a list of layer names in their index order.
The required path argument may be an absolute or relative file or directory path.
A virtual filesystem can be specified. The vfs parameter may be an Apache Commons VFS style string begin-
ning with “zip://” or “tar://”“. In this case, the path must be an absolute path within that container.
fiona.open(fp, mode=’r’, driver=None, schema=None, crs=None, encoding=None, layer=None,
vfs=None, enabled_drivers=None, crs_wkt=None, **kwargs)
Open a collection for read, append, or write
In write mode, a driver name such as “ESRI Shapefile” or “GPX” (see OGR docs or ogr2ogr --help on
the command line) and a schema mapping such as:
{‘geometry’: ‘Point’,
‘properties’: [(‘class’, ‘int’), (‘label’, ‘str’), (‘value’, ‘float’)]}
must be provided. If a particular ordering of properties (“fields” in GIS parlance) in the written file is desired,
a list of (key, value) pairs as above or an ordered dict is required. If no ordering is needed, a standard dict will
suffice.
A coordinate reference system for collections in write mode can be defined by the crs parameter. It takes Proj4
style mappings like
{‘proj’: ‘longlat’, ‘ellps’: ‘WGS84’, ‘datum’: ‘WGS84’, ‘no_defs’: True}
short hand strings like
EPSG:4326
or WKT representations of coordinate reference systems.
The drivers used by Fiona will try to detect the encoding of data files. If they fail, you may provide the proper
encoding, such as ‘Windows-1252’ for the Natural Earth datasets.
When the provided path is to a file containing multiple named layers of data, a layer can be singled out by
layer.
The drivers enabled for opening datasets may be restricted to those listed in the enabled_drivers parameter.
This and the driver parameter afford much control over opening of files.
# Trying only the GeoJSON driver when opening to read, the # following raises DataIOError:
fiona.open(‘example.shp’, driver=’GeoJSON’)
# Trying first the GeoJSON driver, then the Shapefile driver, # the following succeeds: fiona.open(
‘example.shp’, enabled_drivers=[‘GeoJSON’, ‘ESRI Shapefile’])
fp [URI (str or pathlib.Path), or file-like object] A dataset resource identifier or file object.
mode [str] One of ‘r’, to read (the default); ‘a’, to append; or ‘w’, to write.
driver [str] In ‘w’ mode a format driver name is required. In ‘r’ or ‘a’ mode this parameter has no effect.
schema [dict] Required in ‘w’ mode, has no effect in ‘r’ or ‘a’ mode.
crs [str or dict] Required in ‘w’ mode, has no effect in ‘r’ or ‘a’ mode.
encoding [str] Name of the encoding used to encode or decode the dataset.
layer [int or str] The integer index or name of a layer in a multi-layer dataset.
vfs [str] This is a deprecated parameter. A URI scheme such as “zip://” should be used instead.
enabled_drivers [list] An optional list of driver names to used when opening a collection.
crs_wkt [str] An optional WKT representation of a coordinate reference system.
kwargs [mapping] Other driver-specific parameters that will be interpreted by the OGR library as layer creation
or opening options.
Collection
fiona.prop_type(text)
Returns a schema property’s proper Python type.
Example:
62 Chapter 3. fiona
Fiona Documentation, Release 1.8.6
>>> prop_type('int')
<class 'int'>
>>> prop_type('str:25')
<class 'str'>
fiona.prop_width(val)
Returns the width of a str type property.
Undefined for non-str properties. Example:
>>> prop_width('str:25')
25
>>> prop_width('str')
80
64 Chapter 3. fiona
CHAPTER 4
Options:
-v, --verbose Increase verbosity.
-q, --quiet Decrease verbosity.
--version Show the version and exit.
--gdal-version Show the version and exit.
--python-version Show the version and exit.
--help Show this message and exit.
Commands:
bounds Print the extent of GeoJSON objects
calc Calculate GeoJSON property by Python expression
cat Concatenate and print the features of datasets
collect Collect a sequence of features.
distrib Distribute features from a collection.
dump Dump a dataset to GeoJSON.
env Print information about the fio environment.
filter Filter GeoJSON features by python expression.
info Print information about a dataset.
insp Open a dataset and start an interpreter.
load Load GeoJSON to a dataset in another format.
ls List layers in a datasource.
rm Remove a datasource or an individual layer.
65
Fiona Documentation, Release 1.8.6
4.1 bounds
New in 1.4.5.
Fio-bounds reads LF or RS-delimited GeoJSON texts, either features or collections, from stdin and prints their bounds
with or without other data to stdout.
With no options, it works like this:
4.2 calc
New in 1.7b1
The calc command creates a new property on GeoJSON features using the specified expression.
The expression is evaluated in a restricted namespace containing 4 functions (sum, pow, min, max), the math module,
the shapely shape function, type conversions (bool, int, str, len, float), and an object f representing the feature to be
evaluated. This f object allows access in javascript-style dot notation for convenience.
The expression will be evaluated for each feature and its return value will be added to the properties as the specified
property_name. Existing properties will not be overwritten by default (an Exception is raised).
4.3 cat
The cat command concatenates the features of one or more datasets and prints them as a JSON text sequence of
features. In other words: GeoJSON feature objects, possibly pretty printed, optionally separated by ASCII RS (x1e)
chars using –rs.
The output of fio cat can be piped to fio load to create new concatenated datasets.
New in 1.4.0.
4.4 collect
The collect command takes a JSON text sequence of GeoJSON feature objects, such as the output of fio cat and
writes a GeoJSON feature collection.
New in 1.4.0.
4.5 distrib
The inverse of fio-collect, fio-distrib takes a GeoJSON feature collection and writes a JSON text sequence of GeoJSON
feature objects.
New in 1.4.0.
4.6 dump
The dump command reads a vector dataset and writes a GeoJSON feature collection to stdout. Its output can be piped
to fio load (see below).
You can optionally dump out JSON text sequences using --x-json-seq. Since version 1.4.0, fio cat is the
better tool for generating sequences.
˓→74, 51.43], [0.75, 51.44], [0.76, 51.44], [0.79, 51.44], [0.89, 51.42], [0.9, 51.
˓→42], [0.91, 51.42], [0.93, 51.4], [0.94, 51.39], [0.94, 51.38], [0.95, 51.38], [0.
˓→95, 51.37], [0.95, 51.37], [0.94, 51.37], [0.9, 51.36], [0.9, 51.36]]], "type":
4.4. collect 67
Fiona Documentation, Release 1.8.6
˓→19], [-4.65, 51.16], [-4.65, 51.16], [-4.65, 51.16], [-4.66, 51.16]]], "type":
4.7 info
You can also optionally get single info items as plain text (not JSON) strings
4.8 load
The load command reads GeoJSON features from stdin and writes them to a vector dataset using another format.
This command also supports GeoJSON text sequences. RS-separated sequences will be detected. If you want to load
LF-separated sequences, you must specfiy --x-json-seq.
The underscore-cli process command is another way of turning a GeoJSON feature collection into a feature sequence.
4.9 filter
The filter command reads GeoJSON features from stdin and writes the feature to stdout if the provided expression
evalutates to True for that feature.
The python expression is evaluated in a restricted namespace containing 3 functions (sum, min, max), the math module,
the shapely shape function, and an object f representing the feature to be evaluated. This f object allows access in
javascript-style dot notation for convenience.
If the expression evaluates to a “truthy” value, the feature is printed verbatim. Otherwise, the feature is excluded from
the output.
Would create a geojson file with only those features from data.shp where the area was over a given threshold.
4.9. filter 69
Fiona Documentation, Release 1.8.6
4.10 rm
The fio rm command deletes an entire datasource or a single layer in a multi-layer datasource. If the datasource is
composed of multiple files (e.g. an ESRI Shapefile) all of the files will be removed.
$ fio rm countries.shp
$ fio rm --layer forests land_cover.gpkg
New in 1.8.0.
The fio cat command can optionally transform feature geometries to a new coordinate reference system specified
with --dst_crs. The fio collect command can optionally transform from a coordinate reference system
specified with --src_crs to the default WGS84 GeoJSON CRS. Like collect, fio load can accept non-WGS84
features, but as it can write files in formats other than GeoJSON, you can optionally specify a --dst_crs. For
example, the WGS84 features read from docs/data/test_uk.shp,
make a detour through EPSG:3857 (Web Mercator) and are transformed back to WGS84 by fio cat. The following,
• genindex
• modindex
• search
71
Fiona Documentation, Release 1.8.6
73
Fiona Documentation, Release 1.8.6
74 Bibliography
Python Module Index
f
fiona, 60
fiona.collection, 47
fiona.compat, 49
fiona.crs, 49
fiona.drvsupport, 50
fiona.env, 50
fiona.errors, 52
fiona.fio, 47
fiona.fio.bounds, 45
fiona.fio.calc, 45
fiona.fio.cat, 45
fiona.fio.collect, 45
fiona.fio.distrib, 45
fiona.fio.dump, 46
fiona.fio.env, 46
fiona.fio.filter, 46
fiona.fio.helpers, 46
fiona.fio.info, 46
fiona.fio.insp, 46
fiona.fio.load, 46
fiona.fio.ls, 46
fiona.fio.main, 47
fiona.fio.options, 47
fiona.fio.rm, 47
fiona.inspector, 53
fiona.io, 54
fiona.logutils, 54
fiona.ogrext, 54
fiona.path, 56
fiona.rfc3339, 57
fiona.schema, 58
fiona.session, 58
fiona.transform, 59
fiona.vfs, 60
75
Fiona Documentation, Release 1.8.6
77
Fiona Documentation, Release 1.8.6
G L
listlayers() (in module fiona), 61
GDALVersion (class in fiona.env), 50
LogFiltering (class in fiona.logutils), 54
GDALVersionError, 53
GeometryTypeValidationError, 53
get() (fiona.collection.Collection method), 48
M
get() (fiona.ogrext.Session method), 55 main() (in module fiona.inspector), 53
get_credential_options() major (fiona.env.GDALVersion attribute), 51
(fiona.session.AWSSession method), 58 make_ld_context() (in module fiona.fio.helpers),
get_credential_options() 46
(fiona.session.DummySession method), 58 MemoryFile (class in fiona.io), 54
get_credential_options() MemoryFileBase (class in fiona.ogrext), 55
(fiona.session.GSSession method), 58 meta (fiona.collection.Collection attribute), 48
get_credential_options() minor (fiona.env.GDALVersion attribute), 51
(fiona.session.Session method), 59
get_crs() (fiona.ogrext.Session method), 55 N
get_crs_wkt() (fiona.ogrext.Session method), 55 name (fiona.path.ParsedPath attribute), 57
get_driver() (fiona.ogrext.Session method), 55 name (fiona.path.UnparsedPath attribute), 57
get_extent() (fiona.ogrext.Session method), 55 next() (fiona.collection.Collection method), 48
get_feature() (fiona.ogrext.Session method), 55 normalize_field_type() (in module
get_fileencoding() (fiona.ogrext.Session fiona.schema), 58
method), 56 nullable() (in module fiona.fio.helpers), 46
78 Index
Fiona Documentation, Release 1.8.6
S
schema (fiona.collection.Collection attribute), 48
SchemaError, 53
scheme (fiona.path.ParsedPath attribute), 57
seek() (fiona.ogrext.MemoryFileBase method), 55
Session (class in fiona.ogrext), 55
Session (class in fiona.session), 59
setenv() (in module fiona.env), 52
start() (fiona.ogrext.Session method), 56
start() (fiona.ogrext.WritingSession method), 56
stop() (fiona.ogrext.Session method), 56
strencode() (in module fiona.compat), 49
sync() (fiona.ogrext.WritingSession method), 56
T
tell() (fiona.ogrext.MemoryFileBase method), 55
ThreadEnv (class in fiona.env), 51
to_string() (in module fiona.crs), 49
TransactionError, 53
transform() (in module fiona.transform), 59
transform_geom() (in module fiona.transform), 60
Index 79