USEPA / elevatr

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

Error in if (nrow(locations) == 1 & lll & is.null(expand)) { : argument is of length zero #49

Closed jbenzaquen42 closed 2 years ago

jbenzaquen42 commented 3 years ago

I am trying to bring in elevation data with the extent of a topographic map raster, but am getting this error:

topo_map <- raster::brick("https://prd-tnm.s3.amazonaws.com/StagedProducts/Maps/HistoricalTopo/GeoTIFF/TX/TX_Chisos%20Mountains_122109_1985_100000_geo.tif")
topo_map <- raster::stack(topo_map) 

elevation <- get_elev_raster(raster(topo_map), z = 5)

Error in if (nrow(locations) == 1 & lll & is.null(expand)) { : 
  argument is of length zero
jbenzaquen42 commented 3 years ago

Here is session info if it helps. Encountered same issue on fresh install on my work computer.

R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] elevatr_0.3.4    rayshader_0.19.2 raster_3.4-5     sp_1.4-5        

loaded via a namespace (and not attached):
 [1] rgl_0.104.16            Rcpp_1.0.6              lattice_0.20-41         prettyunits_1.1.1       png_0.1-7              
 [6] class_7.3-18            assertthat_0.2.1        digest_0.6.27           foreach_1.5.1           utf8_1.1.4             
[11] mime_0.9                R6_2.5.0                e1071_1.7-4             pillar_1.5.0            rlang_0.4.10           
[16] progress_1.2.2          miniUI_0.1.1.1          magick_2.6.0            rgdal_1.5-23            webshot_0.5.2          
[21] htmlwidgets_1.5.3       munsell_0.5.0           tinytex_0.29            shiny_1.6.0             compiler_4.0.2         
[26] httpuv_1.5.5            xfun_0.21               pkgconfig_2.0.3         htmltools_0.5.1.1       tidyselect_1.1.0       
[31] tibble_3.0.6            codetools_0.2-16        fansi_0.4.2             crayon_1.4.1            dplyr_1.0.4            
[36] later_1.1.0.1           sf_0.9-7                grid_4.0.2              jsonlite_1.7.2          xtable_1.8-4           
[41] lifecycle_1.0.0         DBI_1.1.1               magrittr_2.0.1          units_0.7-0             scales_1.1.1           
[46] KernSmooth_2.23-18      promises_1.2.0.1        doParallel_1.0.16       ellipsis_0.3.1          generics_0.1.0         
[51] vctrs_0.3.6             iterators_1.0.13        tools_4.0.2             manipulateWidget_0.10.1 glue_1.4.2             
[56] rayimage_0.5.1          purrr_0.3.4             hms_1.0.0               crosstalk_1.1.1         parallel_4.0.2         
[61] fastmap_1.1.0           colorspace_2.0-0        classInt_0.4-3          knitr_1.31             
jhollist commented 3 years ago

Your example here is essentially passing an empty raster (but with known location). That is fixed in the development version but not yet up on cran. A work around that should do the trick until I get the latest version pushed up to CRAN is to convert the extent into a data frame and pass that and the prj on their own.

locations <- data.frame(matrix(extent(topo_map), ncol = 2))  
projstring <- raster::projection(topo_map)
elevation <- get_elev_raster(locations, z = 5, prj = projstring)

Use this workaround and don't install the dev version from GitHub right now. It's got some other changes on the main branch that are not yet ready for prime time (should have put them on a separate branch...)

Hope this does the trick.

jbenzaquen42 commented 3 years ago

Hmm with no changes it seems to be working now, very odd. Ill keep this handy in case it comes back. It did work the very first time I did it as well, but never after that until now.

I can keep you updated with any future issues if you keep this open.

p.s. you're awesome, no one answers issues back this fast

jhollist commented 3 years ago

Glad to hear it is working. There has certainly been some weirdness around the CRS issues. I usually can't recreate them, but sometimes I can. I'm planning to move all the sp stuff to sf. That might make it easier to manage and track down.

And thanks! You caught me at a good time...

jhollist commented 3 years ago

Also, fine to keep this open for awhile. In a few months I may circle back to see if can close it.

awiederkehr commented 3 years ago

πŸ‘‹ Hello! I wasn't sure which issue to put this under, but I'm experiencing both this issue and #47.

Just trying to run through Michael Schramm's example here, I first get stopped after attempting to get the elevation raster:

topo_map <- brick("https://prd-tnm.s3.amazonaws.com/StagedProducts/Maps/HistoricalTopo/GeoTIFF/TX/TX_Chisos%20Mountains_122109_1985_100000_geo.tif")
topo_map <- stack(topo_map)

elevationTX <- get_elev_raster(raster(topo_map), z = 9)

...resulting in the error mentioned in this issue: Error in if (nrow(locations) == 1 & lll & is.null(expand)) { : argument is of length zero

I tried the remedy you suggested above ☝️ in this issue, here:

# trying jhollist's fix suggested here https://github.com/jhollist/elevatr/issues/49
locations <- data.frame(matrix(extent(topo_map), ncol = 2))  
projstring <- raster::projection(topo_map)
elevation <- get_elev_raster(locations, z = 5, prj = projstring)

...resulting in the error from issue #47:

Downloading DEMs [===========================] 100% eta:  0s
Mosaicing & Projecting
Note: Elevation units are in meters.
Note: The coordinate reference system is:
 PROJCRS["unknown",
    BASEGEOGCRS["unknown",
        DATUM["North American Datum 1927",
            ELLIPSOID["Clarke 1866",6378206.4,294.978698213898,
                LENGTHUNIT["metre",1]],
            ID["EPSG",6267]],
            .....

I've bounced around several of the issues listed and this is about as far as I get after

Would love, love to be able to use this package, so if you have any further suggestions @jhollist, I'm all ears!

Session info:

R version 4.0.5 (2021-03-31)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Mojave 10.14.6

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] devtools_2.4.1     usethis_2.0.1      rayshader_0.24.10  elevatr_0.3.5.9999
[5] raster_3.4-10      rgdal_1.5-23       sf_0.9-8           sp_1.4-5          

loaded via a namespace (and not attached):
 [1] rgl_0.106.8             Rcpp_1.0.6              lattice_0.20-41        
 [4] prettyunits_1.1.1       class_7.3-18            ps_1.6.0               
 [7] rprojroot_2.0.2         digest_0.6.27           foreach_1.5.1          
[10] mime_0.10               R6_2.5.0                e1071_1.7-6            
[13] httr_1.4.2              rlang_0.4.11            progress_1.2.2         
[16] curl_4.3.1              rstudioapi_0.13         miniUI_0.1.1.1         
[19] callr_3.7.0             desc_1.3.0              webshot_0.5.2          
[22] htmlwidgets_1.5.3       proxy_0.4-25            shiny_1.6.0            
[25] compiler_4.0.5          httpuv_1.6.1            xfun_0.22              
[28] pkgconfig_2.0.3         pkgbuild_1.2.0          htmltools_0.5.1.1      
[31] codetools_0.2-18        crayon_1.4.1            withr_2.4.2            
[34] later_1.2.0             grid_4.0.5              jsonlite_1.7.2         
[37] xtable_1.8-4            lifecycle_1.0.0         DBI_1.1.1              
[40] magrittr_2.0.1          units_0.7-1             KernSmooth_2.23-18     
[43] cli_2.5.0               cachem_1.0.4            remotes_2.3.0          
[46] fs_1.5.0                promises_1.2.0.1        testthat_3.0.2         
[49] doParallel_1.0.16       ellipsis_0.3.2          vctrs_0.3.8            
[52] iterators_1.0.13        tools_4.0.5             manipulateWidget_0.10.1
[55] glue_1.4.2              purrr_0.3.4             hms_1.0.0              
[58] crosstalk_1.1.1         processx_3.5.2          pkgload_1.2.1          
[61] parallel_4.0.5          fastmap_1.1.0           sessioninfo_1.1.1      
[64] classInt_0.4-3          memoise_2.0.0           knitr_1.33   
jhollist commented 3 years ago

@awiederkehr Thanks for the post.

The version of elevatr on CRAN does still throw and error with Michael's example. The latest version on GitHub now has a fix. So a fresh remotes::install_github("jhollist/elevatr") should get you up and running.

Also, based on this

# trying jhollist's fix suggested here https://github.com/jhollist/elevatr/issues/49
locations <- data.frame(matrix(extent(topo_map), ncol = 2))  
projstring <- raster::projection(topo_map)
elevation <- get_elev_raster(locations, z = 5, prj = projstring)

I don't get an error. And what you are reporting above as the output doesn't look like an error to me either. What elevatr is doing is printing the crs (as well known text) to the screen by default in addition to making the assignment, in this case, to the object, elelvation. This has caused confusion. I think I will add a verbose tag or remove this altogether before the next CRAN release.

Lastly, in that fix above, that zoom (e.g. 5) is a bit broad. So to recreate what Michael had done, switch that to z = 9 and I get the same dem that Michel includes in his post.

Let me know if you are still having issues.

awiederkehr commented 3 years ago

@jhollist Thanks for the quick reply!

Ack bummer, yeah. Unfortunately I'm now getting a new error altogether: Error in bbox_to_sp(bbx, prj = prj) : object 'bbx' not found – after running either your fix or Michael's original code, same result.

If what was printed before wasn't an error, then freshly installing elevatr via the remote seemed to change the result to an error πŸ™ƒ I also had to "force" install it, because after running remotes::install_github("jhollist/elevatr") I got:

Skipping install of 'elevatr' from a github remote, the SHA1 (4f03fb26) has not changed since last install.
  Use `force = TRUE` to force installation

I'll try to futz around more, but I even tried to run your lake example from the CRAN documentation and I'm still getting the bbx error.

jhollist commented 3 years ago

It turns out that I am not so good at git... I forgot to push the changes!

Try the install now and install_github shouldn't skip install and require force. After that these should work.

Sorry!

awiederkehr commented 3 years ago

Still getting it, but lemme give git-world a little while to update 🀞

jhollist commented 3 years ago

install_github still gave you the same SHA thing or did the new version of elevatr (should be 0.3.6.9999) given you the same errors?

jhollist commented 3 years ago

If you are still getting the elevatr error, what do you get with the following:

rgdal::getGDALVersionInfo()
rgdal::getPROJ4VersionInfo()
awiederkehr commented 3 years ago

I waited about an hour, restarted R and reran it. Looks like it works now! πŸŽ‰ Thanks so much for your help – and the package, looking forward to making some cool stuff with it.

In case it's still relevant or if others have the error, when I ran getGDAL and getPROJ4: "GDAL 3.2.1, released 2020/12/29"

"Rel. 7.2.1, January 1st, 2021, [PJ_VERSION: 721]"
attr(,"short")
721
jhollist commented 3 years ago

Glad to hear it is working now! And those are both the most (I think) recent versions of GDAL and PROJ.

The development version of elevatr (0.3.6.9999) you now have should be good (it passes tests locally). I will be hopefully getting a similar version ready to roll and submitted to CRAN in a month or two.

Any more problems, let me know.

awiederkehr commented 3 years ago

Nice, thanks for your work!

jhollist commented 3 years ago

No problem! Makes me happy that people are finding it useful!

Again any issues or new features you'd like to see, let me know.

jhollist commented 3 years ago

@BigBallerBenzie and @awiederkehr Just circling back to this to see if I can close the issue. I pushed a new version of elevatr to CRAN yesterday that should have all of the fixes for the problems reported here. If you are still having issues after installing version 0.4.0 from CRAN let me know. If I don't hear anything in a couple of weeks, I'll close the issue.