| 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-01 07:07:17 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 28. 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)# 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)
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 packages in R, sp and sf.
gh_to_sp(geohashes) gh_to_spdf(...) gh_to_sf(...) gh_covering(SP, precision = 6L, minimal = FALSE) ## Default S3 method: gh_to_spdf(geohashes, ...) ## S3 method for class 'data.frame' gh_to_spdf(gh_df, gh_col = 'gh', ...)gh_to_sp(geohashes) gh_to_spdf(...) gh_to_sf(...) gh_covering(SP, precision = 6L, minimal = FALSE) ## Default S3 method: gh_to_spdf(geohashes, ...) ## S3 method for class 'data.frame' gh_to_spdf(gh_df, gh_col = 'gh', ...)
geohashes |
|
... |
Arguments for subsequent methods. |
SP |
A |
precision |
|
minimal |
|
gh_df |
|
gh_col |
|
gh_to_sp 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.
gh_to_spdf.data.frame will use match.ID = FALSE in the call to SpatialPolygonsDataFrame. Please file an issue if you'd like this to be more flexible.
gh_to_sf is just a wrapper of st_as_sf around gh_to_spdf; as such it requires both sp and sf packages to work.
For gh_to_sp, a SpatialPolygons object.
For gh_to_spdf, a SpatialPolygonsDataFrame object.
For gh_to_sf, a sf object.
# get the neighborhood of this geohash in downtown Apia as an sp object downtown = '2jtc5x' apia_nbhd = unlist(gh_neighbors(downtown)) apia_sp = gh_to_sp(apia_nbhd) # all geohashes covering a random sampling within Apia: apia_covering = gh_covering(smp <- sp::spsample(apia_sp, 10L, 'random')) apia_sf = gh_to_sf(apia_nbhd)# get the neighborhood of this geohash in downtown Apia as an sp object downtown = '2jtc5x' apia_nbhd = unlist(gh_neighbors(downtown)) apia_sp = gh_to_sp(apia_nbhd) # all geohashes covering a random sampling within Apia: apia_covering = gh_covering(smp <- sp::spsample(apia_sp, 10L, 'random')) apia_sf = gh_to_sf(apia_nbhd)
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)