Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(geohash, quadkey): New geohash and quadkey modules #319

Merged
merged 3 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions docs/whats-new.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@

## v3.7 (In Development)

**`@math.gl/geohash`** (NEW)

- New module with math for the GeoHash DGGS (Discrete Global Grid System).

**`@math.gl/s2`** (NEW MODULE)

- New module that contains a lightweight implementation of the S2 DGGS (Discrete Global Grid System).

**`@math.gl/quadkey`** (NEW)

- New module with math for the quadkey DGGS (Discrete Global Grid System).

**`@math.gl/core`

- `config` is now truly global (stored on `globalThis`).
Expand All @@ -35,10 +47,6 @@

- Add `isTypedArray()` and `isNumericArray()` utilities that both check values and return properly restricted types to help write strictly typed code (avoids the `DataView` issue with `ArrayBuffer.isView()`).

**`@math.gl/s2`** (NEW MODULE)

- New module that contains a lightweight implementation of the S2 DGGS (Discrete Global Grid System).

## v3.6

Target Release Date: Q4, 2021
Expand Down
7 changes: 7 additions & 0 deletions modules/geohash/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# @math.gl/sun

[math.gl](https://math.gl/docs) is a suite of math modules for 3D and geospatial applications.

This module contains math for the GeoHash DGGS (Discrete Global Grid System).

For documentation please visit the [website](https://math.gl).
19 changes: 19 additions & 0 deletions modules/geohash/docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Overview

`@math.gl/geohash` is a JavaScript library with

## Installation

```bash
npm install @math.gl/sun
```

## Usage

```js
import {getGeohashPolygon} from '@math.gl/quadkey';
const polygon = getGeohashPolygon(geohash);
```

## Attribution

3 changes: 3 additions & 0 deletions modules/geohash/docs/api-reference/geohash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# geohash

> TBA
31 changes: 31 additions & 0 deletions modules/geohash/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "@math.gl/geohash",
"description": "Math for the GeoHash DGGS (Discrete Global Grid System)",
"license": "MIT",
"publishConfig": {
"access": "public"
},
"version": "3.6.3",
"keywords": [
"javascript",
"math",
"geospatial",
"sun",
"suncalc",
"solar"
],
"repository": {
"type": "git",
"url": "https://github.com/uber-web/math.gl.git"
},
"types": "dist/index.d.ts",
"main": "dist/es5/index.js",
"module": "dist/esm/index.js",
"files": [
"dist",
"src"
],
"dependencies": {
"@babel/runtime": "^7.12.0"
}
}
58 changes: 58 additions & 0 deletions modules/geohash/src/geohash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// math.gl, MIT license

/* eslint-disable max-depth */

const BASE32_CODES = '0123456789bcdefghjkmnpqrstuvwxyz';
const BASE32_CODES_DICT: Record<string, number> = {};
for (let i = 0; i < BASE32_CODES.length; i++) {
BASE32_CODES_DICT[BASE32_CODES.charAt(i)] = i;
}

const MIN_LAT = -90;
const MAX_LAT = 90;
const MIN_LON = -180;
const MAX_LON = 180;

// Adapted from ngeohash decode_bbox
export function getGeohashBounds(geohash: string): number[] {
let isLon = true;
let maxLat = MAX_LAT;
let minLat = MIN_LAT;
let maxLon = MAX_LON;
let minLon = MIN_LON;
let mid: number;

let hashValue = 0;
for (let i = 0, l = geohash.length; i < l; i++) {
const code = geohash[i].toLowerCase();
hashValue = BASE32_CODES_DICT[code];

for (let bits = 4; bits >= 0; bits--) {
const bit = (hashValue >> bits) & 1;
if (isLon) {
mid = (maxLon + minLon) / 2;
if (bit === 1) {
minLon = mid;
} else {
maxLon = mid;
}
} else {
mid = (maxLat + minLat) / 2;
if (bit === 1) {
minLat = mid;
} else {
maxLat = mid;
}
}
isLon = !isLon;
}
}

return [minLat, minLon, maxLat, maxLon];
}

export function getGeohashPolygon(geohash: string): number[] {
const [s, w, n, e] = getGeohashBounds(geohash);

return [e, n, e, s, w, s, w, n, e, n];
}
1 change: 1 addition & 0 deletions modules/geohash/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {getGeohashBounds, getGeohashPolygon} from './geohash';
Loading