Make directed network

Prepare a GeoDataFrame of line geometries for directed network analysis.

make_directed_network(gdf, direction_col, direction_vals_bft, dropna=None, dropnegative=None, minute_cols=None, speed_col_kmh=None, flat_speed_kmh=None, reverse_tofrom=True)[source]

Flips the line geometries of roads going backwards and in both directions.

Parameters:
  • gdf (GeoDataFrame) – GeoDataFrame of line geometries.

  • direction_col (str) – name of column specifying the direction of the line geometry.

  • direction_vals_bft (tuple[str, str, str]) – tuple or list with the values of the direction column. Must be in the order ‘both directions’, ‘from’, ‘to’. E.g. (‘B’, ‘F’, ‘T’).

  • dropna (bool | None) – When minutes_cols is set, whether to drop rows with missing values in both columns. Alternatively, set these rows to a positive value before making the directed network.

  • dropnegative (bool | None) – When minutes_cols is set, whether to drop rows with negative values in both columns. Not dropping them will cause an error when building a network graph. Alternatively, set these rows to a positive value before making the directed network.

  • minute_cols (optional) – column or columns containing the number of minutes it takes to traverse the line. If one column name is given, this will be used for both directions. If tuple/list with two column names, the first column will be used as the minute column for the forward direction, and the second column for roads going backwards.

  • speed_col_kmh (optional) – name of column with the road speed limit.

  • flat_speed_kmh (optional) – Speed in kilometers per hour to use as the speed for all roads.

  • reverse_tofrom (bool) – If the geometries of the lines going backwards (i.e. has the last value in ‘direction_vals_bft’). Defaults to True.

Return type:

GeoDataFrame

Returns:

The Network class, with the network attribute updated with flipped

geometries for lines going backwards and both directions.

Adds the column ‘minutes’ if either ‘speed_col_kmh’, ‘minute_col’ or

’flat_speed_kmh’ is specified.

Raises:

ValueError – If ‘flat_speed_kmh’ or ‘speed_col_kmh’ is specified and the unit of the coordinate reference system is not ‘metre’.

make_directed_network_norway(gdf, dropnegative)[source]

Runs the method make_directed_network for Norwegian road data.

The parameters in make_directed_network are set to the correct values for Norwegian road data as of 2023.

The data can be downloaded here: https://kartkatalog.geonorge.no/metadata/nvdb-ruteplan-nettverksdatasett/8d0f9066-34f9-4423-be12-8e8523089313

Parameters:
  • gdf (GeoDataFrame) – Road GeoDataFrame.

  • dropnegative (bool) – Whether to keep rows with negative minute values in both directions. These are mostly circles with no impact on the analyses, but there might be exceptions. Keeping negative values will result in an error when building the network graph. Recode these rows to a non-negative values if you want to keep them.

Return type:

GeoDataFrame

Examples:

2022 data for the municipalities of Oslo and Eidskog can be read directly like this:

>>> 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[["oneway", "drivetime_fw", "drivetime_bw", "geometry"]]
        oneway  drivetime_fw  drivetime_bw                                           geometry
119702       B      0.216611      0.216611  MULTILINESTRING Z ((258028.440 6674249.890 413...
199710      FT      0.099323     -1.000000  MULTILINESTRING Z ((271778.700 6653238.900 138...
199725      FT      0.173963     -1.000000  MULTILINESTRING Z ((271884.510 6653207.540 142...
199726      FT      0.011827     -1.000000  MULTILINESTRING Z ((271884.510 6653207.540 142...
199733      FT      0.009097     -1.000000  MULTILINESTRING Z ((271877.373 6653214.697 141...
...        ...           ...           ...                                                ...
1944129     FT      0.010482     -1.000000  MULTILINESTRING Z ((268649.503 6651869.320 111...
1944392      B      0.135561      0.135561  MULTILINESTRING Z ((259501.570 6648459.580 3.2...
1944398      B      0.068239      0.068239  MULTILINESTRING Z ((258292.600 6648313.440 18....
1944409      B      0.023629      0.023629  MULTILINESTRING Z ((258291.452 6648289.258 19....
1944415      B      0.175876      0.175876  MULTILINESTRING Z ((260762.830 6650240.620 43....

[93395 rows x 4 columns]

And converted to a directed network like this:

>>> roads_directed = sg.make_directed_network_norway(roads, dropnegative=True)
>>> roads_directed[["minutes", "geometry"]]
         minutes                                           geometry
0       0.216611  MULTILINESTRING Z ((258028.440 6674249.890 413...
1       0.028421  MULTILINESTRING Z ((266382.600 6639604.600 -99...
2       0.047592  MULTILINESTRING Z ((266349.200 6639372.499 171...
3       0.026180  MULTILINESTRING Z ((266360.515 6639120.493 172...
4       0.023978  MULTILINESTRING Z ((266351.263 6639416.154 169...
...          ...                                                ...
175620  0.007564  MULTILINESTRING Z ((268641.225 6651871.624 111...
175621  0.020246  MULTILINESTRING Z ((268669.748 6651890.291 110...
175622  0.036810  MULTILINESTRING Z ((268681.757 6651886.457 110...
175623  0.003019  MULTILINESTRING Z ((268682.748 6651886.162 110...
175624  0.036975  MULTILINESTRING Z ((268694.594 6651881.688 111...

[175541 rows x 2 columns]