rasterio.transform module

Geospatial transforms

class rasterio.transform.AffineTransformer(affine_transform)

Bases: TransformerBase

A pure Python class related to affine based coordinate transformations.

class rasterio.transform.GCPTransformer(gcps, tps=False)

Bases: GCPTransformerBase, GDALTransformerBase

Class related to Ground Control Point (GCPs) based coordinate transformations.

Uses GDALCreateGCPTransformer and GDALGCPTransform for computations. Ensure that GDAL transformer objects are destroyed by calling close() method or using context manager interface. If tps is set to True, uses GDALCreateTPSTransformer and GDALTPSTransform instead.

class rasterio.transform.GDALTransformerBase

Bases: TransformerBase

close()
class rasterio.transform.RPCTransformer(rpcs, **rpc_options)

Bases: RPCTransformerBase, GDALTransformerBase

Class related to Rational Polynomial Coeffecients (RPCs) based coordinate transformations.

Uses GDALCreateRPCTransformer and GDALRPCTransform for computations. Options for GDALCreateRPCTransformer may be passed using rpc_options. Ensure that GDAL transformer objects are destroyed by calling close() method or using context manager interface.

class rasterio.transform.TransformMethodsMixin

Bases: object

Mixin providing methods for calculations related to transforming between rows and columns of the raster array and the coordinates.

These methods are wrappers for the functionality in rasterio.transform module.

A subclass with this mixin MUST provide a transform property.

index(x, y, z=None, op=<ufunc 'floor'>, precision=None, transform_method=TransformMethod.affine, **rpc_options)

Get the (row, col) index of the pixel containing (x, y).

Parameters:
  • x (float) – x value in coordinate reference system

  • y (float) – y value in coordinate reference system

  • z (float, optional) – Height associated with coordinates. Primarily used for RPC based coordinate transformations. Ignored for affine based transformations. Default: 0.

  • op (function, optional (default: math.floor)) – Function to convert fractional pixels to whole numbers (floor, ceiling, round)

  • transform_method (TransformMethod, optional) – The coordinate transformation method. Default: TransformMethod.affine.

  • rpc_options (dict, optional) – Additional arguments passed to GDALCreateRPCTransformer

  • precision (int, optional) – This parameter is unused, deprecated in rasterio 1.3.0, and will be removed in version 2.0.0.

Returns:

(row index, col index)

Return type:

tuple

xy(row, col, z=None, offset='center', transform_method=TransformMethod.affine, **rpc_options)

Get the coordinates x, y of a pixel at row, col.

The pixel’s center is returned by default, but a corner can be returned by setting offset to one of ul, ur, ll, lr.

Parameters:
  • row (int) – Pixel row.

  • col (int) – Pixel column.

  • z (float, optional) – Height associated with coordinates. Primarily used for RPC based coordinate transformations. Ignored for affine based transformations. Default: 0.

  • offset (str, optional) – Determines if the returned coordinates are for the center of the pixel or for a corner.

  • transform_method (TransformMethod, optional) – The coordinate transformation method. Default: TransformMethod.affine.

  • rpc_options (dict, optional) – Additional arguments passed to GDALCreateRPCTransformer

Returns:

x, y

Return type:

tuple

class rasterio.transform.TransformerBase

Bases: object

Generic GDAL transformer base class

Notes

Subclasses must have a _transformer attribute and implement a _transform method.

rowcol(xs, ys, zs=None, op=<ufunc 'floor'>, precision=None)

Get rows and cols coordinates given geographic coordinates.

Parameters:
  • xs (float or list of float) – Geographic coordinates

  • ys (float or list of float) – Geographic coordinates

  • zs (float or list of float, optional) – Height associated with coordinates. Primarily used for RPC based coordinate transformations. Ignored for affine based transformations. Default: 0.

  • op (function, optional (default: math.floor)) – Function to convert fractional pixels to whole numbers (floor, ceiling, round)

  • precision (int, optional (default: None)) – This parameter is unused, deprecated in rasterio 1.3.0, and will be removed in version 2.0.0.

Raises:

ValueError – If input coordinates are not all equal length

Return type:

tuple of float or list of float.

xy(rows, cols, zs=None, offset='center')

Returns geographic coordinates given dataset rows and cols coordinates

Parameters:
  • rows (int or list of int) – Image pixel coordinates

  • cols (int or list of int) – Image pixel coordinates

  • zs (float or list of float, optional) – Height associated with coordinates. Primarily used for RPC based coordinate transformations. Ignored for affine based transformations. Default: 0.

  • offset (str, optional) – Determines if the returned coordinates are for the center of the pixel or for a corner. Available options include center, ul, ur, ll, lr.

Raises:

ValueError – If input coordinates are not all equal length

Return type:

tuple of float or list of float

rasterio.transform.array_bounds(height, width, transform)

Return the bounds of an array given height, width, and a transform.

Return the west, south, east, north bounds of an array given its height, width, and an affine transform.

rasterio.transform.from_bounds(west, south, east, north, width, height)

Return an Affine transformation given bounds, width and height.

Return an Affine transformation for a georeferenced raster given its bounds west, south, east, north and its width and height in number of pixels.

rasterio.transform.from_gcps(gcps)

Make an Affine transform from ground control points.

Parameters:

gcps (sequence of GroundControlPoint) – Such as the first item of a dataset’s gcps property.

Return type:

Affine

rasterio.transform.from_origin(west, north, xsize, ysize)

Return an Affine transformation given upper left and pixel sizes.

Return an Affine transformation for a georeferenced raster given the coordinates of its upper left corner west, north and pixel sizes xsize, ysize.

rasterio.transform.get_transformer(transform, **rpc_options)

Return the appropriate transformer class

rasterio.transform.guard_transform(transform)

Return an Affine transformation instance.

rasterio.transform.rowcol(transform, xs, ys, zs=None, op=<ufunc 'floor'>, precision=None, **rpc_options)

Get rows and cols of the pixels containing (x, y).

Parameters:
  • transform (Affine or sequence of GroundControlPoint or RPC) – Transform suitable for input to AffineTransformer, GCPTransformer, or RPCTransformer.

  • xs (list or float) – x values in coordinate reference system.

  • ys (list or float) – y values in coordinate reference system.

  • zs (list or float, optional) – Height associated with coordinates. Primarily used for RPC based coordinate transformations. Ignored for affine based transformations. Default: 0.

  • op (function) – Function to convert fractional pixels to whole numbers (floor, ceiling, round).

  • precision (int or float, optional) – This parameter is unused, deprecated in rasterio 1.3.0, and will be removed in version 2.0.0.

  • rpc_options (dict, optional) – Additional arguments passed to GDALCreateRPCTransformer.

Returns:

  • rows (list of ints) – list of row indices

  • cols (list of ints) – list of column indices

rasterio.transform.tastes_like_gdal(seq)

Return True if seq matches the GDAL geotransform pattern.

rasterio.transform.xy(transform, rows, cols, zs=None, offset='center', **rpc_options)

Get the x and y coordinates of pixels at rows and cols.

The pixel’s center is returned by default, but a corner can be returned by setting offset to one of ul, ur, ll, lr.

Supports affine, Ground Control Point (GCP), or Rational Polynomial Coefficients (RPC) based coordinate transformations.

Parameters:
  • transform (Affine or sequence of GroundControlPoint or RPC) – Transform suitable for input to AffineTransformer, GCPTransformer, or RPCTransformer.

  • rows (list or int) – Pixel rows.

  • cols (int or sequence of ints) – Pixel columns.

  • zs (list or float, optional) – Height associated with coordinates. Primarily used for RPC based coordinate transformations. Ignored for affine based transformations. Default: 0.

  • offset (str, optional) – Determines if the returned coordinates are for the center of the pixel or for a corner.

  • rpc_options (dict, optional) – Additional arguments passed to GDALCreateRPCTransformer.

Returns:

  • xs (float or list of floats) – x coordinates in coordinate reference system

  • ys (float or list of floats) – y coordinates in coordinate reference system