Giter VIP home page Giter VIP logo

h3-r's Introduction

H3-R

R build status Project Status: Active – The project has reached a stable, usable state and is being actively developed. H3 Version CRAN status

Provides R bindings for H3, a hexagonal hierarchical spatial indexing system.

Documentation

Notes

Succesfully built on

  • Linux
  • macOS
  • Windows

Since v3.7.1 {h3} comes with a bundled version of the H3 C library, so that you no longer have to build it yourself before installing the R package.

Installation

Once on CRAN you can install {h3} with:

install.packages("h3")

You can install the latest version of {h3} from github with:

# install.packages("remotes")
remotes::install_github("crazycapivara/h3-r")

Usage

Core functions:

library(h3)

coords <- c(37.3615593, -122.0553238)
resolution <- 7

# Convert a lat/lng point to a hexagon index at resolution 7
(h3_index <- geo_to_h3(coords, resolution)) 
#> [1] "87283472bffffff"

# Get the center of the hexagon
h3_to_geo_sf(h3_index)
#> Simple feature collection with 1 feature and 1 field
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: -122.0503 ymin: 37.35172 xmax: -122.0503 ymax: 37.35172
#> Geodetic CRS:  WGS 84
#>          h3_index                   geometry
#> 1 87283472bffffff POINT (-122.0503 37.35172)

# Get the vertices of the hexagon
h3_to_geo_boundary(h3_index)
#> [[1]]
#>           lat       lng
#> [1,] 37.34110 -122.0416
#> [2,] 37.35290 -122.0340
#> [3,] 37.36352 -122.0428
#> [4,] 37.36234 -122.0591
#> [5,] 37.35054 -122.0666
#> [6,] 37.33992 -122.0579

# Get the polygon of the hexagon
h3_to_geo_boundary_sf(h3_index)
#> Simple feature collection with 1 feature and 1 field
#> Geometry type: POLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -122.0666 ymin: 37.33992 xmax: -122.034 ymax: 37.36352
#> Geodetic CRS:  WGS 84
#>          h3_index                       geometry
#> 1 87283472bffffff POLYGON ((-122.0416 37.3411...

Useful algorithms:

# Get all neighbors within 1 step of the hexagon
radius <- 1
(neighbors <- k_ring(h3_index, radius))
#> [1] "87283472bffffff" "87283472affffff" "87283470cffffff" "87283470dffffff"
#> [5] "872834776ffffff" "872834729ffffff" "872834728ffffff"

h3_to_geo_boundary_sf(neighbors) %>%
  sf::st_geometry() %>% plot(col = "blue")

h3_set_to_multi_polygon(neighbors) %>%
  sf::st_geometry() %>% plot(col = "green")

Run tests

devtools::test(reporter = "minimal")
#> ℹ Loading h3
#> ℹ Testing h3
#> ...................................................

h3-r's People

Contributors

crazycapivara avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

h3-r's Issues

CRAN release planned?

Hi there!

I'm currently working on a package that utilizes functionality from Uber's h3. I have modified a fork for my own use (https://github.com/josiahparry/h3r). However, the package I have forked is not as mature as your own.

Do you plan on releasing {h3} to CRAN? This will help me decide how I want to address the h3 dependency.

Request: Polyfill

Hello,

I have moved from using h3forr to h3-r in most of my projects (it's much faster!).

Recently I had to go over some legacy code, and realized that h30r does not have a polyfill function.

Could it be added? Then I could use h3-r instead of h3forr. (Unless I find another one of course!)

Failed to install H3 for R

I failed to install H3 for R on my mac.

I tried to install it using devtools but got the error as follows.

I guess it occurs when I try to install h3 for C but I know no other ways to fix it.

( based on your guidance I already installed 'cmake' and 'make' before the job )

can you give me hands for this?

If you need more information for my environments let me know.

`h3_hex-ring.cpp:2:10: fatal error: 'h3/h3api.h' file not found
#include <h3/h3api.h>
^~~~~~~~~~~~
1 error generated.
make: *** [h3_hex-ring.o] Error 1
ERROR: compilation failed for package ‘h3’

  • removing ‘/Library/Frameworks/R.framework/Versions/4.0/Resources/library/h3’
    Error: Failed to install 'h3' from GitHub:
    (converted from warning) installation of package ‘/var/folders/vc/khw6v0g10wggw3ygs5_k_nk80000gn/T//RtmpcmlKxz/file12ba2af5644b/h3_3.3.2.tar.gz’ had non-zero exit status`

h3 to sf helper

I think it would be useful to have a helper function to convert a tibble with many h3 indexes into a single sf object.

Presently, h3_to_geo_bounardy_sf() works well with a vector. However it is less friendly when starting with a tibble. As such I think a helper function such as the below would be useful. See below reprex to get a sense of envisioned flow.

library(h3)
library(tidyverse)

# helper function
add_boundary <- function(data, h3_index) {
  cbind(data, h3_to_geo_boundary_sf(data[[quote(h3_index)]])) %>% 
    sf::st_as_sf()
}


k_ring_distances("842a307ffffffff", 3) %>% 
  add_boundary(h3_index) %>% 
  ggplot(aes(fill = distance)) +
  geom_sf(color = "black", lwd = 0.2) +
  theme_minimal()

image

Fail to install

I failed to install h3-r and got messages as follows

do you have any idea to solve it?

here I attach my system environments too

  • $platform

    • [1] "x86_64-apple-darwin17.0"
  • $version.string

    • [1] "R version 4.0.2 (2020-06-22)"

`> devtools::install_github("crazycapivara/h3-r")
Downloading GitHub repo crazycapivara/h3-r@HEAD
✓ checking for file ‘/private/var/folders/83/wmn447_s2ys2jn94_p4cjbpc0000gn/T/RtmpwoYnzC/remotes6df76ab2fef/crazycapivara-h3-r-d9ed303/DESCRIPTION’ ...
─ preparing ‘h3’:
✓ checking DESCRIPTION meta-information ...
─ cleaning src
─ checking for LF line-endings in source and make files and shell scripts
─ checking for empty or unneeded directories
Removed empty directory ‘h3/sandbox’
─ building ‘h3_3.3.2.tar.gz’

  • installing source package ‘h3’ ...
    ** using staged installation
    ** libs
    /usr/local/opt/llvm/bin/clang++ -fopenmp -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I'/Library/Frameworks/R.framework/Versions/4.0/Resources/library/Rcpp/include' -I/usr/local/opt/gettext/include -I/usr/local/opt/llvm/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -fPIC -g -O3 -Wall -pedantic -std=c++11 -mtune=native -pipe -c RcppExports.cpp -o RcppExports.o
    /usr/local/opt/llvm/bin/clang++ -fopenmp -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I'/Library/Frameworks/R.framework/Versions/4.0/Resources/library/Rcpp/include' -I/usr/local/opt/gettext/include -I/usr/local/opt/llvm/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -fPIC -g -O3 -Wall -pedantic -std=c++11 -mtune=native -pipe -c h3_hex-ring.cpp -o h3_hex-ring.o
    h3_hex-ring.cpp:2:10: fatal error: 'h3/h3api.h' file not found
    #include <h3/h3api.h>
    ^~~~~~~~~~~~
    1 error generated.
    make: *** [h3_hex-ring.o] Error 1
    ERROR: compilation failed for package ‘h3’
  • removing ‘/Library/Frameworks/R.framework/Versions/4.0/Resources/library/h3’
    Error: Failed to install 'h3' from GitHub:
    (converted from warning) installation of package ‘/var/folders/83/wmn447_s2ys2jn94_p4cjbpc0000gn/T//RtmpwoYnzC/file6df116a48d6/h3_3.3.2.tar.gz’ had non-zero exit status`

Exhaustive set of indices?

It would be convenient to be able to get the full list of possible indices for a given resolution. I'm using this for a world sim, and I want to pre-populate the world. As is, I'm doing

get_ring <- function(x) {
		return(h3::k_ring(h3::geo_to_h3(c(0,0), resolution),x))
	}
	cells <- unique(unlist(sapply(c(sequence(9), seq(from=10, to=200, by=5)), get_ring)))
	cell_centers <- h3::h3_to_geo(cells)

But this is inefficient.

Thanks for this software!

Possible issue with h3::polyfill on multipolygons

I have been using h3::polyfill on some varied shapes and sometimes I get weird results on multipolygons. I do not know if this is the R code interface or the underlying h3 library. I am using sf:: tools within R.

I have a sample hosted for testing but have found numerous further examples.

item <- sf::st_read("https://gist.githubusercontent.com/hdrs/f273f2820d40d45f8ecda49840bec521/raw/9fdebbaefcb811b9c554a18e73e47d69abb8a54b/map.geojson")

This is a 4 polygon multipolygon. There are no holes. The components all obey the RH rule. The multipolygon and the components individually all test as valid with sf::st_is_valid().

h3ids <- h3::polyfill(item, res=10)
h3geom <- h3::h3_to_geo_boundary_sf(h3ids)

Here is a plot of what those h3 tiles look like

polyfill_issue_01

For reference, here is the result when I break apart the multipolygon with sf::st_cast(item, "POLYGON") and run h3::polyfill on each component. This plot seems correct.

polyfill_issue_02

Note, this also happens with a single polygon with multiple holes. I can update this with an example of that if it seems useful.

Remove `pushd` and `popd` from install instructions

These install instructions -

git clone https://github.com/crazycapivara/h3-r.git
pushd h3-r
chmod +x install-h3c.sh
# Install H3 C Library
./install-h3c.sh
# Install H3 for R
R -q -e 'devtools::install()'
popd
rm -rf h3-r

work when installing manually in bash or when running in a bash script.

However, they do not work when adding them to a Dockerfile. A workaround is to put the commands in a bash script, copy the bash script into the container on docker build... and then RUN bash script.sh. But this adds an unnecessary bash script to a docker build.

Suggest you replace pushd and popd with another method so that these commands can be put in a Dockerfile. I experimented with replacing pushd with cd but couldn't get the package to build.

Currently using the bash script.sh method in by docker build... but it would be great if I could eliminate it.

Unable to install on WSL

Followed instructions to build h3, and it works till
./install-h3c.sh

But then
R -q -e 'devtools::install()'
gives following error.
The same error is thrown if I directly try devtools::install_github("crazycapivara/h3-r") from within R.

Running /usr/lib/R/bin/R CMD INSTALL /tmp/Rtmpgg2BAk/h3_3.3.2.tar.gz \
  --install-tests
* installing to library ‘/usr/local/lib/R/site-library’
* installing *source* package ‘h3’ ...
** using staged installation
** libs
g++ -std=gnu++11 -I"/usr/share/R/include" -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include"   -fpic  -g -O2 -fdebug-prefix-map=/build/r-base-V28x5H/r-base-3.6.3=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c RcppExports.cpp -o RcppExports.o
g++ -std=gnu++11 -I"/usr/share/R/include" -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include"   -fpic  -g -O2 -fdebug-prefix-map=/build/r-base-V28x5H/r-base-3.6.3=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c h3_hex-ring.cpp -o h3_hex-ring.o
h3_hex-ring.cpp:2:10: fatal error: h3/h3api.h: No such file or directory
 #include <h3/h3api.h>
          ^~~~~~~~~~~~
compilation terminated.
/usr/lib/R/etc/Makeconf:177: recipe for target 'h3_hex-ring.o' failed
make: *** [h3_hex-ring.o] Error 1
ERROR: compilation failed for package ‘h3’
* removing ‘/usr/local/lib/R/site-library/h3’

Error: System command 'R' failed, exit status: 1, stdout & stderr were printed

Bundle H3 C Library

Bundle H3 C Library, so that we do not need the dependencies anymore and it can be submitted to CRAN.

Binding functions

  • geoToH3
  • h3ToGeo
  • h3ToGeoBoundary
  • kRing
  • kRingDistances
  • hexRing
  • polyfill
  • h3SetToMultiPolygon
  • degsToRads
  • radsToDegs
  • hexArea
  • edgeLength
  • numHexagons
  • h3GetResolution
  • h3GetBaseCell
  • h3IsValid
  • h3ToParent
  • h3ToChildren
  • compact
  • uncompact
  • h3IsResClassIII
  • h3IsPentagon
  • h3IndexesAreNeighbors
  • getH3UnidirectionalEdge
  • h3UnidirectionalEdgeIsValid
  • getOriginH3IndexFromUnidirectionalEdge
  • getDestinationH3IndexFromUnidirectionalEdge
  • getH3IndexesFromUnidirectionalEdge
  • getH3UnidirectionalEdgesFromHexagon
  • getH3UnidirectionalEdgeBoundary
  • h3Distance
  • h3Line
  • experimentalH3ToLocalIj
  • experimentalLocalIjToH3

Should we use h3-r or h3forr?

Hello,

We're using h3forr in several projects. It's working really well, but for some of our faster code the library is a bit of a speed bottleneck. No complaints, it's working great otherwise.

It occurs to me that this library might perform significantly faster than h3forr. Would this be the case? Our most used functions are k-ring, polyfill and parent-child operations.

Are there any other reasons to adopt one over the other? Which do you see as being better supported going forward?

Does h3-r build h3api.h?

It's wonderful that this works on Windows now. Thank you!

When installing, does the package build h3api.h or just the shared library h3.dll? If it does, any idea where it might do that on Windows? I can't find it.

I'd like to add an h3 directory containing all of the .h files to my PATH environment variable so H3 can be used by other packages, for example, https://github.com/JosiahParry/placekey

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.