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

shift in h3 cells when using polyfill at high resolution #26

Open
hewag1975 opened this issue Nov 23, 2020 · 4 comments
Open

shift in h3 cells when using polyfill at high resolution #26

hewag1975 opened this issue Nov 23, 2020 · 4 comments
Labels
bug Something isn't working

Comments

@hewag1975
Copy link

Hi,
I am trying to find all h3 indices for building shapes. See the reprex for one specific building below. After selecting all h3 indices in resolution 14 and plotting it using mapview, the h3 cells appear to be shifted.

Not sure if this is a bug or an issue with plotting lat/lon at high zoom levels.
Thanks for any help.
Here´s a reprex:

library(mapview)
library(sf)
#> Warning: Paket 'sf' wurde unter R Version 4.0.3 erstellt
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(h3forr)

# polygon coordinates
x = c(5.0834571999999980, 5.0834387000000003, 5.0834428999999979, 
      5.0834264999999998, 5.0835604000000005, 5.0835776999999984, 
      5.0835558999999986, 5.0835739999999978, 5.0834571999999980)
y = c(52.111907699999954, 52.111928399999996, 52.111929799999992, 
      52.111948200000000, 52.111993399999989, 52.111974099999998, 
      52.111966699999996, 52.111946799999998, 52.111907699999954)

p = st_polygon(x = list(matrix(data = c(x, y), ncol = 2))) 
p = st_sfc(p, crs = 4326)
p = st_as_sf(x = p)
mapview(p)

# creating h3 indices on a small polygon introduces a shift
p %>% 
  polyfill(res = 14) %>% 
  h3_to_geo_boundary() %>% 
  geo_boundary_to_sf() %>% 
  mapview() + mapview(p, col.regions = "red")
#> Warning in cbind(`Feature ID` = fid, mat): number of rows of result is not a
#> multiple of vector length (arg 1)

Created on 2020-11-23 by the reprex package (v0.3.0)

Session info
devtools::session_info()
#> - Session info ---------------------------------------------------------------
#>  setting  value                       
#>  version  R version 4.0.2 (2020-06-22)
#>  os       Windows 10 x64              
#>  system   x86_64, mingw32             
#>  ui       RTerm                       
#>  language (EN)                        
#>  collate  German_Germany.1252         
#>  ctype    German_Germany.1252         
#>  tz       Europe/Berlin               
#>  date     2020-11-23                  
#> 
#> - Packages -------------------------------------------------------------------
#>  package           * version date       lib
#>  assertthat          0.2.1   2019-03-21 [1]
#>  base64enc           0.1-3   2015-07-28 [1]
#>  brew                1.0-6   2011-04-13 [1]
#>  callr               3.5.1   2020-10-13 [1]
#>  class               7.3-17  2020-04-26 [2]
#>  classInt            0.4-3   2020-04-07 [1]
#>  cli                 2.2.0   2020-11-20 [1]
#>  codetools           0.2-16  2018-12-24 [2]
#>  colorspace          2.0-0   2020-11-11 [1]
#>  crayon              1.3.4   2017-09-16 [1]
#>  crosstalk           1.1.0.1 2020-03-13 [1]
#>  curl                4.3     2019-12-02 [1]
#>  DBI                 1.1.0   2019-12-15 [1]
#>  desc                1.2.0   2018-05-01 [1]
#>  devtools            2.3.2   2020-09-18 [1]
#>  digest              0.6.27  2020-10-24 [1]
#>  dplyr               1.0.2   2020-08-18 [1]
#>  e1071               1.7-4   2020-10-14 [1]
#>  ellipsis            0.3.1   2020-05-15 [1]
#>  evaluate            0.14    2019-05-28 [1]
#>  fansi               0.4.1   2020-01-08 [1]
#>  fs                  1.5.0   2020-07-31 [1]
#>  gdtools             0.2.2   2020-04-03 [1]
#>  generics            0.1.0   2020-10-31 [1]
#>  glue                1.4.2   2020-08-27 [1]
#>  h3forr            * 0.1.4   2020-09-07 [1]
#>  highr               0.8     2019-03-20 [1]
#>  htmltools           0.5.0   2020-06-16 [1]
#>  htmlwidgets         1.5.2   2020-10-03 [1]
#>  httr                1.4.2   2020-07-20 [1]
#>  jsonlite            1.7.1   2020-09-07 [1]
#>  KernSmooth          2.23-17 2020-04-26 [2]
#>  knitr               1.30    2020-09-22 [1]
#>  lattice             0.20-41 2020-04-02 [2]
#>  leafem              0.1.3   2020-07-26 [1]
#>  leaflet             2.0.3   2019-11-16 [1]
#>  leaflet.providers   1.9.0   2019-11-09 [1]
#>  leafpop             0.0.6   2020-09-22 [1]
#>  lifecycle           0.2.0   2020-03-06 [1]
#>  magrittr            2.0.1   2020-11-17 [1]
#>  mapview           * 2.9.0   2020-09-07 [1]
#>  memoise             1.1.0   2017-04-21 [1]
#>  mime                0.9     2020-02-04 [1]
#>  munsell             0.5.0   2018-06-12 [1]
#>  pillar              1.4.7   2020-11-20 [1]
#>  pkgbuild            1.1.0   2020-07-13 [1]
#>  pkgconfig           2.0.3   2019-09-22 [1]
#>  pkgload             1.1.0   2020-05-29 [1]
#>  png                 0.1-7   2013-12-03 [1]
#>  prettyunits         1.1.1   2020-01-24 [1]
#>  processx            3.4.4   2020-09-03 [1]
#>  ps                  1.4.0   2020-10-07 [1]
#>  purrr               0.3.4   2020-04-17 [1]
#>  R6                  2.5.0   2020-10-28 [1]
#>  raster              3.4-5   2020-11-14 [1]
#>  RColorBrewer        1.1-2   2014-12-07 [1]
#>  Rcpp                1.0.5   2020-07-06 [1]
#>  remotes             2.2.0   2020-07-21 [1]
#>  rlang               0.4.8   2020-10-08 [1]
#>  rmarkdown           2.5     2020-10-21 [1]
#>  rprojroot           2.0.2   2020-11-15 [1]
#>  satellite           1.0.2   2019-12-09 [1]
#>  scales              1.1.1   2020-05-11 [1]
#>  sessioninfo         1.1.1   2018-11-05 [1]
#>  sf                * 0.9-6   2020-09-13 [1]
#>  sp                  1.4-4   2020-10-07 [1]
#>  stringi             1.5.3   2020-09-09 [1]
#>  stringr             1.4.0   2019-02-10 [1]
#>  svglite             1.2.3.2 2020-07-07 [1]
#>  systemfonts         0.3.2   2020-09-29 [1]
#>  testthat            3.0.0   2020-10-31 [1]
#>  tibble              3.0.4   2020-10-12 [1]
#>  tidyselect          1.1.0   2020-05-11 [1]
#>  units               0.6-7   2020-06-13 [1]
#>  usethis             1.6.3   2020-09-17 [1]
#>  uuid                0.1-4   2020-02-26 [1]
#>  V8                  3.4.0   2020-11-04 [1]
#>  vctrs               0.3.5   2020-11-17 [1]
#>  webshot             0.5.2   2019-11-22 [1]
#>  withr               2.3.0   2020-09-22 [1]
#>  xfun                0.19    2020-10-30 [1]
#>  xml2                1.3.2   2020-04-23 [1]
#>  yaml                2.2.1   2020-02-01 [1]
#>  source                               
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.0)                       
#>  CRAN (R 4.0.0)                       
#>  CRAN (R 4.0.3)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.3)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.3)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.3)                       
#>  CRAN (R 4.0.3)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.3)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.3)                       
#>  CRAN (R 4.0.2)                       
#>  Github (crazycapivara/h3forr@1e1d329)
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.3)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.3)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.3)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.3)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.2)                       
#>  Github (r-spatial/mapview@9589e65)   
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.0)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.3)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.0)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.3)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.3)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.0)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.3)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.3)                       
#>  CRAN (R 4.0.3)                       
#>  CRAN (R 4.0.3)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.3)                       
#>  CRAN (R 4.0.3)                       
#>  CRAN (R 4.0.3)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.3)                       
#>  CRAN (R 4.0.0)                       
#>  CRAN (R 4.0.3)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.3)                       
#>  CRAN (R 4.0.3)                       
#>  CRAN (R 4.0.2)                       
#>  CRAN (R 4.0.2)                       
#> 
#> [1] C:/Users/hkwage/R/win-library/4.0
#> [2] C:/Program Files/R/R-4.0.2/library
@crazycapivara crazycapivara added the bug Something isn't working label Nov 25, 2020
@crazycapivara
Copy link
Owner

@hewag1975 Seems to be a bug, maybe in h3-js.

I highly recommend using h3-r provding bindings to the C library. It's also faster.

library(h3)
library(sf)
#> Linking to GEOS 3.6.2, GDAL 2.2.3, PROJ 4.9.3

x <- c(5.0834571999999980, 5.0834387000000003, 5.0834428999999979, 
      5.0834264999999998, 5.0835604000000005, 5.0835776999999984, 
      5.0835558999999986, 5.0835739999999978, 5.0834571999999980)
y <- c(52.111907699999954, 52.111928399999996, 52.111929799999992, 
      52.111948200000000, 52.111993399999989, 52.111974099999998, 
      52.111966699999996, 52.111946799999998, 52.111907699999954)

p <- st_polygon(x = list(matrix(data = c(x, y), ncol = 2)))  %>%
  st_sfc(crs = 4326) %>%
  st_as_sf()

hexagons <- h3::polyfill(p, res = 14) %>%
  h3::h3_to_geo_boundary_sf()

plot(p)
plot(hexagons, add = TRUE)

Created on 2020-11-25 by the reprex package (v0.3.0)

@hewag1975
Copy link
Author

Thanks @crazycapivara, I´ll give it a try. Unfortunately I am on Windows and not really experienced in building C libraries....

@crazycapivara
Copy link
Owner

Tested it directly in the developer console of the browser with the latest h3-js release. Same bug (shift) as above. I think it is due to the numeric precision issues in JavaScript.

@crazycapivara
Copy link
Owner

@hewag1975 You may use h3 in a docker container. See also docker-compose. Another options is running it in the WSL (Windows Subsystem for Linux), which worked well for another user.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants