riogrande.prepare#

Functions used to create windows/ views for parallelization processes in 2D raster maps.

Functions#

create_views(view_size, border, size)

Returns a set of views on which an operation can be applied independently

get_view(data, view)

Return a recatangular view of the data array

relative_view(view, inner_view)

Return the inner_view relative to view

update_view(data, view, block)

Update a view from the data array with a block

Module Contents#

riogrande.prepare.create_views(view_size, border, size)[source]#

Returns a set of views on which an operation can be applied independently

Parameters:
  • view_size (tuple[int, int]) – The size (width, height) in pixels of a single view (excluding borders)

  • border (tuple[int, int]) – The border size (width, height) in number of pixels along each axis

  • size (tuple[int, int]) – The total size of the map in number of pixels (width, height)

Returns:

The first element is a list of tuples (x, y, width, height) defining each view on which to apply an operation. The second element is a list of tuples (x, y, width, height) defining for each view the usable region. A region is usable if it does not contain any artificial border effects that were introduced from splitting up a bigger view into smaller chunks

Return type:

tuple

Notes

  • Handles cases where the region cannot be divided evenly by view_size. The last row/column of views may be smaller (leftovers) and are still included. Uses math.floor() to compute the number of full blocks.

  • Borders on the outer edges are reduced to fit within the total size.

See also

update_view()

Write a block into a view of an array.

get_view()

Read a rectangular view from an array.

relative_view()

Express an inner view relative to an outer view.

Examples

>>> views, usable = create_views((5, 5), (1, 1), (9, 9))
>>> len(views), len(usable)
(4, 4)
>>> views
[(0, 0, 6, 6), (3, 0, 6, 5), (0, 3, 6, 6), (3, 3, 6, 6)]
>>> usable
[(0, 0, 4, 4), (4, 0, 5, 4), (0, 4, 4, 5), (4, 4, 5, 5)]
riogrande.prepare.get_view(data, view)[source]#

Return a recatangular view of the data array

Note

data.shape == height, width!

Parameters:
  • data (NDArray) – np.array to return the view from

  • view (tuple[int, int, int, int]) – tuple (x, y, width, height) defining the view

Returns:

A view (slice) of data with shape (height, width) as specified by the view tuple.

Return type:

NDArray

See also

update_view()

Write a block into a view of an array.

create_views()

Generate a set of views covering an array.

Examples

>>> arr = np.arange(16).reshape(4, 4)
>>> arr
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])
>>> get_view(arr, (1, 1, 2, 2))
array([[ 5,  6],
       [ 9, 10]])
riogrande.prepare.relative_view(view, inner_view)[source]#

Return the inner_view relative to view

Given two rectangular regions defined as (x, y, width, height), this function returns the coordinates of inner_view relative to the origin of view.

Parameters:
  • view (tuple[int, int, int, int]) – A 4-tuple (x, y, width, height) defining the outer view.

  • inner_view (tuple[int, int, int, int]) – A 4-tuple (x, y, width, height) defining a subregion inside view.

Returns:

A 4-tuple (x, y, width, height) giving the position and size of inner_view relative to view. The width and height are unchanged.

Return type:

tuple

See also

create_views()

Generate outer and inner view pairs.

Examples

>>> outer = (10, 20, 100, 50)
>>> inner = (15, 30, 20, 10)
>>> outer
(10, 20, 100, 50)
>>> inner
(15, 30, 20, 10)
>>> relative_view(outer, inner)
(5, 10, 20, 10)
riogrande.prepare.update_view(data, view, block)[source]#

Update a view from the data array with a block

The block must exactly match the shape of the view: block.shape == (view[3], view[2]), where view = (x, y, width, height).

Parameters:
  • data (NDArray) – The array that we want to update

  • view (tuple[int, int, int, int]) – tuple (x, y, width, height) defining the view of the data array to update

  • block (ArrayLike) – np.array with the updated values.

Return type:

None

See also

get_view()

Read a rectangular view from an array.

create_views()

Generate a set of views covering an array.

Examples

>>> data = np.zeros((5, 5))
>>> block = np.ones((2, 3))
>>> update_view(data, (1, 2, 3, 2), block)
>>> data
array([[0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 1., 1., 1., 0.],
       [0., 1., 1., 1., 0.],
       [0., 0., 0., 0., 0.]])