MappingRaster#
For deeper information about the concept see: Raster Mapper
Class Methods
|
Create a |
Main API Methods
Map coordinates by intersecting rays with the raster surface. |
|
Sample heights for given coordinates using bilinear interpolation. |
Methods Raster specific
This functions can be used to interact with the raster and coordinates
|
Convert pixel indices to coordinates in raster CRS. |
|
Return whether pixel coordinates are within raster bounds. |
|
Test if given coordinates in raster CRS are on the raster |
|
Get pixel position of coordinates |
|
Get raster value of coordinates in Raster CRS coordinates |
|
Calculate the intersection of a single ray with the raster surface. |
|
Load raster data into |
Properties
Return the mapper type. |
|
Return the active mapping backend. |
|
Return the in-memory |
|
Return the mapper configuration dictionary. |
|
|
Return the CRS of the mapper (if set). |
|
Return the CRS as WKT string, or |
Return the raster resolution. |
|
Return the affine geo-transform of the raster. |
|
Return raster width in pixels. |
|
Return raster height in pixels. |
- class weitsicht.MappingRaster(raster_path: Path | str, crs: CRS | None = None, force_no_crs: bool = False, preload_full_raster: bool = False, preload_window: None | tuple[float, float, float, float] = None, index_band: int | None = None)[source]#
Bases:
MappingBaseMapping backend using a geo-referenced raster for height sampling and ray intersections.
- Parameters:
raster_path (Path | str)
crs (CRS | None)
force_no_crs (bool)
preload_full_raster (bool)
preload_window (None | tuple[float, float, float, float])
index_band (int | None)
- __init__(raster_path: Path | str, crs: CRS | None = None, force_no_crs: bool = False, preload_full_raster: bool = False, preload_window: None | tuple[float, float, float, float] = None, index_band: int | None = None)[source]#
Create a raster-based mapper.
Provide a valid path for a raster which can be loaded via
rasterio.- Parameters:
raster_path (Path | str) – Path to a raster readable by
rasterio.crs (CRS | None) – CRS overriding the raster’s internal CRS, defaults to
None.force_no_crs (bool) – If
Truethe CRS will be set toNoneregardless of raster metadata, defaults toFalse.preload_full_raster (bool) – If
Truepreload the full raster into memory, defaults toFalse.preload_window (tuple[float, float, float, float] | None) – Optional window to preload as
(xmin, ymin, xmax, ymax)in mapper CRS, defaults toNone.index_band (int | None) – Band index to load (1-based). Mandatory if raster has more than one band, defaults to
None.
- Raises:
ValueError – If input arguments are inconsistent or invalid.
FileNotFoundError – If
raster_pathdoes not exist.CRSInputError – If CRS input is missing or invalid.
CRSnoZaxisError – If the mapper CRS does not define a Z axis.
MappingError – If the raster cannot be opened or is not geo-referenced.
- property type#
Return the mapper type.
- Returns:
Mapper type.
- Return type:
MappingType
- property backend: MappingBase#
Return the active mapping backend.
If a raster window (or the full raster) was preloaded via
load_window()/preload_window/preload_full_raster, this returns the in-memoryMappingGeorefArraybackend. Otherwise, it returnsself.
- property georef_mapper: MappingGeorefArray#
Return the in-memory
MappingGeorefArraybackend.- Raises:
MappingError – If no in-memory window/full raster was loaded.
- classmethod from_dict(mapper_dict: Mapping[str, Any]) MappingRaster[source]#
Create a
MappingRasterinstance from a configuration dictionary.Required keys: -
raster_filepath: file path to the raster -crs: CRS WKT string (orNoneto force no CRS)Optional keys: -
band: band index (1-based), defaults toNone-preload_window: preload window in mapper CRS as(xmin, ymin, xmax, ymax), defaults toNone-preload_full: preload full raster, defaults toFalse- Parameters:
mapper_dict (Mapping[str, Any]) – Mapper configuration dictionary (typically created via
mapper.param_dict).- Returns:
Instantiated mapper.
- Return type:
- Raises:
KeyError – If a required dictionary key is missing.
FileNotFoundError – If the raster file does not exist.
ValueError – If configuration values are invalid.
CRSInputError – If the CRS WKT string is invalid.
CRSnoZaxisError – If the mapper CRS does not define a Z axis.
MappingError – If the raster cannot be opened or is not geo-referenced.
- property param_dict#
Return the mapper configuration dictionary.
- Returns:
Mapper configuration dictionary.
- Return type:
dict[str, Any]
- property resolution#
Return the raster resolution.
- Returns:
Mean raster resolution in CRS units.
- Return type:
float
- property transform: Affine#
Return the affine geo-transform of the raster.
- Returns:
Affine geo-transform.
- Return type:
Affine
- property width#
Return raster width in pixels.
- property height#
Return raster height in pixels.
- load_window(limits_crs: tuple[float, float, float, float] | None = None) MappingGeorefArray[source]#
Load raster data into
georef_array.- Parameters:
limits_crs (tuple[float, float, float, float] | None) – Optional window limits as
(xmin, ymin, xmax, ymax)in mapper CRS, defaults toNone(loads full raster).- Raises:
MappingError – If the provided window is not fully inside the raster extent.
- Return type:
- pixel_to_coordinate(px_row: float, px_col: float) tuple[source]#
Convert pixel indices to coordinates in raster CRS.
- Parameters:
px_row (float) – Pixel row coordinate
px_col (float) – Pixel column coordinate
- Returns:
(x, y)coordinate in raster CRS.- Return type:
tuple[float, float]
- pixel_valid(px_row: float, px_col: float) bool[source]#
Return whether pixel coordinates are within raster bounds.
- Parameters:
px_row (float) – Pixel row coordinate
px_col (float) – Pixel column coordinate
- Returns:
Trueif valid.- Return type:
bool
- pixel_valid_mat(px_row: ArrayN_, px_col: ArrayN_) MaskN_[source]#
Vectorized validity check for pixel coordinates.
- Parameters:
px_row (
ArrayN_) – Array with Pixel row coordinatepx_col (
ArrayN_) – Array with Pixel column coordinate
- Returns:
Boolean mask for valid pixels.
- Return type:
MaskN_
- coordinate_on_raster(x_crs: float, y_crs: float) bool[source]#
Test if given coordinates in raster CRS are on the raster
- Parameters:
x_crs (float) – X coordinate
y_crs (float) – Y coordinate
- Returns:
Trueif on the raster.- Return type:
bool
- coordinate_to_pixel(x_crs: float, y_crs: float) tuple[float, float][source]#
Get pixel position of coordinates
- Parameters:
x_crs (float) – X coordinate
y_crs (float) – Y coordinate
- Returns:
(row, col)pixel coordinates as floats.- Return type:
tuple[float, float]
- coordinate_to_pixel_mat(x: ArrayN_, y: ArrayN_) tuple[ArrayN_, ArrayN_][source]#
Get pixel position of coordinates.
This is pure affine transformation, it will not check if the pixel are valid
- Parameters:
x (
ArrayN_) – Array of X coordinatey (
ArrayN_) – Array of Y coordinate
- Returns:
(row, col)pixel coordinates.- Return type:
tuple[
ArrayN_,ArrayN_]
- get_coordinate_height(x_crs: float, y_crs: float) float | None[source]#
Get raster value of coordinates in Raster CRS coordinates
- Parameters:
x_crs (float) – X coordinate
y_crs (float) – Y coordinate
- Returns:
Raster value or
0.0if no-data / invalid.- Return type:
float
- intersection_ray(ray_vector_crs_s: Vector3D, ray_start_crs_s: Vector3D, crs_s: CRS | None = None, transformer: CoordinateTransformer | None = None, max_iter: int = 20) tuple[Vector3D, bool, bool, bool, Vector3D][source]#
Calculate the intersection of a single ray with the raster surface.
The algorithm iteratively adjusts the intersection plane using heights sampled from the raster.
- Parameters:
ray_vector_crs_s (Vector3D) – Ray direction vector (3,).
ray_start_crs_s (Vector3D) – Ray start point (3,).
crs_s (CRS | None) – CRS of the ray input, defaults to
None.transformer (CoordinateTransformer | None) – Coordinate transformer to mapper CRS, defaults to
None.max_iter (int) – Maximum number of iterations, defaults to
20.
- Returns:
Tuple
(intersection_coordinates, is_invalid, is_outside_raster, Nodata height, normal in crs_s).- Return type:
tuple[Vector3D, bool, bool, Vector3D]
- Raises:
CRSInputError – If both
crs_sandtransformerare provided.CoordinateTransformationError – If coordinate transformation fails.
- map_coordinates_from_rays(ray_vectors_crs_s: ArrayNx3, ray_start_crs_s: ArrayNx3, crs_s: CRS | None = None, transformer: CoordinateTransformer | None = None) MappingResultSuccess | ResultFailure[Issue][source]#
Map coordinates by intersecting rays with the raster surface.
- Parameters:
ray_vectors_crs_s (ArrayNx3) – Ray direction vectors (N×3).
ray_start_crs_s (ArrayNx3) – Ray start points (N×3), same shape as
ray_vectors_crs_s.crs_s (CRS | None) – CRS of the input rays, defaults to
None.transformer (CoordinateTransformer | None) – Coordinate transformer to mapper CRS, defaults to
None.
- Returns:
Mapping result containing intersection coordinates and a validity mask.
- Return type:
MappingResult
- Raises:
ValueError – If input arrays have incompatible shapes.
CRSInputError – If both
crs_sandtransformerare provided.CoordinateTransformationError – If coordinate transformation fails.
- map_heights_from_coordinates_sampling(coordinates_crs_s: ArrayNx3 | ArrayNx2, crs_s: CRS | None = None, transformer: CoordinateTransformer | None = None) MappingResultSuccess | ResultFailure[Issue][source]#
Sample heights using rasterio’s point sampling (no interpolation).
- Parameters:
coordinates_crs_s (ArrayNx3 | ArrayNx2) – Coordinates to sample (N×2 or N×3).
crs_s (CRS | None) – CRS of the input coordinates, defaults to
None.transformer (CoordinateTransformer | None) – Coordinate transformer to mapper CRS, defaults to
None.
- Returns:
Mapping result containing sampled coordinates and a validity mask.
- Return type:
MappingResult
- Raises:
CRSInputError – If both
crs_sandtransformerare provided.CoordinateTransformationError – If coordinate transformation fails.
- map_heights_from_coordinates(coordinates_crs_s: ArrayNx3 | ArrayNx2, crs_s: CRS | None = None, transformer: CoordinateTransformer | None = None) MappingResultSuccess | ResultFailure[Issue][source]#
Sample heights for given coordinates using bilinear interpolation.
- Parameters:
coordinates_crs_s (ArrayNx3 | ArrayNx2) – Coordinates to sample (N×2 or N×3).
crs_s (CRS | None) – CRS of the input coordinates, defaults to
None.transformer (CoordinateTransformer | None) – Coordinate transformer to mapper CRS, defaults to
None.
- Returns:
Mapping result containing sampled coordinates and a validity mask.
- Return type:
MappingResult
- Raises:
CRSInputError – If both
crs_sandtransformerare provided.CoordinateTransformationError – If coordinate transformation fails.