Skip to contents

The function vegnett_to_R can be used to convert the Norwegian road network, downloaded from Geonorge, to formats that enable network analysis in R (tbl_graph and cppRouting).

Usage

vegnett_to_R(
  vegnett,
  crs_out = 25833,
  year = 2022,
  fromnodeID = "FROMNODE",
  tonodeID = "TONODE",
  FT_minutes = "DRIVETIME_FW",
  TF_minutes = "DRIVETIME_BW",
  meters = "SHAPE_LENGTH",
  turn_restrictions = FALSE,
  ferry = TRUE
)

Arguments

vegnett

The Norwegian road network as an sf object, downloaded from Geonorge.

crs_out

Numeric vector with the chosen coordinate reference system (CRS). The default value is set to CRS 25833.

year

Numeric vector with the year the road network is from. Due to changes in the format of the files between 2021 and 2022, the most important thing is to choose between the "old" format (-2021) or the new format (2022-). The default value is set to 2022. Please see the example for the column names for 2021 and earlier.

fromnodeID

Character vector with the name of the column indicating the from node ID. Default value is set to FROMNODE (column name in 2022).

tonodeID

Character vector with the name of the column indicating the to node ID. Default value is set to TONODE (column name in 2022).

FT_minutes

Character vector with the name of the column that contains the cost in minutes from fromnodeID to tonodeID (FT). Default value is set to DRIVETIME_FW (column name in 2022).

TF_minutes

Character vector with the name of the column that contains the cost in minutes from tonodeID to fromnodeID (TF). Default value is set to DRIVETIME_BW (column name in 2022).

meters

Character vector with the name of the column that contains the cost in meters (equal for FT and TF). Default value is set to SHAPE_LENGTH (column name in 2022).

turn_restrictions

Logical. Default value is FALSE. If TRUE turn restrictions will be added to the road network. The turn restrictions layer from the road network file has to be loaded before this can be used (and the object has to be called turnrestrictions_geom). Due to errors in the turn restrictions file for 2022 it is not recommended to use this feature for now.

ferry

Logical/numeric vector. Default value is TRUE which means that all edges that involve ferries are given their original drive time (somewhere between 10 and 13 km/h). If a numeric value is supplied, the cost for all edges involving ferries will be converted to the supplied value in km/h.

Value

List containing the following elements:

[1] graph: the road network structured as a tidy graph (tbl_graph object).

[2] nodes: the road network's nodes (sf object).

[3] edges: road network's edges/node links (data.frame).

[4] graph_cppRouting_minutes: the road network structured as a cppRouting graph with the cost of travel in minutes (cppRouting object).

[5] graph_cppRouting_meters: the road network structured as a cppRouting graph with the cost of travel in meters (cppRouting object).

Examples

vegnett_sampledata
#> Simple feature collection with 1000 features and 45 fields
#> Geometry type: MULTILINESTRING
#> Dimension:     XYZ
#> Bounding box:  xmin: 248861 ymin: 6639120 xmax: 273377.4 ymax: 6672977
#> z_range:       zmin: -999999 zmax: 448.88
#> Projected CRS: ETRS89 / UTM zone 33N
#> # A tibble: 1,000 x 46
#>    ROUTEID FROM_M  TO_M FromNod~1 ToNod~2 GATEN~3 VEGTYPE ONEWAY FT_MI~4 TF_MI~5
#>    <chr>    <dbl> <dbl> <chr>     <chr>   <chr>   <chr>   <chr>    <dbl>   <dbl>
#>  1 1209471  0     0.807 1209475   2673001 Rv150   R       FT     0.0508  0.0508 
#>  2 1209471  0.807 1     2673001   1209476 Rv150   R       FT     0.0125  0.0125 
#>  3 1209472  0     0.218 1209477   2673002 Rv150   R       FT     0.0138  0.0138 
#>  4 1209472  0.218 1     2673002   1209478 Rv150   R       FT     0.0517  0.0517 
#>  5 1209473  0     0.129 627807    3066146 Rv150   R       FT     0.00755 0.00755
#>  6 1209473  0.129 1     3066146   1209475 Rv150   R       FT     0.0399  0.0399 
#>  7 1209474  0     0.828 1209478   3278307 Rv150   R       FT     0.0425  0.0425 
#>  8 1209474  0.828 1     3278307   627806  Rv150   R       FT     0.00883 0.00883
#>  9 1209419  0     1     1209425   1209426 Rv163   R       FT     0.0993  0.0993 
#> 10 1209420  0     1     1209426   1209427 Rv163   R       FT     0.174   0.174  
#> # ... with 990 more rows, 36 more variables: METER_ORG <dbl>, VEGNUMMER <int>,
#> #   FYLKE_ID <chr>, HIERARCHY <int>, HIERARCHY_FVK <int>, FuncRoadClass <int>,
#> #   VEGID <chr>, SPECIALVEG <chr>, URL <chr>, ATTRIBUTES <chr>,
#> #   FT_BOM_LIT <dbl>, FT_BOM_STO <dbl>, TF_BOM_LIT <dbl>, TF_BOM_STO <dbl>,
#> #   SPERRING <int>, DT_TURIST_FT <dbl>, DT_TURIST_TF <dbl>, DT_TRUCK_FT <dbl>,
#> #   DT_TRUCK_TF <dbl>, TOTALVIKT <dbl>, SK_HO_FT <dbl>, SK_HO_TF <dbl>,
#> #   VOGNTOGLENGDE <dbl>, RoadClass <int>, Medium <chr>, FT_Fart <int>, ...
vegnett_list <- vegnett_to_R(vegnett = vegnett_sampledata,
                             year = 2021,
                             fromnodeID = "FROMNODEID",
                             tonodeID = "TONODEID",
                             FT_minutes = "FT_MINUTES",
                             TF_minutes = "TF_MINUTES",
                             meters = "SHAPE_LENGTH")

graph <- vegnett_list[[1]]
nodes <- vegnett_list[[2]]
edges <- vegnett_list[[3]]
graph_cppRouting_minutes <- vegnett_list[[4]]
graph_cppRouting_meters <- vegnett_list[[5]]

graph
#> # A tbl_graph: 1159 nodes and 1741 edges
#> #
#> # A directed multigraph with 195 components
#> #
#> # Node Data: 1,159 x 50 (active)
#>   ROUTEID FROM_M  TO_M FROMNO~ TONODE~ GATENA~ VEGTYPE ONEWAY FT_MIN~ TF_MIN~
#>   <chr>    <dbl> <dbl> <chr>   <chr>   <chr>   <chr>   <chr>    <dbl>   <dbl>
#> 1 1214418      0     1 1662509 1214440 "priva~ P       B       0.0284  0.0284
#> 2 1214418      0     1 1662509 1214440 "priva~ P       B       0.0284  0.0284
#> 3 1214420      0     1 1214442 1662511 "Thorn~ P       B       0.0476  0.0476
#> 4 1214420      0     1 1214442 1662511 "Thorn~ P       B       0.0476  0.0476
#> 5 1214425      0     1 1214447 1662513 "priva~ P       B       0.0262  0.0262
#> 6 1214425      0     1 1214447 1662513 "priva~ P       B       0.0262  0.0262
#> # ... with 1,153 more rows, and 40 more variables: METER_ORG <dbl>,
#> #   VEGNUMMER <int>, FYLKE_ID <chr>, HIERARCHY <int>, HIERARCHY_FVK <int>,
#> #   FUNCROADCLASS <int>, VEGID <chr>, SPECIALVEG <chr>, URL <chr>,
#> #   ATTRIBUTES <chr>, FT_BOM_LIT <dbl>, FT_BOM_STO <dbl>, TF_BOM_LIT <dbl>,
#> #   TF_BOM_STO <dbl>, SPERRING <int>, DT_TURIST_FT <dbl>, DT_TURIST_TF <dbl>,
#> #   DT_TRUCK_FT <dbl>, DT_TRUCK_TF <dbl>, TOTALVIKT <dbl>, SK_HO_FT <dbl>,
#> #   SK_HO_TF <dbl>, VOGNTOGLENGDE <dbl>, ROADCLASS <int>, MEDIUM <chr>,
#> #   FT_FART <int>, TF_FART <int>, BRUKSKLASSE <chr>, BK_VINTER <chr>,
#> #   TOTVEKT_TOMMER <dbl>, LENGDE_TOMMER <dbl>, FROM_Z <dbl>, TO_Z <dbl>,
#> #   ISBIKEROAD <int>, SHAPE_LENGTH <dbl>, direction <chr>,
#> #   FROMNODEID_new <chr>, TONODEID_new <chr>, nodeID <int>, geometry <POINT
#> #   [m]>
#> #
#> # Edge Data: 1,741 x 52
#>    from    to ROUTEID FROM_M  TO_M FROMNO~ TONODE~ GATENA~ VEGTYPE ONEWAY
#>   <int> <int> <chr>    <dbl> <dbl> <chr>   <chr>   <chr>   <chr>   <chr> 
#> 1     1     2 1214418      0     1 1662509 1214440 "priva~ P       B     
#> 2     3     4 1214420      0     1 1214442 1662511 "Thorn~ P       B     
#> 3     5     6 1214425      0     1 1214447 1662513 "priva~ P       B     
#> # ... with 1,738 more rows, and 42 more variables: minutes <dbl>,
#> #   TF_MINUTES <dbl>, METER_ORG <dbl>, VEGNUMMER <int>, FYLKE_ID <chr>,
#> #   HIERARCHY <int>, HIERARCHY_FVK <int>, FUNCROADCLASS <int>, VEGID <chr>,
#> #   SPECIALVEG <chr>, URL <chr>, ATTRIBUTES <chr>, FT_BOM_LIT <dbl>,
#> #   FT_BOM_STO <dbl>, TF_BOM_LIT <dbl>, TF_BOM_STO <dbl>, SPERRING <int>,
#> #   DT_TURIST_FT <dbl>, DT_TURIST_TF <dbl>, DT_TRUCK_FT <dbl>,
#> #   DT_TRUCK_TF <dbl>, TOTALVIKT <dbl>, SK_HO_FT <dbl>, SK_HO_TF <dbl>,
#> #   VOGNTOGLENGDE <dbl>, ROADCLASS <int>, MEDIUM <chr>, FT_FART <int>,
#> #   TF_FART <int>, BRUKSKLASSE <chr>, BK_VINTER <chr>, TOTVEKT_TOMMER <dbl>,
#> #   LENGDE_TOMMER <dbl>, FROM_Z <dbl>, TO_Z <dbl>, ISBIKEROAD <int>,
#> #   meters <dbl>, geometry <LINESTRING [m]>, direction <chr>, edgeID <int>,
#> #   FROMNODEID_new <chr>, TONODEID_new <chr>
nodes
#> Simple feature collection with 1159 features and 2 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 248861 ymin: 6639120 xmax: 273374.5 ymax: 6672977
#> Projected CRS: ETRS89 / UTM zone 33N
#> First 10 features:
#>    nodeID membership                 geometry
#> 1       1          1 POINT (266382.6 6639605)
#> 2       2          1 POINT (266378.9 6639596)
#> 3       3          2 POINT (266349.2 6639372)
#> 4       4          2 POINT (266349.4 6639389)
#> 5       5          3 POINT (266360.5 6639120)
#> 6       6          3 POINT (266369.4 6639120)
#> 7       7          4 POINT (266351.3 6639416)
#> 8       8          4 POINT (266359.4 6639416)
#> 9       9          5 POINT (266548.5 6639482)
#> 10     10          5 POINT (266549.8 6639514)
head(edges)
#>   from to ROUTEID    FROM_M     TO_M FROMNODEID TONODEID
#> 1    1  2 1214418 0.0000000 1.000000    1662509  1214440
#> 2    3  4 1214420 0.0000000 1.000000    1214442  1662511
#> 3    5  6 1214425 0.0000000 1.000000    1214447  1662513
#> 4    7  8 1214427 0.0000000 1.000000    1214448  1662515
#> 5    9 10 1214430 0.2488051 0.311621    2030964  2717563
#> 6   10 11 1214430 0.3116210 0.357439    2717563  1906573
#>                    GATENAVN VEGTYPE ONEWAY    minutes TF_MINUTES METER_ORG
#> 1                privat veg       P      B 0.02842094 0.02842094  9.666985
#> 2 Thorn D<U+00F8>nhaugs Vei       P      B 0.04759177 0.04759177 16.187677
#> 3                privat veg       P      B 0.02618001 0.02618001  8.904767
#> 4 Thorn D<U+00F8>nhaugs Vei       P      B 0.02397816 0.02397816  8.155837
#> 5              kommunal veg       K      B 0.09209707 0.09209707 31.540092
#> 6              kommunal veg       K      B 0.06537617 0.06537617 22.389101
#>   VEGNUMMER FYLKE_ID HIERARCHY HIERARCHY_FVK FUNCROADCLASS    VEGID SPECIALVEG
#> 1     81795        3         3             3             7 {P81795}           
#> 2     20496        3         3             3             7 {P20496}           
#> 3     81754        3         3             3             7 {P81754}           
#> 4     20496        3         3             3             7 {P20496}           
#> 5     81753        3         3             3            -1 {K81753}           
#> 6     81753        3         3             3            -1 {K81753}           
#>   URL ATTRIBUTES FT_BOM_LIT FT_BOM_STO TF_BOM_LIT TF_BOM_STO SPERRING
#> 1                         0          0          0          0       -1
#> 2                         0          0          0          0       -1
#> 3                         0          0          0          0       -1
#> 4                         0          0          0          0       -1
#> 5                         0          0          0          0       -1
#> 6                         0          0          0          0       -1
#>   DT_TURIST_FT DT_TURIST_TF DT_TRUCK_FT DT_TRUCK_TF TOTALVIKT SK_HO_FT SK_HO_TF
#> 1   0.02842094   0.02842094          NA          NA        NA       NA       NA
#> 2   0.04759177   0.04759177          NA          NA        NA       NA       NA
#> 3   0.02618001   0.02618001          NA          NA        NA       NA       NA
#> 4   0.02397816   0.02397816          NA          NA        NA       NA       NA
#> 5   0.09209707   0.09209707          NA          NA        NA       NA       NA
#> 6   0.06537617   0.06537617          NA          NA        NA       NA       NA
#>   VOGNTOGLENGDE ROADCLASS MEDIUM FT_FART TF_FART    BRUKSKLASSE BK_VINTER
#> 1            NA         0             30      30                       -1
#> 2            NA         0             30      30                       -1
#> 3            NA         0             30      30                       -1
#> 4            NA         0             30      30                       -1
#> 5            NA         0             30      30 Bk10 - 50 tonn        -1
#> 6            NA         0             30      30 Bk10 - 50 tonn        -1
#>   TOTVEKT_TOMMER LENGDE_TOMMER   FROM_Z     TO_Z ISBIKEROAD    meters
#> 1             -1          -1.0 160.7000 160.7000          0  9.666985
#> 2             -1          -1.0 171.1600 171.1600          0 16.187677
#> 3             -1          -1.0 172.5704 172.5704          0  8.904767
#> 4             -1          -1.0 169.4539 169.5000          0  8.155837
#> 5             -1          19.5 179.8200 179.8440          1 31.540092
#> 6             -1          19.5 179.8440 179.6700          1 22.389101
#>                         geometry direction edgeID FROMNODEID_new TONODEID_new
#> 1 LINESTRING (266382.6 663960...      B_FT      1        1662509      1214440
#> 2 LINESTRING (266349.2 663937...      B_FT      2        1214442      1662511
#> 3 LINESTRING (266360.5 663912...      B_FT      3        1214447      1662513
#> 4 LINESTRING (266351.3 663941...      B_FT      4        1214448      1662515
#> 5 LINESTRING (266548.5 663948...      B_FT      5        2030964      2717563
#> 6 LINESTRING (266549.8 663951...      B_FT      6        2717563      1906573
head(graph_cppRouting_minutes$data)
#>   from  to       dist
#> 1    0 837 0.02842094
#> 2    1 838 0.04759177
#> 3    2 839 0.02618001
#> 4    3 840 0.02397816
#> 5    4   5 0.09209707
#> 6    5 841 0.06537617
head(graph_cppRouting_minutes$coords)
#>    nodeID        X       Y
#> 1       1 266382.6 6639605
#> 3       3 266349.2 6639372
#> 5       5 266360.5 6639120
#> 7       7 266351.3 6639416
#> 9       9 266548.5 6639482
#> 10     10 266549.8 6639514
head(graph_cppRouting_minutes$dict)
#>   ref id
#> 1   1  0
#> 2   3  1
#> 3   5  2
#> 4   7  3
#> 5   9  4
#> 6  10  5
graph_cppRouting_minutes$nbnode
#> [1] 1159

head(graph_cppRouting_meters$data)
#>   from  to      dist
#> 1    0 837  9.666985
#> 2    1 838 16.187677
#> 3    2 839  8.904767
#> 4    3 840  8.155837
#> 5    4   5 31.540092
#> 6    5 841 22.389101
head(graph_cppRouting_meters$coords)
#>    nodeID        X       Y
#> 1       1 266382.6 6639605
#> 3       3 266349.2 6639372
#> 5       5 266360.5 6639120
#> 7       7 266351.3 6639416
#> 9       9 266548.5 6639482
#> 10     10 266549.8 6639514
head(graph_cppRouting_meters$dict)
#>   ref id
#> 1   1  0
#> 2   3  1
#> 3   5  2
#> 4   7  3
#> 5   9  4
#> 6  10  5
graph_cppRouting_meters$nbnode
#> [1] 1159