geospade package
Submodules
geospade.crs module
Coordinate Reference System (CRS) module.
- class geospade.crs.SpatialRef(arg, sref_type=None)[source]
Bases:
object
This class represents any OGC compliant spatial reference system. Internally, the GDAL OSR SpatialReference class is used, which offers access to and control over different representations, such as EPSG, PROJ4 or WKT. Additionally, it can also create an instance of a Cartopy Projection, which can be used to plot geometries or data.
- static epsg_to_osr(epsg_code) SpatialReference [source]
Converts an EPSG code to an OSR spatial reference object.
- classmethod from_osr(osr_sref) SpatialRef [source]
Creates a SpatialRef object from an OSR spatial reference object. To allow this transformation, PROJ4 is used.
- Parameters
osr_sref (osr.SpatialReference) – OSR spatial reference.
- Returns
Spatial reference defined by the exported PROJ4 string from an OSR spatial reference object.
- Return type
- static osr_to_epsg(osr_sref) int [source]
Converts an osr.SpatialReference instance to an EPSG code.
- Parameters
osr_sref (osr.SpatialReference) – OSR spatial reference.
- Returns
EPSG code.
- Return type
- static osr_to_proj4(osr_sref) str [source]
Converts an osr.SpatialReference instance to a PROJ4 string.
- Parameters
osr_sref (osr.SpatialReference) – OSR spatial reference.
- Returns
PROJ4 string.
- Return type
- static osr_to_wkt(osr_sref) str [source]
Converts an osr.SpatialReference instance to a Well Known Text (WKT) string.
- Parameters
osr_sref (osr.SpatialReference) – OSR spatial reference.
- Returns
WKT string.
- Return type
- static proj4_to_osr(proj4_params) SpatialReference [source]
Converts PROJ4 parameters to an OSR spatial reference object.
- to_cartopy_proj() Projection [source]
Creates a cartopy.crs.Projection instance from PROJ4 parameters.
- Returns
Cartopy projection representing the projection of the spatial reference system.
- Return type
cartopy.crs.projection
- to_pretty_wkt() str [source]
Well Known Text (WKT) representation of the spatial reference formatted with tabs and line breaks.
- to_proj4_dict() dict [source]
Converts internal PROJ4 parameter string to a dictionary, where the keys do not contain a plus and the values are converted to non-string values if possible.
- property wkt: str
Well Known Text (WKT) representation of the spatial reference without tabs or line breaks.
- static wkt_to_osr(wkt_string) SpatialReference [source]
Converts a Well Known Text (WKT) string to an OSR spatial reference object.
- Parameters
wkt_string (str) – WKT string, e.g., ‘GEOGCS[“WGS 84”,DATUM[“WGS_1984”,SPHEROID[“WGS 84”,6378137,298.257223563, AUTHORITY[“EPSG”,”7030”]], AUTHORITY[“EPSG”,”6326”]],PRIMEM[“Greenwich”,0,AUTHORITY[“EPSG”,”8901”]], UNIT[“degree”,0.01745329251994328,AUTHORITY[“EPSG”,”9122”]],AUTHORITY[“EPSG”,”4326”]]’.
- Returns
OSR spatial reference.
- Return type
osr.SpatialReference
geospade.errors module
Module collecting some often used error messages.
geospade.raster module
Module containing class definitions for rasters.
- class geospade.raster.MosaicGeometry(tiles, boundary=None, adjacency_matrix=None, name='', description='', tile_class=<class 'geospade.raster.Tile'>, check_consistency=True, parent=None, **kwargs)[source]
Bases:
object
Represents an irregular mosaic of tiles. Tiles are not allowed to intersect with each other.
- property coord_extent: Tuple[float, float, float, float]
Coordinate extent of the mosaic geometry (min_x, min_y, max_x, max_y).
- classmethod from_definition(definition, check_consistency=True) MosaicGeometry [source]
Creates a mosaic geometry from a human-readable mosaic definition, which is a dictionary containing the following elements:
‘type’
‘boundary’
‘name’
‘description’
‘adjacency_matrix’
‘tile_class’
‘tiles’
The expected values can be taken from the MosaicGeometry, Tile, and RasterGeometry constructor docs.
- Parameters
- Return type
- classmethod from_json(filepath, check_consistency=True) MosaicGeometry [source]
Creates a mosaic geometry from disk.
- Parameters
- Return type
- classmethod from_tile_list(tiles, boundary=None, adjacency_matrix=None, name='', description='', check_consistency=True, **kwargs) MosaicGeometry [source]
Helper method to initiate a mosaic from a list of tiles.
- Parameters
tiles (list of geospade.raster.Tile) – List of tiles.
boundary (ogr.Geometry, optional) – Strictly defined boundary of the mosaic, i.e. it defines where coordinates are valid/belong to the grid and where not. If it is None, the cascaded union of all tiles defines the boundary.
adjacency_matrix (np.array, optional) – Adjacency matrix given as a boolean array defining the direct neighbourhood relationships of the given tiles. It needs to have the same size as the given number of tiles. If it is None, an adjacency matrix is created on-the-fly (default).
name (str, optional) – Name of the mosaic.
description (string, optional) – Verbal description of the mosaic (defaults to “”).
check_consistency (bool, optional) – If True, the tiles are checked for consistency, i.e. to be non-overlapping (defaults to True).
- Return type
- get_neighbouring_tiles(tile_name, active_only=True, apply_mask=True) list [source]
Returns all tiles being in the direct neighbourhood of the tile with the given tile name.
- Parameters
- Returns
List of tiles being in the neighbourhood of tile_name.
- Return type
- name2tile(tile_name, active_only=True, apply_mask=True) Tile [source]
Converts a tile name to a tile.
- Parameters
- Returns
Tile referring to tile_name.
- Return type
- property outer_extent: Tuple[float, float, float, float]
Outer extent of the mosaic geometry, i.e. convex hull covering every pixel (min_x, min_y, max_x, max_y).
- property parent_root: MosaicGeometry
Finds and returns the root/original parent MosaicGeometry.
- plot(ax=None, facecolor='tab:red', edgecolor='black', edgewidth=1, alpha=1.0, proj=None, show=False, label_tiles=False, add_country_borders=True, extent=None, active_only=True, plot_boundary=True)[source]
Plots the tiles of the irregular mosaic geometry on a map.
- Parameters
ax (matplotlib.pyplot.axes) – Pre-defined Matplotlib axis.
facecolor (str, optional) – Color code as described at https://matplotlib.org/3.1.0/tutorials/colors/colors.html (default is ‘tab:red’).
edgecolor (str, optional) – Color code as described at https://matplotlib.org/3.1.0/tutorials/colors/colors.html (default is ‘black’).
edgewidth (float, optional) – Width the of edge line (defaults to 1).
alpha (float, optional) – Opacity of the boundary polygon (default is 1.).
proj (cartopy.crs, optional) – Cartopy projection instance defining the projection of the axes (default is None). If None, the projection of the spatial reference system of the mosaic is taken.
show (bool, optional) – If True, the plot result is shown (default is False).
label_tiles (bool, optional) – If True, the tile names are plotted at the center of each tile (default is False).
add_country_borders (bool, optional) – If True, country borders are added to the plot (cartopy.feature.BORDERS) (default is False).
extent (tuple or list, optional) – Coordinate/Map extent of the plot, given as [min_x, min_y, max_x, max_y] (default is None, meaning global extent).
active_only (bool, optional) – If true, only active tiles are plotted (default).
plot_boundary (bool, optional) – If true, the mosaic boundary is plotted (default).
- Returns
Matplotlib axis containing a Cartopy map with the plotted irregular mosaic tiles.
- Return type
matplotlib.pyplot.axes
- select_by_tile_metadata(metadata, inplace=False) MosaicGeometry [source]
Activates all mosaic tiles matching the given metadata dictionary.
- Parameters
- Returns
Mosaic with tiles being active according to the given tile metadata.
- Return type
- select_by_tile_names(tile_names, inplace=False) MosaicGeometry [source]
Activates all mosaic tiles with the given names.
- Parameters
- Returns
Mosaic with tiles being active according to the given tile names.
- Return type
- to_json(filepath)[source]
Dumps mosaic represented by its human-readable definition and adjacency matrix to disk.
- Parameters
filepath (str) – Full JSON file path.
- xy2tile(x, y, sref=None) Tile [source]
Returns the tile intersecting with the given world system coordinates. If the coordinates are outside the mosaic boundary, no tile is returned.
- Parameters
x (number) – World system coordinate in X direction.
y (number) – World system coordinate in Y direction.
sref (SpatialRef, optional) – Spatial reference system of the world system coordinates. If None, the spatial reference system of the coordinates and the spatial reference system of the mosaic are assumed to be the same (default).
- Returns
Tile intersecting/matching with the given world system coordinates.
- Return type
- class geospade.raster.RasterGeometry(n_rows, n_cols, sref, geotrans=(0, 1, 0, 0, 0, -1), name=None, description='', px_origin='ul', parent=None)[source]
Bases:
object
Represents the geometry of a georeferenced raster. It describes the extent and the grid of the raster along with its spatial reference. The (boundary) geometry can also be used as an OGR geometry, to interact with other geometries.
- property boundary_ogr: Geometry
Returns OGR geometry representation of the boundary of a RasterGeometry.
- property boundary_shapely: Polygon
Boundary of the raster geometry represented as a Shapely polygon.
- property boundary_wkt: str
Returns Well Known Text (WKT) representation of the boundary of a RasterGeometry.
- property coord_corners: Tuple[Tuple[float, float], Tuple[float, float], Tuple[float, float], Tuple[float, float]]
A tuple containing all corners (convex hull, coordinate extent) in a clock-wise order (lower left, lower right, upper right, upper left).
- property coord_extent: Tuple[float, float, float, float]
Extent of the raster geometry with the pixel origins defined during initialisation (min_x, min_y, max_x, max_y).
- classmethod from_definition(definition) RasterGeometry [source]
Creates a raster geometry from a human-readable raster geometry definition, which is a dictionary containing the following elements:
‘number_of_rows’
‘number_of_columns’
‘spatial_reference’
‘geotransformation’
‘name’
‘description’
‘pixel_origin’
The expected values can be taken from the RasterGeometry constructor docs.
- Parameters
definition (dict) – Raster geometry definition.
- Return type
- classmethod from_extent(extent, sref, x_pixel_size, y_pixel_size, **kwargs) RasterGeometry [source]
Creates a RasterGeometry object from a given extent (in units of the spatial reference) and pixel sizes in both pixel-grid directions (pixel sizes determine the resolution).
- Parameters
extent (tuple or list with 4 entries) – Coordinates defining the extent from the lower-left to the upper-right corner. This extent and the outer_boundary_extent definition are equal. (lower left x, lower left y, upper right x, upper right y)
sref (geospade.crs.SpatialRef) – Spatial reference of the geometry/extent.
x_pixel_size (float) – Absolute pixel size in X direction.
y_pixel_size (float) – Absolute pixel size in Y direction.
**kwargs – Keyword arguments for RasterGeometry constructor, i.e. name, description, or parent.
- Returns
Raster geometry object defined by the given extent and pixel sizes.
- Return type
Notes
The upper-left corner of the extent is assumed to be the (pixel) origin.
- classmethod from_json(filepath) RasterGeometry [source]
Creates a raster geometry from a human-readable raster geometry definition in a JSON file. The structure of the dictionary is described in more detail in the from_definition classmethod.
- Parameters
filepath (str) – Full JSON file path.
- Return type
- classmethod from_raster_geometries(raster_geoms, **kwargs) RasterGeometry [source]
Creates a raster geometry, which contains all the given raster geometries given by ˋraster_geomsˋ.
- Parameters
raster_geoms (list of geospade.raster.RasterGeometry) – List of RasterGeometry objects.
- Returns
Raster geometry containing all given raster geometries.
- Return type
Notes
All raster geometries must have the same spatial reference system and pixel sizes.
- property h_pixel_size: float
Pixel size in W-E direction (equal to x_pixel_size if the RasterGeometry is axis-parallel).
- property is_axis_parallel: bool
True if the RasterGeometry is not rotated , i.e. it is axis-parallel.
- is_raster_coord(x, y, sref=None) Tuple[bool, bool] [source]
Checks if a point in the world system exactly lies on the raster grid spanned by the raster geometry.
- Parameters
x (float) – World system coordinate in X direction.
y (float) – World system coordinate in Y direction.
sref (SpatialRef, optional) – Spatial reference of the coordinates. Has to be given if the spatial reference is different than the spatial reference of the raster geometry.
- Returns
bool – True if the specified X coordinate is located on the grid.
bool – True if the specified Y coordinate is located on the grid.
- property ori: float
Counter-clockwise orientation of the raster geometry in radians with respect to the W-E direction/horizontal.
- property outer_boundary_corners: Tuple[Tuple[float, float], Tuple[float, float], Tuple[float, float], Tuple[float, float]]
A tuple containing all corners (convex hull, pixel extent) in a clock-wise order (lower left, lower right, upper right, upper left).
- Type
4-list of 2-tuples
- property outer_boundary_extent: Tuple[float, float, float, float]
Outer extent of the raster geometry containing every pixel (min_x, min_y, max_x, max_y).
- property parent_root: RasterGeometry
Finds and returns the root/original parent RasterGeometry.
- plot(ax=None, facecolor='tab:red', edgecolor='black', edgewidth=1, alpha=1.0, proj=None, show=False, label_geom=False, add_country_borders=True, extent=None)[source]
Plots the boundary of the raster geometry on a map.
- Parameters
ax (matplotlib.pyplot.axes) – Pre-defined Matplotlib axis.
facecolor (str, optional) – Color code as described at https://matplotlib.org/3.1.0/tutorials/colors/colors.html (default is ‘tab:red’).
edgecolor (str, optional) – Color code as described at https://matplotlib.org/3.1.0/tutorials/colors/colors.html (default is ‘black’).
edgewidth (float, optional) – Width the of edge line (defaults to 1).
alpha (float, optional) – Opacity (default is 1.).
proj (cartopy.crs, optional) – Cartopy projection instance defining the projection of the axes (default is None). If None, the projection of the spatial reference system of the raster geometry is taken.
show (bool, optional) – If True, the plot result is shown (default is False).
label_geom (bool, optional) – If True, the geometry name is plotted at the center of the raster geometry (default is False).
add_country_borders (bool, optional) – If True, country borders are added to the plot (cartopy.feature.BORDERS) (default is False).
extent (tuple or list, optional) – Coordinate/Map extent of the plot, given as [min_x, min_y, max_x, max_y] (default is None, meaning global extent).
- Returns
Matplotlib axis containing a Cartopy map with the plotted raster geometry boundary.
- Return type
matplotlib.pyplot.axes
- rc2xy(r, c, px_origin=None) Tuple[float, float] [source]
Returns the coordinates of the center or a corner (depending on ˋpx_originˋ) of a pixel specified by a row and column number.
- Parameters
- Returns
x (float) – World system coordinate in X direction.
y (float) – World system coordinate in Y direction.
- resize(buffer_size, unit='px', inplace=False, **kwargs) RasterGeometry [source]
Resizes the raster geometry. The resizing values can be specified as a scale factor, in pixels, or in spatial reference units. A positive value extends, a negative value shrinks the original object (except for the scaling factor).
- Parameters
buffer_size (number or list of numbers) – Buffering values, which have to be given in a clock-wise order, i.e. [left edge, top edge, right edge, bottom edge] or as one value.
unit (string, optional) –
Unit of the buffering value ˋbuffer_sizeˋ. Possible values are:
’’: ˋbuffer_sizeˋ is given unitless as a positive scale factor with respect to edge length. ‘px’: ˋbuffer_sizeˋ is given as number of pixels. ‘sr’: ˋbuffer_sizeˋ is given in spatial reference units (meters/degrees).
inplace (bool, optional) – If True, the current instance will be modified. If False, a new RasterGeometry instance will be created (default).
**kwargs – Additional keyword arguments for the RasterGeometry constructor, e.g. name or description.
- Returns
Resized raster geometry.
- Return type
- scale(scale_factor, inplace=False, **kwargs) RasterGeometry [source]
Scales the raster geometry as a whole or for each edge. The scaling factor always refers to an edge length of the raster geometry boundary.
- Parameters
scale_factor (number or list of numbers) – Scale factors, which have to be given in a clock-wise order, i.e. [left edge, top edge, right edge, bottom edge] or as one value.
inplace (bool, optional) – If True, the current instance will be modified. If False, a new RasterGeometry instance will be created (default).
**kwargs – Additional keyword arguments for the RasterGeometry constructor, e.g. name or description.
- Returns
Scaled raster geometry.
- Return type
- property shape: Tuple[int, int]
Returns the shape of the raster geometry, which is defined by the height and width in pixels.
- slice_by_rc(row, col, height=1, width=1, inplace=False, **kwargs) RasterGeometry [source]
Intersects the raster geometry with a pixel extent.
- Parameters
row (int) – Top-left row number of the pixel window anchor.
col (int) – Top-left column number of the pixel window anchor.
height (int, optional) – Number of rows/height of the pixel window.
width (int, optional) – Number of columns/width of the pixel window.
inplace (bool) – If true, the current instance will be modified (default). If false, a new RasterGeometry instance will be created (default).
**kwargs – Additional keyword arguments for the RasterGeometry constructor, e.g. name or description.
- Returns
Raster geometry instance defined by the pixel extent.
- Return type
- snap_to_grid(x, y, sref=None, px_origin='ul') Tuple[float, float] [source]
Floors the given world system coordinates x and y to a coordinate point of the grid spanned by the raster geometry. The coordinate anchor specified by px_origin is then returned.
- Parameters
x (float) – World system coordinate in X direction.
y (float) – World system coordinate in Y direction.
sref (SpatialRef, optional) – Spatial reference of the coordinates. Has to be given if the spatial reference is different than the spatial reference of the raster geometry.
px_origin (str, optional) – Defines the world system origin of the pixel. It can be: - upper left (“ul”, default) - upper right (“ur”) - lower right (“lr”) - lower left (“ll”) - center (“c”)
- Returns
new_x (float) – Raster geometry grid coordinate in X direction related to the origin defined by px_origin.
new_y (float) – Raster geometry grid coordinate in Y direction related to the origin defined by px_origin.
- to_definition() dict [source]
Creates a human-readable definition of the raster geometry.
- Return type
- to_json(filepath)[source]
Creates a human-readable definition of the raster geometry in JSON format and writes it to disk.
- Parameters
filepath (str) – Full JSON file path.
- property v_pixel_size: float
Pixel size in N-S direction (equal to y_pixel_size if the RasterGeometry is axis-parallel).
- xy2rc(x, y, sref=None, px_origin=None) Tuple[int, int] [source]
Calculates an index of a pixel in which a given point of a world system lies.
- Parameters
x (float) – World system coordinate in X direction.
y (float) – World system coordinate in Y direction.
sref (SpatialRef, optional) – Spatial reference of the coordinates. Has to be given if the spatial reference is different than the spatial reference of the raster geometry.
px_origin (str, optional) – Defines the world system origin of the pixel. It can be: - upper left (“ul”) - upper right (“ur”) - lower right (“lr”) - lower left (“ll”) - center (“c”) Defaults to None, using the class internal pixel origin.
- Returns
r (int) – Pixel row number.
c (int) – Pixel column number.
Notes
Rounds to the closest, lower integer.
- class geospade.raster.RegularMosaicGeometry(tiles, boundary=None, adjacency_matrix=None, name='', description='', check_consistency=True, **kwargs)[source]
Bases:
MosaicGeometry
Represents a regular, homogeneous mosaic of tiles. This means tiles are not allowed to overlap or vary in size.
- classmethod from_rectangular_definition(n_rows, n_cols, x_tile_size, y_tile_size, sref, geotrans=(0, 1, 0, 0, 0, 1), tile_class=<class 'geospade.raster.Tile'>, tile_kwargs=None, name_frmt='S{:03d}W{:03d}', boundary=None, name='', description='', **kwargs) RegularMosaicGeometry [source]
Creates a RegularMosaicGeometry instance from a well-defined definition of mosaic, i.e. origin, tile sizes, number of tiles and spatial reference system.
- Parameters
n_rows (int) – Number of mosaic tiles on Y direction.
n_cols (int) – Number of mosaic tiles on X direction.
x_tile_size (number) – Size/width of one tile given in X direction and in world system units.
y_tile_size (number) – Size/width of one tile given in Y direction and in world system units.
sref (SpatialRef) – Spatial reference system of the mosaic.
geotrans (6-tuple, optional) – GDAL geotransformation tuple containing information about the origin of the mosaic, its pixel sampling and its orientation (defaults to (0, 1, 0, 0, 0, 1))
tile_class (class, optional) – Class inheriting from Tile (default).
tile_kwargs (dict, optional) – Key-word arguments for the tile initialisation (defaults to None).
name_frmt (str, optional) – Formatter string for the tile name containing placeholders for two arguments, the mosaic row, and the mosaic col (defaults to “S{:03d}W{:03d}”).
name (str, optional) – Name of the mosaic.
description (string, optional) – Verbal description of the mosaic geometry (defaults to “”).
**kwargs – Key-word arguments for the mosaic initialisation.
- Returns
Regular mosaic geometry defined by the given origin, tile sizes, number of tiles and the spatial reference system.
- Return type
- class geospade.raster.Tile(n_rows, n_cols, sref, geotrans=(0, 1, 0, 0, 0, -1), mosaic_topology='INNER', active=True, metadata=None, name=None, description='', px_origin='ul', parent=None)[source]
Bases:
RasterGeometry
A light wrapper around a raster geometry to realise a tile - mosaic relationship.
- classmethod from_definition(definition) Tile [source]
Creates a tile from a human-readable tile definition, which is a dictionary containing the following elements:
‘number_of_rows’
‘number_of_columns’
‘spatial_reference’
‘geotransformation’
‘mosaic_topology’
‘active’
‘metadata’
‘name’
‘description’
‘pixel_origin’
The expected values can be taken from the RasterGeometry and Tile constructor docs.
- Parameters
definition (dict) – Tile definition.
- Return type
- slice_by_rc(row, col, height=1, width=1, inplace=False, **kwargs) Tile [source]
Intersects the tile with a pixel extent.
- Parameters
row (int) – Top-left row number of the pixel window anchor.
col (int) – Top-left column number of the pixel window anchor.
height (int, optional) – Number of rows/height of the pixel window.
width (int, optional) – Number of columns/width of the pixel window.
inplace (bool) – If true, the current instance will be modified (default). If false, a new RasterGeometry instance will be created (default).
**kwargs – Additional keyword arguments for the RasterGeometry constructor, e.g. name or description.
- Returns
Tile instance defined by the pixel extent.
- Return type
geospade.tools module
The tools module collects general-purpose, geospatial functions for raster and vector geometries and their interaction.
- geospade.tools.any_geom2ogr_geom(geom, sref=None) Geometry [source]
- Transforms:
bounding box extents [(x_min, y_min), (x_max, y_max)]
bounding box points [x_min, y_min, x_max, y_max]
a list of points [(x_1, y_1), (x_2, y_2), (x_3, y_3), …]
point coordinates (x_1, y_1)
a shapely.geometry.Point instance
a shapely.geometry.Polygon instance
a ogr.Geometry instance
into an OGR geometry object. If the given geometry representation does not contain information about its spatial reference, this information needs to be supplied via sref.
- Parameters
geom (ogr.Geometry or shapely.geometry or list or tuple) – A vector geometry. It can be a - bounding box extent [(x_min, y_min), (x_max, y_max)] - bounding box point list [x_min, y_min, x_max, y_max] - list of points [(x_1, y_1), (x_2, y_2), (x_3, y_3), …] - point (x_1, y_1) - shapely.geometry.Point instance - shapely.geometry.Polygon instance - ogr.Geometry instance
sref (geospade.crs.SpatialRef, optional) – Spatial reference system applied to the given geometry if it has none.
- Returns
ogr_geom – Vector geometry as an OGR Geometry object including its spatial reference.
- Return type
ogr.Geometry
- geospade.tools.bbox_to_polygon(bbox, sref, segment_size=None) Geometry [source]
Create a polygon geometry from a bounding-box bbox, given by a set of two points, spanning a rectangular area.
- bboxlist of 2-tuples
List of coordinates representing the rectangle-region-of-interest in the format of [(lower-left x, lower-left y), (upper-right x, upper-right y)].
- srefgeospade.crs.SpatialRef
Spatial reference system of the coordinates.
- segment_sizefloat, optional
For precision: distance of longest segment of the geometry polygon in units of the spatial reference system.
- Returns
A polygon geometry representing the input bbox.
- Return type
ogr.Geometry
- geospade.tools.create_polygon_geometry(points, sref, segment_size=None) Geometry [source]
Creates an OGR polygon geometry defined by a list of points.
- Parameters
points (list of tuples) – Points defining the polygon, either 2D: [(x1, y1), (x2, y2), …] or 3D: [(x1, y1, z1), (x2, y2, z2), …].
sref (geospade.crs.SpatialRef, optional) – Spatial reference system of the point coordinates.
segment_size (float, optional) – For precision: distance of longest segment of the geometry polygon in units of the spatial reference system.
- Returns
A polygon defined by the given set of points and located in the given spatial reference system.
- Return type
ogr.Geometry
- geospade.tools.get_inner_angles(polygon, deg=True) list [source]
Computes inner angles between all adjacent poly-lines.
- Parameters
polygon (ogr.Geometry) – Clock-wise ordered OGR polygon.
deg (boolean, optional) –
- Denotes, whether the angles are returned in degrees or radians:
True => degrees (default)
False => radians
- Returns
inner_angles – Inner angles in degree or radians.
- Return type
list of numbers
- geospade.tools.get_quadrant(x, y) int [source]
Returns the quadrant as an interger in a mathematical positive system: 1 => first quadrant 2 => second quadrant 3 => third quadrant 4 => fourth quadrant None => either one or both coordinates are zero
- geospade.tools.is_rectangular(polygon, eps=1e-09) bool [source]
Checks if the given polygon is rectangular.
- geospade.tools.polar_point(x_ori, y_ori, dist, angle, deg=True) Tuple[ndarray, ndarray] [source]
Computes a new point by specifying a distance and an azimuth from a given known point. The computation and values refer to a mathematical positive system.
- Parameters
x_ori (float) – x coordinate of the origin.
y_ori – y coordinate of the origin.
dist (float) – Distance from the origin to the new point.
angle (float) – Azimuth angle to the new point with respect to the x-axis (horizontal).
deg (boolean, optional) –
- Denotes, whether angle is being parsed in degrees or radians:
True => degrees (default)
False => radians
- Returns
x_pp (float or np.ndarray) – x coordinate of the new polar point.
y_pp (float or np.ndarray) – y coordinate of the new polar point.
- geospade.tools.rasterise_polygon(geom, x_pixel_size, y_pixel_size, extent=None) ndarray [source]
Rasterises a Shapely polygon defined by a clockwise list of points.
- Parameters
geom (shapely.geometry.Polygon) – Clockwise list of x and y coordinates defining a polygon.
x_pixel_size (float) – Absolute pixel size in X direction.
y_pixel_size (float) – Absolute pixel size in Y direction.
extent (4-tuple, optional) – Output extent of the raster (x_min, y_min, x_max, y_max). If it is not set the output extent is taken from the given geometry.
- Returns
raster – Binary array where zeros are background pixels and ones are foreground (polygon) pixels. Its shape is defined by the coordinate extent of the input polygon or by the specified extent parameter.
- Return type
np.ndarray
Notes
The coordinates are always expected to refer to the upper-left corner of a pixel, in a right-hand coordinate system. If the coordinates do not match the sampling, they are automatically aligned to upper-left.
For rasterising the actual polygon, PIL’s ImageDraw class is used.
- geospade.tools.rel_extent(origin, extent, x_pixel_size=1, y_pixel_size=1, unit='px') Tuple[float, float, float, float] [source]
Computes extent in relative pixels or world system coordinates with respect to an origin/reference point for the upper left corner.
- Parameters
origin (tuple) – World system coordinates (X, Y) or pixel coordinates (column, row) of the origin/reference point.
extent (4-tuple) – Coordinate (min_x, min_y, max_x, max_y) or pixel extent (min_col, min_row, max_col, max_row).
x_pixel_size (float) – Absolute pixel size in X direction.
y_pixel_size (float) – Absolute pixel size in Y direction.
unit (string, optional) –
Unit of the relative coordinates. Possible values are:
’px’: Relative coordinates are returned as the number of pixels.
’sr’: Relative coordinates are returned in spatial reference units (meters/degrees).
- Returns
Relative position of the given extent with respect to the given origin. The extent values are dependent on the unit:
’px’ : (min_col, min_row, max_col, max_row)
’sr’ : (min_x, min_y, max_x, max_y)
- Return type
4-tuple of numbers
- geospade.tools.segmentize_geometry(geom, segment_size=1.0) Geometry [source]
Segmentizes the lines of a geometry (decreases the point spacing along the lines) according to a given segment_size.
- Parameters
geom (ogr.Geometry) – OGR geometry object.
segment_size (float, optional) – For precision: distance of longest segment of the geometry polygon in units of the spatial reference system.
- Returns
geom_fine – A congruent geometry realised by more vertices along its shape.
- Return type
ogr.Geometry
geospade.transform module
Module collecting all functions dealing with coordinate transformations.
- geospade.transform.build_geotransform(ul_x, ul_y, x_pixel_size, y_pixel_size, rot, deg=True) Tuple[float, float, float, float, float, float] [source]
A helper function, that constructs the GDAL geo-transformation tuple given the upper-left coordinates, the pixel sizes and the rotation angle with respect to the world system grid.
- Parameters
ul_x (float) – X coordinate of the upper-left corner.
ul_y (float) – Y coordinate of the upper-left corner.
x_pixel_size (float) – Absolute pixel size in X direction.
y_pixel_size (float) – Absolute pixel size in Y direction.
rot (float) – Rotation angle in degrees and radians depending on deg.
deg (boolean) –
- Denotes, whether angle is being parsed in degrees or radians:
True => degrees (default)
False => radians
- Returns
GDAL geo-transformation tuple.
- Return type
6-tuple
- geospade.transform.ij2xy(i, j, geotrans, origin='ul') Tuple[float, float] [source]
Transforms global/world system coordinates to pixel coordinates/indexes.
- Parameters
i (int or np.array) – Column number(s) in pixels.
j (int or np.array) – Row number(s) in pixels.
geotrans (6-tuple) – GDAL geo-transformation parameters/dictionary.
origin (str, optional) – Defines the world system origin of the pixel. It can be: - upper left (“ul”, default) - upper right (“ur”) - lower right (“lr”) - lower left (“ll”) - center (“c”)
- Returns
x (float or np.ndarray) – World system coordinate(s) in X direction.
y (float or np.ndarray) – World system coordinate(s) in Y direction.
- geospade.transform.transform_coords(x, y, this_sref, other_sref) Tuple[float, float] [source]
Transforms coordinates from a source to a target spatial reference system.
- Parameters
x (float) – World system coordinate in X direction with this_sref as a spatial reference system.
y (float) – World system coordinate in Y direction with this_sref as a spatial reference system.
this_sref (geospade.crs.SpatialRef, optional) – Spatial reference of the source coordinates.
other_sref (geospade.crs.SpatialRef, optional) – Spatial reference of the target coordinates.
- Returns
x (float) – World system coordinate in X direction with other_sref as a spatial reference system.
y (float) – World system coordinate in Y direction with other_sref as a spatial reference system.
- geospade.transform.transform_geom(geom, other_sref) Geometry [source]
Transforms a OGR geometry from its source to a target spatial reference system.
- Parameters
geom (ogr.Geometry) – OGR geometry with an assigned spatial reference system.
other_sref (geospade.crs.SpatialRef, optional) – Spatial reference of the target geometry.
- Returns
geom – Transformed OGR geometry.
- Return type
ogr.Geometry
- geospade.transform.xy2ij(x, y, geotrans, origin='ul') Tuple[int, int] [source]
Transforms global/world system coordinates to pixel coordinates/indexes.
- Parameters
x (float or np.array) – World system coordinate(s) in X direction.
y (float or np.array) – World system coordinate(s) in Y direction.
geotrans (6-tuple) – GDAL geo-transformation parameters/dictionary.
origin (str, optional) – Defines the world system origin of the pixel. It can be: - upper left (“ul”, default) - upper right (“ur”) - lower right (“lr”) - lower left (“ll”) - center (“c”)
- Returns
i (int or np.ndarray) – Column number(s) in pixels.
j (int or np.ndarray) – Row number(s) in pixels.
geospade.vector module
Module containing class definitions for vectors.