Skip to content

Commit

Permalink
error handling: resplit paths only if required
Browse files Browse the repository at this point in the history
  • Loading branch information
StevePem committed Nov 23, 2024
1 parent a346ba3 commit 718ce36
Showing 1 changed file with 45 additions and 38 deletions.
83 changes: 45 additions & 38 deletions functions/processOsm.R
Original file line number Diff line number Diff line change
Expand Up @@ -156,47 +156,54 @@ processOsm <- function(osmGpkg, outputCrs) {
filter(path_id %in% multiple.endpoint.paths$path_id)


# do a second round of splitting: re-split the paths that have adjacent endpoints,
# do a second round of splitting, if needed: re-split the paths that have adjacent endpoints,
# using 0.1 distance this time, but only where adjacent endpoint is an endpoint
# for a path that has the same bridge_tunnel status as the path to be resplit
echo(paste("Re-splitting", nrow(paths.to.resplit), "paths at adjacent endpoints\n"))

endpoints.for.resplit <- paths.with.nearby.endpoints %>%
# just keep paths that need to be resplit, with their bridge_tunnel status
filter(path_id %in% paths.to.resplit$path_id) %>%
st_drop_geometry() %>%
rename(path_bridge_tunnel = bridge_tunnel) %>%
# join the endpoint geometries
left_join(endpoints, by = "endpoint_id") %>%
st_sf() %>%
# join the bridge_tunnel status of each path that intersects the endpoint
# (this is the endpoint's bridge_tunnel status, but it could have more than one,
# say where a path enters a tunnel)
st_join(paths %>% dplyr::select(endpoint_bridge_tunnel = bridge_tunnel),
join = st_intersects) %>%
# only keep the endpoints if bridge_tunnel status for the endpoint
# matches the bridge_tunnel status of the path to be resplit
filter(path_bridge_tunnel == endpoint_bridge_tunnel) %>%
distinct()

resplit.path.list <-
splitPathsAtPoints(paths.to.resplit, endpoints.for.resplit, 0.1, "path_id")

# convert to dataframe, snap to grid, remove empty geometries
echo("Combining the resplit paths into a single dataframe\n")
system.time(
resplit.paths <- bind_rows(resplit.path.list) %>%
st_snap_to_grid(1) %>%
filter(!st_is_empty(geom))
)
if (nrow(paths.to.resplit) > 0) {
echo(paste("Re-splitting", nrow(paths.to.resplit), "paths at adjacent endpoints\n"))

endpoints.for.resplit <- paths.with.nearby.endpoints %>%
# just keep paths that need to be resplit, with their bridge_tunnel status
filter(path_id %in% paths.to.resplit$path_id) %>%
st_drop_geometry() %>%
rename(path_bridge_tunnel = bridge_tunnel) %>%
# join the endpoint geometries
left_join(endpoints, by = "endpoint_id") %>%
st_sf() %>%
# join the bridge_tunnel status of each path that intersects the endpoint
# (this is the endpoint's bridge_tunnel status, but it could have more than one,
# say where a path enters a tunnel)
st_join(paths %>% dplyr::select(endpoint_bridge_tunnel = bridge_tunnel),
join = st_intersects) %>%
# only keep the endpoints if bridge_tunnel status for the endpoint
# matches the bridge_tunnel status of the path to be resplit
filter(path_bridge_tunnel == endpoint_bridge_tunnel) %>%
distinct()

resplit.path.list <-
splitPathsAtPoints(paths.to.resplit, endpoints.for.resplit, 0.1, "path_id")

# convert to dataframe, snap to grid, remove empty geometries
echo("Combining the resplit paths into a single dataframe\n")
system.time(
resplit.paths <- bind_rows(resplit.path.list) %>%
st_snap_to_grid(1) %>%
filter(!st_is_empty(geom))
)

# remove paths that needed to be resplit, and replace with resplit paths
combined.paths <- split.paths %>%
filter(!path_id %in% paths.to.resplit$path_id) %>%
rbind(resplit.paths) %>%
# add a new id field, for joining to from and to id's
mutate(combined_path_id = row_number())

} else {
combined.paths <- split.paths %>%
# add a new id field, for joining to from and to id's
mutate(combined_path_id = row_number())
}

# remove paths that needed to be resplit, and replace with resplit paths
combined.paths <- split.paths %>%
filter(!path_id %in% paths.to.resplit$path_id) %>%
rbind(resplit.paths) %>%
# add a new id field, for joining to from and to id's
mutate(combined_path_id = row_number())

# temp dev notes (SP):
# (1) compared to network.sql, the second round only resplits at adjacent
# endpoints if those adjacent endpoints are on paths with the same
Expand Down

0 comments on commit 718ce36

Please sign in to comment.