USEPA / elevatr

An R package for accessing elevation data
Other
203 stars 26 forks source link

Error in get_elev_raster for UTM CRS #66

Open carostolle opened 2 years ago

carostolle commented 2 years ago

Hi,

when trying to download elevation data for a polygon projected in a UTM CRS the function fails in loc_check() since the latest version update. The same area transformed into a different CRS works fine, see reprex below:

library(sf)
#> Linking to GEOS 3.10.1, GDAL 3.4.0, PROJ 8.2.0; sf_use_s2() is TRUE
library(elevatr)

x = matrix(c(11.14251,49.75931,11.14321,49.75953,11.1423,49.76039
             ,11.14079, 49.75985, 11.1418, 49.75891, 11.14251, 49.75931),ncol=2, byrow=TRUE)
pts = list(x)

pol = st_polygon(pts)

shp <- st_as_sfc(ID = "pol"
                 , x = list(pol)
                 , crs = 4326)

# crs 3035
LAEAshp = st_transform(shp, crs = 3035)

dem = get_elev_raster(locations = LAEAshp, z = 12)
#> Mosaicing & Projecting
#> Note: Elevation units are in meters.
#plot(dem)

# crs UTM 
lonlat2UTM = function(lonlat) {
    utm = (floor((lonlat[1] + 180) / 6) %% 60) + 1
    if (lonlat[2] > 0) {
        utm + 32600
    } else {
        utm + 32700
    }
}

UTM = st_centroid(shp) |>
    st_coordinates() |> 
    lonlat2UTM()

UTMshp = st_transform(shp,crs = UTM)
st_crs(UTMshp)
#> Coordinate Reference System:
#>   User input: EPSG:32632 
#>   wkt:
#> PROJCRS["WGS 84 / UTM zone 32N",
#>     BASEGEOGCRS["WGS 84",
#>         ENSEMBLE["World Geodetic System 1984 ensemble",
#>             MEMBER["World Geodetic System 1984 (Transit)"],
#>             MEMBER["World Geodetic System 1984 (G730)"],
#>             MEMBER["World Geodetic System 1984 (G873)"],
#>             MEMBER["World Geodetic System 1984 (G1150)"],
#>             MEMBER["World Geodetic System 1984 (G1674)"],
#>             MEMBER["World Geodetic System 1984 (G1762)"],
#>             MEMBER["World Geodetic System 1984 (G2139)"],
#>             ELLIPSOID["WGS 84",6378137,298.257223563,
#>                 LENGTHUNIT["metre",1]],
#>             ENSEMBLEACCURACY[2.0]],
#>         PRIMEM["Greenwich",0,
#>             ANGLEUNIT["degree",0.0174532925199433]],
#>         ID["EPSG",4326]],
#>     CONVERSION["UTM zone 32N",
#>         METHOD["Transverse Mercator",
#>             ID["EPSG",9807]],
#>         PARAMETER["Latitude of natural origin",0,
#>             ANGLEUNIT["degree",0.0174532925199433],
#>             ID["EPSG",8801]],
#>         PARAMETER["Longitude of natural origin",9,
#>             ANGLEUNIT["degree",0.0174532925199433],
#>             ID["EPSG",8802]],
#>         PARAMETER["Scale factor at natural origin",0.9996,
#>             SCALEUNIT["unity",1],
#>             ID["EPSG",8805]],
#>         PARAMETER["False easting",500000,
#>             LENGTHUNIT["metre",1],
#>             ID["EPSG",8806]],
#>         PARAMETER["False northing",0,
#>             LENGTHUNIT["metre",1],
#>             ID["EPSG",8807]]],
#>     CS[Cartesian,2],
#>         AXIS["(E)",east,
#>             ORDER[1],
#>             LENGTHUNIT["metre",1]],
#>         AXIS["(N)",north,
#>             ORDER[2],
#>             LENGTHUNIT["metre",1]],
#>     USAGE[
#>         SCOPE["Engineering survey, topographic mapping."],
#>         AREA["Between 6°E and 12°E, northern hemisphere between equator and 84°N, onshore and offshore. Algeria. Austria. Cameroon. Denmark. Equatorial Guinea. France. Gabon. Germany. Italy. Libya. Liechtenstein. Monaco. Netherlands. Niger. Nigeria. Norway. Sao Tome and Principe. Svalbard. Sweden. Switzerland. Tunisia. Vatican City State."],
#>         BBOX[0,6,84,12]],
#>     ID["EPSG",32632]]

dem = get_elev_raster(locations = UTMshp, z = 12)
#> Error in loc_check(locations, prj): The elevatr package requires longitude in a range from -180 to 180.

Created on 2022-01-18 by the reprex package (v2.0.1)

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.1.2 (2021-11-01) #> os Ubuntu 20.04.3 LTS #> system x86_64, linux-gnu #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz Europe/Berlin #> date 2022-01-18 #> pandoc 2.11.4 @ /usr/lib/rstudio/bin/pandoc/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> assertthat 0.2.1 2019-03-21 [2] CRAN (R 4.1.0) #> class 7.3-19 2021-05-03 [4] CRAN (R 4.0.5) #> classInt 0.4-3 2020-04-07 [2] CRAN (R 4.1.0) #> cli 3.1.0 2021-10-27 [1] CRAN (R 4.1.1) #> codetools 0.2-18 2020-11-04 [4] CRAN (R 4.0.3) #> crayon 1.4.2 2021-10-29 [1] CRAN (R 4.1.2) #> curl 4.3.2 2021-06-23 [2] CRAN (R 4.1.0) #> DBI 1.1.2 2021-12-20 [1] CRAN (R 4.1.2) #> digest 0.6.29 2021-12-01 [1] CRAN (R 4.1.2) #> dplyr 1.0.7 2021-06-18 [2] CRAN (R 4.1.0) #> e1071 1.7-9 2021-09-16 [1] CRAN (R 4.1.2) #> elevatr * 0.4.2 2022-01-07 [1] CRAN (R 4.1.2) #> ellipsis 0.3.2 2021-04-29 [2] CRAN (R 4.1.0) #> evaluate 0.14 2019-05-28 [2] CRAN (R 4.1.0) #> fansi 1.0.0 2022-01-10 [1] CRAN (R 4.1.2) #> fastmap 1.1.0 2021-01-25 [1] CRAN (R 4.1.1) #> fs 1.5.2 2021-12-08 [1] CRAN (R 4.1.2) #> generics 0.1.1 2021-10-25 [1] CRAN (R 4.1.2) #> glue 1.6.0 2021-12-17 [1] CRAN (R 4.1.2) #> highr 0.9 2021-04-16 [2] CRAN (R 4.1.0) #> hms 1.1.1 2021-09-26 [1] CRAN (R 4.1.2) #> htmltools 0.5.2 2021-08-25 [1] CRAN (R 4.1.1) #> httr 1.4.2 2020-07-20 [2] CRAN (R 4.1.0) #> KernSmooth 2.23-20 2021-05-03 [4] CRAN (R 4.0.5) #> knitr 1.37 2021-12-16 [1] CRAN (R 4.1.2) #> lattice 0.20-45 2021-09-22 [4] CRAN (R 4.1.1) #> lifecycle 1.0.1 2021-09-24 [1] CRAN (R 4.1.1) #> magrittr 2.0.1 2020-11-17 [2] CRAN (R 4.1.0) #> pillar 1.6.4 2021-10-18 [1] CRAN (R 4.1.2) #> pkgconfig 2.0.3 2019-09-22 [2] CRAN (R 4.1.0) #> prettyunits 1.1.1 2020-01-24 [2] CRAN (R 4.1.0) #> progress 1.2.2 2019-05-16 [2] CRAN (R 4.1.0) #> progressr 0.10.0 2021-12-19 [1] CRAN (R 4.1.2) #> proxy 0.4-26 2021-06-07 [2] CRAN (R 4.1.0) #> purrr 0.3.4 2020-04-17 [2] CRAN (R 4.1.0) #> R6 2.5.1 2021-08-19 [2] CRAN (R 4.1.1) #> raster 3.5-11 2021-12-23 [1] CRAN (R 4.1.2) #> Rcpp 1.0.7 2021-07-07 [1] CRAN (R 4.1.2) #> reprex 2.0.1 2021-08-05 [1] CRAN (R 4.1.2) #> rgdal 1.5-28 2021-12-15 [1] CRAN (R 4.1.2) #> rlang 0.4.12 2021-10-18 [1] CRAN (R 4.1.1) #> rmarkdown 2.11 2021-09-14 [1] CRAN (R 4.1.2) #> rstudioapi 0.13 2020-11-12 [2] CRAN (R 4.1.0) #> s2 1.0.7 2021-09-28 [1] CRAN (R 4.1.2) #> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.1.2) #> sf * 1.0-5 2021-12-17 [1] CRAN (R 4.1.2) #> slippymath 0.3.1 2019-06-28 [1] CRAN (R 4.1.2) #> sp 1.4-6 2021-11-14 [1] CRAN (R 4.1.2) #> stringi 1.7.6 2021-11-29 [1] CRAN (R 4.1.2) #> stringr 1.4.0 2019-02-10 [2] CRAN (R 4.1.0) #> terra 1.4-22 2021-11-24 [1] CRAN (R 4.1.2) #> tibble 3.1.6 2021-11-07 [1] CRAN (R 4.1.2) #> tidyselect 1.1.1 2021-04-30 [2] CRAN (R 4.1.0) #> units 0.7-2 2021-06-08 [2] CRAN (R 4.1.0) #> utf8 1.2.2 2021-07-24 [1] CRAN (R 4.1.2) #> vctrs 0.3.8 2021-04-29 [2] CRAN (R 4.1.0) #> withr 2.4.3 2021-11-30 [1] CRAN (R 4.1.2) #> wk 0.6.0 2022-01-03 [1] CRAN (R 4.1.2) #> xfun 0.29 2021-12-14 [1] CRAN (R 4.1.2) #> yaml 2.2.1 2020-02-01 [2] CRAN (R 4.1.0) #> #> [1] /home/carola/R/x86_64-pc-linux-gnu-library/4.1 #> [2] /usr/local/lib/R/site-library #> [3] /usr/lib/R/site-library #> [4] /usr/lib/R/library #> #> ────────────────────────────────────────────────────────────────────────────── ```

The error seems to occur in this line

lll <- any(grepl("\\bGEOGCRS\\b",st_crs(prj_test)) |
               grepl("\\bGEODCRS\\b", st_crs(prj_test)) |
               grepl("\\bGEODETICCRS\\b", st_crs(prj_test)) |
               grepl("\\bGEOGRAPHICCRS\\b", st_crs(prj_test)) |
               grepl("\\blonglat\\b", st_crs(prj_test)) |
               grepl("\\blatlong\\b", st_crs(prj_test)) |
               grepl("\\b4326\\b", st_crs(prj_test)))
#> Error in st_crs(prj_test): could not find function "st_crs"

if(lll){
    if(any(sp::coordinates(locations)[,1]>180)){
        stop("The elevatr package requires longitude in a range from -180 to 180.")
    } 
}

There the last check returns a TRUE. We want to implement the function in a shiny app, where you are able to draw your own AOI. To add a buffer we need to transform into a non-geographic CRS, hence the UTM transformation.

Thanks, Caro

jhollist commented 2 years ago

I just pushed a fix for this. So if you install from GitHub it should work. Let me know if that does the trick.

Probably won't get it up to CRAN for at least a week or so. I will leave this open as a reminder!

carostolle commented 2 years ago

Hi, thanks for the quick reply. I cannot find the commit with the fix, can you maybe link it here?

jhollist commented 2 years ago

D'oh! I forgot to push. Should be in main now so remotes::install_github("jhollist/elevatr") should do the trick. Sorry!

carostolle commented 2 years ago

Works perfectly, thanks!

jhollist commented 2 years ago

Closing this as fixed on GitHub now. Hope to push to CRAN in coming days.