Skip to content

Commit

Permalink
PR Feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
jmgaya committed Sep 26, 2024
1 parent f4871a7 commit ef8d9b8
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 18 deletions.
4 changes: 1 addition & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,4 @@ yarn test

1. Create a new version: `yarn version [ major | minor | patch | prerelease ]`

2. Commit, tag, and push to GitHub: `yarn postversion`

3. Execute `yarn publish`
2. Execute `yarn publish`
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@
"build:umd": "webpack --config tsconfig/webpack.config.cjs",
"lint": "prettier --check src",
"test": "yarn lint && yarn test-fast",
"test-fast": "ts-node node_modules/tape/bin/tape test/**/*.spec.js"
"test-fast": "ts-node node_modules/tape/bin/tape test/**/*.spec.js",
"prepublishOnly": "yarn build"
},
"browser": {
"jsdom": false
},
"devDependencies": {
"@babel/register": "^7.13.0",
"@types/geojson": "^7946.0.14",
"babel-loader": "^8.0.0",
"babel-preset-minify": "^0.5.0",
"prettier": "^2.4.1",
Expand All @@ -52,10 +54,14 @@
"webpack-cli": "^4.8.0"
},
"engines": {
"node": ">=14"
"node": ">=18"
},
"dependencies": {
"@mapbox/tile-cover": "3.0.1"
},
"packageManager": "[email protected]"
"packageManager": "[email protected]",
"volta": {
"node": "18.19.0",
"yarn": "1.22.22"
}
}
23 changes: 12 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {tiles} from '@mapbox/tile-cover';
import {Polygon} from 'geojson';

const B = [
0x5555555555555555n,
Expand All @@ -13,16 +14,6 @@ const S = [0n, 1n, 2n, 4n, 8n, 16n];
type Quadbin = bigint;
type Tile = {x: number; y: number; z: number};

function cellToBoundingBox(cell: bigint) {
const tile = cellToTile(cell);
const xmin = tileToLongitude(tile, 0);
const xmax = tileToLongitude(tile, 1);
const ymin = tileToLatitude(tile, 1);
const ymax = tileToLatitude(tile, 0);

return [xmin, ymin, xmax, ymax];
}

function tileToLongitude(tile: ReturnType<typeof cellToTile>, offset: number) {
const {x, z} = tile;
return 180 * ((2.0 * (x + offset)) / (1 << z) - 1.0);
Expand All @@ -34,6 +25,16 @@ function tileToLatitude(tile: ReturnType<typeof cellToTile>, offset: number) {
return 360 * (Math.atan(expy) / Math.PI - 0.25);
}

function cellToBoundingBox(cell: bigint) {
const tile = cellToTile(cell);
const xmin = tileToLongitude(tile, 0);
const xmax = tileToLongitude(tile, 1);
const ymin = tileToLatitude(tile, 1);
const ymax = tileToLatitude(tile, 0);

return [xmin, ymin, xmax, ymax];
}

export function hexToBigInt(hex: string): bigint {
return BigInt(`0x${hex}`);
}
Expand Down Expand Up @@ -111,7 +112,7 @@ export function geometryToCells(geometry, resolution: bigint): Quadbin[] {
}).map(([x, y, z]) => tileToCell({x, y, z}));
}

export function quadbinToBoundary(cell: bigint) {
export function cellToBoundary(cell: bigint): Polygon {
const bbox = cellToBoundingBox(cell);
const boundary = [
[bbox[0], bbox[3]],
Expand Down
40 changes: 39 additions & 1 deletion test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import test from 'tape';
import {tileToCell, cellToTile, cellToParent, geometryToCells, getResolution} from 'quadbin';
import {
tileToCell,
cellToTile,
cellToParent,
geometryToCells,
getResolution,
cellToBoundary
} from 'quadbin';

import {tileToQuadkey} from './quadkey-utils.js';

Expand All @@ -9,6 +16,8 @@ const TEST_TILES = [
{x: 1023, y: 2412, z: 23, q: 5291729562728627583n}
];

const ANY_QUADBIN = BigInt(524800);

test('Quadbin conversion', async t => {
for (const {x, y, z, q} of TEST_TILES) {
const tile = {x, y, z};
Expand Down Expand Up @@ -70,3 +79,32 @@ test('Quadbin geometryToCells', async t => {
}
t.end();
});

test('cellToBoundary works with quadbins', t => {
const result = cellToBoundary(ANY_QUADBIN);

t.equal(result.type, 'Polygon', 'Should return a Polygon');
t.ok(Array.isArray(result.coordinates), 'Coordinates should be an array');
t.equal(result.coordinates.length, 1, 'Should have one boundary array');

t.ok(result.coordinates[0].length === 5, 'Boundary should have 5 points');

t.end();
});

test('cellToBoundary works with Quadbins near the antimeridian', t => {
for (const quadbin of [
BigInt(536903670), // Longitude near +180°
BigInt(536870921) // Longitude near -180°
]) {
const result = cellToBoundary(quadbin);

t.equal(result.type, 'Polygon', 'Should return a Polygon');
t.ok(
result.coordinates[0][0][0] > 170 || result.coordinates[0][0][0] < -170,
'Longitude should be near the antimeridian'
);
}

t.end();
});
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.51.tgz#cfd70924a25a3fd32b218e5e420e6897e1ac4f40"
integrity sha512-CuPgU6f3eT/XgKKPqKd/gLZV1Xmvf1a2R5POBOGQa6uv82xpls89HU5zKeVoyR8XzHd1RGNOlQlvUe3CFkjWNQ==

"@types/geojson@^7946.0.14":
version "7946.0.14"
resolved "https://registry.yarnpkg.com/@types/geojson/-/geojson-7946.0.14.tgz#319b63ad6df705ee2a65a73ef042c8271e696613"
integrity sha512-WCfD5Ht3ZesJUsONdhvm84dmzWOiOzOAqOncN0++w0lBw1o8OuDNJF2McvvCef/yBqb/HYRahp1BYtODFQ8bRg==

"@types/json-schema@*", "@types/json-schema@^7.0.5", "@types/json-schema@^7.0.8":
version "7.0.11"
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.11.tgz#d421b6c527a3037f7c84433fd2c4229e016863d3"
Expand Down

0 comments on commit ef8d9b8

Please sign in to comment.