Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add packaged DEM data #51

Merged
merged 12 commits into from
Dec 2, 2024
12 changes: 10 additions & 2 deletions R/data.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' CRiSp example data for Bucharest
#' CRiSp example OSM data for Bucharest
#'
#' Data extracted from OpenStreetMap for examples used in the CRiSp package.
#'
Expand All @@ -8,4 +8,12 @@
#' Bucharest.}
#' }
#' @source OpenStreetMap
"bucharest"
"bucharest_osm"

#' CRiSp example DEM data for Bucharest
#'
#' Digital Elevation Model (DEM) for examples used in the CRiSp package.
#'
#' @format TODO
#' @source Copernicus DEM 30
"bucharest_dem"
3 changes: 2 additions & 1 deletion R/valley.R
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ dem_to_cog <- function(dem, fpath, output_directory = NULL) {
#' @export
reproject <- function(x, crs, ...) {
if (inherits(x, "SpatRaster")) {
return(terra::project(x, crs, ...))
wkt <- sf::st_crs(crs)$wkt
return(terra::project(x, wkt, ...))
} else if (inherits(x, c("bbox", "sfc", "sf"))) {
return(sf::st_transform(x, crs, ...))
} else {
Expand Down
21 changes: 14 additions & 7 deletions data-raw/bucharest.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@ river_name <- "Dâmbovița"
epsg_code <- 32635
bbox_buffer <- 2000

# Fetch the data
bucharest <- CRiSp::get_osmdata(city_name, river_name,
crs = epsg_code, buffer = bbox_buffer)
# Fetch the OSM data
bucharest_osm <- CRiSp::get_osmdata(city_name, river_name,
crs = epsg_code, buffer = bbox_buffer)

# Fix encoding issue in the WKT string of city boundary
# Fix encoding issue in the WKT strings
fix_wkt_encoding <- function(x) {
wkt <- sf::st_crs(x)$wkt
sf::st_crs(x)$wkt <- gsub("°|º", "\\\u00b0", wkt)
sf::st_crs(x)$wkt <- gsub("°|º", "\\\u00b0", wkt) # replace with ASCII code
x
}
bucharest <- lapply(bucharest, fix_wkt_encoding)
bucharest_osm <- lapply(bucharest_osm, fix_wkt_encoding)

# Fetch the DEM data
bucharest_dem <- CRiSp::get_dem(bucharest_osm$bb) |>
CRiSp::reproject(epsg_code) |>
# SpatRaster objects cannot be directly serialized as RDS/RDA files
terra::wrap()

# Save as package data
usethis::use_data(bucharest, overwrite = TRUE)
usethis::use_data(bucharest_osm, overwrite = TRUE)
usethis::use_data(bucharest_dem, overwrite = TRUE)
Binary file removed data/bucharest.rda
Binary file not shown.
Binary file added data/bucharest_dem.rda
Binary file not shown.
Binary file added data/bucharest_osm.rda
Binary file not shown.
19 changes: 19 additions & 0 deletions man/bucharest_dem.Rd

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

8 changes: 4 additions & 4 deletions man/bucharest.Rd → man/bucharest_osm.Rd

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

12 changes: 6 additions & 6 deletions tests/testthat/test-corridor.R
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
test_that("river_buffer parameters can be configured via initial_corridor", {
river <- bucharest$river_centerline
river <- bucharest_osm$river_centerline
actual <- initial_corridor(river, buffer = 1)
expected <- river_buffer(river, buffer = 1)
expect_setequal(actual, expected)
})

test_that("River buffer properly implements a buffer function", {
river <- bucharest$river_centerline
river <- bucharest_osm$river_centerline
actual <- river_buffer(river, buffer = 0.5)
expected <- sf::st_buffer(river, 0.5)
expect_setequal(actual, expected)
})

test_that("River buffer can trim to the region of interest", {
river <- bucharest$river_centerline
bbox <- sf::st_bbox(bucharest$boundary)
river <- bucharest_osm$river_centerline
bbox <- sf::st_bbox(bucharest_osm$boundary)
actual <- river_buffer(river, buffer = 0.5, bbox = bbox)
river_buffer <- sf::st_buffer(river, 0.5)
overlap_matrix <- sf::st_overlaps(river_buffer, actual, sparse = FALSE)
Expand Down Expand Up @@ -59,8 +59,8 @@ test_that("Splitting an AoI by a more complex river still gives two regions", {
})

test_that("Splitting an AoI by a river works with real data", {
bbox <- sf::st_bbox(bucharest$boundary)
river <- bucharest$river_centerline
bbox <- sf::st_bbox(bucharest_osm$boundary)
river <- bucharest_osm$river_centerline
aoi_split <- split_aoi(bbox, river)
expect_equal(length(aoi_split), 2)
})
Expand Down
22 changes: 11 additions & 11 deletions tests/testthat/test-data.R
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
test_that("Bucharest dataset includes all elements", {
expect_setequal(names(bucharest),
expect_setequal(names(bucharest_osm),
c("bb", "boundary", "river_centerline", "river_surface",
"streets", "railways"))
})

test_that("Bounding box has correct type", {
bb <- bucharest$bb
bb <- bucharest_osm$bb
expect_true(inherits(bb, "bbox"))
})

test_that("Only the bounding box has a geographic CRS", {
is_longlat <- sapply(bucharest, sf::st_is_longlat)
is_longlat <- sapply(bucharest_osm, sf::st_is_longlat)
expect_true(is_longlat["bb"])
others <- is_longlat[names(bucharest) != "bb"]
others <- is_longlat[names(bucharest_osm) != "bb"]
expect_true(!any(others))
})

test_that("City boundary has one multipolygon geometry", {
boundary <- bucharest$boundary
boundary <- bucharest_osm$boundary
expect_equal(length(boundary), 1)
expect_true(sf::st_is(boundary, "MULTIPOLYGON"))
})

test_that("River center line has one multilinestring geometry", {
river_centerline <- bucharest$river_centerline
river_centerline <- bucharest_osm$river_centerline
expect_equal(length(river_centerline), 1)
expect_true(sf::st_is(river_centerline, "MULTILINESTRING"))
})

test_that("River surface has one multipolygon geometry", {
river_surface <- bucharest$river_surface
river_surface <- bucharest_osm$river_surface
expect_equal(length(river_surface), 1)
expect_true(sf::st_is(river_surface, "MULTIPOLYGON"))
})

test_that("Streets are all linestrings", {
expect_true(all(sf::st_is(bucharest$streets, "LINESTRING")))
expect_true(all(sf::st_is(bucharest_osm$streets, "LINESTRING")))
})

test_that("Streets have type column", {
expect_equal(colnames(bucharest$streets), c("type", "geometry"))
expect_equal(colnames(bucharest_osm$streets), c("type", "geometry"))
})

test_that("Railways are all linestrings", {
expect_true(all(sf::st_is(bucharest$railways, "LINESTRING")))
expect_true(all(sf::st_is(bucharest_osm$railways, "LINESTRING")))
})

test_that("Railways have type column", {
expect_equal(colnames(bucharest$railways), c("type", "geometry"))
expect_equal(colnames(bucharest_osm$railways), c("type", "geometry"))
})
2 changes: 1 addition & 1 deletion tests/testthat/test-network.R
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ test_that("Filter network properly splits network across adjacent regions", {
})

test_that("Network setup with real data", {
edges <- bucharest$streets
edges <- bucharest_osm$streets
network <- as_network(edges, clean = FALSE, flatten = FALSE)
edges_actual <- sf::st_geometry(sf::st_as_sf(network, "edges"))
edges_expected <- sf::st_geometry(edges)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-osmdata.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
test_that("City boundary of Bucharest is correctly retreived", {
skip_on_ci()
bucharest_boundary <- bucharest$boundary |>
bucharest_boundary <- bucharest_osm$boundary |>
sf::st_geometry() |>
sf::st_transform(4326)

Expand Down
5 changes: 3 additions & 2 deletions vignettes-drafts/corridor-segmentation.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ For a more detailed analysis of an urban river corridor, corridor-level delineat

By default, the all-in-one function `delineate_corridor()` includes the division of the corridor into segments. It is also possible to use the `delineate_segment()` function to divide the corridor in a separate step.

To demonstrate this as a separate step, we will use the `bucharest$corridor` and `bucharest$streets` layers from the package data as input.
To demonstrate this as a separate step, we will use the `bucharest_osm$corridor` and `bucharest_osm$streets` layers from the package data as input.

```{r eval=FALSE}
segmented_corridor <- delineate_segments(bucharest$corridor, bucharest$streets)
segmented_corridor <- delineate_segments(bucharest_osm$corridor,
bucharest_osm$streets)
```

35 changes: 17 additions & 18 deletions vignettes/getting-osm-data.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ knitr::opts_chunk$set(

library(CRiSp)

city_boundary <- bucharest$boundary
river_surface <- bucharest$river_surface
river_centerline <- bucharest$river_centerline
railways <- bucharest$railways
streets <- bucharest$streets
city_boundary <- bucharest_osm$boundary
river_surface <- bucharest_osm$river_surface
river_centerline <- bucharest_osm$river_centerline
railways <- bucharest_osm$railways
streets <- bucharest_osm$streets
```

```{r packages, eval=FALSE, message=FALSE}
Expand Down Expand Up @@ -56,7 +56,7 @@ railways <- get_osm_railways(bb, crs)
Individual layers can be written to disk before being read in for delineation.

```{r eval=FALSE}
bucharest <- list(
bucharest_osm <- list(
bb = bb,
boundary = city_boundary,
river_centerline = river$centerline,
Expand All @@ -65,13 +65,12 @@ bucharest <- list(
railways = railways
)

walk(
st_write_list,
Comment on lines -68 to -69
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When updating the name of the dataset, I have noticed this function probably had a placeholder for the name of the dataset (st_write_list -> bucharest)? Also I made a suggestion to replace walk for walk2, which seems to simplify quite a bit the overall snippet?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, much better!

walk2(
bucharest_osm,
names(bucharest_osm),
~ st_write(
.x,
dsn = sprintf("%s_%s.gpkg",
names(st_write_list)[map_lgl(st_write_list, identical, .x)],
city_name),
dsn = sprintf("%s_%s.gpkg", .y, city_name),
append = FALSE,
quiet = TRUE
)
Expand All @@ -81,25 +80,25 @@ walk(
The above layers can also be obtained with the all-in-one function `get_osmdata()`. Optionally, a bounding box buffer can be specified to expand the bounding box.

```{r eval=FALSE}
bucharest <- get_osmdata(city_name, river_name, buffer = bbox_buffer)
bucharest_osm <- get_osmdata(city_name, river_name, buffer = bbox_buffer)
```

The resulting object is a list with all the layers obtained individually above.

```{r}
names(bucharest)
names(bucharest_osm)
```


```{r, eval=TRUE, fig.alt="All layers combined", fig.cap="All layers combined"}
if (requireNamespace("ggplot2", quietly = TRUE)) {
library(ggplot2)
ggplot() +
geom_sf(data = bucharest$boundary, fill = "grey", color = "black") +
geom_sf(data = bucharest$railways, color = "orange") +
geom_sf(data = bucharest$streets, color = "black") +
geom_sf(data = bucharest$river_surface, fill = "blue", color = "blue") +
geom_sf(data = bucharest$river_centerline, color = "blue") +
geom_sf(data = bucharest_osm$boundary, fill = "grey", color = "black") +
geom_sf(data = bucharest_osm$railways, color = "orange") +
geom_sf(data = bucharest_osm$streets, color = "black") +
geom_sf(data = bucharest_osm$river_surface, fill = "blue", color = "blue") +
geom_sf(data = bucharest_osm$river_centerline, color = "blue") +
xlim(417000, 439000) +
ylim(4908800, 4932500)
} else {
Expand Down
6 changes: 3 additions & 3 deletions vignettes/network-preparation.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ In this article we show how to set up the spatial network for a city before usin
We start by loading the OSM data. According to the delineation method, all persistent physical structures need to be considered. Therefore, the network will contain both streets and railways from OSM.

```{r}
streets <- bucharest$streets
railways <- bucharest$railways
streets <- bucharest_osm$streets
railways <- bucharest_osm$railways
```

**Note**: If the city in question contains other surface-level structures that need to be included in the network, such as above-ground metro lines, retrieve them with the appropriate OSM tags following the instructions in `vignette("getting-osm-data")` and include them here in the network.
Expand All @@ -45,7 +45,7 @@ network <- bind_rows(streets, railways) |>
as_sfnetwork(directed = FALSE)
```

To be able to use the network for delineation, we need to flatten it (that is, project bridges to the ground surface) and add nodes at all intersections between edges.
To be able to use the network for delineation, we need to flatten it (that is, project bridges to the ground surface) and add nodes at all intersections between edges.

```{r}
network_new <- flatten_network(network)
Expand Down
Loading