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.

All operations on a Raster return a new Raster.

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 a single dask value by applying all across all bands.

any(*args, **kwargs)

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

astype(dtype[, warn_about_null_change])

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.

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.

max(*args, **kwargs)

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

mean(*args, **kwargs)

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

min(*args, **kwargs)

Reduce the raster to a single 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.

plot(*args, **kwargs)

Plot the raster.

prod(*args, **kwargs)

Reduce the raster to a single 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[, no_data_value])

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

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.

std(*args, **kwargs)

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

sum(*args, **kwargs)

Reduce the raster to a single 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 a single 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.

geobox

GeoBox object describing the raster's grid.

geochunks

Produce an array of GeoChunks that correspond to the underlying chunks in the data.

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.