1+ # ' Convert all highway lines into polygons bounded by the surrounding polygon
2+ # ' geometry of buildings and open spaces.
3+ # '
4+ # ' @param osmdat Object returned from \link{sb_osmdata_extract}.
5+ # ' @return An \pkg{sf} `data.frame` of polygons tracing polygons around all
6+ # ' interior streets until the edges of surrounding polygons.
7+ # ' @export
8+ hws_to_polygons <- function (osmdat ) {
9+
10+ hws_internal <- reduce_osm_highways (osmdat $ highways , osmdat $ hw_names )
11+ all_polys <- dplyr :: bind_rows (osmdat $ buildings , osmdat $ open_spaces )
12+
13+ index <- seq_len (nrow (hws_internal ))
14+ hw_polys <- lapply (index , function (i ) {
15+ xy_i <- as.matrix (hws_internal $ geometry [[i ]])
16+
17+ ret <- hw_to_polygon (xy_i , all_polys ) | >
18+ sfheaders :: sf_polygon () | >
19+ sf :: st_make_valid ()
20+ if (nrow (sf :: st_coordinates (ret )) < 4 ) {
21+ # polygons require at least 4 points
22+ return (NULL )
23+ }
24+
25+ if (sf :: st_geometry_type (ret ) == " MULTIPOLYGON" ) {
26+ ret <- sf :: st_cast (ret , " POLYGON" )
27+ }
28+
29+ return (ret )
30+ })
31+
32+ g <- do.call (rbind , hw_polys ) | >
33+ sf :: st_sf (crs = 4326 ) | >
34+ sf :: st_union () | >
35+ sf :: st_cast (" POLYGON" )
36+ sf :: st_sf (geometry = g )
37+ }
38+
139# ' Convert one highway as an \pkg{sf} "linestring" object into a polygon
240# ' defined by nearest points in "polygons".
341# '
@@ -13,9 +51,9 @@ hw_to_polygon <- function (xy, polygons) {
1351 xy <- extend_xy_to_bb (xy , bb )
1452
1553 xy_sf_p <- sfheaders :: sf_polygon (xy ) | > sf :: st_sf (crs = 4326 )
16- index <- sf :: st_within (all_polys , xy_sf_p , sparse = FALSE ) [, 1 ]
17- these_polys <- all_polys [which (index ), ]
18- those_polys <- all_polys [which (! index ), ]
54+ index <- sf :: st_within (polygons , xy_sf_p , sparse = FALSE ) [, 1 ]
55+ these_polys <- polygons [which (index ), ]
56+ those_polys <- polygons [which (! index ), ]
1957
2058 get_nearest_poly_points <- function (xy , polys ) {
2159 g_pts <- sf :: st_coordinates (polys ) [, 1 : 2 ]
0 commit comments