raster_tools.Raster#

class raster_tools.Raster(raster, _fast_path=False)[source]#

Abstraction of georeferenced raster data with lazy function evaluation.

Raster is a wrapper around xarray Datasets and DataArrays. It takes advantage of xarray’s dask integration to allow lazy loading and evaluation. It allows a pipeline of operations on underlying raster sources to be built in a lazy fashion and then evaluated effiently. Most mathematical operations have been overloaded so operations such as z = x - y and r = x**2 are possible.

Two Rasters can be combined if they share a CRS and cell size and their grids are offset by a whole number of cells. The result covers their overlap. Otherwise a ValueError is raised and the rasters must be put on a common grid first, for example with raster_tools.align(). Bands are matched by position: the Rasters must have the same number of bands, or one of them must have a single band, which is applied to every band of the other. In-place operations (x += y) raise a ValueError if the result would not have the same grid and band count as x.

When combining a Raster with an xarray.DataArray, the Raster must be the left operand (raster + dataarray), or the DataArray should be wrapped first (Raster(dataarray) + raster). With the DataArray on the left, xarray converts the Raster to a numpy array through __array__. This computes the Raster eagerly and drops its null mask, so its null cells enter the operation as plain values, and the result is a numpy-backed DataArray rather than a Raster.

Operations on a Raster return a new Raster, except in-place operators (x += y, x *= 2, …), which update x itself.

Parameters

raster (str, Raster, xarray.DataArray, xarray.Dataset, numpy.ndarray) – The raster source to use for this Raster. If raster is a string, it is treated like a path. If raster is a Raster, a copy is made and its raster source is used. If raster is an xarray DataArray, Dataset, or numpy array, it is used as the source. Dataset objects must contain ‘raster’ and ‘mask’ variables. The dimensions of these variables are assumed to be “band”, “y”, “x”, in that order. The ‘mask’ variable must have boolean dtype.

__init__(raster, _fast_path=False)[source]#

Methods

__init__(raster[, _fast_path])

all(*args, **kwargs)

Reduce the raster to one dask value by applying all across all bands.

any(*args, **kwargs)

Reduce the raster to one dask value by applying any across all bands.

astype(dtype[, new_null_value, ...])

Return a copy of the Raster cast to the specified type.

burn_mask()

Fill null-masked cells with null value.

chunk(chunks)

Rechunk the underlying raster

close()

Close the underlying source

copy()

Returns a copy of this Raster.

eval()

Compute any applied operations and return the result as new Raster.

explore(band, *args, **kwargs)

Plot the raster band on an interactive folium map.

geo_map_blocks(func, *rasters[, dtype, ...])

Apply func block-wise across one or more aligned rasters, handing it georeferenced xarray.DataArray blocks.

geo_map_overlap(func, *rasters, depth[, ...])

Apply func block-wise with overlap, handing it georeferenced xarray.DataArray blocks.

get_bands(bands)

Retrieve the specified bands as a new Raster.

get_chunk_bounding_boxes([include_band])

Return GeoDataFrame with the chunk bounding boxes.

get_chunk_rasters()

Return the underlying data chunks as an array of Rasters.

get_chunked_coords()

Get lazy coordinate arrays, in x-y order, chunked to match data.

index(x, y)

Return the (row, col) index of the pixel at (x, y).

load()

Compute delayed operations and load the result into memory.

map_blocks(func, *rasters[, dtype, ...])

Apply func block-wise across one or more aligned rasters.

map_overlap(func, *rasters, depth[, ...])

Apply func block-wise with overlap across one or more rasters.

max(*args, **kwargs)

Reduce the raster to one dask value by applying max across all bands.

mean(*args, **kwargs)

Reduce the raster to one dask value by applying mean across all bands.

min(*args, **kwargs)

Reduce the raster to one dask value by applying min across all bands.

model_predict(model[, n_outputs])

Generate a new raster using the provided model to predict new values.

pad(target, *[, fill_values])

Pad the raster's grid out to cover the given extent.

plot(*args, **kwargs)

Plot the raster.

prod(*args, **kwargs)

Reduce the raster to one dask value by applying prod across all bands.

reclassify(remapping[, unmapped_to_null])

Reclassify raster values based on a mapping.

remap_range(mapping[, inclusivity])

Remaps values based on a mapping or list of mappings.

replace_null(value)

Replaces null values with value.

reproject([crs_or_geobox, resample_method, ...])

Reproject to a new projection or resolution.

round([decimals])

Evenly round to the given number of decimals

save(path[, null_value, color_table, ...])

Compute the final raster and save it to the provided location.

save_chunks(prefix, *[, per_band, ext])

Save each dask chunk to a separate raster file.

set_crs(crs)

Set the CRS for the underlying data.

set_null(mask_raster)

Update the raster's null pixels using the provided mask

set_null_value(value)

Sets or replaces the null value for the raster.

split_bands()

Split this raster into a list of single-band rasters.

std(*args, **kwargs)

Reduce the raster to one dask value by applying std across all bands.

sum(*args, **kwargs)

Reduce the raster to one dask value by applying sum across all bands.

to_dataset()

Returns the underlying xarray.Dataset.

to_null_mask()

Returns a boolean Raster with True at null values and False otherwise.

to_numpy()

Return the raw internal raster as a numpy array.

to_points()

Convert the raster into a vector where each non-null cell is a point.

to_polygons([neighbors])

Convert the raster to a vector of polygons.

to_quadrants()

Split the raster into quadrants

to_vector([as_polygons, neighbors])

Convert the raster to a vector.

var(*args, **kwargs)

Reduce the raster to one dask value by applying var across all bands.

where(condition, other)

Filter elements from this raster according to condition.

xy(row, col[, offset])

Return the (x, y) coordinates of the pixel at (row, col).

Attributes

affine

The affine transformation for the raster data.

band

The band coordinate values.

bandwise

Returns an adapter for band-wise operations on this raster

bounds

Bounds tuple (minx, miny, maxx, maxy)

crs

The raster's CRS.

data

The underlying dask array of data

dtype

The dtype of the data.

geo_block_infos

Chunksize-1 dask array of GeoBlockInfo objects.

geobox

GeoBox object describing the raster's grid.

mask

The null value mask as a dask array.

nbands

The number of bands.

null_value

The raster's null value used to fill missing or invalid entries.

pxrs

resolution

The x and y cell sizes as a tuple.

shape

The shape of the underlying raster.

size

The number of grid cells across all bands.

values

The raw internal raster as a numpy array.

x

The x (horizontal) coordinate values.

xdata

The underlying xarray.DataArray (read only)

xmask

The null value mask as an xarray DataArray.

xrs

y

The y (vertical) coordinate values.