rasterio.warp module

Raster warping and reprojection.

rasterio.warp.aligned_target(transform, width, height, resolution)

Aligns target to specified resolution

Parameters
  • transform (Affine) – Input affine transformation matrix

  • height (width,) – Input dimensions

  • resolution (tuple (x resolution, y resolution) or float) – Target resolution, in units of target coordinate reference system.

Returns

  • transform (Affine) – Output affine transformation matrix

  • width, height (int) – Output dimensions

rasterio.warp.calculate_default_transform(src_crs, dst_crs, width, height, left=None, bottom=None, right=None, top=None, gcps=None, resolution=None, dst_width=None, dst_height=None)

Output dimensions and transform for a reprojection.

Source and destination coordinate reference systems and output width and height are the first four, required, parameters. Source georeferencing can be specified using either ground control points (gcps) or spatial bounds (left, bottom, right, top). These two forms of georeferencing are mutually exclusive.

The destination transform is anchored at the left, top coordinate.

Destination width and height (and resolution if not provided), are calculated using GDAL’s method for suggest warp output.

Parameters
  • src_crs (CRS or dict) – Source coordinate reference system, in rasterio dict format. Example: CRS({‘init’: ‘EPSG:4326’})

  • dst_crs (CRS or dict) – Target coordinate reference system.

  • height (width,) – Source raster width and height.

  • bottom, right, top (left,) – Bounding coordinates in src_crs, from the bounds property of a raster. Required unless using gcps.

  • gcps (sequence of GroundControlPoint, optional) – Instead of a bounding box for the source, a sequence of ground control points may be provided.

  • resolution (tuple (x resolution, y resolution) or float, optional) – Target resolution, in units of target coordinate reference system.

  • dst_height (dst_width,) – Output file size in pixels and lines. Cannot be used together with resolution.

Returns

  • transform (Affine) – Output affine transformation matrix

  • width, height (int) – Output dimensions

Notes

Some behavior of this function is determined by the CHECK_WITH_INVERT_PROJ environment variable:

YES: constrain output raster to extents that can be inverted

avoids visual artifacts and coordinate discontinuties.

NO: reproject coordinates beyond valid bound limits

rasterio.warp.reproject(source, destination=None, src_transform=None, gcps=None, src_crs=None, src_nodata=None, dst_transform=None, dst_crs=None, dst_nodata=None, dst_resolution=None, src_alpha=0, dst_alpha=0, resampling=<Resampling.nearest: 0>, num_threads=1, init_dest_nodata=True, warp_mem_limit=0, **kwargs)

Reproject a source raster to a destination raster.

If the source and destination are ndarrays, coordinate reference system definitions and affine transformation parameters or ground control points (gcps) are required for reprojection.

If the source and destination are rasterio Bands, shorthand for bands of datasets on disk, the coordinate reference systems and transforms or GCPs will be read from the appropriate datasets.

Parameters
  • source (ndarray or Band) – The source is a 2 or 3-D ndarray, or a single or a multiple Rasterio Band object. The dimensionality of source and destination must match, i.e., for multiband reprojection the lengths of the first axes of the source and destination must be the same.

  • destination (ndarray or Band, optional) – The destination is a 2 or 3-D ndarray, or a single or a multiple Rasterio Band object. The dimensionality of source and destination must match, i.e., for multiband reprojection the lengths of the first axes of the source and destination must be the same.

  • src_transform (affine.Affine(), optional) – Source affine transformation. Required if source and destination are ndarrays. Will be derived from source if it is a rasterio Band. An error will be raised if this parameter is defined together with gcps.

  • gcps (sequence of GroundControlPoint, optional) – Ground control points for the source. An error will be raised if this parameter is defined together with src_transform.

  • src_crs (CRS or dict, optional) – Source coordinate reference system, in rasterio dict format. Required if source and destination are ndarrays. Will be derived from source if it is a rasterio Band. Example: CRS({‘init’: ‘EPSG:4326’})

  • src_nodata (int or float, optional) – The source nodata value. Pixels with this value will not be used for interpolation. If not set, it will default to the nodata value of the source image if a masked ndarray or rasterio band, if available.

  • dst_transform (affine.Affine(), optional) – Target affine transformation. Required if source and destination are ndarrays. Will be derived from target if it is a rasterio Band.

  • dst_crs (CRS or dict, optional) – Target coordinate reference system. Required if source and destination are ndarrays. Will be derived from target if it is a rasterio Band.

  • dst_nodata (int or float, optional) – The nodata value used to initialize the destination; it will remain in all areas not covered by the reprojected source. Defaults to the nodata value of the destination image (if set), the value of src_nodata, or 0 (GDAL default).

  • dst_resolution (tuple (x resolution, y resolution) or float, optional) – Target resolution, in units of target coordinate reference system.

  • src_alpha (int, optional) – Index of a band to use as the alpha band when warping.

  • dst_alpha (int, optional) – Index of a band to use as the alpha band when warping.

  • resampling (int) –

    Resampling method to use. One of the following:

    Resampling.nearest, Resampling.bilinear, Resampling.cubic, Resampling.cubic_spline, Resampling.lanczos, Resampling.average, Resampling.mode, Resampling.max (GDAL >= 2.2), Resampling.min (GDAL >= 2.2), Resampling.med (GDAL >= 2.2), Resampling.q1 (GDAL >= 2.2), Resampling.q3 (GDAL >= 2.2)

    An exception will be raised for a method not supported by the running version of GDAL.

  • num_threads (int, optional) – The number of warp worker threads. Default: 1.

  • init_dest_nodata (bool) – Flag to specify initialization of nodata in destination; prevents overwrite of previous warps. Defaults to True.

  • warp_mem_limit (int, optional) – The warp operation memory limit in MB. Larger values allow the warp operation to be carried out in fewer chunks. The amount of memory required to warp a 3-band uint8 2000 row x 2000 col raster to a destination of the same size is approximately 56 MB. The default (0) means 64 MB with GDAL 2.2.

  • kwargs (dict, optional) – Additional arguments passed to transformation function.

Returns

  • destination (ndarray or Band) – The transformed narray or Band.

  • dst_transform (Affine) – THe affine transformation matrix of the destination.

rasterio.warp.transform(src_crs, dst_crs, xs, ys, zs=None)

Transform vectors from source to target coordinate reference system.

Transform vectors of x, y and optionally z from source coordinate reference system into target.

Parameters
  • src_crs (CRS or dict) – Source coordinate reference system, as a rasterio CRS object. Example: CRS({‘init’: ‘EPSG:4326’})

  • dst_crs (CRS or dict) – Target coordinate reference system.

  • xs (array_like) – Contains x values. Will be cast to double floating point values.

  • ys (array_like) – Contains y values.

  • zs (array_like, optional) – Contains z values. Assumed to be all 0 if absent.

Returns

out – Tuple of x, y, and optionally z vectors, transformed into the target coordinate reference system.

Return type

tuple of array_like, (xs, ys, [zs])

rasterio.warp.transform_bounds(src_crs, dst_crs, left, bottom, right, top, densify_pts=21)

Transform bounds from src_crs to dst_crs.

Optionally densifying the edges (to account for nonlinear transformations along these edges) and extracting the outermost bounds.

Note: this does not account for the antimeridian.

Parameters
  • src_crs (CRS or dict) – Source coordinate reference system, in rasterio dict format. Example: CRS({‘init’: ‘EPSG:4326’})

  • dst_crs (CRS or dict) – Target coordinate reference system.

  • bottom, right, top (left,) – Bounding coordinates in src_crs, from the bounds property of a raster.

  • densify_pts (uint, optional) – Number of points to add to each edge to account for nonlinear edges produced by the transform process. Large numbers will produce worse performance. Default: 21 (gdal default).

Returns

left, bottom, right, top – Outermost coordinates in target coordinate reference system.

Return type

float

rasterio.warp.transform_geom(src_crs, dst_crs, geom, antimeridian_cutting=True, antimeridian_offset=10.0, precision=-1)

Transform geometry from source coordinate reference system into target.

Parameters
  • src_crs (CRS or dict) – Source coordinate reference system, in rasterio dict format. Example: CRS({‘init’: ‘EPSG:4326’})

  • dst_crs (CRS or dict) – Target coordinate reference system.

  • geom (GeoJSON like dict object) –

  • antimeridian_cutting (bool, optional) – If True, cut geometries at the antimeridian, otherwise geometries will not be cut (default). If False and GDAL is 2.2.0 or newer an exception is raised. Antimeridian cutting is always on as of GDAL 2.2.0 but this could produce an unexpected geometry.

  • antimeridian_offset (float) – Offset from the antimeridian in degrees (default: 10) within which any geometries will be split.

  • precision (float) – If >= 0, geometry coordinates will be rounded to this number of decimal places after the transform operation, otherwise original coordinate values will be preserved (default).

Returns

out – Transformed geometry in GeoJSON dict format

Return type

GeoJSON like dict object