diff --git a/README.md b/README.md index 6da9152..46e14f8 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ tileToQuadkey(tile) | get the quadkey for a tile quadkeyToTile(quadkey) | get the tile for a quadkey pointToTile(lon, lat, zoom) | get the tile for a point at a specified zoom level pointToTileFraction(lon, lat, zoom) | get the precise fractional tile location for a point at a zoom level +tileToCenterPoint(tile) | Get the GeoJSON geometry of the center of the tile. ## tests diff --git a/index.js b/index.js index 25d0e93..e0bc687 100644 --- a/index.js +++ b/index.js @@ -280,6 +280,29 @@ function pointToTileFraction(lon, lat, z) { return [x, y, z]; } + +/** + * Get the center of a tile as Point GeoJSON + * + * @name tileToCenterPoint + * @param {Array} bbox + * @returns {Feature} + * @example + * var point = tileToCenterPoint([0,0,10]) + * //= point + */ + +function tileToCenterPoint(tile) { + var bbox = tileToBBOX(tile) + + return {type: 'Point', + coordinates: [ + (bbox[0] + bbox[2]) / 2, + (bbox[1] + bbox[3]) / 2 + ] + } +} + module.exports = { tileToGeoJSON: tileToGeoJSON, tileToBBOX: tileToBBOX, @@ -293,5 +316,6 @@ module.exports = { quadkeyToTile: quadkeyToTile, pointToTile: pointToTile, bboxToTile: bboxToTile, - pointToTileFraction: pointToTileFraction + pointToTileFraction: pointToTileFraction, + tileToCenterPoint: tileToCenterPoint }; diff --git a/package.json b/package.json index ba011d7..3a33485 100644 --- a/package.json +++ b/package.json @@ -28,4 +28,4 @@ "tap": "^1.3.4" }, "dependencies": {} -} \ No newline at end of file +} diff --git a/test.js b/test.js index 5367f04..19f1dd2 100644 --- a/test.js +++ b/test.js @@ -214,3 +214,17 @@ test('pointToTile -- cross meridian', function (t) { t.deepEqual(tilebelt.bboxToTile([-0.000001, -85, 1000000, 85]), [0, 0, 0]); t.end(); }); + +test('tile to center point', function (t) { + var tile = tilebelt.pointToTile(0, 0, 10); + var center = tilebelt.tileToCenterPoint(tile) + t.deepEqual(center, + { type: 'Point', coordinates: [0.17578125, -0.17578014699614353]}) + + var center2 = tilebelt.tileToCenterPoint(tilebelt.quadkeyToTile('300000000000000')) + t.deepEqual(center2, + { type: 'Point', coordinates: [0.0054931640625, -0.005493164028840922]} + ) + + t.end(); +});