Title: | Network Analysis on the Norwegian Road Network |
---|---|
Description: | A collection of GIS (Geographic Information System) functions in R, created for use in Statistics Norway. The functions are primarily related to network analysis on the Norwegian road network. |
Authors: | Sindre Mikael Haugen [aut, cre] |
Maintainer: | Sindre Mikael Haugen <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.1 |
Built: | 2024-10-21 04:17:57 UTC |
Source: | https://github.com/statisticsnorway/gissb |
The function address_to_coords
can be used to find coordinates to supplied Norwegian addresses. Internet access is required as the function utilizes the Norwegian Mapping Authority’s address API.
address_to_coords(zip_code, address, format = "sf", crs_out = 25833)
address_to_coords(zip_code, address, format = "sf", crs_out = 25833)
zip_code |
Character vector with zip codes. |
address |
Character vector with addresses (street name and house number). |
format |
Format of the returned object. Default value is set to “sf” (which returns an |
crs_out |
Numeric vector with the chosen coordinate reference system (CRS). Default value is set to |
If there are no coordinates found for the supplied address it means that it does not exist in Matrikkelen - Norway's official property register. See www.rettikartet.no to search for existing addresses.
Object with coordinates to the supplied addresses.
address_to_coords(zip_code = "0185", address = "Schweigaards gate 10")
address_to_coords(zip_code = "0185", address = "Schweigaards gate 10")
The function coords_to_google
can be used to convert coordinates of an sf
object to a format that is easy to copy and paste into Google Maps.
coords_to_google(coords, crs_out = 25833)
coords_to_google(coords, crs_out = 25833)
coords |
An |
crs_out |
Chosen coordinate reference system (CRS) for the geometry column of the returned |
An sf
object with a new column added (coords_google
), and an addtional sf
geometry column with the chosen CRS.
address_to_coords(zip_code = "0185", address = "Schweigaards gate 10") %>% coords_to_google()
address_to_coords(zip_code = "0185", address = "Schweigaards gate 10") %>% coords_to_google()
The function coords_to_node
can be used to find the nearest nodes in the Norwegian road network (in meters) for chosen coordinates.
coords_to_node( coords, nodes_object = nodes, edges_object = edges, direction = "from", ID_col = "ID", crs_out = 25833, knn = 1, membership = FALSE )
coords_to_node( coords, nodes_object = nodes, edges_object = edges, direction = "from", ID_col = "ID", crs_out = 25833, knn = 1, membership = FALSE )
coords |
An |
nodes_object |
An |
edges_object |
A data frame with the edges of the road network. This can be created with the function |
direction |
Character vector with |
ID_col |
Character vector with the name of the ID column. Default value is set to “ID”. |
crs_out |
Numeric vector for the chosen coordinate reference system (CRS). |
knn |
Numeric vector with the chosen number of nodes that should be returned for each of the coordinates. If |
membership |
Logical. If |
Before the function can be used, the nodes of the road network must be converted to an sf
object that is called nodes
(or another name supplied to the nodes_object
argument). This can be done with the function vegnett_to_R()
.
An object (data.frame
) with the following columns; from_nodeID
/to_nodeID
, membership_from_node
/membership_to_node
, coords_google_from_node
/coords_google_to_node
, knn_from_node
/knn_to_node
, and ID
.
coords <- address_to_coords(zip_code = "0185", address = "Schweigaards gate 10") coords_to_node(coords = coords, direction = "from", nodes_object = nodes_sampledata, edges_object = edges_sampledata)
coords <- address_to_coords(zip_code = "0185", address = "Schweigaards gate 10") coords_to_node(coords = coords, direction = "from", nodes_object = nodes_sampledata, edges_object = edges_sampledata)
Sample of the Norwegian Road Network (vegnettRuteplan_FGDB_20210528.gdb
) downloaded from Geonorge. Only the first 1000 rows from Oslo county are included.
data(edges_sampledata)
data(edges_sampledata)
A data frame
National roads database - road network for routing
data(edges_sampledata) class(edges_sampledata) head(edges_sampledata)
data(edges_sampledata) class(edges_sampledata) head(edges_sampledata)
Sample of the Norwegian Road Network (vegnettRuteplan_FGDB_20210528.gdb
) downloaded from Geonorge. Only the first 1000 rows from Oslo county are included.
data(graph_cppRouting_meters_sampledata)
data(graph_cppRouting_meters_sampledata)
A cppRouting
object
National roads database - road network for routing
data(graph_cppRouting_meters_sampledata) class(graph_cppRouting_meters_sampledata)
data(graph_cppRouting_meters_sampledata) class(graph_cppRouting_meters_sampledata)
Sample of the Norwegian Road Network (vegnettRuteplan_FGDB_20210528.gdb
) downloaded from Geonorge. Only the first 1000 rows from Oslo county are included.
data(graph_cppRouting_minutes_sampledata)
data(graph_cppRouting_minutes_sampledata)
A cppRouting
object
National roads database - road network for routing
data(graph_cppRouting_minutes_sampledata) class(graph_cppRouting_minutes_sampledata)
data(graph_cppRouting_minutes_sampledata) class(graph_cppRouting_minutes_sampledata)
Sample of the Norwegian Road Network (vegnettRuteplan_FGDB_20210528.gdb
) downloaded from Geonorge. Only the first 1000 rows from Oslo county are included.
data(graph_sampledata)
data(graph_sampledata)
An igraph
object.
National roads database - road network for routing
data(graph_sampledata) class(graph_sampledata)
data(graph_sampledata) class(graph_sampledata)
Sample of the Norwegian Road Network (vegnettRuteplan_FGDB_20210528.gdb
) downloaded from Geonorge. Only the first 1000 rows from Oslo county are included.
data(nodes_sampledata)
data(nodes_sampledata)
An object of class sf
.
National roads database - road network for routing
data(nodes_sampledata) class(nodes_sampledata) head(nodes_sampledata)
data(nodes_sampledata) class(nodes_sampledata) head(nodes_sampledata)
The function path_leaflet
visualizes the shortest path (in minutes or meters) that has been calculated with the function shortest_path_igraph()
(where path = TRUE
).
Internet connection is required to load the background tiles.
path_leaflet(path, graph_object = graph)
path_leaflet(path, graph_object = graph)
path |
Object ( |
graph_object |
The road network structured as a tidy graph ( |
Interactive Leaflet map that shows the shortest path (in minutes or meters) between a chosen from and to node in the road network.
shortest_path_igraph(from_node_ID = 25, to_node_ID = 33, unit = "minutes", path = TRUE, graph_object = graph_sampledata) %>% path_leaflet(graph_object = graph_sampledata)
shortest_path_igraph(from_node_ID = 25, to_node_ID = 33, unit = "minutes", path = TRUE, graph_object = graph_sampledata) %>% path_leaflet(graph_object = graph_sampledata)
The function shortest_path_cppRouting
can be used to calculate the shortest path (either in minutes or meters) between two or more nodes in the Norwegian road network. The function also works with vectors with multiple from and to node ID’s.
Before the function can be used, the road network must be converted to a cppRouting
object that is called graph_cppRouting_minutes
or graph_cppRouting_meters
(or other with other names supplied to the graph_cppRouting_object
argument). This can be done with the function vegnett_to_R()
.
shortest_path_cppRouting( from_node_ID, to_node_ID, unit = "minutes", dist = "all", graph_cppRouting_object = graph_cppRouting_minutes )
shortest_path_cppRouting( from_node_ID, to_node_ID, unit = "minutes", dist = "all", graph_cppRouting_object = graph_cppRouting_minutes )
from_node_ID |
Numeric vector with one or more from node ID’s. |
to_node_ID |
Numeric vector with one or more to node ID’s. |
unit |
Character vector with |
dist |
Character vector that specifies if all the shortest paths between all the supplied from and to nodes are returned ( |
graph_cppRouting_object |
The road network structured as a |
Object (data.frame
) with how many minutes or meters the shortest path is between the supplied from and to node ID’s.
shortest_path_cppRouting(from = 25, to = 33, unit = "minutes", graph_cppRouting_object = graph_cppRouting_minutes_sampledata) shortest_path_cppRouting(from = 25, to = 33, unit = "meters", graph_cppRouting_object = graph_cppRouting_meters_sampledata) shortest_path_cppRouting(from = 25, to = c(32, 33), unit = "minutes", dist = "min", graph_cppRouting_object = graph_cppRouting_minutes_sampledata)
shortest_path_cppRouting(from = 25, to = 33, unit = "minutes", graph_cppRouting_object = graph_cppRouting_minutes_sampledata) shortest_path_cppRouting(from = 25, to = 33, unit = "meters", graph_cppRouting_object = graph_cppRouting_meters_sampledata) shortest_path_cppRouting(from = 25, to = c(32, 33), unit = "minutes", dist = "min", graph_cppRouting_object = graph_cppRouting_minutes_sampledata)
The function shortest_path_igraph
can be used to calculate the shortest path (either in minutes or meters) between nodes in the Norwegian road network. The function can also return the node link, i.e. path, that the shortest path consists of.
shortest_path_igraph( from_node_ID, to_node_ID, graph_object = graph, unit = "minutes", path = FALSE )
shortest_path_igraph( from_node_ID, to_node_ID, graph_object = graph, unit = "minutes", path = FALSE )
from_node_ID |
Numeric value with the from node ID (if multiple node ID’s are to be used, see the function |
to_node_ID |
Numeric value with the to node ID (if multiple node ID’s are to be used, see the function |
graph_object |
The road network structured as a tidy graph ( |
unit |
Character vector with |
path |
Logical. If |
Before the function can be used, the road network must be converted to a tbl_graph
object. This can be done with the function vegnett_to_R()
). There objects graph
and edges
needs to be loaded.
Vector with the shortest path in minutes or meters. If path = TRUE
the node link that the shortest path consists of is returned.
shortest_path_igraph(from_node_ID = 25, to_node_ID = 33, unit = "minutes", graph_object = graph_sampledata) shortest_path_igraph(from_node_ID = 25, to_node_ID = 33, unit = "meters", graph_object = graph_sampledata) shortest_path_igraph(from_node_ID = 25, to_node_ID = 33, unit = "minutes", path = TRUE, graph_object = graph_sampledata)
shortest_path_igraph(from_node_ID = 25, to_node_ID = 33, unit = "minutes", graph_object = graph_sampledata) shortest_path_igraph(from_node_ID = 25, to_node_ID = 33, unit = "meters", graph_object = graph_sampledata) shortest_path_igraph(from_node_ID = 25, to_node_ID = 33, unit = "minutes", path = TRUE, graph_object = graph_sampledata)
Sample of the Norwegian Road Network (vegnettRuteplan_FGDB_20210528.gdb
) downloaded from Geonorge. Only the first 1000 rows from Oslo county are included.
data(vegnett_sampledata)
data(vegnett_sampledata)
An object of class sf
.
National roads database - road network for routing
data(vegnett_sampledata) class(vegnett_sampledata) head(vegnett_sampledata)
data(vegnett_sampledata) class(vegnett_sampledata) head(vegnett_sampledata)
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
).
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 )
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 )
vegnett |
The Norwegian road network as an |
crs_out |
Numeric vector with the chosen coordinate reference system (CRS). The default value is set to |
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 |
tonodeID |
Character vector with the name of the column indicating the to node ID. Default value is set to |
FT_minutes |
Character vector with the name of the column that contains the cost in minutes from |
TF_minutes |
Character vector with the name of the column that contains the cost in minutes from |
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 |
turn_restrictions |
Logical. Default value is |
ferry |
Logical/numeric vector. Default value is |
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).
vegnett_sampledata 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 nodes head(edges) head(graph_cppRouting_minutes$data) head(graph_cppRouting_minutes$coords) head(graph_cppRouting_minutes$dict) graph_cppRouting_minutes$nbnode head(graph_cppRouting_meters$data) head(graph_cppRouting_meters$coords) head(graph_cppRouting_meters$dict) graph_cppRouting_meters$nbnode
vegnett_sampledata 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 nodes head(edges) head(graph_cppRouting_minutes$data) head(graph_cppRouting_minutes$coords) head(graph_cppRouting_minutes$dict) graph_cppRouting_minutes$nbnode head(graph_cppRouting_meters$data) head(graph_cppRouting_meters$coords) head(graph_cppRouting_meters$dict) graph_cppRouting_meters$nbnode