Closing network holes

Functions for filling gaps in the network with straight lines.

close_network_holes(gdf, max_distance, max_angle, hole_col='hole')[source]

Fills network gaps with straigt lines.

Fills holes in the network by connecting deadends with the nodes that are within the ‘max_distance’ distance.

Parameters:
  • gdf (GeoDataFrame) – GeoDataFrame with lines.

  • max_distance (int | float) – The maximum distance for the holes to be filled.

  • max_angle (int) – Absolute number between 0 and 180 that represents the maximum difference in angle between the new line and the prior, i.e. the line at which the deadend terminates. A value of 0 means the new lines must have the exact same angle as the prior line, and 180 means the new lines can go in any direction.

  • hole_col (str | None) – If you want to keep track of which lines were added, you can add a column with a value of 1. Defaults to ‘hole’

Return type:

GeoDataFrame

Returns:

The input GeoDataFrame with new lines added.

Note

The holes will have missing values in the weight column used in NetworkAnalysis. These values must be filled before analysis.

Examples:

Read road data with small gaps.

>>> import sgis as sg
>>> roads = sg.read_parquet_url("https://media.githubusercontent.com/media/statisticsnorway/ssb-sgis/main/tests/testdata/roads_oslo_2022.parquet")

Roads need to be singlepart linestrings for this to work.

>>> from shapely import line_merge
>>> roads.geometry = line_merge(roads.geometry)

Fill gaps shorter than 1.1 meters.

>>> filled = sg.close_network_holes(roads, max_distance=1.1, max_angle=180)
>>> filled.hole.value_counts()
hole
0    93395
1     7102
Name: count, dtype: int64

Compare the number of isolated lines before and after.

>>> roads = sg.get_connected_components(roads)
>>> roads.connected.value_counts()
1.0    85638
0.0     7757
Name: connected, dtype: int64
>>> filled = sg.get_connected_components(filled)
>>> filled.connected.value_counts()
1.0    100315
0.0       180
Name: connected, dtype: int64

Fill only gaps with an angle deviation between 0 and 30 compared to the prior line.

>>> filled = sg.close_network_holes(roads, max_distance=1.1, max_angle=30)
>>> filled.hole.value_counts()
hole
0    93395
1     7092
Name: count, dtype: int64

It’s not always wise to fill gaps. In the case of this data, these small gaps are intentional. They are road blocks where most cars aren’t allowed to pass. Fill the holes only if it makes the travel times/routes more realistic.

close_network_holes_to_deadends(gdf, max_distance, hole_col='hole')[source]

Fills gaps between two deadends if the distance is less than ‘max_distance’.

Fills holes between deadends in the network with straight lines if the distance is less than ‘max_distance’.

Parameters:
  • gdf (GeoDataFrame) – GeoDataFrame with lines

  • max_distance (int | float) – The maximum distance between two nodes to be considered a hole.

  • hole_col (str | None) – If you want to keep track of which lines were added, you can add a column with a value of 1. Defaults to ‘hole’

Return type:

GeoDataFrame

Returns:

The input GeoDataFrame with new lines added.

Examples:

Read road data with small gaps.

>>> import sgis as sg
>>> roads = sg.read_parquet_url("https://media.githubusercontent.com/media/statisticsnorway/ssb-sgis/main/tests/testdata/roads_oslo_2022.parquet")

Roads need to be singlepart linestrings for this to work.

>>> from shapely import line_merge
>>> roads.geometry = line_merge(roads.geometry)

Check for number of isolated lines now.

>>> roads = sg.get_connected_components(roads)
>>> roads.connected.value_counts()
1.0    85638
0.0     7757
Name: connected, dtype: int64

Fill gaps shorter than 1.1 meters.

>>> filled = sg.close_network_holes_to_deadends(roads, max_distance=1.1)
>>> roads = sg.get_connected_components(roads)
>>> roads.connected.value_counts()
1.0    100315
0.0       180
Name: connected, dtype: int64

It’s not always wise to fill gaps. In the case of this data, these small gaps are intentional. They are road blocks where most cars aren’t allowed to pass. Fill the holes only if it makes the travel times/routes more realistic.