From 7eef702fac80316a693cc1034e76aeac1375745e Mon Sep 17 00:00:00 2001 From: Douglas Gaskell <1400380+douglasg14b@users.noreply.github.com> Date: Sat, 27 Mar 2021 00:20:16 -0700 Subject: [PATCH 1/5] Create types.d.ts --- types.d.ts | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 types.d.ts diff --git a/types.d.ts b/types.d.ts new file mode 100644 index 0000000..14df224 --- /dev/null +++ b/types.d.ts @@ -0,0 +1,44 @@ +declare module '@mapbox/tilebelt' { + import { BBox, Geometry } from "geojson"; + + export type Tile = [number, number, number]; // [x, y, z] + + /** get a geojson representation of a tile */ + export function tileToGeoJSON(tile: Tile): Geometry; + + /** get the bbox of a tile */ + export function tileToBBOX(time: Tile): BBox; + + /** get the smallest tile to cover a bbox */ + export function bboxToTile(bbox: BBox): Tile; + + /** get the 4 tiles one zoom level higher */ + export function getChildren(tile: Tile): Tile[]; + + /** get the tile one zoom level lower */ + export function getParent(tile: Tile): Tile; + + /** get the 3 sibling tiles for a tile */ + export function getSiblings(tile: Tile): Tile[]; + + /** check to see if an array of tiles contains a tiles siblings */ + export function hasSiblings(tiles: Tile[], tile: Tile): boolean; + + /** check to see if an array of tiles contains a particular tile */ + export function hasTile(tiles: Tile[], tile: Tile): boolean; + + /** check to see if two tiles are the same */ + export function tilesEqual(tile1: Tile, tile2: Tile): boolean; + + /** get the quadkey for a tile */ + export function tileToQuadkey(tile: Tile): string; + + /** get the tile for a quadkey */ + export function quadkeyToTile(quadkey: string): Tile; + + /** get the tile for a point at a specified zoom level */ + export function pointToTile(lon: number, lat: number, zoom: number): Tile; + + /** get the precise fractional tile location for a point at a zoom level */ + export function pointToTileFraction(lon: number, lat: number, zoom: number): Tile; +} From 6c81351e38854c07630d7b0d76508c0ab08c9928 Mon Sep 17 00:00:00 2001 From: Douglas Gaskell <1400380+douglasg14b@users.noreply.github.com> Date: Sat, 27 Mar 2021 00:21:00 -0700 Subject: [PATCH 2/5] Adds @types/geojson as a dev dependancy --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index e35c03c..cfff047 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ }, "homepage": "https://github.com/mapbox/tilebelt", "devDependencies": { + "@types/geojson": "^7946.0.7", "benchmark": "~2.1.4", "eslint": "^7.8.1", "eslint-config-mourner": "~2.0.3", From 726f04e2ce6edaeab450cd2f0098639ea0ef3adf Mon Sep 17 00:00:00 2001 From: Douglas Gaskell <1400380+douglasg14b@users.noreply.github.com> Date: Sat, 27 Mar 2021 00:30:27 -0700 Subject: [PATCH 3/5] Adds proper JSDoc comments from source --- types.d.ts | 155 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 137 insertions(+), 18 deletions(-) diff --git a/types.d.ts b/types.d.ts index 14df224..4d0e5ec 100644 --- a/types.d.ts +++ b/types.d.ts @@ -3,42 +3,161 @@ declare module '@mapbox/tilebelt' { export type Tile = [number, number, number]; // [x, y, z] - /** get a geojson representation of a tile */ - export function tileToGeoJSON(tile: Tile): Geometry; - - /** get the bbox of a tile */ + /** + * Get the bbox of a tile + * + * @name tileToBBOX + * @param {Array} tile + * @returns {Array} bbox + * @example + * var bbox = tileToBBOX([5, 10, 10]) + * //=bbox + */ export function tileToBBOX(time: Tile): BBox; - /** get the smallest tile to cover a bbox */ - export function bboxToTile(bbox: BBox): Tile; + /** + * Get a geojson representation of a tile + * + * @name tileToGeoJSON + * @param {Array} tile + * @returns {Feature} + * @example + * var poly = tileToGeoJSON([5, 10, 10]) + * //=poly + */ + export function tileToGeoJSON(tile: Tile): Geometry; + + /** + * Get the tile for a point at a specified zoom level + * + * @name pointToTile + * @param {number} lon + * @param {number} lat + * @param {number} z + * @returns {Array} tile + * @example + * var tile = pointToTile(1, 1, 20) + * //=tile + */ + export function pointToTile(lon: number, lat: number, zoom: number): Tile; - /** get the 4 tiles one zoom level higher */ + /** + * Get the 4 tiles one zoom level higher + * + * @name getChildren + * @param {Array} tile + * @returns {Array>} tiles + * @example + * var tiles = getChildren([5, 10, 10]) + * //=tiles + */ export function getChildren(tile: Tile): Tile[]; - /** get the tile one zoom level lower */ + /** + * Get the tile one zoom level lower + * + * @name getParent + * @param {Array} tile + * @returns {Array} tile + * @example + * var tile = getParent([5, 10, 10]) + * //=tile + */ export function getParent(tile: Tile): Tile; + /** + * Get the 3 sibling tiles for a tile + * + * @name getSiblings + * @param {Array} tile + * @returns {Array>} tiles + * @example + * var tiles = getSiblings([5, 10, 10]) + * //=tiles + */ + export function hasSiblings(tiles: Tile[], tile: Tile): boolean; + /** get the 3 sibling tiles for a tile */ export function getSiblings(tile: Tile): Tile[]; - /** check to see if an array of tiles contains a tiles siblings */ - export function hasSiblings(tiles: Tile[], tile: Tile): boolean; - - /** check to see if an array of tiles contains a particular tile */ + /** + * Check to see if an array of tiles contains a particular tile + * + * @name hasTile + * @param {Array>} tiles + * @param {Array} tile + * @returns {boolean} + * @example + * var tiles = [ + * [0, 0, 5], + * [0, 1, 5], + * [1, 1, 5], + * [1, 0, 5] + * ] + * hasTile(tiles, [0, 0, 5]) + * //=boolean + */ export function hasTile(tiles: Tile[], tile: Tile): boolean; - /** check to see if two tiles are the same */ + /** + * Check to see if two tiles are the same + * + * @name tilesEqual + * @param {Array} tile1 + * @param {Array} tile2 + * @returns {boolean} + * @example + * tilesEqual([0, 1, 5], [0, 0, 5]) + * //=boolean + */ export function tilesEqual(tile1: Tile, tile2: Tile): boolean; - /** get the quadkey for a tile */ + /** + * Get the quadkey for a tile + * + * @name tileToQuadkey + * @param {Array} tile + * @returns {string} quadkey + * @example + * var quadkey = tileToQuadkey([0, 1, 5]) + * //=quadkey + */ export function tileToQuadkey(tile: Tile): string; - /** get the tile for a quadkey */ + /** + * Get the tile for a quadkey + * + * @name quadkeyToTile + * @param {string} quadkey + * @returns {Array} tile + * @example + * var tile = quadkeyToTile('00001033') + * //=tile + */ export function quadkeyToTile(quadkey: string): Tile; - /** get the tile for a point at a specified zoom level */ - export function pointToTile(lon: number, lat: number, zoom: number): Tile; + /** + * Get the smallest tile to cover a bbox + * + * @name bboxToTile + * @param {Array} bbox + * @returns {Array} tile + * @example + * var tile = bboxToTile([ -178, 84, -177, 85 ]) + * //=tile + */ + export function bboxToTile(bbox: BBox): Tile; - /** get the precise fractional tile location for a point at a zoom level */ + /** + * Get the precise fractional tile location for a point at a zoom level + * + * @name pointToTileFraction + * @param {number} lon + * @param {number} lat + * @param {number} z + * @returns {Array} tile fraction + * var tile = pointToTileFraction(30.5, 50.5, 15) + * //=tile + */ export function pointToTileFraction(lon: number, lat: number, zoom: number): Tile; } From 49246c26146788ee610bce1e8b6e2d8186a15c52 Mon Sep 17 00:00:00 2001 From: Douglas Gaskell <1400380+douglasg14b@users.noreply.github.com> Date: Sat, 27 Mar 2021 00:45:11 -0700 Subject: [PATCH 4/5] Adds getNeighbors(tile) function --- bench.js | 6 ++++++ index.js | 30 +++++++++++++++++++++++++++++- types.d.ts | 11 +++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/bench.js b/bench.js index e28f702..ee43993 100644 --- a/bench.js +++ b/bench.js @@ -65,6 +65,12 @@ new Benchmark.Suite() .add('pointToTileFraction#tile2', function () { tilebelt.pointToTileFraction(558004.8, 363898.8, 20); }) + .add('getNeighbors#tile1', function () { + tilebelt.getNeighbors(tile1); + }) + .add('getNeighbors#tile2', function () { + tilebelt.getNeighbors(tile2); + }) .on('error', function (event) { console.log(event.target.error); }) diff --git a/index.js b/index.js index eaeee9e..737fda5 100644 --- a/index.js +++ b/index.js @@ -280,6 +280,33 @@ function pointToTileFraction(lon, lat, z) { return [x, y, z]; } +/** + * Get the 8 neighbors surrounding a tile + * + * @name getNeighbors + * @param {Array} tile + * @returns {Array>} tiles + * @example + * var tiles = getNeighbors([5, 10, 10]) + * //=tiles + */ +function getNeighbors(tile) { + const x = tile[0]; + const y = tile[1]; + const z = tile[2]; + + return [ + [x - 1, y - 1, z], // NW + [x, y - 1, z], // N + [x + 1, y - 1, z], // NE + [x + 1, y, z], // E + [x + 1, y + 1, z], // SE + [x, y + 1, z], // S + [x - 1, y + 1, z], //SW + [x - 1, y, z], // W + ]; +} + module.exports = { tileToGeoJSON: tileToGeoJSON, tileToBBOX: tileToBBOX, @@ -293,5 +320,6 @@ module.exports = { quadkeyToTile: quadkeyToTile, pointToTile: pointToTile, bboxToTile: bboxToTile, - pointToTileFraction: pointToTileFraction + pointToTileFraction: pointToTileFraction, + getNeighbors: getNeighbors }; diff --git a/types.d.ts b/types.d.ts index 4d0e5ec..e8386f5 100644 --- a/types.d.ts +++ b/types.d.ts @@ -160,4 +160,15 @@ declare module '@mapbox/tilebelt' { * //=tile */ export function pointToTileFraction(lon: number, lat: number, zoom: number): Tile; + + /** + * Get the 8 neighbors surrounding a tile + * + * @name getNeighbors + * @param {Array} tile + * @returns {Array>} tiles + * var tiles = getNeighbors([5, 10, 10]) + * //=tiles + */ + export function getNeighbors(tile: Tile): Tile[] } From 1ba46f425bbafa36431503c9f8a04333ebc0d4cd Mon Sep 17 00:00:00 2001 From: Douglas Gaskell <1400380+douglasg14b@users.noreply.github.com> Date: Sat, 27 Mar 2021 00:50:23 -0700 Subject: [PATCH 5/5] Adds tilesToFeatureCollection(tiles) --- index.js | 36 +++++++++++++++++++++++++++++++++++- types.d.ts | 21 +++++++++++++++++++-- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 737fda5..4dfe490 100644 --- a/index.js +++ b/index.js @@ -307,6 +307,39 @@ function getNeighbors(tile) { ]; } +/** + * Generates the GeoJSON FeatureCollection from an array or tiles + * + * @name tilesToFeatureCollection + * @param {Array>} tiles + * @returns {FeatureCollection} featureCollection + * var tiles = [ + * [0, 0, 5], + * [0, 1, 5], + * [1, 1, 5], + * [1, 0, 5] + * ] + * var featureCollection = tilesToFeatureCollection(tiles) + * //=featureCollection + */ +function tilesToFeatureCollection(tiles) { + const collection = { + type: "FeatureCollection", + features: [] + }; + + for (let i = 0; i < tiles.length; i++) { + const geometry = tileToGeoJSON(tiles[i]); + collection.features.push({ + type: "Feature", + geometry: geometry, + properties: null + }) + } + + return collection; +} + module.exports = { tileToGeoJSON: tileToGeoJSON, tileToBBOX: tileToBBOX, @@ -321,5 +354,6 @@ module.exports = { pointToTile: pointToTile, bboxToTile: bboxToTile, pointToTileFraction: pointToTileFraction, - getNeighbors: getNeighbors + getNeighbors: getNeighbors, + tilesToFeatureCollection: tilesToFeatureCollection }; diff --git a/types.d.ts b/types.d.ts index e8386f5..8e339af 100644 --- a/types.d.ts +++ b/types.d.ts @@ -1,5 +1,5 @@ declare module '@mapbox/tilebelt' { - import { BBox, Geometry } from "geojson"; + import { BBox, FeatureCollection, Geometry } from "geojson"; export type Tile = [number, number, number]; // [x, y, z] @@ -160,7 +160,7 @@ declare module '@mapbox/tilebelt' { * //=tile */ export function pointToTileFraction(lon: number, lat: number, zoom: number): Tile; - + /** * Get the 8 neighbors surrounding a tile * @@ -171,4 +171,21 @@ declare module '@mapbox/tilebelt' { * //=tiles */ export function getNeighbors(tile: Tile): Tile[] + + /** + * Generates the GeoJSON FeatureCollection from an array or tiles + * + * @name tilesToFeatureCollection + * @param {Array>} tiles + * @returns {FeatureCollection} featureCollection + * var tiles = [ + * [0, 0, 5], + * [0, 1, 5], + * [1, 1, 5], + * [1, 0, 5] + * ] + * var featureCollection = tilesToFeatureCollection(tiles) + * //=featureCollection + */ + export function tilesToFeatureCollection(tiles: Tile[]): FeatureCollection }