ice_shelf_state_and_fate.raster_tools.raster_np

A geospatially aware extension of the numpy array class.

Classes

RasterNp(input_array, bounding_box, resolution)

A geospatial aware numpy array.

class RasterNp(input_array: ndarray[tuple[Any, ...], dtype[float64]], bounding_box: BoundingBox, resolution: float, y_affine_negative: bool = True, proj4_code: str | None = None)

Bases: ndarray

A geospatial aware numpy array.

Usage is: arr_instance = RasterNp(np_array, bounding_box, resolution, [y_affine_negative=True], [proj4_code=’’])

at_yx(y: ndarray[tuple[Any, ...], dtype[float64]] | list, x: ndarray[tuple[Any, ...], dtype[float64]] | list) → ndarray[tuple[Any, ...], dtype[_ScalarT]]

Return values from raster for given y and x coordinate lists.

Note, x and y expected to be the same length and no interpolation is applied.

Parameters:
  • y (NDArray[np.float64] | list) – List of y coordinates to extract values for.

  • x (NDArray[np.float64] | list) – List of x coordinates to extract values for.

Returns:

2D Array of values for the specified coordinates.

Return type:

NDArray

bounding_box = None
static bounding_box_intersects_tiff(file_path: Path, bounding_box: BoundingBox) → bool

Check if any data exists in the tiff for the given extent.

Parameters:
  • file_path (Path) – Tiff file path.

  • bounding_box (BoundingBox) – Extent to check tiff data exists for.

Returns:

True if extent overlaps tiff.

Return type:

bool

convert_to_dataframe(value_name: str = 'value') → DataFrame

Convert object to a dataframe with x coordinates, y coordinates, and values.

Parameters:

value_name (str) – The new column name for the data in the DataFrame, by default ‘value’

Returns:

New DataFrame instance with ‘x’, ‘y’, value_name columns.

Return type:

pd.DataFrame

get_centre_points() → tuple[ndarray[tuple[Any, ...], dtype[float64]], ndarray[tuple[Any, ...], dtype[float64]], ndarray[tuple[Any, ...], dtype[float64]]]

Get x coords, y coords, and values in an x-oriented array for mesh grid consistency.

This was originally for backwards compatibility for use in the EOLIS processing.

Returns:

x centre point coordinates. y centre point coordinates. Array values in x-oriented order.

Return type:

tuple[NDArray[np.float64], NDArray[np.float64], NDArray[np.float64]]

static load_from_tiff(file_path: Path, band: int = 1, bounding_box: BoundingBox | None = None) → RasterNp

Build a RasterNp object from a tiff file.

This may work with other geo-image types, too, but is untested.

Parameters:
  • file_path (Path) – The file path to attempt to load.

  • band (int) – Which raster band to load, by default 1 (the first one)

  • bounding_box (BoundingBox | None) – If a bounding box is passed, only a sub-area of the tiff is loaded. If the bounding box bounds are greater than the image bounds, they are trimmed down to the image bounds. All partial pixels touched by the bounding box are returned. If None, the entire tiff is loaded. By default None.

Returns:

New RasterNp instance.

Return type:

RasterNp

Raises:

ValueError – If requested extent does not overlap the file extent.

mask_from_file(file_path: Path, include_mask_value: float = 1, outside_mask_return_value: float = nan) → None

Mask object in place from another geotiff file.

Parameters:
  • file_path (Path) – File path of mask file.

  • include_mask_value (float) – The in-mask value in the mask geotiff, by default 1.

  • outside_mask_return_value (float) – The value assinged to data found outside of the mask, by default np.nan. note - the default outside_mask_return_value value of np.nan does not work with int-based arrays.

mask_from_raster_np(mask: RasterNp, include_mask_value: float = 1, outside_mask_return_value: float = nan) → None

Mask object in place from a np.ndarray mask.

Parameters:
  • mask (RasterNp) – The mask represented as a RasterNp.

  • include_mask_value (float) – The in-mask value in the mask geotiff, by default 1.

  • outside_mask_return_value (float) – The value assigned to data found outside of the mask, by default np.nan. note - the default outside_mask_return_value value of np.nan does not work with int-based arrays.

new_extent_copy(bounding_box: BoundingBox, fill_to_extent: bool = False, fill_value: float = nan) → RasterNp

Return a new RasterNp object, resized to the new extent.

Parameters:
  • bounding_box (BoundingBox) – Target extent for new array.

  • fill_to_extent (bool) – If True and extent is greater than source image, fill with fill_value, by default False

  • fill_value (float) – Default value for pixels outside the extent of the existing array (e.g. if resizing to a larger extent), by default np.nan

Returns:

New resized RasterNp object

Return type:

RasterNp

proj4_code = None
static rasterise_mask_from_geometry(geometries: list | MultiPolygon | Polygon, y_min: int | float, y_max: int | float, x_min: int | float, x_max: int | float, resolution: int | float, y_affine_negative: bool = False) → RasterNp

Rasterise a mask based on a geometry.

Parameters:
  • geometries (list | MultiPolygon | Polygon) – A list of multipolygons

  • y_min (int | float) – The extent y_min.

  • y_max (int | float) – The extent y_max.

  • x_min (int | float) – The extent x_min.

  • x_max (int | float) – The extent x_max.

  • resolution (int | float) – The resolution.

  • y_affine_negative (bool) – Whether the raster is y affine negative, by default False

Returns:

The raster mask.

Return type:

RasterNp

resolution = None
rows_cols_from_yx(y: float | ndarray[tuple[Any, ...], dtype[_ScalarT]] | tuple | list, x: float | ndarray[tuple[Any, ...], dtype[_ScalarT]] | tuple | list) → tuple[int | int64 | ndarray[tuple[Any, ...], dtype[_ScalarT]], int | int64 | ndarray[tuple[Any, ...], dtype[_ScalarT]]]

Return row, col array indices for for real y, x coordinates values.

return shape is the same as the input shape (can be single values or nd-arrays)

The rasterio function for this is too slow unfortunately, otherwise the following is a neater solution. e.g y_idx, x_idx = tr.rowcol(self.transform, x, y)

Parameters:
  • y (float | NDArray | tuple | list) – y coordinates to retrieve index/indices for.

  • x (float | NDArray | tuple | list) – x coordinates to retrieve index/indices for.

Returns:

y index or indices. x index or indices.

Return type:

tuple[int | np.int64 | NDArray, int | np.int64 | NDArray]

save_as_tiff(file_path: Path, data_type: str = 'float32', no_data: float | None = None, **kwargs: dict[str, Any] | str | None) → None

Save object as a geotiff.

Parameters:
  • file_path (Path) – Output file path.

  • data_type (str) – Override the output number type, by default ‘float32’

  • no_data (float | None) – Override the no-data value, by default None

  • **kwargs (dict[str, Any] | str | None) – Keyword arguments for rasterio.open. These are passed to format drivers as directives for creating or interpreting datasets.

transform = None
y_affine_negative = None
yx_from_rows_cols(y_idx: int | ndarray[tuple[Any, ...], dtype[_ScalarT]], x_idx: int | ndarray[tuple[Any, ...], dtype[_ScalarT]]) → tuple[float | ndarray[tuple[Any, ...], dtype[_ScalarT]], float | ndarray[tuple[Any, ...], dtype[_ScalarT]]]

Get y, x coordinates (centre points) for corresponding index values in the array data.

return shape is the same as the input shape (can be single values or nd-arrays)

Parameters:
  • y_idx (int | NDArray) – y index or indices to retrieve coordinates for.

  • x_idx (int | NDArray) – x index or indices to retrieve coordinates for.

Returns:

y coordinate value(s) x coordinate value(s)

Return type:

tuple[float | NDArray, float | NDArray]

yx_gridpoints() → tuple[ndarray[tuple[Any, ...], dtype[float64]], ndarray[tuple[Any, ...], dtype[float64]]]

Return arrays of y and x coordinates for the entire raster.

Returns:

List of centre point coordinates for y axis List of centre point coordinates for x axis

Return type:

tuple[NDArray[np.float64], NDArray[np.float64]]