Convert geographic coordinates (longitude/latitude) to Geohash strings, or convert Geohash strings back to coordinates.

geohash_fwd(x, len = 12L)

geohash_rev(geohash)

geohash_resolution(len)

geohash_length(resolution = NULL, lat_resolution = NULL, lon_resolution = NULL)

Arguments

x

A two-column matrix or data frame of coordinates (longitude, latitude) in decimal degrees, or a list with longitude and latitude components. Can also be a length-2 numeric vector for a single point.

len

Integer specifying the length of the Geohash string (1-18). Default is 12, which gives approximately 19mm precision. Can be a vector to specify different lengths for each point.

geohash

Character vector of Geohash strings to convert back to coordinates.

resolution

Numeric. Desired resolution in degrees for geohash_length().

lat_resolution

Numeric. Desired latitude resolution in degrees.

lon_resolution

Numeric. Desired longitude resolution in degrees.

Value

  • geohash_fwd(): Character vector of Geohash strings.

  • geohash_rev(): Data frame with columns:

    • lon: Longitude in decimal degrees (center of cell)

    • lat: Latitude in decimal degrees (center of cell)

    • len: Length of the Geohash string

    • lat_resolution: Latitude resolution in degrees (half-height of cell)

    • lon_resolution: Longitude resolution in degrees (half-width of cell)

  • geohash_resolution(): Data frame with columns:

    • len: Geohash length

    • lat_resolution: Latitude resolution in degrees

    • lon_resolution: Longitude resolution in degrees

  • geohash_length(): Integer, minimum Geohash length to achieve the specified resolution.

Details

Geohash is a geocoding system that encodes geographic coordinates into a short string of letters and digits. It has a useful property: truncating a Geohash reduces precision but the truncated code still refers to a location containing the original point.

The Geohash length determines precision:

  • Length 1: ~5000 km

  • Length 4: ~20 km

  • Length 6: ~610 m

  • Length 8: ~19 m

  • Length 10: ~0.6 m

  • Length 12: ~19 mm (default)

  • Length 18: ~0.0001 mm (maximum)

Both geohash_fwd() and geohash_rev() are fully vectorized.

See also

mgrs_fwd() for Military Grid Reference System encoding, which provides a different grid-based coordinate system.

Examples

# Single point conversion
(gh <- geohash_fwd(c(147.325, -42.881)))
#> [1] "r22u03yb164p"
geohash_rev(gh)
#>       lon     lat len lat_resolution lon_resolution
#> 1 147.325 -42.881  12   1.676381e-07   3.352761e-07

# Multiple points with varying precision
pts <- cbind(
  lon = c(147, -74, 0),
  lat = c(-42, 40.7, 51.5)
)
geohash_fwd(pts, len = c(6, 8, 12))
#> [1] "r28914"       "dr5rs14y"     "u10hbp210pb4"

# Truncation preserves containment
gh <- geohash_fwd(c(147.325, -42.881), len = 12)
substr(gh, 1, 6)  # Lower precision, but still contains original point
#> [1] "r22u03"

# Resolution for different lengths
geohash_resolution(1:12)
#>    len lat_resolution lon_resolution
#> 1    1   4.500000e+01   4.500000e+01
#> 2    2   5.625000e+00   1.125000e+01
#> 3    3   1.406250e+00   1.406250e+00
#> 4    4   1.757812e-01   3.515625e-01
#> 5    5   4.394531e-02   4.394531e-02
#> 6    6   5.493164e-03   1.098633e-02
#> 7    7   1.373291e-03   1.373291e-03
#> 8    8   1.716614e-04   3.433228e-04
#> 9    9   4.291534e-05   4.291534e-05
#> 10  10   5.364418e-06   1.072884e-05
#> 11  11   1.341105e-06   1.341105e-06
#> 12  12   1.676381e-07   3.352761e-07

# Find length needed for ~1km precision
geohash_length(1/111)  # ~1 degree / 111 km
#> [1] 7