# library(stplanr)
::load_all()
devtoolslibrary(dplyr)
library(tmap)
library(ggplot2)
library(tmaptools)
sf::read_sf("https://github.com/ropensci/stplanr/releases/download/v1.0.2/rnet_x_ed.geojson")
rnet_x = sf::read_sf("https://github.com/ropensci/stplanr/releases/download/v1.0.2/rnet_y_ed.geojson")
rnet_y =# dups = duplicated(rnet_x$geometry)
# summary(dups)
# rnet_x = rnet_x |>
# filter(!dups)
# sf::write_sf(rnet_x, "~/github/ropensci/stplanr/rnet_x_ed.geojson", delete_dsn = TRUE)
We pre-processed the input simple geometry to make it even simpler as shown below.
# tmap_mode("view")
# nrow(rnet_x)
# summary(sf::st_length(rnet_x))
plot(sf::st_geometry(rnet_x))
rnet_subset(rnet_x, rnet_y, dist = 20)
rnet_x =# nrow(rnet_x)
# plot(sf::st_geometry(rnet_x))
rnet_subset(rnet_x, rnet_y, dist = 20, min_length = 5)
rnet_x =# summary(sf::st_length(rnet_x))
# nrow(rnet_x)
# plot(sf::st_geometry(rnet_x))
rnet_subset(rnet_x, rnet_y, dist = 20, rm_disconnected = TRUE)
rnet_x =# nrow(rnet_x)
plot(sf::st_geometry(rnet_x))
The initial merged result was as follows (original data on left)
list(value = sum, Quietness = mean)
funs = c(0, 100, 500, 1000, 5000)
brks =system.time({
rnet_merge(rnet_x, rnet_y, dist = 10, segment_length = 20, funs = funs)
rnet_merged =
}) tm_shape(rnet_y) + tm_lines("value", palette = "viridis", lwd = 5, breaks = brks) +
m1 = tm_scale_bar()
tm_shape(rnet_merged) + tm_lines("value", palette = "viridis", lwd = 5, breaks = brks)
m2 =tmap_arrange(m1, m2, sync = TRUE, nrow = 1)
Speed-up the results by transforming to a projected coordinate system:
sf::st_transform(rnet_x, 27700)
rnet_x = sf::st_transform(rnet_y, 27700) rnet_y =
line_segment(rnet_y, segment_length = 20, use_rsgeo = TRUE)
rnet_y_segmented =system.time({
rnet_merge(rnet_x, rnet_y, dist = 10, segment_length = 20, funs = funs)
rnet_merged2 = })
Let’s check the results:
names(rnet_merged)
summary(rnet_merged$value)
summary(rnet_y$value)
sum(rnet_merged$value * sf::st_length(rnet_merged), na.rm = TRUE)
sum(rnet_y$value * sf::st_length(rnet_y), na.rm = TRUE)
We can more reduce the minimum segment length to ensure fewer NA values in the outputs:
rnet_merge(rnet_x, rnet_y, dist = 20, segment_length = 10, funs = funs)
rnet_merged = tm_shape(rnet_y) + tm_lines("value", palette = "viridis", lwd = 5, breaks = brks)
m1 = tm_shape(rnet_merged) + tm_lines("value", palette = "viridis", lwd = 5, breaks = brks)
m2 =tmap_arrange(m1, m2, sync = TRUE, nrow = 1)
As shown in the results, some sideroad values have unrealistically high values:
Let’s see the results again:
summary(rnet_merged$value)
summary(rnet_y$value)
sum(rnet_merged$value * sf::st_length(rnet_merged), na.rm = TRUE)
sum(rnet_y$value * sf::st_length(rnet_y), na.rm = TRUE)
The good news: the number of NAs is down to only 21 compared with the previous 100+. Bad news: sideroads have been assigned values from the main roads.
We can fix this with the max_angle_diff
argument:
rnet_merge(rnet_x, rnet_y, dist = 20, segment_length = 10, funs = funs, max_angle_diff = 20)
rnet_merged = tm_shape(rnet_y) + tm_lines("value", palette = "viridis", lwd = 5, breaks = brks)
m1 = tm_shape(rnet_merged) + tm_lines("value", palette = "viridis", lwd = 5, breaks = brks)
m2 =tmap_arrange(m1, m2, sync = TRUE, nrow = 1)
As shown in the results, the sideroad values are fixed:
Let’s see the results again:
summary(rnet_merged$value)
summary(rnet_y$value)
sum(rnet_merged$value * sf::st_length(rnet_merged), na.rm = TRUE)
sum(rnet_y$value * sf::st_length(rnet_y), na.rm = TRUE)
It also works with charaster strings:
$char = paste0("road", sample(1:3, nrow(rnet_y), replace = TRUE))
rnet_y function(x) {
most_common = unique(x)
ux =which.max(tabulate(match(x, ux)))]
ux[
} list(char = most_common)
funs =system.time({
rnet_merge(rnet_x, rnet_y, dist = 10, segment_length = 20, funs = funs)
rnet_merged =
})plot(rnet_y["char"])
plot(rnet_merged["char"])
Now let’s testing on 3km dataset
sf::read_sf("https://github.com/nptscot/networkmerge/releases/download/v0.1/os_3km.geojson")
rnet_x = sf::read_sf("https://github.com/nptscot/npt/releases/download/rnet_3km_buffer/rnet_3km_buffer.geojson") rnet_y =
Read columns from rnet_y to assign functions to them
# Extract column names from the rnet_x data frame
names(rnet_y)
name_list <-
name_list# Initialize an empty list
list()
funs <-
# Loop through each name and assign it a function based on specific conditions
for (name in name_list) {
if (name == "geometry") {
next # Skip the current iteration
else if (name %in% c("Gradient", "Quietness")) {
} mean
funs[[name]] <-else {
} sum
funs[[name]] <-
} }
c(0, 100, 500, 1000, 5000,10000)
brks = c("green", "yellow", "blue", "purple", "red")
colors <- rnet_merge(rnet_x, rnet_y, dist = 20, segment_length = 10, funs = funs, max_angle_diff = 20)
rnet_merged =# st_write(rnet_merged, "data-raw/3km_exmaple_merged.geojson", driver = "GeoJSON")
st_make_valid(rnet_merged)
rnet_merged <- tm_shape(rnet_y) + tm_lines("all_fastest_bicycle", palette = "viridis", lwd = 5, breaks = brks)
m1 = tm_shape(rnet_merged) + tm_lines("all_fastest_bicycle", palette = "viridis", lwd = 5, breaks = brks)
m2 =tmap_arrange(m1, m2, sync = TRUE, nrow = 1)
Read 3km_exmaple_merged from github
sf::read_sf("https://github.com/nptscot/networkmerge/releases/download/v0.1/3km_exmaple_merged.geojson")
exmaple_3km =names(rnet_y)
summary(rnet_y$all_fastest_bicycle)
summary(exmaple_3km$all_fastest_bicycle)
sum(exmaple_3km$all_fastest_bicycle * sf::st_length(exmaple_3km), na.rm = TRUE)
sum(rnet_y$all_fastest_bicycle * sf::st_length(rnet_y), na.rm = TRUE)