-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhexagon.R
150 lines (114 loc) · 4.49 KB
/
hexagon.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
library("sf")
library("tidyverse")
library("geodata")
library("parallel")
library("osrm")
library("cppRouting")
library("data.table")
# Setup --------------------------------------------------------------------
osrm_folder <- "~/osrm/osrm-backend"
n <- 10
set.seed(10)
extract <- FALSE
contract <- FALSE
cores <- parallel::detectCores()
nb <- function(cell) {
lapply(seq_along(cell), function(c) {
# Gather target cells
data.table(
from = cell[c],
to = unlist(nb_contiguity[cell[c]]),
cost = unlist(nb_durations[cell[c]])
)
}) %>%
rbindlist()
}
# Download data -----------------------------------------------------------
dir.create("./data", recursive = TRUE, showWarnings = FALSE)
osm_pbf_file <- "http://download.geofabrik.de/europe/malta-latest.osm.pbf"
if (!file.exists(file.path(".", "data", basename(osm_pbf_file)))) {
download.file(osm_pbf_file,
destfile = file.path(".", "data", basename(osm_pbf_file)))
}
shp_file <- "http://download.geofabrik.de/europe/malta-latest-free.shp.zip"
if (!file.exists(file.path(".", "data", basename(shp_file)))) {
download.file(shp_file,
destfile = file.path(".", "data", basename(shp_file)))
}
outline <- geodata::gadm(country = "MLT", level = 0, path = "./data") %>%
st_as_sf()
# Preprocess OSRM ---------------------------------------------------------
## extract -----------------------------------------------------------------
if (extract) {
system(paste("osrm-extract", file.path(".", "data", basename(osm_pbf_file)),
"-p", paste0(osrm_folder, "/profiles/bicycle.lua")))
}
## contract ----------------------------------------------------------------
osrm_file <- file.path(".", "data", str_replace(basename(osm_pbf_file),
".osm.pbf$", ".osrm"))
if (contract) {
system(paste("osrm-contract", osrm_file))
}
## run server --------------------------------------------------------------
system(paste("osrm-routed", osrm_file, "--max-matching-radius=-1"), wait = F)
# Build sample data set -------------------------------------------------------
points <- st_sample(x = outline, size = n) %>%
st_sfc() %>%
st_as_sf()
# Hexagon Approach -------------------------------------------------------
## Build Gird --------------------------------------------------------------
hex_grid <- st_as_sf(sf::st_make_grid(st_union(outline),
square = F, flat_topped = F,
cellsize = 0.01)) %>%
st_transform(crs = 4326) %>%
rename(geometry = x) %>%
mutate(area = units::set_units(st_area(.),"km^2"))
# Average Distance beween Grid Points
dist <- as.numeric(units::set_units(2*(sqrt(3)/2) * sqrt((2*mean(hex_grid$area))/(3*sqrt(3))),"m"))
# Create Centroids
hex_points <- st_centroid(hex_grid)
# Intersect Grid with Country Outline
hex_grid <- hex_grid[st_intersects(hex_grid,st_union(outline), sparse = FALSE),] %>%
mutate(hexid = row_number())
rownames(hex_grid) <- NULL
# Restrict Hex-Points to Hex Grid
hex_points <- hex_points %>%
dplyr::select(-area) %>%
sf::st_join(hex_grid,join = st_within) %>%
dplyr::filter(!is.na(hexid))
ggplot()+
geom_sf(data = hex_grid)+
geom_sf(data = outline, fill = NA)
## Get Neighbor dist/dura ----------------------------------------------------
nb_contiguity <- st_touches(hex_grid)
t0 <- Sys.time()
nb_durations <- lapply(1:length(nb_contiguity), function(n) {
dura <- osrmTable(src = hex_points[n,],
dst = hex_points[nb_contiguity[[n]], ],
osrm.server = "http://0.0.0.0:5000/")
return(dura$durations)
})
t1 <- Sys.time()
print(t1 - t0)
# Convert to graph, make isochrones -----------------------------------------
db <- mclapply(1:nrow(hex_grid), nb, mc.cores = cores) %>%
rbindlist()
db[is.na(cost), cost:= 9998]
# Convert into edge-weighted, directed graph
graph <- cppRouting::makegraph(df = db, directed = T)
t0 <- Sys.time()
isochrone_cells <- mclapply(1:nrow(hex_grid), function(i) {
unlist(cppRouting::get_isochrone(Graph = graph, from = i,lim = 15))
}, mc.cores = cores)
t1 <- Sys.time()
print(t1 - t0)
isochrone_count <- lapply(isochrone_cells, length)
hex_grid$accessibility <- unlist(isochrone_count)
ggplot()+
geom_sf(data = hex_grid, aes(fill = accessibility))+
geom_sf(data = outline, fill = NA)
c <- c(10,20)
ggplot()+
geom_sf(data = hex_grid[unlist(isochrone_cells[c]),], fill = "#00ff00", alpha = 0.5)+
geom_sf(data = hex_grid[c,], fill = "#ff0000")+
geom_sf(data = outline, fill = NA)