Convert latitude/longitude coordinates into geohash-encoded strings

gh_encode(latitude, longitude, precision = 6L)

Arguments

latitude

numeric vector of input latitude (y) coordinates. Must be in [-90, 90).

longitude

numeric vector of input longitude (x) coordinates. Should be in [-180, 180).

precision

Positive integer controlling the 'zoom level' – how many characters should be used in the output. Either a single value applied to all coordinates, or a vector the same length as latitude/longitude giving a per-coordinate precision.

Value

character vector of geohashes corresponding to the input. NA in gives NA out.

Details

precision is limited to at most 25. This level of precision encodes locations on the globe at a nanometer scale and is already more than enough for basically all applications.

Longitudes outside [-180, 180) will be wrapped appropriately to the standard longitude grid.

References

http://geohash.org/ ( Gustavo Niemeyer's original geohash service )

Author

Michael Chirico

Examples

# scalar input is treated as a vector
gh_encode(2.345, 6.789)
#> [1] "s0kv66"

# geohashes are left-closed, right-open, so boundary coordinates are
#   associated to the east and/or north
gh_encode(0, 0)
#> [1] "s00000"

# precision can vary by coordinate
gh_encode(c(2.345, 0), c(6.789, 0), precision = c(4L, 8L))
#> [1] "s0kv"     "s0000000"