| Title: | Tools for Working with Geohashes |
|---|---|
| Description: | Tools for working with Gustavo Niemeyer's geohash coordinate system, including API for interacting with other common R GIS libraries. |
| Authors: | Michael Chirico [aut, cre], Dmitry Shkolnik [ctb] |
| Maintainer: | Michael Chirico <[email protected]> |
| License: | MPL-2.0 | file LICENSE |
| Version: | 0.3.3 |
| Built: | 2026-06-10 20:35:58 UTC |
| Source: | https://github.com/michaelchirico/geohashtools |
Convert geohash-encoded strings into latitude/longitude coordinates
gh_decode(geohashes, include_delta = FALSE, coord_loc = "c")gh_decode(geohashes, include_delta = FALSE, coord_loc = "c")
geohashes |
|
include_delta |
|
coord_loc |
|
coord_loc can be the cell's center ('c' or 'centroid'), or it can be any of the 8 corners
(e.g. 's'/'south' for the midpoint of the southern boundary of the cell, or 'ne'/'northeast' for the
upper-right corner.
For factor input, decoding will be done on the levels for efficiency.
list with the following entries:
latitude |
|
longitude |
|
delta_latitude |
|
delta_longitude |
|
Michael Chirico
http://geohash.org/ ( Gustavo Niemeyer's original geohash service )
# Riddle me this gh_decode('stq4s8c') # Cell half-widths might be convenient to include for downstream analysis gh_decode('tjmd79', include_delta = TRUE)# Riddle me this gh_decode('stq4s8c') # Cell half-widths might be convenient to include for downstream analysis gh_decode('tjmd79', include_delta = TRUE)
Convert latitude/longitude coordinates into geohash-encoded strings
gh_encode(latitude, longitude, precision = 6L)gh_encode(latitude, longitude, precision = 6L)
latitude |
|
longitude |
|
precision |
Positive |
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.
character vector of geohashes corresponding to the input. NA in gives NA out.
Michael Chirico
http://geohash.org/ ( Gustavo Niemeyer's original geohash service )
# scalar input is treated as a vector gh_encode(2.345, 6.789) # geohashes are left-closed, right-open, so boundary coordinates are # associated to the east and/or north gh_encode(0, 0) # precision can vary by coordinate gh_encode(c(2.345, 0), c(6.789, 0), precision = c(4L, 8L))# scalar input is treated as a vector gh_encode(2.345, 6.789) # geohashes are left-closed, right-open, so boundary coordinates are # associated to the east and/or north gh_encode(0, 0) # precision can vary by coordinate gh_encode(c(2.345, 0), c(6.789, 0), precision = c(4L, 8L))
Return the geohashes adjacent to input geohashes
gh_neighbors(geohashes, self = TRUE) gh_neighbours(geohashes, self = TRUE)gh_neighbors(geohashes, self = TRUE) gh_neighbours(geohashes, self = TRUE)
geohashes |
|
self |
Should the input also be returned as a list element? Convenient for one-line usage / piping |
North/south-pole adjacent geohashes are missing three of their neighbors; these will be returned as NA_character_.
list with character vector entries in the direction relative to the input geohashes indicated by their name
(e.g. value$south gives all of the southern neighbors of the input geohashes).
The order is self (if self = TRUE), southwest, south, southeast, west, east, northwest, north,
northeast (reflecting an easterly, then northerly traversal of the neighborhod).
Michael Chirico
http://geohash.org/ ( Gustavo Niemeyer's original geohash service )
gh_neighbors('d7q8u4')gh_neighbors('d7q8u4')
These functions smooth the gateway between working with geohashes and geospatial information built for the major geospatial package in R, sf::sf.
gh_to_sf(...) ## Default S3 method: gh_to_sf(geohashes, ...) ## S3 method for class 'data.frame' gh_to_sf(gh_df, gh_col = "gh", ...) gh_covering(x, precision = 6L, minimal = FALSE)gh_to_sf(...) ## Default S3 method: gh_to_sf(geohashes, ...) ## S3 method for class 'data.frame' gh_to_sf(gh_df, gh_col = "gh", ...) gh_covering(x, precision = 6L, minimal = FALSE)
... |
Arguments for subsequent methods. |
geohashes |
|
gh_df |
|
gh_col |
|
x |
An |
precision |
|
minimal |
|
gh_to_sf relies on the gh_decode() function. Note in particular that this function accepts any length
of geohash (geohash-6, geohash-4, etc.) and is agnostic to potential overlap, though duplicates will be caught
and excluded.
For gh_to_sf, a sf::sf object.
# get the neighborhood of this geohash in downtown Apia as an sf object downtown = '2jtc5x' apia_nbhd = unlist(gh_neighbors(downtown)) apia_sf = gh_to_sf(apia_nbhd) # all geohashes covering a random sampling within Apia: apia_covering = gh_covering(sf::st_sample(apia_sf, 10L, type = 'random'))# get the neighborhood of this geohash in downtown Apia as an sf object downtown = '2jtc5x' apia_nbhd = unlist(gh_neighbors(downtown)) apia_sf = gh_to_sf(apia_nbhd) # all geohashes covering a random sampling within Apia: apia_covering = gh_covering(sf::st_sample(apia_sf, 10L, type = 'random'))
Various common functions that arise when working often with geohashes
gh_delta(precision)gh_delta(precision)
precision |
|
Length-2 numeric vector; the first element is the latitude (y-coordinate) half-width at the input
precision, the second element is the longitude (x-coordinate).
Caveat coder: not much is done in the way of consistency checking since this is a convenience function. So e.g. real-valued "precision"s will give results.
Michael Chirico
http://geohash.org/ ( Gustavo Niemeyer's original geohash service )
gh_delta(6)gh_delta(6)