verde.grid_coordinates

verde.grid_coordinates(region, shape=None, spacing=None, adjust='spacing', pixel_register=False)[source]

Generate the coordinates for each point on a regular grid.

The grid can be specified by either the number of points in each dimension (the shape) or by the grid node spacing.

If the given region is not divisible by the desired spacing, either the region or the spacing will have to be adjusted. By default, the spacing will be rounded to the nearest multiple. Optionally, the East and North boundaries of the region can be adjusted to fit the exact spacing given. See the examples below.

Parameters:
region : list = [W, E, S, N]

The boundaries of a given region in Cartesian or geographic coordinates.

shape : tuple = (n_north, n_east) or None

The number of points in the South-North and West-East directions, respectively.

spacing : float, tuple = (s_north, s_east), or None

The grid spacing in the South-North and West-East directions, respectively. A single value means that the spacing is equal in both directions.

adjust : {‘spacing’, ‘region’}

Whether to adjust the spacing or the region if required. Ignored if shape is given instead of spacing. Defaults to adjusting the spacing.

pixel_register : bool

If True, the coordinates will refer to the center of each grid pixel instead of the grid lines. In practice, this means that there will be one less element per dimension of the grid when compared to grid line registered. Default is False.

Returns:
easting, northing : 2d arrays

The West-East and South-North coordinates of each point in the grid. The arrays have the specified shape.

See also

scatter_points
Generate the coordinates for a random scatter of points
profile_coordinates
Coordinates for a profile between two points

Examples

>>> east, north = grid_coordinates(region=(0, 5, 0, 10), shape=(5, 3))
>>> print(east.shape, north.shape)
(5, 3) (5, 3)
>>> # Lower printing precision to shorten this example
>>> import numpy as np; np.set_printoptions(precision=1, suppress=True)
>>> print(east)
[[0.  2.5 5. ]
 [0.  2.5 5. ]
 [0.  2.5 5. ]
 [0.  2.5 5. ]
 [0.  2.5 5. ]]
>>> print(north)
[[ 0.   0.   0. ]
 [ 2.5  2.5  2.5]
 [ 5.   5.   5. ]
 [ 7.5  7.5  7.5]
 [10.  10.  10. ]]
>>> # The grid can also be specified using the spacing between points
>>> # instead of the shape.
>>> east, north = grid_coordinates(region=(0, 5, 0, 10), spacing=2.5)
>>> print(east.shape, north.shape)
(5, 3) (5, 3)
>>> print(east)
[[0.  2.5 5. ]
 [0.  2.5 5. ]
 [0.  2.5 5. ]
 [0.  2.5 5. ]
 [0.  2.5 5. ]]
>>> print(north)
[[ 0.   0.   0. ]
 [ 2.5  2.5  2.5]
 [ 5.   5.   5. ]
 [ 7.5  7.5  7.5]
 [10.  10.  10. ]]
>>> # The spacing can be different for northing and easting, respectively
>>> east, north = grid_coordinates(region=(-5, 1, 0, 10), spacing=(2.5, 1))
>>> print(east.shape, north.shape)
(5, 7) (5, 7)
>>> print(east)
[[-5. -4. -3. -2. -1.  0.  1.]
 [-5. -4. -3. -2. -1.  0.  1.]
 [-5. -4. -3. -2. -1.  0.  1.]
 [-5. -4. -3. -2. -1.  0.  1.]
 [-5. -4. -3. -2. -1.  0.  1.]]
>>> print(north)
[[ 0.   0.   0.   0.   0.   0.   0. ]
 [ 2.5  2.5  2.5  2.5  2.5  2.5  2.5]
 [ 5.   5.   5.   5.   5.   5.   5. ]
 [ 7.5  7.5  7.5  7.5  7.5  7.5  7.5]
 [10.  10.  10.  10.  10.  10.  10. ]]
>>> # If the region can't be divided into the desired spacing, the spacing
>>> # will be adjusted to conform to the region
>>> east, north = grid_coordinates(region=(-5, 0, 0, 5), spacing=2.6)
>>> print(east.shape, north.shape)
(3, 3) (3, 3)
>>> print(east)
[[-5.  -2.5  0. ]
 [-5.  -2.5  0. ]
 [-5.  -2.5  0. ]]
>>> print(north)
[[0.  0.  0. ]
 [2.5 2.5 2.5]
 [5.  5.  5. ]]
>>> east, north = grid_coordinates(region=(-5, 0, 0, 5), spacing=2.4)
>>> print(east.shape, north.shape)
(3, 3) (3, 3)
>>> print(east)
[[-5.  -2.5  0. ]
 [-5.  -2.5  0. ]
 [-5.  -2.5  0. ]]
>>> print(north)
[[0.  0.  0. ]
 [2.5 2.5 2.5]
 [5.  5.  5. ]]
>>> # You can also choose to adjust the East and North boundaries of the
>>> # region instead.
>>> east, north = grid_coordinates(region=(-5, 0, 0, 5), spacing=2.6,
...                                adjust='region')
>>> print(east.shape, north.shape)
(3, 3) (3, 3)
>>> print(east)
[[-5.  -2.4  0.2]
 [-5.  -2.4  0.2]
 [-5.  -2.4  0.2]]
>>> print(north)
[[0.  0.  0. ]
 [2.6 2.6 2.6]
 [5.2 5.2 5.2]]
>>> east, north = grid_coordinates(region=(-5, 0, 0, 5), spacing=2.4,
...                                adjust='region')
>>> print(east.shape, north.shape)
(3, 3) (3, 3)
>>> print(east)
[[-5.  -2.6 -0.2]
 [-5.  -2.6 -0.2]
 [-5.  -2.6 -0.2]]
>>> print(north)
[[0.  0.  0. ]
 [2.4 2.4 2.4]
 [4.8 4.8 4.8]]
>>> # We can optionally generate coordinates for the center of each grid
>>> # pixel instead of the corner (default)
>>> east, north = grid_coordinates(region=(0, 5, 0, 10), spacing=2.5,
...                                pixel_register=True)
>>> # Lower printing precision to shorten this example
>>> import numpy as np; np.set_printoptions(precision=2, suppress=True)
>>> print(east.shape, north.shape)
(4, 2) (4, 2)
>>> print(east)
[[1.25 3.75]
 [1.25 3.75]
 [1.25 3.75]
 [1.25 3.75]]
>>> print(north)
[[1.25 1.25]
 [3.75 3.75]
 [6.25 6.25]
 [8.75 8.75]]

Examples using verde.grid_coordinates