ropensci / slopes

Package to calculate slopes of roads, rivers and trajectories
https://docs.ropensci.org/slopes/
GNU General Public License v3.0
70 stars 6 forks source link

Adapt osmdata_sc approach? #9

Closed mpadge closed 3 years ago

mpadge commented 4 years ago

Awesome idea @Robinlovelace!! Great initiative leaping into this! Just wanted to say that dodgr has the ability to route with slopes on osmdata_sc objects, and it looks like incorporating aspects of the approach used there would be useful here. Check the benchmark comparison:

library (slopes)
library (osmdata)
#> Data (c) OpenStreetMap contributors, ODbL 1.0. https://www.openstreetmap.org/copyright
f <- "lisbon-hw.Rds"
if (!file.exists (f)) {
    hw <- opq ("lisbon portugal") %>%
        add_osm_feature (key = "highway") %>%
        osmdata_sc (quiet = FALSE)
    saveRDS (hw, file = "lisbon-hw.Rds")
}
f_sf <- "lisbon-hw-sf.Rds"
if (!file.exists (f_sf)) {
    hw_sf <- opq ("lisbon portugal") %>%
        add_osm_feature (key = "highway") %>%
        osmdata_sf (quiet = FALSE)
    saveRDS (hw_sf, file = "lisbon-hw-sf.Rds")
}

hw <- readRDS ("lisbon-hw.Rds")
hw_sf <- readRDS ("lisbon-hw-sf.Rds") %>%
    osm_poly2line () %>%
    magrittr::extract2 ("osm_lines")

elev_file <- "<path>/<to>/srtm_35_05.tif"
r <- raster::raster (elev_file)
system.time (
    hw <- osm_elevation (hw, elev_file)
    )
#>    user  system elapsed 
#>   1.622   0.160   1.789
system.time (
    s <- slope_raster (hw_sf$geometry, r)
    )
#>    user  system elapsed 
#> 183.226   0.445 184.387

Created on 2020-05-01 by the reprex package (v0.3.0)

The dodgr approach is something like 100 times faster. That is admittedly not yet the actual slopes, but the benefit of the SC approach is that slope calculation is then just a simple dplyr::left_join of two tables, and so is effectively free in terms of computation time. So you'll still end up around 100 times faster. Do you want me to have a dig into the respective code bases, to see what might be gleaned from the osmdata/dodgr approach for use in this little project?

Robinlovelace commented 4 years ago

Do you want me to have a dig into the respective code bases, to see what might be gleaned from the osmdata/dodgr approach for use in this little project?

Sure but not a priority I'd say. Polishing it up and comparing against peer reviewed methods of slope estimation, e.g. as in #5, is the priority for this project I think. And there other projects...