/rasterio-cookbook

[ARCHIVED] Python scripts demonstrating the usage of Rasterio

Primary LanguagePythonMIT LicenseMIT

Attention!

this repo is out of date and has been archived.

Rasterio Cookbook

.. todo::

    Fill out examples of using rasterio to handle tasks from typical
    GIS and remote sensing workflows.

The Rasterio cookbook is intended to provide in-depth examples of rasterio usage that are not covered by the basic usage in the User's Manual. Before using code from the cookbook, you should be familiar with the basic usage of rasterio; see "Reading Datasets", "Working with Datasets" and "Writing Datasets" to brush up on the fundamentals.

Generating summary statistics for each band

.. literalinclude:: recipes/band_summary_stats.py
    :language: python
    :linenos:

$ python recipes/band_summary_stats.py
[{'max': 255, 'mean': 29.94772668847656, 'median': 13.0, 'min': 0},
 {'max': 255, 'mean': 44.516147889382289, 'median': 30.0, 'min': 0},
 {'max': 255, 'mean': 48.113056354742945, 'median': 30.0, 'min': 0}]

Raster algebra

Resampling rasters to a different cell size

Reproject/warp a raster to a different CRS

Reproject to a Transverse Mercator projection, Hawaii zone 3 (ftUS), aka EPSG code 3759.

.. literalinclude:: recipes/reproject.py
    :language: python
    :linenos:

$ python recipes/reproject.py

The original image

img/world.jpg

Warped to EPSG:3759. Notice that the bounds are constrained to the new projection's valid region (CHECK_WITH_INVERT_PROJ=True on line 13) and the new raster is wrapped seamlessly across the anti-meridian.

img/reproject.jpg

Raster to polygon features

Rasterizing GeoJSON features

Masking raster with a polygon feature

Using rasterio with fiona, we can open a shapefile, read geometries, and mask out regions of a raster that are outside the polygons defined in the shapefile.

This shapefile contains a single polygon, a box near the center of the raster, so in this case, our list of geometries is one element long.

Applying the features in the shapefile as a mask on the raster sets all pixels outside of the features to be zero. Since crop=True in this example, the extent of the raster is also set to be the extent of the features in the shapefile.

We can then use the updated spatial transform and raster height and width to write the masked raster to a new file.

.. literalinclude:: recipes/mask_shp.py
    :language: python
    :linenos:

$ python recipes/mask_shp.py

The original image with the shapefile overlayed

img/box_rgb.png

Masked and cropped to the geometry

img/box_masked_rgb.png

Creating valid data bounding polygons

Raster to vector line feature

Creating raster from numpy array

Creating a least cost path

Using a scipy filter to smooth a raster

This recipe demonstrates scipy's signal processing filters to manipulate multi-band raster imagery and save the results to a new GeoTIFF. Here we apply a median filter to smooth the image and remove small inclusions (at the expense of some sharpness and detail).

.. literalinclude:: recipes/filter.py
    :language: python
    :linenos:

$ python recipes/filter.py

The original image

img/RGB.byte.jpg

With median filter applied

img/filtered.jpg

Using skimage to adjust the saturation of a RGB raster

This recipe demonstrates manipulating color with the scikit image color module.

.. literalinclude:: recipes/saturation.py
    :language: python
    :linenos:

$ python recipes/saturation.py

The original image

img/RGB.byte.jpg

With increased saturation

img/saturation.jpg

Generating a KMZ from a raster

A raster can be converted to a KMZ and opened in Google Earth using rasterio to access the raster metadata. Executing

$ python recipes/raster_to_kmz.py

creates the file green_box.tif, which is a green image that extends from longitude -36 to -35 and latitude 74 to 75 in EPSG:4326 projection, and then embeds this raster in a KMZ file green_box.kmz. In Google Earth, we can see the box inside Greenland (screenshot below).

img/green_box_kmz.png