Skip to content
This repository was archived by the owner on Jul 12, 2024. It is now read-only.

Commit 3c7ead2

Browse files
committed
chore(gdds-geohash): Geohash polish
1 parent f1454c4 commit 3c7ead2

File tree

4 files changed

+431
-0
lines changed

4 files changed

+431
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Geohash Functions
2+
3+
Note: This page should be moved to documentation repo.
4+
5+
Geohash: Gustavo Niemeyer’s geocoding system.
6+
7+
### API Reference
8+
9+
##### geohashIsValid
10+
11+
Check if a value is a string representing an geohash
12+
13+
```typescript
14+
geohashIsValid(value: string): boolean;
15+
```
16+
17+
- `value` value to test
18+
- Returns false if not a geohash, true means it can be a geohash
19+
20+
##### geoToGeohash
21+
22+
Encode latitude/longitude to geohash, either to specified precision or to automatically
23+
evaluated precision.
24+
25+
```typescript
26+
geoToGeohash(lngLat: number[], precision?: number): string;
27+
geoToGeohash(lng: number, lat: number, precision?: number): string;
28+
```
29+
30+
- `lat` - Latitude in degrees.
31+
- `lon` - Longitude in degrees.
32+
- `precision` - Number of characters in resulting geohash.
33+
34+
- Returns Geohash of supplied latitude/longitude
35+
36+
```typescript
37+
const geohash = geoToGeohash([0.119, 52.205], 7); // => 'u120fxw'
38+
```
39+
40+
##### geohashToGeo
41+
42+
Decode geohash to latitude/longitude (location is approximate centre of geohash cell,
43+
to reasonable precision).
44+
45+
```typescript
46+
geohashToGeo(geohash: string): number[];
47+
```
48+
49+
- `geohash` - Geohash string to be converted to latitude/longitude.
50+
- Returns `[lng, lat]` Center of geohashed location.
51+
52+
```typescript
53+
const latlon = Geohash.decode('u120fxw'); // => { lat: 52.205, lon: 0.1188 }
54+
```
55+
56+
##### geohashToBounds
57+
58+
Returns SW/NE latitude/longitude bounds of specified geohash.
59+
60+
```typescript
61+
geohashToBounds(geohash: string, closed?: boolean): geojson;
62+
```
63+
64+
- `geohash` - Cell that bounds are required of.
65+
- `closed` - Whether first vertex should be duplicated at end
66+
67+
- Returns Polygon representing bounds of geohash
68+
69+
##### geohashToNeighbor
70+
71+
Determines adjacent cell in given direction.
72+
73+
```typescript
74+
geohashToNeighbor(geohash: string, direction: string): string;
75+
```
76+
77+
- `geohash` - Cell to which adjacent cell is required.
78+
- `direction` - Direction from geohash (`'N'`/`'S'`/`'E'`/`'W'`).
79+
80+
- Returns Geocode of adjacent cell.
81+
82+
##### geohashToNeighbors
83+
84+
Returns all 8 adjacent cells to specified geohash.
85+
86+
```typescript
87+
geohashToNeighbors(geohash: string): string[];
88+
```
89+
90+
- `geohash` - Geohash neighbors are required of.
91+
92+
- Returns array of 8 adjacent geohashes
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright 2022 Foursquare Labs, Inc. All Rights Reserved.
2+
3+
export {
4+
geohashIsValid,
5+
geoToGeohash,
6+
geohashToGeo,
7+
geohashToBounds,
8+
geohashToNeighbor,
9+
geohashToNeighbors
10+
} from './geohash';

0 commit comments

Comments
 (0)