rio_terrain Package#

terrain Module#

rio_terrain.core.terrain.aspect(arr, res=(1, 1), pcs='compass', neighbors=4)#

Calculates aspect.

Parameters:
  • arr – 2D numpy array

  • res – tuple of raster cell width and height

  • pcs – choice of polar coordinate system

  • neighbors – use four or eight neighbor cells in calculation

Returns:

2D array representing slope aspect

rio_terrain.core.terrain.curvature(arr, res=(1, 1), neighbors=4)#

Calculates curvature.

Parameters:
  • arr – 2D numpy array

  • res – tuple of raster cell width and height

  • neighbors – use four or eight neighbor cells in calculation

Returns:

2D array representing surface curvature

rio_terrain.core.terrain.slope(arr, res=(1, 1), units='grade', neighbors=4)#

Calculates slope.

Parameters:
  • arr – 2D numpy array

  • res – tuple of raster cell width and height

  • units – choice of grade or degrees

  • neighbors – use four or eight neighbor cells in calculation

Returns:

2D array representing slope

windowing Module#

rio_terrain.core.windowing.block_count(shape, block_shapes, band=1)#

Determine the number of blocks in a band

Parameters:
  • shape (tuple[int, int]) – tuple containing raster height and width in cells

  • block_shapes (list[tuple[int, int]]) – block shapes for a rasterio read source

  • band (int) – raster band to count on

Returns:

number of blocks in the raster

Return type:

int

rio_terrain.core.windowing.bounds_window(bounds, affine, constrain=True)#

Create a full cover rasterio-style window

Parameters:
  • bounds (tuple[float, float, float, float]) – boundary coordinates ordered w, s, e, n

  • affine (Affine) – transformation

  • constrain (bool) –

Returns:

row slice coordinates, col slice coordinates

Return type:

tuple[slice, slice]

rio_terrain.core.windowing.expand_window(window, src_shape, margin=10)#

Expand a window by a margin

Parameters:
  • window (Window) – Window

  • src_shape (tuple[int, int]) – shape in

  • margin (int) – margin width in cells

Returns:

Window

Return type:

Window

rio_terrain.core.windowing.intersect_bounds(bbox0, bbox1)#

Get the intersection in w s e n

Parameters:
  • bbox0 (tuple[float, float, float, float]) – bounding coordinates

  • bbox1 (tuple[float, float, float, float]) – bounding coordinates

Returns:

intersection coordinate bounds (w, s, e, n)

Return type:

tuple[float, float, float, float]

rio_terrain.core.windowing.is_raster_aligned(src0, src1)#

Check two rasters for cell alignment

Parameters:
  • src0 (DatasetReader) – rasterio read source

  • src1 (DatasetReader) – rasterio read source

Returns:

True if the raster source cells align

Return type:

bool

rio_terrain.core.windowing.is_raster_congruent(src0, src1, band=1)#

Tests two rasters for coincident bounds.

Parameters:
  • src0 (DatasetReader) – rasterio read source

  • src1 (DatasetReader) – rasterio read source

  • band (int) –

Returns:

True if the rasters are coincident

Return type:

bool

rio_terrain.core.windowing.is_raster_intersecting(src0, src1)#

Test two rasters for overlap

Parameters:
  • src0 (DatasetReader) – rasterio read source

  • src1 (DatasetReader) – rasterio read source

Returns:

True if the raster sources intersect

Return type:

bool

rio_terrain.core.windowing.margins(window0, window1)#

Size of collar between a pair of windows

Parameters:
  • window0 (Window) – read window

  • window1 (Window) – write window

Returns:

margins in cells in order w, s, e, n

Return type:

tuple[int, int, int, int]

rio_terrain.core.windowing.slices_to_window(rows, cols)#
Parameters:
  • rows (slice) –

  • cols (slice) –

Return type:

Window

rio_terrain.core.windowing.subsample(blocks, probability=1.0)#

Subsample an iterable at a given probability

Parameters:
  • blocks (Iterator[Window]) – an iterable of rasterio windows

  • probability (float) – fraction of blocks to sample

Yields:

yield a rasterio window if sampled

Return type:

Iterator[Window]

rio_terrain.core.windowing.tile_dim(stop, step, min_size=1, balance=False, merge=False, as_chunks=False)#

Tile a range to coordinates or tile shapes

Divide a range using a specified size, and optionally remove last coordinate to meet a minimum size

Parameters:
  • dim – size of the ranges

  • step (int) – desired tile width

  • min_size (int) – minumum allowed width

  • balance (bool) – distribute remainder to balance tile sizes

  • merge (bool) – include remainder in final tile

  • as_chunks (bool) – return tile width instead of coordinates

  • stop (int) –

Returns:

array of start coordinates or chunk widths

rio_terrain.core.windowing.tile_dims(shape, tile_shape, min_size=1, balance=False, merge=False, as_chunks=False)#

Tile a 2D array

Tile a 2D array with a specified size and return corner coordinate series.

Parameters:
  • shape (tuple[int, int]) – shape to tile as a tuple

  • tile_shape (tuple[int, int]) – desired tile shape as a tuple

  • min_size (int) – minimum allowed tile length

  • balance (bool) – distribute remainder to balance tile sizes

  • merge (bool) – include remainder in final tile

  • as_chunks (bool) – return tile shapes instead of coordinates

Returns:

xx and yy start corner coordinate arrays, or tile height and width arrays

rio_terrain.core.windowing.tile_grid(ncols, nrows, blockxsize, blockysize, col_offset=0, row_offset=0, overlap=0, min_size=1, balance=False, merge=False)#

Return a generator containing read and write windows with specified dimensions and overlap

mgrid returns not as expected so used broadcast_arrays instead base_rows, base_cols = np.mgrid[0:h:blockysize, 0:w:blockxsize]

Parameters:
  • ncols (int) – raster width in columns

  • nrows (int) – raster height in rows

  • blockxsize (int) – block width in rows

  • blockysize (int) – block height in rows

  • col_offset (int) – columns to offset the grid

  • row_offset (int) – rows to offset the grid

  • overlap (int) – overlap between windows

  • min_size (int) –

  • balance (bool) –

  • merge (bool) –

Yields:

window within a tiling for a region

Return type:

Iterator[Window]

rio_terrain.core.windowing.tile_grid_intersection(src0, src1, blockxsize=None, blockysize=None)#

Generate tiled windows for the intersection between two grids.

Given two rasters having different dimensions calculate read-window generators for each and a write-window generator for the intersecion.

Parameters:
  • src0 (DatasetReader) – rasterio read source

  • src1 (DatasetReader) – rasterio read source

  • blockxsize (None | int) – write-window width

  • blockysize (None | int) – write-window height

Returns:

read windows for src0, read windows for src1, write windows for the intersection, write raster Affine, write raster width in columns write raster height in rows

Return type:

tuple[Iterator[Window], Iterator[Window], Iterator[Window], Affine, int, int]

rio_terrain.core.windowing.trim(arr, margins)#

Trim a 2D array by a set of margins

Parameters:
  • arr – an array to trim

  • margins – number of cells to trim from each side

Returns:

trimmed array

rio_terrain.core.windowing.window_bounds(window, affine, offset='center')#

Create bounds coordinates from a rasterio window

Parameters:
  • window (Window) – Window

  • affine (Affine) – Affine

  • offset (str) – str

Returns:

coordinate bounds (w, s, e, n)

Return type:

tuple[float, float, float, float]

statistics Module#

rio_terrain.core.statistics.mean(src, windows, njobs)#

Calculates the mean of a rasterio source

Parameters:
  • src (DatasetReader) – rasterio source

  • windows (Iterator[Window]) – iterable of read and write windows

  • njobs (int) – number of processing jobs

Returns:

mean value

Return type:

float

Note

ArcGIS mean = 140.04371922353

rio_terrain.core.statistics.minmax(src, windows, njobs)#

Calculates the minimum and maximum values in a rasterio source.

Parameters:
  • src (DatasetReader) – rasterio source

  • windows (Iterator[Window]) – iterable of read and write windows

  • njobs (int) – number of processing jobs

Returns:

minimum value, maximum value

Return type:

tuple[float | None, float | None]

Note

ArcGIS min = 77.278923034668 ArcGIS max = 218.81454467773

rio_terrain.core.statistics.stddev(src, mean, windows, njobs)#

Calculates the standard deviation of a rasterio source

Parameters:
  • src (DatasetReader) – rasterio source

  • mean (float) – mean value

  • windows (Iterator[Window]) – iterable of read and write windows

  • njobs (int) – number of processing jobs

Returns:

standard deviation

Return type:

float

Note

ArcGIS stddev = 23.555450665488

focalstatistics Module#

rio_terrain.core.focalstatistics.mad(arr, size=(3, 3))#

Calculates the median absolute deviation (MAD) for an array

Parameters:
  • arr – data array

  • size – kernel size

Returns:

array of median absolute deviation

rio_terrain.core.focalstatistics.std(arr, size=(3, 3))#

Calculates the standard deviation for a neighborhood

Parameters:
  • arr – data array

  • size – kernel size

Returns:

array of standard deviation

rio_terrain.core.focalstatistics.std_ndimage(arr, size=(3, 3))#

Calculates the standard deviation for a neighborhood

Parameters:
  • arr – data array

  • size – kernel size

Returns:

array of standard deviation