Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lad regions #548

Draft
wants to merge 48 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
c19bb40
Update gitignore
Robinlovelace Oct 31, 2024
84396cc
comment-out everything after school routing, for tests
Robinlovelace Oct 31, 2024
a4f69ad
Testing pattern in targets
Robinlovelace Oct 31, 2024
087f067
Working dynamic branching (it seems)!
Robinlovelace Oct 31, 2024
0c48176
Dynamic branching for commuting too
Robinlovelace Oct 31, 2024
9759cdf
Add garbage_collection
Robinlovelace Oct 31, 2024
cdd03f1
Try lads as regions
Robinlovelace Oct 31, 2024
170b00a
Use point_on_surface to avoid edge case errors with zone selection
Robinlovelace Oct 31, 2024
b1bedac
point_on_surface II
Robinlovelace Oct 31, 2024
b7f4640
Fixes for LAD builds
Robinlovelace Oct 31, 2024
35b8db4
Use dynamic branching for utility trips
Robinlovelace Oct 31, 2024
3d4bd8b
Demonstrate dynamic branching on list input
Robinlovelace Oct 31, 2024
42126bb
Use local_authority not region in parameters
Robinlovelace Nov 1, 2024
bfd33f5
Test tar_make() in for loop
Robinlovelace Nov 1, 2024
985441e
Debug error message related to region type
Robinlovelace Nov 1, 2024
4e43819
Finish debugging, tidy-up in next commit
Robinlovelace Nov 1, 2024
7c95f86
Tidy-up, re-run
Robinlovelace Nov 1, 2024
b88f9c3
Update cbd layer for LAD level buid
Robinlovelace Nov 1, 2024
cb41b5c
Merge branch 'main' into lad-regions
Robinlovelace Nov 1, 2024
749f3f0
use la_name replace region
wangzhao0217 Nov 1, 2024
4af8275
update CN using LA - in halfway
wangzhao0217 Nov 2, 2024
3c4c327
update CN
wangzhao0217 Nov 2, 2024
33cbdd1
Update route_ids
Robinlovelace Nov 4, 2024
929a223
Minor change to cn code, around line 260 in build.R
Robinlovelace Nov 4, 2024
babf03f
Add random wait time
Robinlovelace Nov 4, 2024
24a530e
Merge pull request #552 from nptscot/random-wait-time
wangzhao0217 Nov 4, 2024
f17b8de
stop la name change
wangzhao0217 Nov 6, 2024
04f3325
La based CN works
wangzhao0217 Nov 7, 2024
4d4a360
Test on 2 regions
Robinlovelace Nov 7, 2024
b86abdb
Add tests for Edinburgh and Lothians regions
Robinlovelace Nov 7, 2024
564956f
Test for clackmannashire
Robinlovelace Nov 27, 2024
113f0ba
Merge branch 'lad-regions' into la-approach2
Robinlovelace Nov 27, 2024
4bc4efd
Merge pull request #553 from nptscot/la-approach2
Robinlovelace Nov 27, 2024
b00c6e6
fix all conflicts
wangzhao0217 Dec 2, 2024
a11d050
update route id
wangzhao0217 Dec 4, 2024
ee48bd2
fix conflict
wangzhao0217 Dec 4, 2024
bae4ce2
update for corenet
wangzhao0217 Dec 4, 2024
f127ad8
update R/core_network_link.R
wangzhao0217 Dec 4, 2024
71c211e
refine corenet in Build.R
wangzhao0217 Dec 4, 2024
4660aff
update cn build for la
wangzhao0217 Dec 9, 2024
7eb46f5
get commit from main
wangzhao0217 Dec 9, 2024
d77a872
refine targets::tar_make(names = corenetwork)
wangzhao0217 Dec 9, 2024
e208886
test for full CN build
wangzhao0217 Dec 10, 2024
23a2c2b
fix the bugs in la core network
wangzhao0217 Dec 16, 2024
82e7bee
add remove disconnected routes
wangzhao0217 Dec 16, 2024
cc2d34a
test on CN combined
wangzhao0217 Dec 16, 2024
9753238
remove unused files
wangzhao0217 Dec 16, 2024
5acfb96
refine build.R for CN combine
wangzhao0217 Dec 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the NAs?

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