wolfhece.wolf_array._header_wolf
Header class for WOLF arrays.
Contains: - WOLF_ARRAY_* type constants - getkeyblock / decodekeyblock helper functions - header_wolf class (spatial header: origin, resolution, shape, translation, multi-block support)
This module is intentionally free of wx, OpenGL, and heavy GUI dependencies so that it can be imported cheaply in non-interactive contexts (CLI tools, unit tests, etc.).
Author: HECE - University of Liege, Pierre Archambeau Date: 2024
Copyright (c) 2024 University of Liege. All rights reserved.
This script and its content are protected by copyright law. Unauthorized copying or distribution of this file, via any medium, is strictly prohibited.
Module Contents
- wolfhece.wolf_array._header_wolf.getkeyblock(i, addone=True) str[source]
Name/Key of a block in the dictionnary of a WolfArrayMB instance
For Fortran compatibility, addone is True by default so first block is “block1” and not “block0”
- wolfhece.wolf_array._header_wolf.decodekeyblock(key, addone=True) int[source]
Decode key of a block in the dictionnary of a WolfArrayMB instance
For Fortran compatibility, addone is True by default so first block is “block1” and not “block0”
- class wolfhece.wolf_array._header_wolf.header_wolf[source]
Header of WolfArray
In case of a mutliblock, the header have informations about all the blocks in head_blocks dictionnary. Block keys are generated by “getkeyblock” function
- head_blocks: dict[str, header_wolf][source]
- set_origin(x: float, y: float, z: float = 0.0)[source]
Set origin
- Parameters:
x – origin along X
y – origin along Y
z – origin along Z
- set_translation(tr_x: float, tr_y: float, tr_z: float = 0.0)[source]
Set translation
- Parameters:
tr_x – translation along X
tr_y – translation along Y
tr_z – translation along Z
- get_bounds(abs=True)[source]
Return bounds in coordinates
- Parameters:
abs – if True, add translation to (x, y) (coordinate to global space)
- Returns:
tuple of two lists of two floats - ([xmin, xmax],[ymin, ymax])
- get_extent(abs=True)[source]
Return extent in coordinates :param abs: if True, add translation to (x, y) (coordinate to global space) :return: list of four floats - [xmin, xmax, ymin, ymax]
- get_bounds_ij(abs=False)[source]
Return bounds in indices
Firstly, get_bounds is called to get bounds in coordinates and then get_ij_from_xy is called to get bounds in indices.
- Parameters:
abs – if True, add translation to (x, y) (coordinate to global space)
- get_ij_from_xy(x: float, y: float, z: float = 0.0, scale: float = 1.0, aswolf: bool = False, abs: bool = True, forcedims2: bool = False) tuple[numpy.int32, numpy.int32] | tuple[numpy.int32, numpy.int32, numpy.int32][source]
Get indices from coordinates
- Parameters:
x – X coordinate
y – Y coordinate
z – Z coordinate (optional)
scale – scaling of the spatial resolution (dx,dy,[dz])
aswolf – if True, return if one-based (as Wolf VB6 or Fortran), otherwise 0-based (default Python standard)
abs – if True, remove translation from (x, y, [z]) (coordinate from global space)
forcedims2 – if True, force to return only 2 indices even if z is supplied
- get_ij_from_xy_array(xy: numpy.ndarray, scale: float = 1.0, aswolf: bool = False, abs: bool = True, forcedims2: bool = False) numpy.ndarray[source]
Get indices from coordinates
:param xy = numpy array containing (x, y, [z]) coordinates - shape (n, 2) or (n, 3) :param scale = scaling of the spatial resolution (dx,dy,[dz]) :param aswolf = if True, return if one-based (as Wolf VB6 or Fortran), otherwise 0-based (default Python standard) :param abs = if True, remove translation from (x, y, [z]) (coordinate from global space) :param forcedims2 = if True, force to return only 2 indices even if z is supplied
- Returns:
numpy array containing (i, j, [k]) indices - shape (n, 2) or (n, 3)
- get_xy_from_ij(i: int, j: int, k: int = 0, scale: float = 1.0, aswolf: bool = False, abs: bool = True) tuple[numpy.float64, numpy.float64] | tuple[numpy.float64, numpy.float64, numpy.float64][source]
Get coordinates from indices
:param i = index along X coordinate :param j = index along Y coordinate :param k = index along Z coordinate (optional) :param scale = scaling of the spatial resolution (dx,dy,[dz]) :param aswolf = if True, input is one-based (as Wolf VB6 or Fortran), otherwise 0-based (default Python standard) :param abs = if True, add translation to results (x, y, [z]) (coordinate to global space)
- transform()[source]
Return the affine transformation.
Similar to rasterio.transform.Affine
- In WOLF, the convention is :
origin is at the lower-left corner
the origin is at the corner of the cell dx, dy, so the center of the cell is at dx/2, dy/2
X axis is along the rows - i index
Y axis is along the columns - j index
- So, the affine transformation is :
(dx, 0, origx + translx + dx /2, 0, dy, origy + transly + dy/2)
- _transform_gmrio()[source]
Return the affine transformation.
!! Inverted ij/ji convention !!
Similar to rasterio.transform.Affine
- In WOLF, the convention is :
origin is at the lower-left corner
the origin is at the corner of the cell dx, dy, so the center of the cell is at dx/2, dy/2
X axis is along the rows - i index
Y axis is along the columns - j index
- So, the affine transformation is :
(dx, 0, origx + translx + dx /2, 0, dy, origy + transly + dy/2)
- get_xy_from_ij_array(ij: numpy.ndarray, scale: float = 1.0, aswolf: bool = False, abs: bool = True) numpy.ndarray[source]
Converts array coordinates (numpy cells) to this array’s world coodinates.
:param ij = numpy array containing (i, j, [k]) indices - shape (n, 2) or (n, 3) :param scale = scaling of the spatial resolution (dx,dy,[dz]) :param aswolf = if True, input is one-based (as Wolf VB6 or Fortran), otherwise 0-based (default Python standard) :param abs = if True, add translation to results (x, y, [z]) (coordinate to global space)
..warning: ‘ij’ is not the result of np.where() but if you want to use np.where() you can use the following code:
` np.vstack((ij[0], ij[1])).T `
- ij2xy(i: int, j: int, k: int = 0, scale: float = 1.0, aswolf: bool = False, abs: bool = True) tuple[numpy.float64, numpy.float64] | tuple[numpy.float64, numpy.float64, numpy.float64][source]
alias for get_xy_from_ij
- ij2xy_np(ij: numpy.ndarray, scale: float = 1.0, aswolf: bool = False, abs: bool = True) numpy.ndarray[source]
alias for get_xy_from_ij_array
- Parameters:
ij – numpy array containing (i, j, [k]) indices - like np.argwhere() - shape (n, 2) or (n, 3)
scale – scaling of the spatial resolution (dx,dy,[dz])
aswolf – if True, input is one-based (as Wolf VB6 or Fortran), otherwise 0-based (default Python standard)
abs – if True, add translation to results (x, y, [z]) (coordinate to global space)
..warning: ‘ij’ is not the result of np.where() but if you want to use np.where() you can use the following code:
` np.vstack((ij[0], ij[1])).T `- Returns:
numpy array containing (x, y, [z]) coordinates - shape (n, 2) or (n, 3)
- ij2xy_np_ij_from_npwhere(ij: numpy.ndarray, scale: float = 1.0, aswolf: bool = False, abs: bool = True) numpy.ndarray[source]
alias for get_xy_from_ij_array but with ij as np.where result
- xy2ij(x: float, y: float, z: float = 0.0, scale: float = 1.0, aswolf: bool = False, abs: bool = True, forcedims2: bool = False) tuple[numpy.int32, numpy.int32] | tuple[numpy.int32, numpy.int32, numpy.int32][source]
alias for get_ij_from_xy
- xy2ij_np(xy: numpy.ndarray, scale: float = 1.0, aswolf: bool = False, abs: bool = True) numpy.ndarray[source]
alias for get_ij_from_xy_array
- Parameters:
xy – numpy array containing (x, y, [z]) coordinates - shape (n, 2) or (n, 3)
scale – scaling of the spatial resolution (dx,dy,[dz])
aswolf – if True, return if one-based (as Wolf VB6 or Fortran), otherwise 0-based (default Python standard)
abs – if True, remove translation from (x, y, [z]) (coordinate from global space)
forcedims2 – if True, force to return only 2 indices even if z is supplied
:return : numpy array containing (i, j, [k]) indices - shape (n, 2) or (n, 3)
- xyz2ijk_np(xyz: numpy.ndarray, scale: float = 1.0, aswolf: bool = False, abs: bool = True) numpy.ndarray[source]
alias for get_xy_from_ij_array
- ijk2xyz_np(ijk: numpy.ndarray, scale: float = 1.0, aswolf: bool = False, abs: bool = True) numpy.ndarray[source]
alias for get_xy_from_ij_array
- find_intersection(other: header_wolf, ij: bool = False) tuple[list[float], list[float]] | tuple[list[list[float]], list[list[float]]][source]
Find the intersection of two header
- Parameters:
other – other header
ij – if True, return indices instead of coordinates
- Returns:
None or tuple of two lists of two floats - ([xmin, xmax],[ymin, ymax]) or indices in each header (if ij=True) [[imin1, imax1], [jmin1, jmax1]], [[imin2, imax2], [jmin2, jmax2]]
- find_union(other: header_wolf | list[header_wolf]) tuple[list[float], list[float]][source]
Find the union of two header
- Returns:
tuple of two lists of two floats - ([xmin, xmax],[ymin, ymax])
- read_txt_header(filename: str)[source]
Read informations from header .txt
- Parameters:
filename – path and filename of the basefile
If filename is a Path object, it is converted to a string If filename ends with ‘.tif’, nothing is done because infos are in the .tif file If filename ends with ‘.flt’, a .hdr file must be present and it will be read Otherwise, a filename.txt file must be present
- classmethod read_header(filename: str) header_wolf[source]
alias for read_txt_header
- Parameters:
filename – path and filename of the basefile
- write_header(filename: str, wolftype: int, forceupdate: bool = False)[source]
alias for write_txt_header
- write_txt_header(filename: str, wolftype: int, forceupdate: bool = False)[source]
Writing the header to a text file
Nullvalue is not written
- Parameters:
filename – path and filename with ‘.txt’ extension, which will NOT be automatically added
wolftype – type of the WOLF_ARRAY_* array
forceupdate – if True, the file is rewritten even if it already exists
- is_like(other: header_wolf, check_mb: bool = False) bool[source]
Comparison of two headers
- Parameters:
other – other header to compare
check_mb – if True, the comparison is done on the blocks too
The nullvalue is not taken into account
- align2grid(x1: float, y1: float, eps: float = 0.0001) tuple[float, float][source]
Align coordinates to nearest grid point where the grid is defined by the borders of the array.
- _rasterize_segment(x1: float, y1: float, x2: float, y2: float, xstart: float = None, ystart: float = None, n_packet: int = 10) list[list[float]][source]
Rasterize a segment according to the grid where the grid is defined by the borders of the array.
- Parameters:
x1 – x coordinate of the first point
y1 – y coordinate of the first point
x2 – x coordinate of the second point
y2 – y coordinate of the second point
xstart – x coordinate of the starting point
ystart – y coordinate of the starting point
- Returns:
numpy array of the rasterized segment
- rasterize_vector_along_grid(vector2raster: wolfhece.PyVertexvectors.vector, outformat=None) numpy.ndarray | wolfhece.PyVertexvectors.vector[source]
Rasterize a vector according to the grid
- Parameters:
vector2raster – vector to rasterize
outformat – output format (np.ndarray or vector)
- rasterize_zone_along_grid(zone2raster: wolfhece.PyVertexvectors.zone, outformat=None) numpy.ndarray | wolfhece.PyVertexvectors.zone[source]
Rasterize a zone according to the grid
- Parameters:
zone2raster – zone to rasterize
outformat – output format (np.ndarray or zone)
- rasterize_vector(vector2raster: wolfhece.PyVertexvectors.vector, outformat=None) numpy.ndarray | wolfhece.PyVertexvectors.vector[source]
DEPRECATED since 2.2.8 – use rasterize_vector_along_grid instead.
Will be removed in 2.3.0
- get_xy_infootprint_vect(myvect: vector | Polygon, eps: float = 0.0) tuple[numpy.ndarray, numpy.ndarray][source]
Return the coordinates and the indices of the cells in the footprint of a vector.
Coordinates are stored in a numpy array of shape (n,2) where n is the number of cells in the footprint. Indices are stored in a numpy array of shape (n,2) where n is the number of cells in the footprint. -> See get_ij_infootprint
- Parameters:
myvect – target vector or Shapely Polygon
eps – epsilon to avoid rounding errors
- Returns:
tuple of two numpy arrays - (coordinates, indices)
- get_xy_infootprint(myvect: vector | Polygon, eps: float = 0.0) tuple[numpy.ndarray, numpy.ndarray][source]
Return the coordinates and the indices of the cells in the footprint of a vector.
Coordinates are stored in a numpy array of shape (n,2) where n is the number of cells in the footprint. Indices are stored in a numpy array of shape (n,2) where n is the number of cells in the footprint. -> See get_ij_infootprint
Main principle: - get the indices with ‘get_ij_infootprint’ - then convert them to coordinates.
- Parameters:
myvect – target vector or Shapely Polygon
eps – epsilon to avoid rounding errors
- Returns:
tuple of two numpy arrays - (coordinates, indices)
- get_ij_infootprint_vect(myvect: vector | Polygon, eps: float = 0.0) numpy.ndarray[source]
Return the indices of the cells in the footprint of a vector.
Main principle: - get the bounding box of the vector - convert the bounding box to indices - limit indices to the array size - create a meshgrid of indices
Indices are stored in a numpy array of shape (n,2) where n is the number of cells in the footprint.
- Parameters:
myvect – target vector or Shapely Polygon
eps – epsilon to avoid rounding errors
- Returns:
numpy array of indices
- get_ij_infootprint(myvect: vector | Polygon, eps: float = 0.0) numpy.ndarray[source]
Return the indices of the cells in the footprint of a vector
Main principle: - get the bounding box of the vector - convert the bounding box to indices - limit indices to the array size - create a meshgrid of indices
Indices are stored in a numpy array of shape (n,2) where n is the number of cells in the footprint.
- Parameters:
myvect – target vector or Shapely Polygon
eps – epsilon to avoid rounding errors
- Returns:
numpy array of indices
- convert_xy2ij_np(xy)[source]
Convert XY coordinates to IJ indices (0-based) with Numpy without any check/options
- Parameters:
xy – = numpy array of shape (n,2) with XY coordinates
- convert_ij2xy_np(ij)[source]
Convert IJ indices (0-based) to XY coordinates with Numpy without any check/options
- Parameters:
ij – = numpy array of shape (n,2) with IJ indices
- is_in_footprint(x: float, y: float, eps: float = 0.0) bool[source]
Check if a point is in the footprint of the array :param x: x coordinate :param y: y coordinate :param eps: epsilon to avoid rounding errors :return: True if the point is in the footprint, False otherwise
- classmethod make(orig: tuple[float, float] = (0.0, 0.0), nb: tuple[int, int] = (0, 0), d: tuple[float, float] = (1.0, 1.0))[source]
- classmethod make_from_xybounds_grid(xbounds: tuple[float, float], ybounds: tuple[float, float], d: tuple[float, float] = (1.0, 1.0), grid_to_align: header_wolf = None)[source]
Create a header_wolf from the bounds in x and y and the resolution in x and y. If grid_size is provided, the origin will be adjusted to fit the grid size.
- Parameters:
xbounds – tuple of two floats - (xmin, xmax)
ybounds – tuple of two floats - (ymin, ymax)
d – tuple of two floats - (dx, dy)
grid_to_align – header_wolf - grid to adjust the origin
- Returns:
header_wolf instance
- classmethod make_from_header(other: header_wolf)[source]
- copy() header_wolf[source]
Return a copy of the header_wolf instance
- classmethod from_slices(x_slice: slice, y_slice: slice, d: tuple[float, float] = (1.0, 1.0))[source]
- classmethod from_dict(d: dict, dxdy: tuple[float, float] = None, grid: header_wolf = None) header_wolf[source]