From ef8d9b8c31982518a508ced21a8266adf22a81a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julio=20Mu=C3=B1oz?= Date: Thu, 26 Sep 2024 11:15:19 +0200 Subject: [PATCH] PR Feedback --- CONTRIBUTING.md | 4 +--- package.json | 12 +++++++++--- src/index.ts | 23 ++++++++++++----------- test/index.spec.js | 40 +++++++++++++++++++++++++++++++++++++++- yarn.lock | 5 +++++ 5 files changed, 66 insertions(+), 18 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 5655bd2..1adce3c 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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` \ No newline at end of file +2. Execute `yarn publish` \ No newline at end of file diff --git a/package.json b/package.json index 071640a..9544cee 100644 --- a/package.json +++ b/package.json @@ -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", @@ -52,10 +54,14 @@ "webpack-cli": "^4.8.0" }, "engines": { - "node": ">=14" + "node": ">=18" }, "dependencies": { "@mapbox/tile-cover": "3.0.1" }, - "packageManager": "yarn@1.22.22" + "packageManager": "yarn@1.22.22", + "volta": { + "node": "18.19.0", + "yarn": "1.22.22" + } } diff --git a/src/index.ts b/src/index.ts index e399888..a86d2da 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ import {tiles} from '@mapbox/tile-cover'; +import {Polygon} from 'geojson'; const B = [ 0x5555555555555555n, @@ -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, offset: number) { const {x, z} = tile; return 180 * ((2.0 * (x + offset)) / (1 << z) - 1.0); @@ -34,6 +25,16 @@ function tileToLatitude(tile: ReturnType, 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}`); } @@ -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]], diff --git a/test/index.spec.js b/test/index.spec.js index 3d542a8..d634d32 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -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'; @@ -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}; @@ -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(); +}); diff --git a/yarn.lock b/yarn.lock index 817b403..5e75014 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"