Skip to content
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 25 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<number>} bbox
* @returns {Feature<Point>}
* @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,
Expand All @@ -293,5 +316,6 @@ module.exports = {
quadkeyToTile: quadkeyToTile,
pointToTile: pointToTile,
bboxToTile: bboxToTile,
pointToTileFraction: pointToTileFraction
pointToTileFraction: pointToTileFraction,
tileToCenterPoint: tileToCenterPoint
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@
"tap": "^1.3.4"
},
"dependencies": {}
}
}
14 changes: 14 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});