Miscellaneous raster-grid functions#

calculate_slope_aspect_bfp(xs, ys, zs)[source]#

Calculate slope and aspect.

Code author: Katy Barnhart <katherine.barnhart@colorado.edu>

Fits a plane to the given N points with given xs, ys, and zs values using single value decomposition.

Returns a tuple of (slope, aspect) based on the normal vector to the best fit plane.


This function does not check if the points fall on a line, rather than a plane.

find_nearest_node(rmg, coords, mode='raise')[source]#

Find the node nearest a point.

Find the index to the node nearest the given x, y coordinates. Coordinates are provided as numpy arrays in the coords tuple. coords is tuple of coordinates, one for each dimension.

The mode keyword to indicate what to do if a point is outside of the grid. Valid choices are the same as that used with the numpy function ravel_multi_index.

A point is considered to be outside of the grid if it is outside the perimeter of the grid by one half of the grid spacing.

  • rmg (RasterModelGrid) – The source grid.

  • coords (tuple) – Coordinates of point as (x, y)

  • mode ({'raise', 'wrap', 'clip'}, optional) – Action to take if a point is outside of the grid.


Indices of the nodes nearest the given coordinates.

Return type:



Create a grid of 4 by 5 nodes with unit spacing.

>>> import landlab
>>> from landlab.grid.raster_funcs import find_nearest_node
>>> rmg = landlab.RasterModelGrid((4, 5))

The points can be either a tuple of scalars or of arrays.

>>> find_nearest_node(rmg, (0.2, 0.6))
>>> find_nearest_node(rmg, (np.array([1.6, 3.6]), np.array([2.3, 0.7])))
array([12,  9])

The mode keyword indicates what to do if a point is outside of the grid.

>>> find_nearest_node(rmg, (-0.6, 0.6), mode="raise")
Traceback (most recent call last):
ValueError: invalid entry in coordinates array
>>> find_nearest_node(rmg, (-0.6, 0.6), mode="clip")
>>> find_nearest_node(rmg, (-0.6, 0.6), mode="wrap")
is_coord_on_grid(rmg, coords, axes=(0, 1))[source]#

Check if coordinates are contained on a grid.

  • rmg (RasterModelGrid) – Source grid.

  • coords (tuple) – Coordinates of point as (x, y)

  • axes (tuple, optional) – Check bounds only on a particular axis


Create a grid that ranges from x=0 to x=4, and y=0 to y=3.

>>> from landlab import RasterModelGrid
>>> from landlab.grid.raster_funcs import is_coord_on_grid
>>> grid = RasterModelGrid((4, 5))
>>> is_coord_on_grid(grid, (3.999, 2.999))

Check two points with one call. Numpy broadcasting rules apply for the point coordinates.

>>> is_coord_on_grid(grid, ([3.9, 4.1], 2.9))
array([ True, False])
>>> is_coord_on_grid(grid, ([3.9, 4.1], 2.9), axes=(0,))
array([ True,  True])
line_to_grid_coords(c0, r0, c1, r1)[source]#

Return integer grid coords forming line segment (c0, r0)->(c1, r1).

  • c0 (int) – column and row coordinates of “starting” endpoint

  • r0 (int) – column and row coordinates of “starting” endpoint

  • c1 (int) – column and row coordinates of “ending” endpoint

  • r1 (int) – column and row coordinates of “ending” endpoint


rr, cc – row and column coordinates of nodes in the line

Return type:

(N,) ndarray of int


>>> line_to_grid_coords(0, 0, 4, 1)
(array([0, 0, 0, 1, 1]), array([0, 1, 2, 3, 4]))


Inputs must be grid coordinates rather than actual (x, y) values (unless the grid has unit spacing, in which case they are the same). To convert from real (x, y) to (x_grid, y_grid) use x_grid = x / Dx, where Dx is horizontal grid spacing (and similarly for y).

To convert a raster-grid node ID to column and row coords, use numpy.unravel_index(node_id, (num_rows, num_cols)).

To convert the returned grid coordinates to node IDs, use the RasterModelGrid method grid_coords_to_node_id().

This function uses an incremental algorithm for line scan-conversion (see, e.g., Foley et al., 1990, chapter 3). For a line with a slope 0 > m > 1, start with the x coordinates for a set of grid columns that span the line. The corresponding y of the first one is just y0. The y for the next one is y0 + m, for the next y0 + 2m, etc. If m > 1, you use y instead of x. In the below, any line segments that “point” toward the lower-left half-grid (i.e., with azimuth between 135o and 315o) have their endpoints flipped first.

neighbor_node_at_cell(grid, inds, *args)[source]#

node_id_of_cell_neighbor(grid, neighbor_ids [, cell_ids])

Return an array of the node ids for neighbors of cell_id cells. neighbor_ids is an index into the neighbors of a cell as measured clockwise starting from the south.

If cell_ids is not given, return neighbors for all cells in the grid.

  • grid (RasterModelGrid) – Input grid.

  • neighbor_ids (array_like) – IDs of the neighbor nodes.

  • cell_ids (array_like, optional) – IDs of cell about which to get neighbors.


Node IDs for given neighbors of cells.

Return type:



>>> from landlab import RasterModelGrid
>>> from landlab.grid.raster_funcs import neighbor_node_at_cell
>>> grid = RasterModelGrid((4, 5), xy_spacing=1.0)
>>> neighbor_node_at_cell(grid, 0, 0)

Get the lower and the the upper neighbors for all the cells.

>>> neighbor_node_at_cell(grid, 0)
array([1, 2, 3, 6, 7, 8])
>>> neighbor_node_at_cell(grid, 2)
array([11, 12, 13, 16, 17, 18])

As an alternative to the above, use fancy-indexing to get both sets of neighbors with one call.

>>> neighbor_node_at_cell(grid, np.array([0, 2]), [1, 4])
array([[ 2, 12],
       [ 7, 17]])