raster_tools.general.where#

raster_tools.general.where(condition, true_rast, false_rast)[source]#

Return elements chosen from true_rast or false_rast depending on condition.

The mask for the result is a combination of all three raster inputs. Any cells that are masked in the condition raster will be masked in the output. The rest of the cells are masked based on which raster they were taken from, as determined by the condition raster, and if the cell in that raster was null. Effectively, the resulting mask is determined as follows: where(condition.mask, True, where(condition, true_rast.mask, false_rast.mask).

true_rast and false_rast can both be None to indicate a null value but they cannot both be None at the same time. An error is raised in that case.

The raster inputs are combined on a common grid, following the same rules as arithmetic between Rasters: they must share a CRS (a raster without a CRS matches any CRS) and cell size, and their grids must be offset by a whole number of cells. The result covers their overlap, on the condition raster’s cell coordinates. Otherwise a ValueError is raised and the rasters must be put on a common grid first, for example with align(). Bands are matched by position, whatever their labels: the rasters must have the same number of bands, or a single band, which is applied to every band of the others. The result takes its band labels from the first raster with the most bands.

Parameters
  • condition (str or Raster) – A boolean or int raster that indicates where elements in the result should be selected from. If the condition is an int raster, it is coerced to bool using condition > 0. True cells pull values from true_rast and False cells pull from y. str is treated as a path to a raster.

  • true_rast (scalar, Raster, str, None) – Raster or scalar to pull from if the corresponding location in condition is True. If None, this is treated as a null value.

  • false_rast (scalar, Raster, str, None) – Raster or scalar to pull from if the corresponding location in condition is False. If None, this is treated as a null value.

Returns

The resulting Raster.

Return type

Raster