Skip to content

Commit

Permalink
Tidy up for #48
Browse files Browse the repository at this point in the history
  • Loading branch information
Robinlovelace committed Oct 10, 2020
1 parent 39e3504 commit 17ede7a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 28 deletions.
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
50 changes: 27 additions & 23 deletions R/osm_cleaning_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -110,46 +114,46 @@ 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)
boundaries_df = as.data.frame(sf::st_coordinates(boundaries))
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
Expand Down
15 changes: 11 additions & 4 deletions man/osm_get_junctions.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 17ede7a

Please sign in to comment.