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.
Installation
Section titled “Installation”commonMain { dependencies { implementation("org.maplibre.spatialk:geohash:0.8.0") }}dependencies { implementation("org.maplibre.spatialk:geohash-jvm:0.8.0")}Encode a position
Section titled “Encode a position”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)// u4pruydqqvjParse and inspect a cell
Section titled “Parse and inspect a cell”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.centerval boundingBox = cell.boundingBoxval parent = cell.parentval children = cell.childrenMove between cells
Section titled “Move between cells”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.eastval existingNeighbors = cell.neighbors.toList()val twoCellsWest = cell.offsetBy(east = -2, north = 0)Test containment
Section titled “Test containment”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 cellval containsChild = Geohash.parse("ezs420") in cellSerialize as text
Section titled “Serialize as text”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)OpenStreetMap shortlinks
Section titled “OpenStreetMap shortlinks”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, )