rasterio is a Python package which aims to provide a friendlier API to GDAL than GDAL’s own Python API (which feels very C-like). It is an open source project on GitHub that is created and maintained by mapbox.
Most of the code examples below assume you have imported
rasterio into the current module with:
rasterio’s API documentation can be found at https://rasterio.readthedocs.io/en/stable/index.html. Be warned that it is very incomplete (as of November 2019) — there is missing documentation for many
Reading A GeoTIFF
There are two common ways to do this, with or without a context manager.
With a context manager:
Without a context manager:
read() function as used above will read all bands of data from the
.tif file. You can read a specific band by providing a band index to
read(). The band indexes start from
1, just as they do in GDAL. The following example just reads the first band:
You can also open a raster by passing in a
Path object to
open() (Python v3.4 or higher only):
Getting Projection Info
The projection information in obtained through the
You can get also get the “Well Known Text” (WKT) syntax:
To get the Affine transformation:
Converting Coordinates To Pixels
rasterio provides the
index() function in the
Dataset class to convert coordinates from the projection space (e.g.
(latitude, longitude) if in WGS 84) of a dataset to
(x, y) pixel coordinates in the image.
reproject() does not create the destination array for you, you have to create the array yourself and pass it into the function.
More on masking can be found at https://rasterio.readthedocs.io/en/latest/api/rasterio.mask.html.
This error usually occurs if you are trying to reproject an image into a projection space that does not contain the image (e.g. images are in completely different UTM zones).
The documentation for the latest version of
rasterio can be found at https://rasterio.readthedocs.io/en/latest/.
This work is licensed under a Creative Commons Attribution 4.0 International License .