R-ArcGIS / arcgislayers

ArcGIS Location Services
http://r.esri.com/arcgislayers/
Apache License 2.0
37 stars 8 forks source link

Create more informative error message when no features in `filter_geom` #145

Closed bocinsky closed 4 months ago

bocinsky commented 4 months ago

Is your feature request related to a problem? Please describe. When attempting to select data that doesn't exist within a specific area of interest, arcgislayers::arc_select throws an un-helpful error.

Describe the solution you'd like It would be nice for there to be a clear error that informs the user that no data exists within their area of interest. Alternatively, arc_select could return an empty sf object.

Describe alternatives you've considered I was able to work around this in a script I'm developing by wrapping the error in a tryCatch statement, but it took me a while to realize what the issue really was.

Additional context Here is a reprex (pardon the use of other packages):

library(arcgislayers)
library(FedData)
#> You have loaded FedData v4.
#> As of FedData v4 we have retired
#> dependencies on the `sp` and `raster` packages.
#> All functions in FedData v4 return `terra` (raster)
#> or `sf` (vector) objects by default, and there may be
#> other breaking changes.
library(tigris)
#> To enable caching of data, set `options(tigris_use_cache = TRUE)`
#> in your R script or .Rprofile.

layer <-
  "https://hydro.nationalmap.gov/arcgis/rest/services/NHDPlus_HR/MapServer" |>
  arcgislayers::arc_open() |>
  arcgislayers::get_layer(name = "NHDLine")

montezuma_county <-
  tigris::counties(state = "CO", cb = TRUE) |>
  dplyr::filter(NAME == "Montezuma") |>
  sf::st_as_sfc() |>
  sf::st_union() |>
  sf::st_cast("POLYGON")
#> Retrieving data for the year 2022
#>   |                                                                              |                                                                      |   0%  |                                                                              |                                                                      |   1%  |                                                                              |=                                                                     |   1%  |                                                                              |==                                                                    |   2%  |                                                                              |==                                                                    |   3%  |                                                                              |==                                                                    |   4%  |                                                                              |===                                                                   |   4%  |                                                                              |===                                                                   |   5%  |                                                                              |====                                                                  |   5%  |                                                                              |====                                                                  |   6%  |                                                                              |=====                                                                 |   7%  |                                                                              |======                                                                |   8%  |                                                                              |======                                                                |   9%  |                                                                              |=======                                                               |  11%  |                                                                              |========                                                              |  11%  |                                                                              |========                                                              |  12%  |                                                                              |=========                                                             |  12%  |                                                                              |=========                                                             |  13%  |                                                                              |==========                                                            |  14%  |                                                                              |==========                                                            |  15%  |                                                                              |===========                                                           |  15%  |                                                                              |===========                                                           |  16%  |                                                                              |============                                                          |  17%  |                                                                              |=============                                                         |  18%  |                                                                              |==============                                                        |  19%  |                                                                              |==============                                                        |  21%  |                                                                              |===============                                                       |  21%  |                                                                              |===============                                                       |  22%  |                                                                              |================                                                      |  22%  |                                                                              |================                                                      |  23%  |                                                                              |=================                                                     |  24%  |                                                                              |=================                                                     |  25%  |                                                                              |======================                                                |  31%  |                                                                              |======================                                                |  32%  |                                                                              |=======================                                               |  33%  |                                                                              |=======================                                               |  34%  |                                                                              |========================                                              |  34%  |                                                                              |========================                                              |  35%  |                                                                              |=========================                                             |  36%  |                                                                              |==========================                                            |  36%  |                                                                              |==========================                                            |  37%  |                                                                              |===========================                                           |  38%  |                                                                              |===========================                                           |  39%  |                                                                              |============================                                          |  40%  |                                                                              |=============================                                         |  41%  |                                                                              |==============================                                        |  42%  |                                                                              |==============================                                        |  43%  |                                                                              |===============================                                       |  44%  |                                                                              |================================                                      |  46%  |                                                                              |=================================                                     |  47%  |                                                                              |=================================                                     |  48%  |                                                                              |==================================                                    |  48%  |                                                                              |==================================                                    |  49%  |                                                                              |===================================                                   |  50%  |                                                                              |====================================                                  |  52%  |                                                                              |=====================================                                 |  52%  |                                                                              |======================================                                |  54%  |                                                                              |======================================                                |  55%  |                                                                              |=======================================                               |  56%  |                                                                              |========================================                              |  57%  |                                                                              |========================================                              |  58%  |                                                                              |=========================================                             |  58%  |                                                                              |=========================================                             |  59%  |                                                                              |==============================================                        |  66%  |                                                                              |===============================================                       |  67%  |                                                                              |===============================================                       |  68%  |                                                                              |================================================                      |  68%  |                                                                              |================================================                      |  69%  |                                                                              |=================================================                     |  71%  |                                                                              |==================================================                    |  71%  |                                                                              |==================================================                    |  72%  |                                                                              |===================================================                   |  72%  |                                                                              |===================================================                   |  73%  |                                                                              |====================================================                  |  74%  |                                                                              |=====================================================                 |  75%  |                                                                              |=====================================================                 |  76%  |                                                                              |======================================================                |  77%  |                                                                              |======================================================                |  78%  |                                                                              |=======================================================               |  78%  |                                                                              |========================================================              |  79%  |                                                                              |==========================================================            |  83%  |                                                                              |===========================================================           |  84%  |                                                                              |===========================================================           |  85%  |                                                                              |============================================================          |  85%  |                                                                              |============================================================          |  86%  |                                                                              |=============================================================         |  87%  |                                                                              |=============================================================         |  88%  |                                                                              |==============================================================        |  88%  |                                                                              |==============================================================        |  89%  |                                                                              |===============================================================       |  90%  |                                                                              |================================================================      |  91%  |                                                                              |================================================================      |  92%  |                                                                              |=================================================================     |  93%  |                                                                              |==================================================================    |  94%  |                                                                              |======================================================================| 100%

mesa_verde <-
  FedData::meve |>
  sf::st_union() |>
  sf::st_cast("POLYGON")

# This works
arcgislayers::arc_select(x = layer, filter_geom = montezuma_county)
#> Simple feature collection with 42 features and 13 fields
#> Geometry type: MULTILINESTRING
#> Dimension:     XY
#> Bounding box:  xmin: -12108650 ymin: 4468347 xmax: -12049240 ymax: 4522032
#> Projected CRS: WGS 84 / Pseudo-Mercator
#> First 10 features:
#>    OBJECTID permanent_identifier      fdate resolution gnis_id gnis_name
#> 1     27770             82522862 2003-04-29          2      NA        NA
#> 2     27773             82535974 2003-04-29          2      NA        NA
#> 3     27776             82535998 2003-04-29          2      NA        NA
#> 4     27777             82536020 2003-04-29          2      NA        NA
#> 5     27778             82536126 2003-04-29          2      NA        NA
#> 6     27779             82536182 2003-04-29          2      NA        NA
#> 7     27780             82536192 2003-04-29          2      NA        NA
#> 8     27781             82536214 2003-04-29          2      NA        NA
#> 9     27782             82536328 2003-04-29          2      NA        NA
#> 10    27783             82536454 2003-04-29          2      NA        NA
#>    lengthkm ftype fcode visibilityfilter   nhdplusid vpuid Shape_Length
#> 1     0.016   318 31800                0 4.10005e+13  1408     20.63788
#> 2     0.025   318 31800                0 4.10005e+13  1408     31.79392
#> 3     0.012   318 31800                0 4.10005e+13  1408     15.26291
#> 4     0.020   318 31800                0 4.10005e+13  1408     24.88884
#> 5     0.014   318 31800                0 4.10005e+13  1408     17.77748
#> 6     0.015   318 31800                0 4.10005e+13  1408     18.66730
#> 7     0.015   318 31800                0 4.10005e+13  1408     19.39548
#> 8     0.016   318 31800                0 4.10005e+13  1408     19.99006
#> 9     0.015   318 31800                0 4.10005e+13  1408     19.63253
#> 10    0.021   318 31800                0 4.10005e+13  1408     26.52085
#>                          geometry
#> 1  MULTILINESTRING ((-12078456...
#> 2  MULTILINESTRING ((-12102322...
#> 3  MULTILINESTRING ((-12101488...
#> 4  MULTILINESTRING ((-12102798...
#> 5  MULTILINESTRING ((-12095925...
#> 6  MULTILINESTRING ((-12095452...
#> 7  MULTILINESTRING ((-12093742...
#> 8  MULTILINESTRING ((-12094771...
#> 9  MULTILINESTRING ((-12092431...
#> 10 MULTILINESTRING ((-12092459...

# This fails
arcgislayers::arc_select(x = layer, filter_geom = mesa_verde)
#> Error in fts_raw[["attributes"]]: subscript out of bounds

Created on 2024-02-10 with reprex v2.1.0