Preprocessing#

Data import and preprocessing#

class pysewer.preprocessing.DEM(file_path: str | None = None)[source]#

Bases: object

A class for handling digital elevation model (DEM) data.

Variables:
  • file_path (str, optional) – The file path to the DEM raster file.

  • raster (rasterio.DatasetReader, optional) – The rasterio dataset reader object for the DEM raster file.

  • no_dem (bool) – A flag indicating whether or not a DEM has been loaded.

get_elevation(point: shapely.geometry.Point) int:[source]#

Returns elevation data in meters for a given point rounded to two decimals.

get_profile(line: shapely.geometry.LineString, dx: int = 10) List[Tuple[float, int]]:[source]#

Extracts elevation data from a digital elevation model (DEM) along a given path.

reproject_dem(crs: CRS) None:[source]#

Reprojects the DEM raster to the given CRS.

Properties()#
----------
get_crs : CRS

Returns the coordinate system of the DEM object.

file_path: str | None = None#
raster: DatasetReader = None#
no_dem: bool = True#
remove_nodata(fill_value: float = 0)[source]#

Removes or replaces nodata values in the DEM raster.

Parameters:

fill_value (float, optional) – The value to replace nodata values with. Default is 0.

Raises:

ValueError – If no DEM is loaded, cannot remove nodata values.

get_elevation(point: Point)[source]#

Returns elevation data in meters for a given point rounded to two decimals.

Parameters:

point (shapely.geometry.Point) – The point for which to retrieve elevation data.

Returns:

The elevation in meters.

Return type:

int

Raises:

ValueError – If the query point is out of bounds or if there is no elevation data for the given coordinates.

get_profile(line: LineString, dx: int | None = None)[source]#

Extracts elevation data from a digital elevation model (DEM) along a given path.

Parameters:
  • line (shapely.geometry.LineString) – The path along which to extract elevation data.

  • dx (float, optional) – The sampling resolution in meters. Default is 10.

Returns:

A list of (x, elevation) tuples representing the x-coordinate and elevation data of the profile. The x-coordinate values start at 0 and are spaced at intervals of dx meters.

Return type:

List of Tuples

property get_crs: CRS | None#

Returns the coordinate system of the DEM Object

reproject_dem(crs: CRS)[source]#

Reprojects the DEM raster to the given CRS.

Parameters:

crs (CRS) – The target CRS to reproject the raster to.

Raises:

ValueError – If no DEM is loaded, cannot reproject DEM.

class pysewer.preprocessing.Buildings(input_data: str | GeoDataFrame, roads_obj: Roads)[source]#

Bases: object

A class to preprocess building data.

Parameters:
  • input_data (str or geopandas.GeoDataFrame) – Path to a shapefile or a GeoDataFrame containing the input data.

  • roads_obj (pysewer.Roads) – A Roads object containing the road network data.

Variables:
  • gdf (geopandas.GeoDataFrame) – The input building data.

  • roads_obj (pysewer.Roads) – The road network data.

get_gdf():

Returns building data as geopandas dataframe.

get_crs():

Returns the Coordinate System of the DEM Object.

cluster_centers(max_connection_length):

Returns a list of cluster centers for all buildings with greater than max_connection_length distance to the nearest street.

get_gdf()[source]#

Returns building data as geopandas dataframe.

Returns:

The building data.

Return type:

geopandas.GeoDataFrame

get_crs()[source]#

Returns the Coordinate System of the DEM Object.

Returns:

The Coordinate Reference System (CRS) of the building data.

Return type:

dict

set_crs(epsg: int)[source]#
static convert_to_points(geometry)[source]#

Converts the building polygons or multipolygons to points.

cluster_centers(max_connection_length: float)[source]#

Returns a list of cluster centers for all buildings with greater than max_connection_length distance to the nearest street.

Parameters:

max_connection_length (float) – The maximum distance between a building and the nearest street for it to be included in the cluster centers list.

Returns:

A GeoDataFrame containing the cluster centers and their distances to the nearest street, sorted by distance.

Return type:

geopandas.GeoDataFrame

class pysewer.preprocessing.Roads(input_data: str | GeoDataFrame)[source]#

Bases: object

A class to represent road data from either a shapefile or a geopandas dataframe. Attributes: ———- gdf : geopandas.GeoDataFrame

A geopandas dataframe containing road data.

merged_roadsshapely.geometry.MultiLineString

A shapely MultiLineString object representing the merged road data.

Methods:#

__init__(self, input_data: str or geopandas.GeoDataFrame) -> None

Initializes a Roads object with road data from either a shapefile or a geopandas dataframe.

get_nearest_point(point)[source]#

Returns the nearest location to the input point on the street network. :Parameters: point (shapely.geometry.Point) – Point to find nearest location to.

Returns:

Nearest location to the input point on the street network.

Return type:

shapely.geometry.Point

get_gdf()[source]#

Returns the road data as a geopandas dataframe. :returns: The road data as a geopandas dataframe. :rtype: geopandas.GeoDataFrame

get_crs()[source]#

Gets the coordinate reference system (CRS) of the Roads object.

Returns:

The coordinate system of the Roads object.

Return type:

dict

set_crs(epsg: int)[source]#
get_merged_roads()[source]#

Merge the road network as a shapely MultiLineString.

Returns:

merged road network as a shapely MultiLineString

Return type:

shapely MultiLineString

Simplfy Graph#

pysewer.simplify.simplify_graph(G: MultiDiGraph, strict: bool = True, remove_rings: bool = True) MultiDiGraph[source]#

Simplify a graph’s topology by removing interstitial nodes. Simplify graph topology by removing all nodes that are not intersections or dead-ends. Create an edge directly between the end points that encapsulate them, but retain the geometry of the original edges, saved as an attribute in new edge. Some of the resulting consolidated edges may comprise multiple OSM ways, and if so, their multiple attribute values are stored as a list. :Parameters: * G (networkx.MultiDiGraph) – input graph

  • strict (bool) – if False, allow nodes to be end points even if they fail all other rules but have incident edges with different OSM IDs. Lets you keep nodes at elbow two-way intersections, but sometimes individual blocks have multiple OSM IDs within them too.

  • remove_rings (bool) – if True, remove isolated self-contained rings that have no endpoints

Returns:

G – topologically simplified graph

Return type:

networkx.MultiDiGraph

pysewer.simplify.get_essential_nodes(G)[source]#

Returns a list of essential nodes to build the simplified graph. This includes all junctions (degree > 2), buildings, and connection points.

Parameters:

G (networkx.Graph) – NetworkX street graph with connected buildings.

Returns:

List of node keys.

Return type:

List

Examples

>>> G = nx.Graph()
>>> G.add_node(1, road_network=True)
>>> G.add_node(2, road_network=True)
>>> G.add_node(3, road_network=False)
>>> G.add_node(4, connection_node=True)
>>> G.add_node(5, connection_node=False)
>>> G.add_edge(1, 2)
>>> G.add_edge(2, 3)
>>> G.add_edge(2, 4)
>>> G.add_edge(4, 5)
>>> get_essential_nodes(G)
[1, 2, 3, 5]

Generate Connection Graph#

class pysewer.preprocessing.ModelDomain(dem: str, roads: str, buildings: str, clustering: str | None = None, pump_penalty: int | None = None, connect_buildings: bool | None = None)[source]#

Bases: object

Class for preprocessing input data for the sewer network.

Parameters:
  • dem (str) – Path to the digital elevation model file.

  • roads (str) – Path to the roads shapefile.

  • buildings (str) – Path to the buildings shapefile.

  • clustering (str, optional) – Clustering method for connecting buildings to the sewer network. Default is “centers”.

  • pump_penalty (int, optional) – Penalty for adding a pump to the sewer network. Default is 1000.

  • connect_buildings (bool, optional) – Whether to connect buildings to the sewer network. Default is True.

Variables:
  • roads (Roads) – Roads object.

  • buildings (Buildings) – Buildings object.

  • dem (DEM) – DEM object.

  • connection_graph (nx.Graph) – Graph representing the road network.

  • pump_penalty (int) – Penalty for adding a pump to the sewer network.

create_unsimplified_graph(roads_gdf: GeoDataFrame) Graph[source]#

Create an unsimplified graph from a GeoDataFrame of roads. :Parameters: roads_gdf (gpd.GeoDataFrame) – A GeoDataFrame containing road data.

Returns:

An unsimplified graph containing nodes and edges from the GeoDataFrame.

Return type:

nx.Graph

connect_buildings(max_connection_length: int | None = None, clustering: str | None = None)[source]#

Connects the buildings in the network by adding nodes to the graph. :Parameters: * max_connection_length (int, optional) – The maximum distance between a building and the nearest street for it to be included in the cluster

centers list. The default is 30.

  • clustering (str, optional) – The method used to cluster the buildings. Can be “centers” or “none”. The default is “centers”.

Return type:

None

Notes

This method adds nodes to the graph to connect the buildings in the network. It first gets the building points and then clusters them based on the specified method. If clustering is set to “centers”, it gets the cluster centers and finds the closest edges to them. It then adds nodes to the graph for each cluster center, with the closest edge as an attribute. If clustering is set to “none”, it simply adds nodes to the graph for each building. In both cases, it finds the closest edges to the buildings and adds nodes to the graph for each building, with the closest edge as an attribute.

Examples

>>> network = Network()
>>> network.connect_buildings(max_connection_length=50, clustering="centers")
add_node(point, node_type, closest_edge=None, node_attributes=None)[source]#

Adds a node to the connection graph.

Parameters:
  • point (shapely.geometry.Point) – The point to add as a node.

  • node_type (str) – The type of node to add.

  • closest_edge (shapely.geometry.LineString, optional) – The closest edge to the point. Defaults to None.

  • node_attributes (dict, optional) – Additional attributes to add to the node. Defaults to None.

Return type:

None

Notes

This method adds a node to the connection graph. If closest_edge is not provided, it finds the closest edge to the point and uses that as the closest_edge. It then disconnects edges from the node and adds the node to the connection graph. If there are any cluster centers, it connects the node to the nearest cluster center. If there are no cluster centers, it connects the node to the road network.

connect_to_roadnetwork(G: Graph, new_node, conn_point, closest_edge, add_private_sewer: bool | None = None)[source]#

Connects a new node to the road network by inserting a connection point on the closest edge and adjusting edges.

Parameters:
  • G (networkx.Graph) – The road network graph.

  • new_node (NodeTpye) – The new node to be connected to the road network.

  • conn_point (pysewer.Point) – The point where the new node will be connected to the road network.

  • closest_edge (pysewer.Edge) – The closest edge to the new node.

  • add_private_sewer (bool, optional) – Whether to add a private sewer between the new node and the connection point. Defaults to True.

Returns:

True if the connection was successful, False otherwise.

Return type:

bool

generate_connection_graph() MultiDiGraph[source]#

Generates a connection graph from the given connection data and returns it. This method simplifies the connection graph, removes any self loops, sets trench depth node attributes to 0, calculates the geometry, distance, profile, needs_pump, weight, and elevation attributes for each edge and node in the connection graph.

Returns:#

nx.MultiDiGraph

A directed graph representing the connections between the different points in the network.

add_sink(sink_location: tuple, label: str = 'wwtp')[source]#

Adds a sink node to the graph at the specified location.

Parameters:

sink_location (tuple) – A tuple containing the x and y coordinates of the sink location.

Return type:

None

reset_sinks()[source]#

Resets the sinks in the connection graph by setting their node_type attribute to an empty string. :returns: This method does not return anything. :rtype: None

set_sink_lowest(candidate_nodes: list | None = None)[source]#

Sets the sink node to the lowest point in the graph.

Parameters:

candidate_nodes (list, optional) – A list of candidate nodes to consider for the sink node. If None, all nodes except buildings are considered.

Return type:

None

Notes

This method sets the sink node to the lowest point in the graph. If candidate_nodes is not None, only the nodes in candidate_nodes that are not buildings are considered.

get_sinks()[source]#

Returns a list of node keys for all wastewater treatment plants (wwtp) in the connection graph.

set_pump_penalty(pp)[source]#

Set the pump penalty for the current instance of the ModelDomain class. :Parameters: pp (float) – The pump penalty to set.

Return type:

None

get_buildings()[source]#

Returns a list of node keys for all buildings in the connection graph.

connect_subgraphs()[source]#

Identifies unconnected street subnetworks and connects them based on shortest distance