From 17ede7a2660d4476be3e9d0ea21f387db505cc99 Mon Sep 17 00:00:00 2001 From: Robin Lovelace Date: Sat, 10 Oct 2020 08:48:45 +0100 Subject: [PATCH] Tidy up for #48 --- NAMESPACE | 1 - R/osm_cleaning_functions.R | 50 ++++++++++++++++++++------------------ man/osm_get_junctions.Rd | 15 +++++++++--- 3 files changed, 38 insertions(+), 28 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index bcfe87c..547544a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -7,7 +7,6 @@ export(nn_line) export(nn_point) export(osm_consolidate) export(osm_get_junctions) -export(osm_get_junctions2) export(osm_main_roads) export(tc_get_cid) export(tc_get_osm) diff --git a/R/osm_cleaning_functions.R b/R/osm_cleaning_functions.R index cf1c874..5fd789e 100644 --- a/R/osm_cleaning_functions.R +++ b/R/osm_cleaning_functions.R @@ -100,6 +100,10 @@ osm_consolidate = function(x, segment = 500){ #' @param x a SF data frame of OSM linestrings #' @param overline should the route network be pre-processed with the overline #' function? +#' @param method which method to use? The default, "stplanr" uses the +#' `rnet_breakup_vertices()` function in the stplanr package. +#' An alternative method, "duplicated", is simpler but returns junctions on +#' straight road sections, e.g. where a road name changes. #' @export #' @family OSM #' @return Returns an SF data frame of POINTS @@ -110,36 +114,35 @@ osm_consolidate = function(x, segment = 500){ #' library(sf) #' x = osm_main_roads(tc_data_osm) #' junctions = osm_get_junctions(x) -#' junctions2 = osm_get_junctions2(x) +#' boundary_points = stplanr::rnet_get_nodes(x) +#' summary(duplicated(boundary_points)) +#' junctions2 = osm_get_junctions(x, method = "duplicates") #' length(junctions) #' length(junctions2) #' plot(x$geometry, col = "grey") #' plot(junctions, add = TRUE) #' plot(junctions2, add = TRUE, col = "red", cex = 0.5) -osm_get_junctions = function(x){ - # points = sf::st_cast(x, "MULTIPOINT") - # nrow(points) # 125 - # points = points$geometry - # points = sf::st_cast(points,"POINT") - # length(points) # 542 - points = sf::st_cast(x, "POINT")$geometry - nrow(points) # 542 - # TO be a junction their must be duplication of points - dup = duplicated(points) - points = points[dup] - # But we only want on version of the junction - dup = duplicated(points) - points = points[!dup] - return(points) -} - -#' @rdname osm_get_junctions -#' @export -osm_get_junctions2 = function(x, overline = FALSE) { +osm_get_junctions = function(x, method = "stplanr", overline = FALSE){ if(overline) { x$attrib = 1 x = stplanr::overline(x, "attrib") } + if(method == "duplicates") { + # points = sf::st_cast(x, "MULTIPOINT") + # nrow(points) # 125 + # points = points$geometry + # points = sf::st_cast(points,"POINT") + points = sf::st_cast(x, "POINT")$geometry + # length(points) # 542 + # TO be a junction their must be duplication of points + dup = duplicated(points) + points = points[dup] + # But we only want on version of the junction + dup = duplicated(points) + points = points[!dup] + return(points) + } + rnet_vertices = stplanr::rnet_breakup_vertices(x) nrow(rnet_vertices) / nrow(x) # vertices have been added... boundaries = stplanr::line2points(rnet_vertices) @@ -147,9 +150,10 @@ osm_get_junctions2 = function(x, overline = FALSE) { boundaries_n = dplyr::summarise(dplyr::group_by(boundaries_df, X, Y), n = dplyr::n()) junction_df = boundaries_n[boundaries_n$n >= 3, ] nrow(junction_df) / nrow(x) - sf::st_as_sf(junction_df, coords = c("X", "Y"), crs = sf::st_crs(x)) - # plot(rnet_vertices) + points = sf::st_as_sf(junction_df, coords = c("X", "Y"), crs = sf::st_crs(x)) + return(points$geometry) } + #' Cluster junction points into polygons #' #' @param x a SF data frame of joints diff --git a/man/osm_get_junctions.Rd b/man/osm_get_junctions.Rd index 0f0bf16..b3d1988 100644 --- a/man/osm_get_junctions.Rd +++ b/man/osm_get_junctions.Rd @@ -2,16 +2,18 @@ % Please edit documentation in R/osm_cleaning_functions.R \name{osm_get_junctions} \alias{osm_get_junctions} -\alias{osm_get_junctions2} \title{Extract junction points from OSM road linestrings} \usage{ -osm_get_junctions(x) - -osm_get_junctions2(x, overline = TRUE) +osm_get_junctions(x, method = "stplanr", overline = FALSE) } \arguments{ \item{x}{a SF data frame of OSM linestrings} +\item{method}{which method to use? The default, "stplanr" uses the +\code{rnet_breakup_vertices()} function in the stplanr package. +An alternative method, "duplicated", is simpler but returns junctions on +straight road sections, e.g. where a road name changes.} + \item{overline}{should the route network be pre-processed with the overline function?} } @@ -29,9 +31,14 @@ where two roads meet. It excludes road crossings e.g. bridges. library(sf) x = osm_main_roads(tc_data_osm) junctions = osm_get_junctions(x) +boundary_points = stplanr::rnet_get_nodes(x) +summary(duplicated(boundary_points)) junctions2 = osm_get_junctions2(x) +length(junctions) +length(junctions2) plot(x$geometry, col = "grey") plot(junctions, add = TRUE) +plot(junctions2, add = TRUE, col = "red", cex = 0.5) } \seealso{ Other OSM: