Skip to content

Commit

Permalink
La based CN works
Browse files Browse the repository at this point in the history
  • Loading branch information
wangzhao0217 committed Nov 7, 2024
1 parent f17b8de commit 04f3325
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 82 deletions.
160 changes: 81 additions & 79 deletions R/corenet_build_OS.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ corenet_build_OS = function(os_scotland, osm_scotland, la_names) {

message("Generate the coherent network for each LA")

for (la_name in la_names[2:32]) {
for (la_name in la_names) {
# la_name = la_names[2] "Edinburgh and Lothians"
message("Processing coherent network for LA: ", la_name)
la_name_snake = snakecase::to_snake_case(la_name)
Expand All @@ -17,104 +17,106 @@ corenet_build_OS = function(os_scotland, osm_scotland, la_names) {
dir.create(folder_path, recursive = TRUE)
}

la_name_boundary = filter(lads, LAD23NM == la_name) |>
sf::st_transform(crs = "EPSG:27700")
tryCatch(
{
message("Generating coherent network for: ", la_name)
la_name_boundary = filter(lads, LAD23NM == la_name) |>
sf::st_transform(crs = "EPSG:27700")

combined_net_la_name_boundary = combined_net[sf::st_union(la_name_boundary), , op = sf::st_intersects]
combined_net_la_name_boundary = combined_net[sf::st_union(la_name_boundary), , op = sf::st_intersects]

min_percentile_value = stats::quantile(combined_net_la_name_boundary$all_fastest_bicycle_go_dutch, probs = parameters$coherent_percentile[1], na.rm = TRUE)
min_percentile_value = stats::quantile(combined_net_la_name_boundary$all_fastest_bicycle_go_dutch, probs = parameters$coherent_percentile[1], na.rm = TRUE)

os_scotland_la_name_boundary = os_scotland[sf::st_union(la_name_boundary), , op = sf::st_intersects]
os_scotland_la_name_boundary = os_scotland[sf::st_union(la_name_boundary), , op = sf::st_intersects]

os_combined_net_la_name_boundary = corenet::cohesive_network_prep(
base_network = os_scotland_la_name_boundary,
influence_network = combined_net_la_name_boundary,
la_name_boundary,
crs = "EPSG:27700",
key_attribute = "road_function",
attribute_values = c("A Road", "B Road", "Minor Road", "Local Road" , "Secondary Access Road" , "Local Access Road" )
)
os_combined_net_la_name_boundary = corenet::cohesive_network_prep(
base_network = os_scotland_la_name_boundary,
influence_network = combined_net_la_name_boundary,
la_name_boundary,
crs = "EPSG:27700",
key_attribute = "road_function",
attribute_values = c("A Road", "B Road", "Minor Road", "Local Road" , "Secondary Access Road" , "Local Access Road" )
)

cohesive_network_la_name_boundary = corenet::corenet(combined_net_la_name_boundary, os_combined_net_la_name_boundary, la_name_boundary,
key_attribute = "all_fastest_bicycle_go_dutch",
crs = "EPSG:27700", maxDistPts = 3000, minDistPts = 2, npt_threshold = min_percentile_value,
road_scores = list("A Road" = 1, "B Road" = 1, "Minor Road" = 100, "Local Road" = 100, "Secondary Access Road" = 100 , "Local Access Road" = 100), n_removeDangles = 6, penalty_value = 1, group_column = "name_1"
)
cohesive_network_la_name_boundary = corenet::corenet(combined_net_la_name_boundary, os_combined_net_la_name_boundary, la_name_boundary,
key_attribute = "all_fastest_bicycle_go_dutch",
crs = "EPSG:27700", maxDistPts = 3000, minDistPts = 2, npt_threshold = min_percentile_value,
road_scores = list("A Road" = 1, "B Road" = 1, "Minor Road" = 100, "Local Road" = 100, "Secondary Access Road" = 100 , "Local Access Road" = 100), n_removeDangles = 6, penalty_value = 1, group_column = "name_1"
)

message("Generating Off Road Cycle Path network for: ", la_name)

orcp_la_name_boundary = orcp_network(area = la_name_boundary, NPT_zones = combined_net_la_name_boundary, percentile_value = 0.7)

if (!is.null(orcp_la_name_boundary)) {
osm_la_name = osm_scotland[sf::st_union(la_name_boundary), , op = sf::st_intersects] |> sf::st_transform(27700)
osm_la_name = osm_la_name[!is.na(osm_la_name$highway), ]

orcp_la_name_boundary = find_orcp_path(orcp_la_name_boundary, cohesive_network_la_name_boundary, osm_la_name, os_scotland_la_name_boundary, combined_net_la_name_boundary)
tryCatch({
orcp_la_name_boundary = orcp_la_name_boundary |> dplyr::rename(all_fastest_bicycle_go_dutch = mean_all_fastest_bicycle_go_dutch)
}, error = function(e) {
message(e)
})
message("Generating Off Road Cycle Path network for: ", la_name)


if (is.null(orcp_la_name_boundary)) {
orcp_la_name_boundary = orcp_la_name_boundary |>
group_by(component) |>
summarize(
all_fastest_bicycle_go_dutch = round(mean(all_fastest_bicycle_go_dutch, na.rm = TRUE)),
geometry = sf::st_line_merge(st_combine(st_union(geometry)))
orcp_la_name_boundary = orcp_network(area = la_name_boundary, NPT_zones = combined_net_la_name_boundary, percentile_value = 0.7)

if (!is.null(orcp_la_name_boundary)) {
osm_la_name = osm_scotland[sf::st_union(la_name_boundary), , op = sf::st_intersects] |> sf::st_transform(27700)
osm_la_name = osm_la_name[!is.na(osm_la_name$highway), ]

orcp_la_name_boundary = find_orcp_path(orcp_la_name_boundary, cohesive_network_la_name_boundary, osm_la_name, os_scotland_la_name_boundary, combined_net_la_name_boundary)

orcp_la_name_boundary = orcp_la_name_boundary |>
group_by(component) |>
summarize(
all_fastest_bicycle_go_dutch = round(mean(all_fastest_bicycle_go_dutch, na.rm = TRUE)),
geometry = st_line_merge(st_combine(st_union(geometry)))
)

p_90 = quantile(orcp_la_name_boundary$all_fastest_bicycle_go_dutch, 0.90, na.rm = TRUE)
p_25 = quantile(orcp_la_name_boundary$all_fastest_bicycle_go_dutch, 0.25, na.rm = TRUE)

# Update road function based on percentile thresholds
orcp_la_name_boundary$road_function = case_when(
orcp_la_name_boundary$all_fastest_bicycle_go_dutch > p_90 ~ "Primary",
orcp_la_name_boundary$all_fastest_bicycle_go_dutch > p_25 & orcp_la_name_boundary$all_fastest_bicycle_go_dutch <= p_90 ~ "Secondary",
orcp_la_name_boundary$all_fastest_bicycle_go_dutch <= p_25 ~ "Local Access",
TRUE ~ "Local Access"
)
}
p_90 = quantile(orcp_la_name_boundary$all_fastest_bicycle_go_dutch, 0.90, na.rm = TRUE)
p_25 = quantile(orcp_la_name_boundary$all_fastest_bicycle_go_dutch, 0.25, na.rm = TRUE)

# Update road function based on percentile thresholds
orcp_la_name_boundary$road_function = case_when(
orcp_la_name_boundary$all_fastest_bicycle_go_dutch > p_90 ~ "Primary",
orcp_la_name_boundary$all_fastest_bicycle_go_dutch > p_25 & orcp_la_name_boundary$all_fastest_bicycle_go_dutch <= p_90 ~ "Secondary",
orcp_la_name_boundary$all_fastest_bicycle_go_dutch <= p_25 ~ "Local Access",
TRUE ~ "Local Access"
)
orcp_la_name_boundary$name_1 = orcp_la_name_boundary$component

orcp_la_name_boundary$name_1 = orcp_la_name_boundary$component
# Combine the two networks
# Check if the two networks have
# Identify common columns
common_columns = intersect(names(cohesive_network_la_name_boundary), names(orcp_la_name_boundary))

# Identify common columns
common_columns = intersect(names(cohesive_network_la_name_boundary), names(orcp_la_name_boundary))
# Subset both data frames to common columns
cohesive_network_filtered = cohesive_network_la_name_boundary[common_columns]

# Subset both data frames to common columns
cohesive_network_filtered = cohesive_network_la_name_boundary[common_columns]
cohesive_network_filtered = line_merge(cohesive_network_filtered, os_combined_net_la_name_boundary, combined_net_la_name_boundary, group_column = "name_1")

cohesive_network_filtered = line_merge(cohesive_network_filtered, os_combined_net_la_name_boundary, combined_net_la_name_boundary, group_column = "name_1")
orcp_la_name_boundary_filtered = orcp_la_name_boundary[common_columns]

orcp_la_name_boundary_filtered = orcp_la_name_boundary[common_columns]
if (nrow(cohesive_network_filtered) != 0) {
orcp_la_name_boundary_filtered = convert_to_linestrings(orcp_la_name_boundary_filtered)

if (nrow(cohesive_network_filtered) != 0) {
orcp_la_name_boundary_filtered = convert_to_linestrings(orcp_la_name_boundary_filtered)
grouped_network = rbind(cohesive_network_filtered, orcp_la_name_boundary_filtered)
} else {
grouped_network = orcp_la_name_boundary_filtered
}

grouped_network = rbind(cohesive_network_filtered, orcp_la_name_boundary_filtered)
} else {
grouped_network = orcp_la_name_boundary_filtered
}
# Remove duplicates in grouped_network
grouped_network = grouped_network[!duplicated(grouped_network), ]
} else {
grouped_network = cohesive_network_la_name_boundary
}

# Remove duplicates in grouped_network
grouped_network = grouped_network[!duplicated(grouped_network), ]
} else {
grouped_network = cohesive_network_la_name_boundary
}
grouped_network = grouped_network %>%
mutate(road_function = case_when(
road_function == "A Road" ~ "Primary",
road_function %in% c("B Road", "Minor Road") ~ "Secondary",
road_function %in% c("Local Road", "Local Access Road", "Secondary Access Road") ~ "Local Access",
TRUE ~ as.character(road_function) # Keeps other values as they are
))

grouped_network = grouped_network |>
mutate(road_function = case_when(
road_function == "A Road" ~ "Primary",
road_function %in% c("B Road", "Minor Road") ~ "Secondary",
road_function %in% c("Local Road", "Local Access Road", "Secondary Access Road") ~ "Local Access",
TRUE ~ as.character(road_function) # Keeps other values as they are
))
# Use la_name name in the filename
corenet::create_coherent_network_PMtiles(folder_path = folder_path, city_filename = glue::glue("{la_name_filename}_{date_folder}_4"), cohesive_network = grouped_network|> sf::st_transform(4326))

# Use la_name name in the filename
corenet::create_coherent_network_PMtiles(folder_path = folder_path, city_filename = glue::glue("{la_name_snake}_{date_folder}"), cohesive_network = grouped_network|> sf::st_transform(4326))
message("Coherent network for: ", la_name, " generated successfully")
},
error = function(e) {
message(sprintf("An error occurred with %s: %s", la_name, e$message))
}
)
}


message("Generate the links coherent network for the LAs")

Expand Down
6 changes: 3 additions & 3 deletions parameters.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"plans": ["fastest", "balanced", "quietest", "ebike"],
"min_flow": [1],
"max_to_route": [100],
"date_routing": ["2024-11-30"],
"max_to_route": [1000000],
"date_routing": ["2024-11-31"],
"geo_subset": [false],
"open_data_build": [false],
"disag_threshold": [100],
"min_distance_meters": [500],
"region_buffer_distance_meters": [5000],
"local_authority": ["City of Edinburgh"],
"local_authority": ["Dumfries and Galloway"],
"generate_CN_start": [true],
"coherent_sources": ["OS"],
"coherent_percentile": [0.94, 0.9]
Expand Down
6 changes: 6 additions & 0 deletions route_ids.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
nrow,plan,purpose,region,date,id
4140,ebike,commute,dumfries_and_galloway,2024-11-31,11332
987,ebike,school,dumfries_and_galloway,NA,11328
987,fastest,school,dumfries_and_galloway,NA,11325
1789,fastest,utility,clackmannanshire,NA,11324
1657,balanced,commute,clackmannanshire,NA,11318
991,quietest,school,clackmannanshire,NA,11316
100,balanced,utility,city_of_edinburgh,2024-11-30,11014
100,balanced,commute,city_of_edinburgh,2024-11-30,11011
100,quietest,school,city_of_edinburgh,2024-11-30,11009
Expand Down

0 comments on commit 04f3325

Please sign in to comment.