Coverage for stems/gis/coords.py : 95%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
""" Coordinate and geotransforms """
# ============================================================================ # From transform bbox=None, width=None, height=None, center=True): """ Return the coordinates for a given transform
This function needs to know how many pixels are in the raster, so either a :py:class:`rasterio.coords.BoundingBox` or height and width arguments must be supplied.
Parameters ---------- transform : affine.Affine Affine transform bbox : BoundingBox Return the coordinates for this transform that are contained inside the provided bounds (left, bottom, right, up). Coordinates will be aligned/spaced according to the transform and will not necessarily match the upper-left of this ``bbox`` height : int Number of pixels tall width : int Number of pixels wide center : bool, optional Return coordinates for the center of each pixel
Returns ------- y, x : tuple[np.ndarray, np.ndarray] Y/X coordinate pairs (e.g., latitude/longitude) """ # TODO Guard type of transform and bbox using convert func # transform = convert.to_transform(transform)
# TODO Guard bbox # bbox = convert.to_bbox(bbox)
# Find nearest y/x for bbox given posting from transform # Determine (potentially) new upper-left for bbox
# Calculate size needed to cover bounds
# Move the transform to this location "or the dimensions (`height` and `width`) " "of the raster grid.")
""" Return the resolution from a transform
Parameters ---------- transform : affine.Affine Affine transform
Returns ------- tuple[float, float] Width and height """ else: return math.sqrt(a * a + d * d), math.sqrt(b * b + e * e)
""" Return a BoundingBox for some transform and shape
Parameters ---------- transform : affine.Affine Affine transformation height : int Height in pixels width : int Width in pixels
Returns ------- BoundingBox left, bottom, right, & top bounding box coordinates """ else: raise ValueError("Not implemented for rotated transformations (TODO)")
# ============================================================================ # From coordinates """ Calculate Affine transform from coordinates
Note: y/x will be loaded into memory
Parameters ---------- y : array-like Y coordinates (latitude, y, etc) x : array-like X coordinates (longitude, x, etc) center : bool, optional Are coordinates provided pixel center (True) or upper-left (False) assume_unique : bool, optional Assume coordinates are sorted and unique. Otherwise, will run ``np.unique`` on each
Returns ------- affine.Affine Affine transformation """ assume_unique=assume_unique)
""" Calculate the BoundingBox of the coordinates
If the Affine ``transform`` of the data is known, it can be provided. Otherwise this function runs :py:func:`coords_to_transform` to determine the pixel sizing.
Parameters ---------- y : array-like Y coordinates (latitude, y, etc) x : array-like X coordinates (longitude, x, etc) center : bool, optional Are coordinates provided pixel center (True) or upper-left (False) assume_unique : bool, optional Assume coordinates are sorted and unique. Otherwise, will run ``np.unique`` on each
Returns ------- BoundingBox Bounds expressed as left, bottom, right, top """ assume_unique=assume_unique)
# TODO: @warn_dask? # TODO: @lru_cache? # returns transform, bounds, shape
# If spacing is not equal we guess from `(max - min) / n` else: # np.unique returns sorted y so dy is negative, but not if unsorted
else:
dx, 0.0, minmax_x[0], 0., dy, minmax_y[1] )
# affine transform is relative to upper-left, not pixel center # create bounds that cover pixel area minmax_y[0] + dy / 2, minmax_x[1] + dx / 2, minmax_y[1] - dy / 2) else: # create bounds that cover pixel area minmax_y[0] + dy, minmax_x[1] + dx, minmax_y[1])
""" Check for equal spacing (see GDAL NetCDF driver)
Returns -------- bool or float False if unequal spacing, otherwise returns the calculated pixel spacing/resolution """
return 0. else: else: mid = end
else: |