Reference for SWMManywhere/geospatial_utilities.py
Geospatial utilities module for SWMManywhere.
A module containing functions to perform a variety of geospatial operations, such as reprojecting coordinates and handling raster data.
Grid
A class to represent a grid.
Source code in swmmanywhere/geospatial_utilities.py
__init__(affine, shape, crs, bbox)
Initialize the Grid class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
affine
|
Affine
|
The affine transformation. |
required |
shape
|
tuple
|
The shape of the grid. |
required |
crs
|
int
|
The CRS of the grid. |
required |
bbox
|
tuple
|
The bounding box of the grid. |
required |
Source code in swmmanywhere/geospatial_utilities.py
attach_unconnected_subareas(polys_gdf, unconnected_subareas)
Attach unconnected subareas to the nearest polygon.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
polys_gdf
|
GeoDataFrame
|
A GeoDataFrame containing polygons with columns: 'geometry', 'area', and 'id'. |
required |
unconnected_subareas
|
List[Polygon]
|
A list of subareas that are not attached to others. |
required |
Returns:
Type | Description |
---|---|
GeoDataFrame
|
gpd.GeoDataFrame: A GeoDataFrame containing polygons. |
Source code in swmmanywhere/geospatial_utilities.py
burn_shape_in_raster(geoms, depth, raster_fid, new_raster_fid)
Burn a depth into a raster along a list of shapely geometries.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
geoms
|
list
|
List of Shapely geometries. |
required |
depth
|
float
|
Depth to carve. |
required |
raster_fid
|
Path
|
Filepath to input raster. |
required |
new_raster_fid
|
Path
|
Filepath to save the carved raster. |
required |
Source code in swmmanywhere/geospatial_utilities.py
calculate_angle(point1, point2, point3)
Calculate the angle between three points.
Calculate the angle between the vectors formed by (point1, point2) and (point2, point3)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
point1
|
tuple
|
The first point (x,y). |
required |
point2
|
tuple
|
The second point (x,y). |
required |
point3
|
tuple
|
The third point (x,y). |
required |
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
The angle between the three points in degrees. |
Source code in swmmanywhere/geospatial_utilities.py
calculate_slope(polys_gdf, grid, cell_slopes)
Calculate the average slope of each polygon.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
polys_gdf
|
GeoDataFrame
|
A GeoDataFrame containing polygons with columns: 'geometry', 'area', and 'id'. |
required |
grid
|
Grid
|
Information of the raster (affine, shape, crs, bbox) |
required |
cell_slopes
|
ndarray
|
The slopes of each cell in the grid. |
required |
Returns:
Type | Description |
---|---|
GeoDataFrame
|
gpd.GeoDataFrame: A GeoDataFrame containing polygons with an added 'slope' column. |
Source code in swmmanywhere/geospatial_utilities.py
delineate_catchment_pyflwdir(grid, flow_dir, G)
Derive subcatchments from the nodes on a graph and a DEM.
Uses the pyflwdir catchment delineation functionality. About a magnitude faster than delineate_catchment.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
grid
|
Grid
|
Information of the raster (affine, shape, crs, bbox). |
required |
flow_dir
|
array
|
Flow directions. |
required |
G
|
Graph
|
The input graph with nodes containing 'x' and 'y'. |
required |
Returns:
Type | Description |
---|---|
GeoDataFrame
|
gpd.GeoDataFrame: A GeoDataFrame containing polygons with columns: 'geometry', 'area', 'id', 'width', and 'slope'. |
Source code in swmmanywhere/geospatial_utilities.py
derive_rc(subcatchments, building_footprints, streetcover)
Derive the Runoff Coefficient (RC) of each subcatchment.
The runoff coefficient is the ratio of impervious area to total area. The impervious area is calculated by overlaying the subcatchments with building footprints and all edges in G buffered by their width parameter (i.e., to calculate road area).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
subcatchments
|
GeoDataFrame
|
A GeoDataFrame containing polygons that represent subcatchments with columns: 'geometry', 'area', and 'id'. |
required |
building_footprints
|
GeoDataFrame
|
A GeoDataFrame containing building footprints with a 'geometry' column. |
required |
streetcover
|
GeoDataFrame
|
A GeoDataFrame containing street cover with a 'geometry' column. |
required |
Returns:
Type | Description |
---|---|
GeoDataFrame
|
gpd.GeoDataFrame: A GeoDataFrame containing polygons with columns: 'geometry', 'area', 'id', 'impervious_area', and 'rc'. |
Source code in swmmanywhere/geospatial_utilities.py
derive_subbasins_streamorder(fid, streamorder=None, x=[], y=[])
Derive subbasins.
Use the pyflwdir snap function to find the most downstream points in each subbasin. If streamorder is provided it will use that instead, although defaulting to snap if there are no cells of the correct streamorder.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fid
|
Path
|
Filepath to the DEM. |
required |
streamorder
|
int
|
The stream order to delineate subbasins for. |
None
|
x
|
list
|
X coordinates. |
[]
|
y
|
list
|
Y coordinates. |
[]
|
Returns:
Type | Description |
---|---|
GeoDataFrame
|
gpd.GeoDataFrame: A GeoDataFrame containing polygons. |
Source code in swmmanywhere/geospatial_utilities.py
derive_subcatchments(G, fid, method='whitebox')
Derive subcatchments from the nodes on a graph and a DEM.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
G
|
Graph
|
The input graph with nodes containing 'x' and 'y'. |
required |
fid
|
Path
|
Filepath to the DEM. |
required |
method
|
str
|
The method to use for conditioning. |
'whitebox'
|
Returns:
Type | Description |
---|---|
GeoDataFrame
|
gpd.GeoDataFrame: A GeoDataFrame containing polygons with columns: 'geometry', 'area', 'id', 'width', and 'slope'. |
Source code in swmmanywhere/geospatial_utilities.py
edges_to_features(G)
Convert a graph to a GeoJSON edge feature collection.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
G
|
Graph
|
The input graph. |
required |
Returns:
Name | Type | Description |
---|---|---|
dict |
A GeoJSON feature collection. |
Source code in swmmanywhere/geospatial_utilities.py
flwdir_whitebox(fid)
Calculate flow direction using WhiteboxTools.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fid
|
Path
|
Filepath to the DEM. |
required |
Returns:
Type | Description |
---|---|
array
|
np.array: Flow directions. |
Source code in swmmanywhere/geospatial_utilities.py
get_transformer(source_crs, target_crs)
Get a transformer object for reprojection.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source_crs
|
str
|
Source CRS in EPSG format (e.g., EPSG:32630). |
required |
target_crs
|
str
|
Target CRS in EPSG format (e.g., EPSG:32630). |
required |
Returns:
Type | Description |
---|---|
Transformer
|
pyproj.Transformer: Transformer object for reprojection. |
Example
transformer = get_transformer('EPSG:4326', 'EPSG:32630') x, y = transformer.transform(-0.1276, 51.5074) print(f"{x:.6f}, {y:.6f}") 699330.110690, 5710164.303007
Source code in swmmanywhere/geospatial_utilities.py
get_utm_epsg(x, y, crs='EPSG:4326', datum_name='WGS 84')
Get the UTM CRS code for a given coordinate.
Note, this function is taken from GeoPandas and modified to use for getting the UTM CRS code for a given coordinate.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
float
|
Longitude in crs |
required |
y
|
float
|
Latitude in crs |
required |
crs
|
str | int | CRS
|
The CRS of the input coordinates. Defaults to 'EPSG:4326'. |
'EPSG:4326'
|
datum_name
|
str
|
The datum name to use for the UTM CRS |
'WGS 84'
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
Formatted EPSG code for the UTM zone. |
Example
get_utm_epsg(-0.1276, 51.5074) 'EPSG:32630'
Source code in swmmanywhere/geospatial_utilities.py
graph_to_geojson(graph, fid_nodes, fid_edges, crs)
Write a graph to a GeoJSON file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
graph
|
Graph
|
The input graph. |
required |
fid_nodes
|
Path
|
The filepath to save the nodes GeoJSON file. |
required |
fid_edges
|
Path
|
The filepath to save the edges GeoJSON file. |
required |
crs
|
str
|
The CRS of the graph. |
required |
Source code in swmmanywhere/geospatial_utilities.py
interp_with_nans(xy, interp, grid, values)
Wrap the interpolation function to handle NaNs.
Picks the nearest non NaN grid point if the interpolated value is NaN, otherwise returns the interpolated value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
xy
|
tuple
|
Coordinate of interest |
required |
interp
|
RegularGridInterpolator
|
The interpolator object. |
required |
grid
|
ndarray
|
List of xy coordinates of the grid points. |
required |
values
|
list
|
The list of values at each point in the grid. |
required |
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
The interpolated value. |
Source code in swmmanywhere/geospatial_utilities.py
interpolate_points_on_raster(x, y, elevation_fid)
Interpolate points on a raster.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
list
|
X coordinates. |
required |
y
|
list
|
Y coordinates. |
required |
elevation_fid
|
Path
|
Filepath to elevation raster. |
required |
Returns:
Name | Type | Description |
---|---|---|
elevation |
float
|
Elevation at point. |
Source code in swmmanywhere/geospatial_utilities.py
load_and_process_dem(fid, method='whitebox')
Load and condition a DEM.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fid
|
Path
|
Filepath to the DEM. |
required |
method
|
str
|
The method to use for conditioning. Defaults to "whitebox". |
'whitebox'
|
Returns:
Name | Type | Description |
---|---|---|
tuple |
tuple[Grid, array, array]
|
A tuple containing the grid, flow directions, and cell slopes. |
Source code in swmmanywhere/geospatial_utilities.py
merge_points(coordinates, threshold)
Merge points that are within a threshold distance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
coordinates
|
list
|
List of coordinates as tuples. |
required |
threshold(float)
|
The threshold distance for merging points. |
required |
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
A dictionary mapping the original point index to the merged point and new coordinate. |
Source code in swmmanywhere/geospatial_utilities.py
nearest_node_buffer(points1, points2, threshold)
Find the nearest node within a given buffer threshold.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
points1
|
dict
|
A dictionary where keys are labels and values are Shapely points geometries. |
required |
points2
|
dict
|
A dictionary where keys are labels and values are Shapely points geometries. |
required |
threshold
|
float
|
The maximum distance for a node to be considered 'nearest'. If no nodes are within this distance, the node is not included in the output. |
required |
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
A dictionary where keys are labels from points1 and values are labels from points2 of the nearest nodes within the threshold. |
Source code in swmmanywhere/geospatial_utilities.py
nodes_to_features(G)
Convert a graph to a GeoJSON node feature collection.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
G
|
Graph
|
The input graph. |
required |
Returns:
Name | Type | Description |
---|---|---|
dict |
A GeoJSON feature collection. |
Source code in swmmanywhere/geospatial_utilities.py
remove_intersections(polys)
Remove intersections from a GeoDataFrame of polygons.
Subcatchments are derived for a given point, and so larger subcatchments will contain smaller ones. This function removes the smaller subcatchments from the larger ones.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
polys
|
GeoDataFrame
|
A GeoDataFrame containing polygons with columns: 'geometry', 'area', and 'id'. |
required |
Returns:
Type | Description |
---|---|
GeoDataFrame
|
gpd.GeoDataFrame: A GeoDataFrame containing polygons with no intersections. |
Source code in swmmanywhere/geospatial_utilities.py
remove_zero_area_subareas(mp, removed_subareas)
Remove subareas with zero area from a multipolygon.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mp
|
MultiPolygon
|
A multipolygon. |
required |
removed_subareas
|
List[Polygon]
|
A list of removed subareas. |
required |
Returns:
Type | Description |
---|---|
MultiPolygon
|
sgeom.MultiPolygon: A multipolygon with zero area subareas removed. |
Source code in swmmanywhere/geospatial_utilities.py
reproject_df(df, source_crs, target_crs)
Reproject the coordinates in a DataFrame.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
DataFrame with columns 'longitude' and 'latitude'. |
required |
source_crs
|
str
|
Source CRS in EPSG format (e.g., EPSG:4326). |
required |
target_crs
|
str
|
Target CRS in EPSG format (e.g., EPSG:32630). |
required |
Source code in swmmanywhere/geospatial_utilities.py
reproject_graph(G, source_crs, target_crs)
Reproject the coordinates in a graph.
osmnx.projection.project_graph might be suitable if some other behaviour needs to be captured, but it currently fails the tests so I will ignore for now.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
G
|
Graph
|
Graph with nodes containing 'x' and 'y' properties. |
required |
source_crs
|
str
|
Source CRS in EPSG format (e.g., EPSG:4326). |
required |
target_crs
|
str
|
Target CRS in EPSG format (e.g., EPSG:32630). |
required |
Returns:
Type | Description |
---|---|
Graph
|
nx.Graph: Graph with nodes containing 'x' and 'y' properties. |
Source code in swmmanywhere/geospatial_utilities.py
reproject_raster(target_crs, fid, new_fid=None)
Reproject a raster to a new CRS.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target_crs
|
str
|
Target CRS in EPSG format (e.g., EPSG:32630). |
required |
fid
|
Path
|
Filepath to the raster to reproject. |
required |
new_fid
|
Path
|
Filepath to save the reprojected raster. Defaults to None, which will just use fid with '_reprojected'. |
None
|
Source code in swmmanywhere/geospatial_utilities.py
vectorize(data, nodata, transform, crs, name='value')
Vectorize raster data into a geodataframe.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
ndarray
|
The raster data. |
required |
nodata
|
float
|
The nodata value. |
required |
transform
|
Affine
|
The affine transformation. |
required |
crs
|
int
|
The CRS of the data. |
required |
name
|
str
|
The name of the data. Defaults to "value". |
'value'
|
Returns:
Type | Description |
---|---|
GeoDataFrame
|
gpd.GeoDataFrame: A GeoDataFrame containing the vectorized data. |