-
Notifications
You must be signed in to change notification settings - Fork 0
/
2_network_construction_and_cleaning.R
101 lines (66 loc) · 2.07 KB
/
2_network_construction_and_cleaning.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# load the libraries
library(sfnetworks)
library(sf)
library(tidygraph)
library(igraph)
library(ggplot2)
library(sp)
library(here)
library(tmap)
library(dplyr)
###### CONSTRUCT THE NETWORK #######
### DRIVEABLE NETWORK
# read in shp downloaded with osmnx for ULEZ21 in London
edgesShpDrive <- st_read(here("data",
'driving',
"edges.shp"))
# length is a built in function in R so need to rename it,
# otherwise it will throw error while calculating centralities
edgesShpDrive <- edgesShpDrive %>%
dplyr::rename(
weight = length)
# check for null values in street lengths
sum(is.na(edgesShpDrive$weight))
sum(is.infinite(edgesShpDrive$weight))
# no null values, no infinites
# check the type and a class
typeof(edgesShpDrive$weight)
class(edgesShpDrive$weight)
# choose only the columns I want to use
edgesShpDrive2 <- edgesShpDrive %>%
select(from, to, name, weight, maxspeed, geometry)
# construct a network out of it
gDrive <- as_sfnetwork(edgesShpDrive2, directed = FALSE)
gDrive
# check the network
# class(gDrive)
# gDrive
# extract nodes and edges to an sf object
edgesDrive <- gDrive %>%
activate("edges") %>%
st_as_sf()
nodesDrive <- gDrive %>%
activate("nodes") %>%
st_as_sf()
####### NETWORK CLEANING AND PRE-PROCESSING #######
# based on this post https://luukvdmeer.github.io/sfnetworks/articles/preprocess_and_clean.html
### simplify network
# get rid of multiple and edges (edges that connect the same pair of nodes,
# or start and end at the same node)
# this is optional as OSMnx performs topological correction and simplification automatically
gDriveSimple = gDrive %>%
activate("edges") %>%
arrange(edge_length()) %>%
filter(!edge_is_multiple()) %>%
filter(!edge_is_loop())
### smooth pseudo nodes
# get rid of pseudo nodes
gDriveSimple = convert(gDriveSimple, to_spatial_smooth)
gDriveSimple
# extract nodes and edges to an sf object
edgesDriveSimple <- gDriveSimple %>%
activate("edges") %>%
st_as_sf()
nodesDriveSimple <- gDriveSimple %>%
activate("nodes") %>%
st_as_sf()