3

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?

0

2 Answers 2

6

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
1

If you look in the rasterio __init__.py file, you'll see the following parameters listed for the open and update method:

Parameters
----------
mode: string
    "r" (read), "r+" (read/write), or "w" (write)
driver: string
    driver code specifying the format name (e.g. "GTiff" or
    "JPEG"). See GDAL docs at
    http://www.gdal.org/formats_list.html (optional, required
    for writing).
width: int
    number of pixels per line
    (optional, required for write)
height: int
    number of lines
    (optional, required for write)
count: int > 0
    number of bands
    (optional, required for write)
dtype: rasterio.dtype
    the data type for bands such as ``rasterio.ubyte`` for
    8-bit bands or ``rasterio.uint16`` for 16-bit bands
    (optional, required for write)
crs: dict or string
    Coordinate reference system
    (optional, recommended for write)
transform: Affine instance
    Affine transformation mapping the pixel space to geographic
    space (optional, recommended for writing).
nodata: number
    Defines pixel value to be interpreted as null/nodata
    (optional, recommended for write)

which is funny because I don't see "compress" listed. I am using an older version of rasterio though. (0.36 I believe)

1
  • You don't see "compress" listed as it's a format creation option. It's captured by the optional extra keyword arguments **kwargs.
    – user2856
    Commented Jul 30, 2018 at 4:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.