Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'reverse_geocode()' should recognize and avoid empty geometries #16

Closed
munterfinger opened this issue Dec 8, 2019 · 1 comment
Closed

'reverse_geocode()' should recognize and avoid empty geometries #16

munterfinger opened this issue Dec 8, 2019 · 1 comment
Assignees
Labels
bug

Comments

@munterfinger
Copy link
Owner

@munterfinger munterfinger commented Dec 8, 2019

Attempts to reverse_geocode() empty geometries should be recognized and trigger an error.

library(hereR)
library(sf)
#> Linking to GEOS 3.7.2, GDAL 2.4.2, PROJ 5.2.0

x <- data.frame(
  id = c(1, 2, 3),
  company = c("Schweizerische Bundesbahnen SBB", "Bahnhof AG", "Deutsche Bahn AG"),
  address = c("Wylerstrasse 123, 3000 Bern 65", "not_an_address", "Potsdamer Platz 2, 10785 Berlin"),
  stringsAsFactors = FALSE
)
locs <- geocode(x$address)
#> Address(es) 'not_an_address' not found.

(y <- st_as_sf(merge(x, locs, all.x = TRUE, by = "id")))
#> Simple feature collection with 3 features and 13 fields (with 1 geometry empty)
#> geometry type:  POINT
#> dimension:      XY
#> bbox:           xmin: 7.44657 ymin: 46.96138 xmax: 13.37525 ymax: 52.51005
#> epsg (SRID):    4326
#> proj4string:    +proj=longlat +datum=WGS84 +no_defs
#>   id                         company                       address.x
#> 1  1 Schweizerische Bundesbahnen SBB  Wylerstrasse 123, 3000 Bern 65
#> 2  2                      Bahnhof AG                  not_an_address
#> 3  3                Deutsche Bahn AG Potsdamer Platz 2, 10785 Berlin
#>                                      address.y          street houseNumber
#> 1    Wylerstrasse 123, 3014 Bern Bern, Schweiz    Wylerstrasse         123
#> 2                                         <NA>            <NA>        <NA>
#> 3 Potsdamer Platz 2, 10785 Berlin, Deutschland Potsdamer Platz           2
#>   postalCode   district   city          county  state country  type
#> 1       3014      Wyler   Bern Bern-Mittelland     BE     CHE point
#> 2       <NA>       <NA>   <NA>            <NA>   <NA>    <NA>  <NA>
#> 3      10785 Tiergarten Berlin          Berlin Berlin     DEU point
#>                    geometry
#> 1  POINT (7.44657 46.96138)
#> 2               POINT EMPTY
#> 3 POINT (13.37525 52.51005)

(z <- reverse_geocode(y))
#> Warning in (function (res) : Request failed: HTTP status code 400.
#> Simple feature collection with 2 features and 13 fields
#> geometry type:  POINT
#> dimension:      XY
#> bbox:           xmin: 7.44657 ymin: 46.96138 xmax: 13.37525 ymax: 52.51005
#> epsg (SRID):    4326
#> proj4string:    +proj=longlat +datum=WGS84 +no_defs
#>   id rank distance       level
#> 1  1    1        0 houseNumber
#> 2  2    1        0 houseNumber
#>                                          label country  state
#> 1    Wylerstrasse 123, 3014 Bern Bern, Schweiz     CHE     BE
#> 2 Potsdamer Platz 2, 10785 Berlin, Deutschland     DEU Berlin
#>            county   city   district          street houseNumber postalCode
#> 1 Bern-Mittelland   Bern      Wyler    Wylerstrasse         123       3014
#> 2          Berlin Berlin Tiergarten Potsdamer Platz           2      10785
#>                    geometry
#> 1  POINT (7.44657 46.96138)
#> 2 POINT (13.37525 52.51005)

This warning occurs because the NAs are sent as coordinates to the API:

#> Warning in (function (res) : Request failed: HTTP status code 400.

Empty geometries should therefore be avoided in the input of reverese_geocode() by checking the sf object via st_is_empty() and an error should be thrown if some are detected.

Session info
devtools::session_info()
#> ─ Session info ──────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 3.6.1 (2019-07-05)
#>  os       macOS Catalina 10.15.1      
#>  system   x86_64, darwin15.6.0        
#>  ui       X11                         
#>  language (EN)                        
#>  collate  de_CH.UTF-8                 
#>  ctype    de_CH.UTF-8                 
#>  tz       Europe/Zurich               
#>  date     2019-12-08                  
#> 
#> ─ Packages ──────────────────────────────────────────────────────────────
#>  package     * version    date       lib source        
#>  assertthat    0.2.1      2019-03-21 [1] CRAN (R 3.6.0)
#>  backports     1.1.5      2019-10-02 [1] CRAN (R 3.6.0)
#>  callr         3.3.2      2019-09-22 [1] CRAN (R 3.6.0)
#>  class         7.3-15     2019-01-01 [1] CRAN (R 3.6.1)
#>  classInt      0.4-2      2019-10-17 [1] CRAN (R 3.6.0)
#>  cli           1.1.0      2019-03-19 [1] CRAN (R 3.6.0)
#>  crayon        1.3.4      2017-09-16 [1] CRAN (R 3.6.0)
#>  curl          4.2        2019-09-24 [1] CRAN (R 3.6.0)
#>  data.table    1.12.6     2019-10-18 [1] CRAN (R 3.6.0)
#>  DBI           1.0.0      2018-05-02 [1] CRAN (R 3.6.0)
#>  desc          1.2.0      2018-05-01 [1] CRAN (R 3.6.0)
#>  devtools      2.2.1      2019-09-24 [1] CRAN (R 3.6.0)
#>  digest        0.6.22     2019-10-21 [1] CRAN (R 3.6.0)
#>  e1071         1.7-2      2019-06-05 [1] CRAN (R 3.6.0)
#>  ellipsis      0.3.0      2019-09-20 [1] CRAN (R 3.6.0)
#>  evaluate      0.14       2019-05-28 [1] CRAN (R 3.6.0)
#>  fs            1.3.1      2019-05-06 [1] CRAN (R 3.6.0)
#>  glue          1.3.1      2019-03-12 [1] CRAN (R 3.6.0)
#>  hereR       * 0.2.0.9000 2019-12-07 [1] local         
#>  htmltools     0.4.0      2019-10-04 [1] CRAN (R 3.6.0)
#>  jsonlite      1.6        2018-12-07 [1] CRAN (R 3.6.0)
#>  KernSmooth    2.23-16    2019-10-15 [1] CRAN (R 3.6.0)
#>  knitr         1.25       2019-09-18 [1] CRAN (R 3.6.0)
#>  magrittr      1.5        2014-11-22 [1] CRAN (R 3.6.0)
#>  memoise       1.1.0      2017-04-21 [1] CRAN (R 3.6.0)
#>  pkgbuild      1.0.6      2019-10-09 [1] CRAN (R 3.6.0)
#>  pkgload       1.0.2      2018-10-29 [1] CRAN (R 3.6.0)
#>  prettyunits   1.0.2      2015-07-13 [1] CRAN (R 3.6.0)
#>  processx      3.4.1      2019-07-18 [1] CRAN (R 3.6.0)
#>  ps            1.3.0      2018-12-21 [1] CRAN (R 3.6.0)
#>  R6            2.4.1      2019-11-12 [1] CRAN (R 3.6.0)
#>  Rcpp          1.0.3      2019-11-08 [1] CRAN (R 3.6.0)
#>  remotes       2.1.0      2019-06-24 [1] CRAN (R 3.6.0)
#>  rlang         0.4.1      2019-10-24 [1] CRAN (R 3.6.0)
#>  rmarkdown     1.16       2019-10-01 [1] CRAN (R 3.6.0)
#>  rprojroot     1.3-2      2018-01-03 [1] CRAN (R 3.6.0)
#>  sessioninfo   1.1.1      2018-11-05 [1] CRAN (R 3.6.0)
#>  sf          * 0.8-0      2019-09-17 [1] CRAN (R 3.6.0)
#>  stringi       1.4.3      2019-03-12 [1] CRAN (R 3.6.0)
#>  stringr       1.4.0      2019-02-10 [1] CRAN (R 3.6.0)
#>  testthat      2.2.1      2019-07-25 [1] CRAN (R 3.6.0)
#>  units         0.6-5      2019-10-08 [1] CRAN (R 3.6.0)
#>  usethis       1.5.1      2019-07-04 [1] CRAN (R 3.6.0)
#>  withr         2.1.2      2018-03-15 [1] CRAN (R 3.6.0)
#>  xfun          0.10       2019-10-01 [1] CRAN (R 3.6.0)
#>  yaml          2.2.0      2018-07-25 [1] CRAN (R 3.6.0)
#> 
#> [1] /Library/Frameworks/R.framework/Versions/3.6/Resources/library
@munterfinger
Copy link
Owner Author

@munterfinger munterfinger commented Dec 16, 2019

Added check for empty entries in the geometry column in 5d66f33. Attempts to apply functions on empty geometries are now recognized and trigger an error.

Construct an empty sf, data.frame:

library(sf)
#> Linking to GEOS 3.7.2, GDAL 2.4.2, PROJ 5.2.0

nrows <- 3
(x <- st_sf(
  id = 1:nrows,
  geometry = st_sfc(lapply(1:nrows, function(x) st_geometrycollection()))
))
#> Simple feature collection with 3 features and 1 field (with 3 geometries empty)
#> geometry type:  GEOMETRYCOLLECTION
#> dimension:      XY
#> bbox:           xmin: NA ymin: NA xmax: NA ymax: NA
#> epsg (SRID):    NA
#> proj4string:    NA
#>   id                 geometry
#> 1  1 GEOMETRYCOLLECTION EMPTY
#> 2  2 GEOMETRYCOLLECTION EMPTY
#> 3  3 GEOMETRYCOLLECTION EMPTY

And test the functions:

library(hereR)

# POIs
reverse_geocode(x)
#> Error in .check_points(poi): 'points' has empty entries in the geometry column.
weather(x)
#> Error in .check_points(poi): 'points' has empty entries in the geometry column.

# AOIs
traffic(x)
#> Error in .check_polygon(aoi): 'polygon' has empty entries in the geometry column.

And with the example from above:

x <- data.frame(
  id = c(1, 2, 3),
  company = c("Schweizerische Bundesbahnen SBB", "Bahnhof AG", "Deutsche Bahn AG"),
  address = c("Wylerstrasse 123, 3000 Bern 65", "not_an_address", "Potsdamer Platz 2, 10785 Berlin"),
  stringsAsFactors = FALSE
)
locs <- geocode(x$address)
#> Address(es) 'not_an_address' not found.

y <- st_as_sf(merge(x, locs, all.x = TRUE, by = "id"))
reverse_geocode(y)
#> Error in .check_points(poi): 'points' has empty entries in the geometry column.
Session info
devtools::session_info()
#> ─ Session info ──────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 3.6.1 (2019-07-05)
#>  os       macOS Catalina 10.15.1      
#>  system   x86_64, darwin15.6.0        
#>  ui       X11                         
#>  language (EN)                        
#>  collate  de_CH.UTF-8                 
#>  ctype    de_CH.UTF-8                 
#>  tz       Europe/Zurich               
#>  date     2019-12-16                  
#> 
#> ─ Packages ──────────────────────────────────────────────────────────────
#>  package     * version    date       lib source        
#>  assertthat    0.2.1      2019-03-21 [1] CRAN (R 3.6.0)
#>  backports     1.1.5      2019-10-02 [1] CRAN (R 3.6.0)
#>  callr         3.3.2      2019-09-22 [1] CRAN (R 3.6.0)
#>  class         7.3-15     2019-01-01 [1] CRAN (R 3.6.1)
#>  classInt      0.4-2      2019-10-17 [1] CRAN (R 3.6.0)
#>  cli           1.1.0      2019-03-19 [1] CRAN (R 3.6.0)
#>  crayon        1.3.4      2017-09-16 [1] CRAN (R 3.6.0)
#>  curl          4.2        2019-09-24 [1] CRAN (R 3.6.0)
#>  data.table    1.12.6     2019-10-18 [1] CRAN (R 3.6.0)
#>  DBI           1.0.0      2018-05-02 [1] CRAN (R 3.6.0)
#>  desc          1.2.0      2018-05-01 [1] CRAN (R 3.6.0)
#>  devtools      2.2.1      2019-09-24 [1] CRAN (R 3.6.0)
#>  digest        0.6.22     2019-10-21 [1] CRAN (R 3.6.0)
#>  e1071         1.7-2      2019-06-05 [1] CRAN (R 3.6.0)
#>  ellipsis      0.3.0      2019-09-20 [1] CRAN (R 3.6.0)
#>  evaluate      0.14       2019-05-28 [1] CRAN (R 3.6.0)
#>  fs            1.3.1      2019-05-06 [1] CRAN (R 3.6.0)
#>  glue          1.3.1      2019-03-12 [1] CRAN (R 3.6.0)
#>  hereR       * 0.2.0.9000 2019-12-16 [1] local         
#>  htmltools     0.4.0      2019-10-04 [1] CRAN (R 3.6.0)
#>  jsonlite      1.6        2018-12-07 [1] CRAN (R 3.6.0)
#>  KernSmooth    2.23-16    2019-10-15 [1] CRAN (R 3.6.0)
#>  knitr         1.25       2019-09-18 [1] CRAN (R 3.6.0)
#>  magrittr      1.5        2014-11-22 [1] CRAN (R 3.6.0)
#>  memoise       1.1.0      2017-04-21 [1] CRAN (R 3.6.0)
#>  pkgbuild      1.0.6      2019-10-09 [1] CRAN (R 3.6.0)
#>  pkgload       1.0.2      2018-10-29 [1] CRAN (R 3.6.0)
#>  prettyunits   1.0.2      2015-07-13 [1] CRAN (R 3.6.0)
#>  processx      3.4.1      2019-07-18 [1] CRAN (R 3.6.0)
#>  ps            1.3.0      2018-12-21 [1] CRAN (R 3.6.0)
#>  R6            2.4.1      2019-11-12 [1] CRAN (R 3.6.0)
#>  Rcpp          1.0.3      2019-11-08 [1] CRAN (R 3.6.0)
#>  remotes       2.1.0      2019-06-24 [1] CRAN (R 3.6.0)
#>  rlang         0.4.1      2019-10-24 [1] CRAN (R 3.6.0)
#>  rmarkdown     1.16       2019-10-01 [1] CRAN (R 3.6.0)
#>  rprojroot     1.3-2      2018-01-03 [1] CRAN (R 3.6.0)
#>  sessioninfo   1.1.1      2018-11-05 [1] CRAN (R 3.6.0)
#>  sf          * 0.8-0      2019-09-17 [1] CRAN (R 3.6.0)
#>  stringi       1.4.3      2019-03-12 [1] CRAN (R 3.6.0)
#>  stringr       1.4.0      2019-02-10 [1] CRAN (R 3.6.0)
#>  testthat      2.2.1      2019-07-25 [1] CRAN (R 3.6.0)
#>  units         0.6-5      2019-10-08 [1] CRAN (R 3.6.0)
#>  usethis       1.5.1      2019-07-04 [1] CRAN (R 3.6.0)
#>  withr         2.1.2      2018-03-15 [1] CRAN (R 3.6.0)
#>  xfun          0.10       2019-10-01 [1] CRAN (R 3.6.0)
#>  yaml          2.2.0      2018-07-25 [1] CRAN (R 3.6.0)
#> 
#> [1] /Library/Frameworks/R.framework/Versions/3.6/Resources/library
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

1 participant
You can’t perform that action at this time.