|
| 1 | +// Copyright 2022 Foursquare Labs, Inc. All Rights Reserved. |
| 2 | + |
| 3 | +import Geohash from './latlon-geohash'; |
| 4 | + |
| 5 | +const BASE32_GEOHASH_REGEX = /^[0-9bcdefghjkmnpqrstuvwxyz]+$/; |
| 6 | + |
| 7 | +/** |
| 8 | + * Check if a value is a string representing an geohash |
| 9 | + * @returns false if not a geohash, true means it can be a geohash |
| 10 | + */ |
| 11 | +export function geohashIsValid(geohash: unknown): boolean { |
| 12 | + return typeof geohash === 'string' && BASE32_GEOHASH_REGEX.test(geohash); |
| 13 | +} |
| 14 | +/** |
| 15 | + * Encodes latitude/longitude to geohash, either to specified precision or to automatically |
| 16 | + * evaluated precision. |
| 17 | + * |
| 18 | + * @param lng Longitude in degrees. |
| 19 | + * @param lat Latitude in degrees. |
| 20 | + * @param precision Number of characters in resulting geohash. |
| 21 | + * @returns Geohash of supplied latitude/longitude. |
| 22 | + * @throws Invalid geohash. |
| 23 | + * |
| 24 | + * @example |
| 25 | + * const geohash = geoToGeohash([0.119, 52.205], 7); // => 'u120fxw' |
| 26 | + */ |
| 27 | +export function geoToGeohash(lngLat: [number, number], precision?: number): string; |
| 28 | +export function geoToGeohash(lng: number, lat: number, precision?: number): string; |
| 29 | +export function geoToGeohash( |
| 30 | + lng: number | [number, number], |
| 31 | + latOrPrecision?: number, |
| 32 | + precision?: number |
| 33 | +): string { |
| 34 | + if (Array.isArray(lng)) { |
| 35 | + return Geohash.encode(lng[1], lng[0], latOrPrecision); |
| 36 | + } |
| 37 | + return Geohash.encode(Number(latOrPrecision), lng, precision); |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Decode geohash to latitude/longitude (location is approximate centre of geohash cell, |
| 42 | + * to reasonable precision). |
| 43 | + * |
| 44 | + * @param geohash - Geohash string to be converted to latitude/longitude. |
| 45 | + * @returns [lng, lat] Center of geohashed location. |
| 46 | + * @throws Invalid geohash |
| 47 | + */ |
| 48 | +export function geohashToGeo(geohash: string): [number, number] { |
| 49 | + const latlng = Geohash.decode(geohash); |
| 50 | + return [latlng.lon, latlng.lat]; |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Returns SW/NE lng/lat bounds of specified geohash. |
| 55 | + * |
| 56 | + * @param geohash - Cell that bounds are required of. |
| 57 | + * @param closed - Whether first vertex should be duplicated at end |
| 58 | + * @returns Polygon representing bounds of geohash, in [lng, lat] order |
| 59 | + * @throws Invalid geohash. |
| 60 | + */ |
| 61 | +export function geohashToBounds(geohash: string): [number, number][] { |
| 62 | + const extents = Geohash.bounds(geohash); |
| 63 | + return [ |
| 64 | + [extents.sw.lon, extents.sw.lat], |
| 65 | + [extents.sw.lon, extents.ne.lat], |
| 66 | + [extents.ne.lon, extents.ne.lat], |
| 67 | + [extents.ne.lon, extents.sw.lat], |
| 68 | + // close polygon |
| 69 | + [extents.sw.lon, extents.sw.lat] |
| 70 | + ]; |
| 71 | +} |
| 72 | +/** |
| 73 | + * Determines adjacent cell in given direction. |
| 74 | + * |
| 75 | + * @param geohash - Cell to which adjacent cell is required. |
| 76 | + * @param direction - Direction from geohash (n/s/e/w). |
| 77 | + * @returns Geocode of adjacent cell. |
| 78 | + * @throws Invalid geohash. |
| 79 | + */ |
| 80 | +export function geohashToNeighbor(geohash: string, direction: 'n' | 's' | 'e' | 'w'): string { |
| 81 | + return Geohash.adjacent(geohash, direction); |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * Returns all 8 adjacent cells to specified geohash. |
| 86 | + * |
| 87 | + * @param geohash - Geohash neighbors are required of. |
| 88 | + * @returns array of 8 adjacent geohashes |
| 89 | + * @throws Invalid geohash. |
| 90 | + */ |
| 91 | +export function geohashToNeighbors(geohash: string): string[] { |
| 92 | + const n = geohashToNeighbor(geohash, 'n'); |
| 93 | + const e = geohashToNeighbor(geohash, 'e'); |
| 94 | + const s = geohashToNeighbor(geohash, 's'); |
| 95 | + const w = geohashToNeighbor(geohash, 'w'); |
| 96 | + |
| 97 | + return [ |
| 98 | + n, |
| 99 | + geohashToNeighbor(n, 'e'), |
| 100 | + e, |
| 101 | + geohashToNeighbor(s, 'e'), |
| 102 | + s, |
| 103 | + geohashToNeighbor(s, 'w'), |
| 104 | + w, |
| 105 | + geohashToNeighbor(n, 'w') |
| 106 | + ]; |
| 107 | +} |
0 commit comments