Finding isolated networks¶
Functions for Finding network components in a GeoDataFrame of lines.
- get_component_size(gdf)[source]
Finds the size of each component in the network.
Takes a GeoDataFrame of linea and creates the column “component_size”, which indicates the size of the network component the line is a part of.
- Parameters:
gdf (
GeoDataFrame
) – a GeoDataFrame of lines.- Return type:
GeoDataFrame
- Returns:
A GeoDataFrame with a new column “component_size”.
Examples:¶
>>> from sgis import read_parquet_url, get_component_size >>> roads = read_parquet_url("https://media.githubusercontent.com/media/statisticsnorway/ssb-sgis/main/tests/testdata/roads_oslo_2022.parquet")
>>> roads = get_component_size(roads) >>> roads["component_size"].value_counts().head() component_size 79180 85638 2 1601 4 688 6 406 3 346 Name: count, dtype: int64
- get_connected_components(gdf)[source]
Finds the largest network component.
It takes a GeoDataFrame of lines and finds the lines that are part of the largest connected network component. These lines are given the value 1 in the added column ‘connected’, while isolated network islands get the value 0.
Uses the connected_components function from the networkx package.
- Parameters:
gdf (
GeoDataFrame
) – A GeoDataFrame of lines.- Return type:
GeoDataFrame
- Returns:
The GeoDataFrame with a new column “connected”.
Examples:¶
>>> from sgis import read_parquet_url, get_connected_components >>> roads = read_parquet_url("https://media.githubusercontent.com/media/statisticsnorway/ssb-sgis/main/tests/testdata/roads_oslo_2022.parquet")
>>> roads = get_connected_components(roads) >>> roads.connected.value_counts() 1.0 85638 0.0 7757 Name: connected, dtype: int64
Removing the isolated network islands.
>>> connected_roads = get_connected_components(roads).loc[lambda x: x["connected"] == 1] >>> roads.connected.value_counts() 1.0 85638 Name: connected, dtype: int64