Skip to content

Geohash

A Geohash is a WGS84 cell in the Geohash grid. Length is stored on the value, so ezs42 and ezs420 are different cells. OsmShortlink is the OpenStreetMap shortlink /go/ encoding. Different alphabet, carries zoom, no parent or neighbors.

See the API reference.

commonMain {
dependencies {
implementation("org.maplibre.spatialk:geohash:0.8.0")
}
}

Geohash.of returns the cell of a given length that contains a longitude and latitude. Altitude does not change the cell.

val cell =
Geohash.of(
Position(longitude = 10.40744, latitude = 57.64911),
length = 11,
)
println(cell.text)
// u4pruydqqvj

Geohash.parse folds ASCII case. text comes back lowercase. The cell also has center, boundingBox, parent, and children.

val cell = Geohash.parse("ezs42")
val center = cell.center
val boundingBox = cell.boundingBox
val parent = cell.parent
val children = cell.children

neighbors is one property per compass direction. East and west always exist. A step past a pole is null. offsetBy moves more than one cell at a time.

val cell = Geohash.parse("ezs42")
val east = cell.neighbors.east
val existingNeighbors = cell.neighbors.toList()
val twoCellsWest = cell.offsetBy(east = -2, north = 0)

position in cell is true when encoding that position at the same length yields the same cell. A longer hash whose prefix matches is inside too.

val cell = Geohash.parse("ezs42")
val containsCenter = cell.center in cell
val containsChild = Geohash.parse("ezs420") in cell

JSON for both types is the canonical string. "ezs42" or "0EEQjE--", not an object.

val cell = Geohash.parse("ezs42")
val json = Json.encodeToString(cell)
val parsed = Json.decodeFromString<Geohash>(json)

OsmShortlink.parse takes a bare code, a /go/ path, or an osm.org / openstreetmap.org URL. Old @ and = still parse. text uses ~ and -.

val shortlink = OsmShortlink.parse("https://osm.org/go/0EEQjE--")
println(shortlink.zoom)
println(shortlink.center)
val encoded =
OsmShortlink.of(
Position(longitude = 0.054, latitude = 51.510),
zoom = 9,
)