rasterio.vrt module

rasterio.vrt: a module concerned with GDAL VRTs

class rasterio.vrt.WarpedVRT

Bases: rasterio._warp.WarpedVRTReaderBase, rasterio.windows.WindowMethodsMixin, rasterio.transform.TransformMethodsMixin

A virtual warped dataset.

Abstracts the details of raster warping and allows access to data that is reprojected when read.

This class is backed by an in-memory GDAL VRTWarpedDataset VRT file.

Parameters
  • src_dataset (dataset object) – The warp source.

  • src_crs (CRS or str, optional) – Overrides the coordinate reference system of src_dataset.

  • src_transfrom (Affine, optional) – Overrides the transform of src_dataset.

  • src_nodata (float, optional) – Overrides the nodata value of src_dataset, which is the default.

  • crs (CRS or str, optional) – The coordinate reference system at the end of the warp operation. Default: the crs of src_dataset. dst_crs is a deprecated alias for this parameter.

  • transform (Affine, optional) – The transform for the virtual dataset. Default: will be computed from the attributes of src_dataset. dst_transform is a deprecated alias for this parameter.

  • width (height,) – The dimensions of the virtual dataset. Defaults: will be computed from the attributes of src_dataset. dst_height and dst_width are deprecated alias for these parameters.

  • nodata (float, optional) – Nodata value for the virtual dataset. Default: the nodata value of src_dataset or 0.0. dst_nodata is a deprecated alias for this parameter.

  • resampling (Resampling, optional) – Warp resampling algorithm. Default: Resampling.nearest.

  • tolerance (float, optional) – The maximum error tolerance in input pixels when approximating the warp transformation. Default: 0.125, or one-eigth of a pixel.

  • src_alpha (int, optional) – Index of a source band to use as an alpha band for warping.

  • add_alpha (bool, optional) – Whether to add an alpha masking band to the virtual dataset. Default: False. This option will cause deletion of the VRT nodata value.

  • init_dest_nodata (bool, optional) – Whether or not to initialize output to nodata. Default: True.

  • warp_mem_limit (int, optional) – The warp operation’s memory limit in MB. The default (0) means 64 MB with GDAL 2.2.

  • dtype (str, optional) – The working data type for warp operation and output.

  • warp_extras (dict) – GDAL extra warp options. See http://www.gdal.org/structGDALWarpOptions.html.

src_dataset

The dataset object to be virtually warped.

Type

dataset

resampling

One of the values from rasterio.enums.Resampling. The default is Resampling.nearest.

Type

int

tolerance

The maximum error tolerance in input pixels when approximating the warp transformation. The default is 0.125.

Type

float

src_nodata

The source nodata value. Pixels with this value will not be used for interpolation. If not set, it will be default to the nodata value of the source image, if available.

Type

int or float, optional

dst_nodata

The nodata value used to initialize the destination; it will remain in all areas not covered by the reprojected source. Defaults to the value of src_nodata, or 0 (gdal default).

Type

int or float, optional

working_dtype

The working data type for warp operation and output.

Type

str, optional

warp_extras

GDAL extra warp options. See http://www.gdal.org/structGDALWarpOptions.html.

Type

dict

Examples

>>> with rasterio.open('tests/data/RGB.byte.tif') as src:
...     with WarpedVRT(src, crs='EPSG:3857') as vrt:
...         data = vrt.read()
block_shapes

An ordered list of block shapes for each bands

Shapes are tuples and have the same ordering as the dataset’s shape: (count of image rows, count of image columns).

Returns

Return type

list

block_size()

Returns the size in bytes of a particular block

Only useful for TIFF formatted datasets.

Parameters
  • bidx (int) – Band index, starting with 1.

  • i (int) – Row index of the block, starting with 0.

  • j (int) – Column index of the block, starting with 0.

Returns

Return type

int

block_window()

Returns the window for a particular block

Parameters
  • bidx (int) – Band index, starting with 1.

  • i (int) – Row index of the block, starting with 0.

  • j (int) – Column index of the block, starting with 0.

Returns

Return type

Window

block_windows()

Iterator over a band’s blocks and their windows

The primary use of this method is to obtain windows to pass to read() for highly efficient access to raster block data.

The positional parameter bidx takes the index (starting at 1) of the desired band. This iterator yields blocks “left to right” and “top to bottom” and is similar to Python’s enumerate() in that the first element is the block index and the second is the dataset window.

Blocks are built-in to a dataset and describe how pixels are grouped within each band and provide a mechanism for efficient I/O. A window is a range of pixels within a single band defined by row start, row stop, column start, and column stop. For example, ((0, 2), (0, 2)) defines a 2 x 2 window at the upper left corner of a raster band. Blocks are referenced by an (i, j) tuple where (0, 0) would be a band’s upper left block.

Raster I/O is performed at the block level, so accessing a window spanning multiple rows in a striped raster requires reading each row. Accessing a 2 x 2 window at the center of a 1800 x 3600 image requires reading 2 rows, or 7200 pixels just to get the target 4. The same image with internal 256 x 256 blocks would require reading at least 1 block (if the window entire window falls within a single block) and at most 4 blocks, or at least 512 pixels and at most 2048.

Given an image that is 512 x 512 with blocks that are 256 x 256, its blocks and windows would look like:

Blocks:

        0       256     512
      0 +--------+--------+
        |        |        |
        | (0, 0) | (0, 1) |
        |        |        |
    256 +--------+--------+
        |        |        |
        | (1, 0) | (1, 1) |
        |        |        |
    512 +--------+--------+


Windows:

    UL: ((0, 256), (0, 256))
    UR: ((0, 256), (256, 512))
    LL: ((256, 512), (0, 256))
    LR: ((256, 512), (256, 512))
Parameters

bidx (int, optional) – The band index (using 1-based indexing) from which to extract windows. A value less than 1 uses the first band if all bands have homogeneous windows and raises an exception otherwise.

Yields

block, window

bounds

Returns the lower left and upper right bounds of the dataset in the units of its coordinate reference system.

The returned value is a tuple: (lower left x, lower left y, upper right x, upper right y)

checksum()

Compute an integer checksum for the stored band

Parameters
  • bidx (int) – The band’s index (1-indexed).

  • window (tuple, optional) – A window of the band. Default is the entire extent of the band.

Returns

Return type

An int.

close()

Close the dataset

closed

Test if the dataset is closed

Returns

Return type

bool

colorinterp

Returns a sequence of ColorInterp.<enum> representing color interpretation in band order.

To set color interpretation, provide a sequence of ColorInterp.<enum>:

import rasterio from rasterio.enums import ColorInterp

with rasterio.open(‘rgba.tif’, ‘r+’) as src:
src.colorinterp = (

ColorInterp.red, ColorInterp.green, ColorInterp.blue, ColorInterp.alpha)

Returns

Return type

tuple

colormap()

Returns a dict containing the colormap for a band or None.

compression
count

The number of raster bands in the dataset

Returns

Return type

int

crs

The dataset’s coordinate reference system

dataset_mask()

Calculate the dataset’s 2D mask. Derived from the individual band masks provided by read_masks().

Parameters

out_shape, window, boundless and resampling are passed directly to read_masks() (out,) –

Returns

  • ndarray, shape=(self.height, self.width), dtype=’uint8’

  • 0 = nodata, 255 = valid data

  • The dataset mask is calculate based on the individual band masks according to

  • the following logic, in order of precedence

  • 1. If a .msk file, dataset-wide alpha or internal mask exists, – it will be used as the dataset mask.

  • 2. If an 4-band RGBA with a shadow nodata value, – band 4 will be used as the dataset mask.

  • 3. If a nodata value exists, use the binary OR (|) of the band masks

  • 4. If no nodata value exists, return a mask filled with 255

  • Note that this differs from read_masks and GDAL RFC15

  • in that it applies per-dataset, not per-band

  • (see https (//trac.osgeo.org/gdal/wiki/rfc15_nodatabitmask))

descriptions

Descriptions for each dataset band

To set descriptions, one for each band is required.

Returns

Return type

list of str

driver
dtypes

The data types of each band in index order

Returns

Return type

list of str

files

Returns a sequence of files associated with the dataset.

Returns

Return type

tuple

gcps

ground control points and their coordinate reference system.

This property is a 2-tuple, or pair: (gcps, crs).

gcpslist of GroundControlPoint

Zero or more ground control points.

crs: CRS

The coordinate reference system of the ground control points.

get_gcps()

Get GCPs and their associated CRS.

get_nodatavals()
get_tag_item()

Returns tag item value

Parameters
  • ns (str) – The key for the metadata item to fetch.

  • dm (str) – The domain to fetch for.

  • bidx (int) – Band index, starting with 1.

  • ovr (int) – Overview level

Returns

Return type

str

get_transform()

Returns a GDAL geotransform in its native form.

height
index(x, y, op=<built-in function floor>, precision=None)

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

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

Returns

(row index, col index)

Return type

tuple

indexes

The 1-based indexes of each band in the dataset

For a 3-band dataset, this property will be [1, 2, 3].

Returns

Return type

list of int

interleaving
is_tiled
lnglat()
mask_flag_enums

Sets of flags describing the sources of band masks.

all_valid: There are no invalid pixels, all mask values will be
  1. When used this will normally be the only flag set.

per_dataset: The mask band is shared between all bands on the

dataset.

alpha: The mask band is actually an alpha band and may have

values other than 0 and 255.

nodata: Indicates the mask is actually being generated from

nodata values (mutually exclusive of “alpha”).

Returns

One list of rasterio.enums.MaskFlags members per band.

Return type

list [, list*]

Examples

For a 3 band dataset that has masks derived from nodata values:

>>> dataset.mask_flag_enums
([<MaskFlags.nodata: 8>], [<MaskFlags.nodata: 8>], [<MaskFlags.nodata: 8>])
>>> band1_flags = dataset.mask_flag_enums[0]
>>> rasterio.enums.MaskFlags.nodata in band1_flags
True
>>> rasterio.enums.MaskFlags.alpha in band1_flags
False
meta

The basic metadata of this dataset.

mode
name
nodata

The dataset’s single nodata value

Notes

May be set.

Returns

Return type

float

nodatavals

Nodata values for each band

Notes

This may not be set.

Returns

Return type

list of float

offsets

Raster offset for each dataset band

To set offsets, one for each band is required.

Returns

Return type

list of float

options
overviews()
photometric
profile

Basic metadata and creation options of this dataset.

May be passed as keyword arguments to rasterio.open() to create a clone of this dataset.

read()

Read a dataset’s raw pixels as an N-d array

read_crs()

Return the GDAL dataset’s stored CRS

read_masks()

Read raster band masks as a multidimensional array

read_transform()

Return the stored GDAL GeoTransform

res

Returns the (width, height) of pixels in the units of its coordinate reference system.

sample()

Get the values of a dataset at certain positions

Values are from the nearest pixel. They are not interpolated.

Parameters
  • xy (iterable) – Pairs of x, y coordinates (floats) in the dataset’s reference system.

  • indexes (int or list of int) – Indexes of dataset bands to sample.

  • masked (bool, default: False) – Whether to mask samples that fall outside the extent of the dataset.

Returns

Arrays of length equal to the number of specified indexes containing the dataset values for the bands corresponding to those indexes.

Return type

iterable

scales

Raster scale for each dataset band

To set scales, one for each band is required.

Returns

Return type

list of float

shape
start()

Starts the VRT’s life cycle.

stop()

Ends the VRT’s life cycle

subdatasets

Sequence of subdatasets

tag_namespaces()

Get a list of the dataset’s metadata domains.

Returned items may be passed as ns to the tags method.

Parameters

int, optional (bidx) – Can be used to select a specific band, otherwise the dataset’s general metadata domains are returned.

Returns

Return type

list of str

tags()

Returns a dict containing copies of the dataset or band’s tags.

Tags are pairs of key and value strings. Tags belong to namespaces. The standard namespaces are: default (None) and ‘IMAGE_STRUCTURE’. Applications can create their own additional namespaces.

The optional bidx argument can be used to select the tags of a specific band. The optional ns argument can be used to select a namespace other than the default.

transform

The dataset’s georeferencing transformation matrix

This transform maps pixel row/column coordinates to coordinates in the dataset’s coordinate reference system.

Returns

Return type

Affine

units

one units string for each dataset band

Possible values include ‘meters’ or ‘degC’. See the Pint project for a suggested list of units.

To set units, one for each band is required.

Returns

Return type

list of str

Type

A list of str

width
window(left, bottom, right, top, precision=None)

Get the window corresponding to the bounding coordinates.

The resulting window is not cropped to the row and column limits of the dataset.

Parameters
  • left (float) – Left (west) bounding coordinate

  • bottom (float) – Bottom (south) bounding coordinate

  • right (float) – Right (east) bounding coordinate

  • top (float) – Top (north) bounding coordinate

  • precision (int, optional) – Number of decimal points of precision when computing inverse transform.

Returns

window

Return type

Window

window_bounds(window)

Get the bounds of a window

Parameters

window (rasterio.windows.Window) – Dataset window

Returns

bounds – x_min, y_min, x_max, y_max for the given window

Return type

tuple

window_transform(window)

Get the affine transform for a dataset window.

Parameters

window (rasterio.windows.Window) – Dataset window

Returns

transform – The affine transform matrix for the given window

Return type

Affine

write_transform()
xy(row, col, offset='center')

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.

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

Returns

(x, y)

Return type

tuple