rasterio.transform module

Geospatial transforms

class rasterio.transform.AffineTransformer(affine_transform)

Bases: rasterio.transform.TransformerBase

A pure Python class related to affine based coordinate transformations.

close()
class rasterio.transform.GCPTransformer(gcps)

Bases: rasterio._transform.GCPTransformerBase, rasterio.transform.TransformerBase

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.

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

Bases: rasterio._transform.RPCTransformerBase, rasterio.transform.TransformerBase

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=<built-in function floor>, precision=None, transform_method=TransformMethod.affine, **rpc_options)

Returns the (row, col) index of the pixel containing (x, y) given a coordinate reference system.

Use an epsilon, magnitude determined by the precision parameter and sign determined by the op function: positive for floor, negative for ceil.

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)

  • precision (int, optional (default: None)) – Decimal places of precision in indexing, as in round().

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

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

Returns

(row index, col index)

Return type

tuple

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

Returns the coordinates (x, y) of a pixel at row and 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.

close()
rowcol(xs, ys, zs=None, op=<built-in function floor>, precision=None)

Returns 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)) – Decimal places of precision in indexing, as in round().

Raises

ValueError – If input coordinates are not all equal length

Returns

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.

Raises

ValueError – If input coordinates are not all equal length

Returns

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.

Returns

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=<built-in function floor>, precision=None, **rpc_options)

Returns the rows and cols of the pixels containing (x, y) given a coordinate reference system.

Use an epsilon, magnitude determined by the precision parameter and sign determined by the op function: positive for floor, negative for ceil.

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) – An integer number of decimal points of precision when computing inverse transform, or an absolute float precision.

  • 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)

Returns 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