Closed temospena closed 3 years ago
That looks like a bug to me. Thanks for reporting, will check the code.
library(slopes)
class(lisbon_road_segments)
#> [1] "sf" "tbl_df" "tbl" "data.frame"
raster::plot(dem_lisbon_raster)
plot(sf::st_geometry(lisbon_road_segments), add = TRUE)
demterra = terra::rast(dem_lisbon_raster)
plot(sf::st_geometry(lisbon_road_segments), add = TRUE)
lisbon_road_segments$sloperaster = slope_raster(lisbon_road_segments, e = dem_lisbon_raster)
#> [1] TRUE
library(terra)
#> terra version 0.8.6 (beta-release)
plot(demterra)
lisbon_road_segments$slopeterra = slope_raster(lisbon_road_segments, e = demterra, terra = T)
#> Warning in cbind(m, z): number of rows of result is not a multiple of vector
#> length (arg 2)
summary(lisbon_road_segments$sloperaster)
#> Min. 1st Qu. Median Mean 3rd Qu. Max.
#> 0.00000 0.01246 0.03534 0.05462 0.08251 0.27583
summary(lisbon_road_segments$slopeterra)
#> Min. 1st Qu. Median Mean 3rd Qu. Max.
#> 0.1007 0.1199 0.1294 0.1547 0.1540 2.1752
Created on 2020-10-29 by the reprex package (v0.3.0)
This updated example shows what's going on: terra::extract()
results in a matrix. I'm not sure when this was added but I think it was a change 'upstream' that caused the issue and we've only just noticed. Many thanks for identifying the bug.
library(slopes)
class(lisbon_road_segments)
#> [1] "sf" "tbl_df" "tbl" "data.frame"
raster::plot(dem_lisbon_raster)
plot(sf::st_geometry(lisbon_road_segments), add = TRUE)
demterra = terra::rast(dem_lisbon_raster)
plot(sf::st_geometry(lisbon_road_segments), add = TRUE)
lisbon_road_segments$sloperaster = slope_raster(lisbon_road_segments, e = dem_lisbon_raster)
#> [1] TRUE
library(terra)
#> terra version 0.8.6 (beta-release)
plot(demterra)
lisbon_road_segments$slopeterra = slope_raster(lisbon_road_segments, e = demterra, terra = T)
#> Warning in cbind(m, z): number of rows of result is not a multiple of vector
#> length (arg 2)
summary(lisbon_road_segments$sloperaster)
#> Min. 1st Qu. Median Mean 3rd Qu. Max.
#> 0.00000 0.01246 0.03534 0.05462 0.08251 0.27583
summary(lisbon_road_segments$slopeterra)
#> Min. 1st Qu. Median Mean 3rd Qu. Max.
#> 0.1007 0.1199 0.1294 0.1547 0.1540 2.1752
# Looking at the code, this seems to be the culprit:
# res = as.numeric(terra::extract(e, m[, 1:2], method = method))
# vs
# res = as.numeric(raster::extract(e, m[, 1:2], method = method))
# lets try it...
method = "bilinear"
m = sf::st_coordinates(lisbon_road_segment)[1:2, ]
e = demterra
as.numeric(terra::extract(e, m[, 1:2], method = method))
#> [1] 1.00000 2.00000 92.31126 91.93055
as.numeric(terra::extract(dem_lisbon_raster, m[, 1:2], method = method))
#> [1] 92.31126 91.93055
res_terra = terra::extract(e, m[, 1:2], method = method)
class(res_terra)
#> [1] "matrix" "array"
res_terra
#> ID r1
#> [1,] 1 92.31126
#> [2,] 2 91.93055
as.numeric(res_terra[, "r1"])
#> [1] 92.31126 91.93055
Created on 2020-10-29 by the reprex package (v0.3.0)
Good news @temospena, this issue seems to have resulted in an important bug fix. Many thanks!
remotes::install_github("itsleeds/slopes")
#> Using github PAT from envvar GITHUB_PAT
#> Skipping install of 'slopes' from a github remote, the SHA1 (5c9120f4) has not changed since last install.
#> Use `force = TRUE` to force installation
library(slopes)
class(lisbon_road_segments)
#> [1] "sf" "tbl_df" "tbl" "data.frame"
raster::plot(dem_lisbon_raster)
plot(sf::st_geometry(lisbon_road_segments), add = TRUE)
demterra = terra::rast(dem_lisbon_raster)
plot(sf::st_geometry(lisbon_road_segments), add = TRUE)
lisbon_road_segments$sloperaster = slope_raster(lisbon_road_segments, e = dem_lisbon_raster)
#> [1] TRUE
library(terra)
#> terra version 0.8.6 (beta-release)
plot(demterra)
lisbon_road_segments$slopeterra = slope_raster(lisbon_road_segments, e = demterra, terra = T)
summary(lisbon_road_segments$sloperaster)
#> Min. 1st Qu. Median Mean 3rd Qu. Max.
#> 0.00000 0.01246 0.03534 0.05462 0.08251 0.27583
summary(lisbon_road_segments$slopeterra)
#> Min. 1st Qu. Median Mean 3rd Qu. Max.
#> 0.00000 0.01205 0.03534 0.05458 0.08251 0.27583
Created on 2020-10-29 by the reprex package (v0.3.0)
I noticed some weird results if using
terra
in this function, when compared with the defaultraster
.Created on 2020-10-27 by the reprex package (v0.3.0)
Session info
``` r devtools::session_info() #> - Session info --------------------------------------------------------------- #> setting value #> version R version 3.6.2 (2019-12-12) #> os Windows 7 x64 SP 1 #> system x86_64, mingw32 #> ui RTerm #> language (EN) #> collate Portuguese_Portugal.1252 #> ctype Portuguese_Portugal.1252 #> tz Europe/London #> date 2020-10-27 #> #> - Packages ------------------------------------------------------------------- #> package * version date lib source #> assertthat 0.2.1 2019-03-21 [1] CRAN (R 3.5.3) #> backports 1.1.4 2019-04-10 [1] CRAN (R 3.5.3) #> callr 3.4.3 2020-03-28 [1] CRAN (R 3.6.3) #> class 7.3-15 2019-01-01 [2] CRAN (R 3.6.2) #> classInt 0.4-1 2019-08-06 [1] CRAN (R 3.5.3) #> cli 2.0.2 2020-02-28 [1] CRAN (R 3.6.3) #> codetools 0.2-16 2018-12-24 [2] CRAN (R 3.6.2) #> crayon 1.3.4 2017-09-16 [1] CRAN (R 3.5.1) #> curl 4.3 2019-12-02 [1] CRAN (R 3.6.3) #> DBI 1.0.0 2018-05-02 [1] CRAN (R 3.5.1) #> desc 1.2.0 2018-05-01 [1] CRAN (R 3.5.1) #> devtools 2.3.0 2020-04-10 [1] CRAN (R 3.6.3) #> digest 0.6.18 2018-10-10 [1] CRAN (R 3.5.3) #> e1071 1.7-1 2019-03-19 [1] CRAN (R 3.5.3) #> ellipsis 0.3.0 2019-09-20 [1] CRAN (R 3.5.3) #> evaluate 0.13 2019-02-12 [1] CRAN (R 3.5.3) #> fansi 0.4.0 2018-10-05 [1] CRAN (R 3.5.3) #> fs 1.4.1 2020-04-04 [1] CRAN (R 3.6.3) #> glue 1.3.1 2019-03-12 [1] CRAN (R 3.5.3) #> highr 0.8 2019-03-20 [1] CRAN (R 3.5.3) #> htmltools 0.4.0 2019-10-04 [1] CRAN (R 3.5.3) #> httr 1.4.1 2019-08-05 [1] CRAN (R 3.5.3) #> KernSmooth 2.23-16 2019-10-15 [2] CRAN (R 3.6.2) #> knitr 1.28 2020-02-06 [1] CRAN (R 3.5.3) #> lattice 0.20-38 2018-11-04 [2] CRAN (R 3.6.2) #> magrittr 1.5 2014-11-22 [1] CRAN (R 3.5.1) #> memoise 1.1.0 2017-04-21 [1] CRAN (R 3.5.1) #> mime 0.6 2018-10-05 [1] CRAN (R 3.5.2) #> pbapply 1.4-0 2019-02-05 [1] CRAN (R 3.5.2) #> pkgbuild 1.0.8 2020-05-07 [1] CRAN (R 3.6.3) #> pkgload 1.0.2 2018-10-29 [1] CRAN (R 3.5.1) #> prettyunits 1.0.2 2015-07-13 [1] CRAN (R 3.5.1) #> processx 3.4.4 2020-09-03 [1] CRAN (R 3.6.3) #> ps 1.3.0 2018-12-21 [1] CRAN (R 3.5.3) #> R6 2.4.0 2019-02-14 [1] CRAN (R 3.5.3) #> raster 3.3-13 2020-07-17 [1] CRAN (R 3.6.3) #> Rcpp 1.0.1 2019-03-17 [1] CRAN (R 3.5.3) #> remotes 2.1.1 2020-02-15 [1] CRAN (R 3.6.3) #> rgdal 1.4-3 2019-03-14 [1] CRAN (R 3.5.3) #> rlang 0.4.6 2020-05-02 [1] CRAN (R 3.6.3) #> rmarkdown 2.1 2020-01-20 [1] CRAN (R 3.5.3) #> rprojroot 1.3-2 2018-01-03 [1] CRAN (R 3.5.1) #> sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 3.5.3) #> sf 0.9-3 2020-05-04 [1] CRAN (R 3.6.3) #> slopes * 0.0.0.9000 2020-05-11 [1] Github (itsleeds/slopes@ecc77ae) #> sp 1.4-1 2020-02-28 [1] CRAN (R 3.6.3) #> stringi 1.4.3 2019-03-12 [1] CRAN (R 3.5.3) #> stringr 1.4.0 2019-02-10 [1] CRAN (R 3.5.3) #> terra 0.8-6 2020-08-01 [1] CRAN (R 3.6.3) #> testthat 2.2.1 2019-07-25 [1] CRAN (R 3.5.3) #> units 0.6-2 2018-12-05 [1] CRAN (R 3.5.3) #> usethis 1.6.1 2020-04-29 [1] CRAN (R 3.6.3) #> withr 2.1.2 2018-03-15 [1] CRAN (R 3.5.1) #> xfun 0.6 2019-04-02 [1] CRAN (R 3.5.3) #> xml2 1.2.2 2019-08-09 [1] CRAN (R 3.5.3) #> yaml 2.2.1 2020-02-01 [1] CRAN (R 3.5.3) #> #> [1] D:/rosa/Documents/R/win-library/3.6 #> [2] C:/Program Files/R/R-3.6.2/library ```Even for a large dataset, with 23k segments,
terra
retrieves this results: