[GIS] What options are available for the rasterio .update() method

clipfionageotiff-tiffpythonrasterio

I have a script which clips raster data by a shapefile using rasterio and fiona:

import fiona
import rasterio
import rasterio.mask

def clip_raster(shp, imagery, out_imagery):
    with fiona.open(shp, "r") as shapefile:
        features = [feature["geometry"] for feature in shapefile]

    with rasterio.open(imagery) as src:
        out_image, out_transform = rasterio.mask.mask(src, features, crop=True)
        out_meta = src.meta.copy()

    out_meta.update({"driver": "GTiff",
                     "height": out_image.shape[1],
                     "width": out_image.shape[2],
                     "transform": out_transform,
                     "compress": "LZW",
                     "nodata": 255})
    with rasterio.open(out_imagery, "w", **out_meta) as dest:
        dest.write(out_image)


if __name__ == "__main__":
    shp = r'/path/to/shapefile.shp'
    imagery = r'/path/to/input_imagery.tif'
    out_imagery = r'/path/to/output_imagery.tif'

    clip_raster(shp, imagery, out_imagery)

You can see that the .update() method has a variety of options such as setting the compression type ("compress": "LZW") or nodata value ("nodata": 255):

out_meta.update({"driver": "GTiff",
                 "height": out_image.shape[1],
                 "width": out_image.shape[2],
                 "transform": out_transform,
                 "compress": "LZW",
                 "nodata": 255})

Is there a list or documentation of all the available options available to the .update() method when writing raster data using rasterio?

Best Answer

rasterio objects don't have an update method. Your out_meta var is a python dict, which is being updated and is then passed to the rasterio.open method as keyword args using ** dict unpacking.

The main keyword arguments for the rasterio.open method are documented in "Opening a dataset in writing mode":

  • driver: the name of the desired format driver
  • width: the number of columns of the dataset
  • height: the number of rows of the dataset
  • count: a count of the dataset bands dtype: the data type of the dataset
  • crs: a coordinate reference system identifier or description
  • transform: an affine transformation matrix, and
  • nodata: a “nodata” value

You should also read the docs for creation options which notes:

Each format has it’s own set of driver-specific creation options that can be used to fine tune the output rasters. For details on a particular driver, see the formats list.

For the purposes of this document, we will focus on the GeoTIFF creation options. Some of the common GeoTIFF creation options include:

  • TILED, BLOCKXSIZE, and BLOCKYSIZE to define the internal tiling
  • COMPRESS to define the compression method
  • PHOTOMETRIC to define the band’s color interpretation
Related Question