bordado.shape_to_spacing#
- bordado.shape_to_spacing(region, shape, *, pixel_register=False)[source]#
Calculate the spacing of a regular grid given a region and shape.
The spacing is assumed to be constant along each direction but can vary between directions.
- Parameters:
- region
tuple
= (W
,E
,S
,N
, …) The boundaries of a given region in Cartesian or geographic coordinates. Should have a lower and an upper boundary for each dimension of the coordinate system.
- shape
tuple
= (…,size_SN
,size_WE
) The number of points in each direction of the given region, in reverse order. Must have one integer value per dimension of the region. The order of arguments is the opposite of the order of the region for compatibility with numpy’s
.shape
attribute.- pixel_registerbool
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.
- region
- Returns:
- spacing
tuple
= (…,space_SN
,space_WE
) The grid spacing in each direction of the given region, in reverse order. Has one value per dimension of the region. The order of arguments is the opposite of the order of the region for compatibility with shape.
- spacing
Examples
For 2-dimensional grids, the region has 4 elements and the shape must have 2 elements:
>>> spacing = shape_to_spacing((0, 10, -5, 1), (7, 11)) >>> print(f"{spacing[0]:.1f}, {spacing[1]:.1f}") 1.0, 1.0
The spacing doesn’t have to be the same in each direction:
>>> spacing = shape_to_spacing((0, 10, -5, 1), (14, 11)) >>> print(f"{spacing[0]:.1f}, {spacing[1]:.1f}") 0.5, 1.0
Notice that the spacing is in the same order as the shape:
>>> spacing = shape_to_spacing((0, 10, -5, 1), (7, 21)) >>> print(f"{spacing[0]:.1f}, {spacing[1]:.1f}") 1.0, 0.5
Pixel registration is also supported:
>>> spacing = shape_to_spacing( ... (-0.5, 10.5, -5.5, 1.5), (7, 11), pixel_register=True, ... ) >>> print(f"{spacing[0]:.1f}, {spacing[1]:.1f}") 1.0, 1.0 >>> spacing = shape_to_spacing( ... (-0.25, 10.25, -5.5, 1.5), (7, 21), pixel_register=True, ... ) >>> print(f"{spacing[0]:.1f}, {spacing[1]:.1f}") 1.0, 0.5
Grids don’t have to be 2-dimensional:
>>> spacing = shape_to_spacing((0, 10, -5, 1, 10, 14), (5, 7, 11)) >>> print(f"{spacing[0]:.1f}, {spacing[1]:.1f}, {spacing[2]:.1f}") 1.0, 1.0, 1.0 >>> spacing = shape_to_spacing( ... (-0.25, 10.25, -5.5, 1.5, -0.1, 1.1), (5, 7, 21), pixel_register=True, ... ) >>> print(f"{spacing[0]:.1f}, {spacing[1]:.1f}, {spacing[2]:.1f}") 0.2, 1.0, 0.5