Skip to content

Commit 71db910

Browse files
committed
finish 'hws_to_polys' fn
1 parent 676c73e commit 71db910

6 files changed

Lines changed: 66 additions & 5 deletions

File tree

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: superblock
22
Title: Analyses of potential superblock
3-
Version: 0.0.1.011
3+
Version: 0.0.1.012
44
Authors@R:
55
person(given = "Mark",
66
family = "Padgham",

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22

33
export(connect_highways)
44
export(get_bbox)
5+
export(hws_to_polygons)
56
export(sb_osmdata_extract)

R/hw-to-polygons.R

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,41 @@
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]

R/osmdata-extract.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ sb_osmdata_extract <- function (bbox, hw_names, outer = TRUE) {
1818
open_spaces <- extract_osm_open_spaces (bbox, bounding_poly)
1919

2020
list (
21+
bbox = bbox,
22+
hw_names = hw_names,
2123
bounding_poly = bounding_poly,
2224
highways = highways,
2325
buildings = buildings,

codemeta.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"codeRepository": "https://github.com/UrbanAnalyst/superblock-ms",
88
"issueTracker": "https://github.com/UrbanAnalyst/superblock-ms/issues",
99
"license": "https://spdx.org/licenses/GPL-3.0",
10-
"version": "0.0.1.011",
10+
"version": "0.0.1.012",
1111
"programmingLanguage": {
1212
"@type": "ComputerLanguage",
1313
"name": "R",

man/hws_to_polygons.Rd

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)