geokit/geohash
Geohash encoding and decoding. A geohash names a rectangular cell
with a string over the alphabet 0123456789bcdefghjkmnpqrstuvwxyz.
Each character adds 5 bits, alternating between longitude and
latitude, so a longer hash names a smaller cell inside the shorter
one. Decoding ignores the case of ASCII letters.
Types
A direction for neighbor.
pub type Direction {
North
South
East
West
NorthEast
NorthWest
SouthEast
SouthWest
}
Constructors
-
North -
South -
East -
West -
NorthEast -
NorthWest -
SouthEast -
SouthWest
The reason a geohash operation failed.
pub type GeohashError {
PrecisionOutOfRange(precision: Int)
EmptyHash
InvalidCharacter(character: String, position: Int)
NoNeighborBeyondPole
}
Constructors
-
PrecisionOutOfRange(precision: Int)The precision was outside [1, 12].
-
EmptyHashThe hash was the empty string.
-
InvalidCharacter(character: String, position: Int)A character outside the geohash alphabet, at a 0-based position.
-
NoNeighborBeyondPoleA step north of the northernmost row or south of the southernmost row was asked for.
The eight cells around a geohash cell, as returned by
neighbors.
pub type Neighbors {
Neighbors(
north: String,
south: String,
east: String,
west: String,
north_east: String,
north_west: String,
south_east: String,
south_west: String,
)
}
Constructors
-
Neighbors( north: String, south: String, east: String, west: String, north_east: String, north_west: String, south_east: String, south_west: String, )
Values
pub fn decode(
hash: String,
) -> Result(geokit.LatLng, GeohashError)
Returns the center of the cell named by hash.
Examples
decode("xn76urx6")
|> result.map(fn(center) {
#(geokit.latitude(center), geokit.longitude(center))
})
// -> Ok(#(35.68127632141113, 139.76720809936523))
decode("XN7A")
// -> Error(InvalidCharacter(character: "A", position: 3))
pub fn decode_bounds(
hash: String,
) -> Result(geokit.BoundingBox, GeohashError)
Returns the cell named by hash.
Examples
decode_bounds("xn")
|> result.map(fn(cell) {
#(geokit.latitude(cell.south_west), geokit.longitude(cell.north_east))
})
// -> Ok(#(33.75, 146.25))
decode_bounds("")
// -> Error(EmptyHash)
pub fn encode(
point: geokit.LatLng,
precision precision: Int,
) -> Result(String, GeohashError)
Returns the geohash of the cell that contains point. precision is
the number of characters, in [1, 12]. A point exactly on a cell edge
belongs to the cell to its north or east.
Examples
let tokyo = geokit.wrap_lat_lng(latitude: 35.6812, longitude: 139.7671)
encode(tokyo, precision: 8)
// -> Ok("xn76urx6")
encode(tokyo, precision: 13)
// -> Error(PrecisionOutOfRange(precision: 13))
pub fn neighbor(
hash: String,
toward direction: Direction,
) -> Result(String, GeohashError)
Returns the cell next to hash in the given direction, at the same
precision. East and west wrap across the antimeridian. Returns
Error(NoNeighborBeyondPole) for a step past a pole, and
Error(EmptyHash) or Error(InvalidCharacter(..)) for a bad hash.
Examples
neighbor("xn76urx6", toward: North)
// -> Ok("xn76urx7")
neighbor("b", toward: North)
// -> Error(NoNeighborBeyondPole)
pub fn neighbors(hash: String) -> Result(Neighbors, GeohashError)
Returns the eight cells around hash. A cell in the northernmost or
southernmost row has no cell on one side, so the result is
Error(NoNeighborBeyondPole) there. Use neighbor to
get the cells that do exist.
Examples
neighbors("xn76urx6") |> result.map(fn(cells) { cells.east })
// -> Ok("xn76urxd")
neighbors("b")
// -> Error(NoNeighborBeyondPole)