To get bounding box in Mercator coordinates (EPSG:3857) of a circle with a given center [lng, lat] coordinates and radius in kilometers, you can use turf.destination and turf.toMercator methods.
With the turf.destination(point, distance, bearing) method you can get coordinates of a point that is distance kilometers offset from the source point in the bearing direction.
With the turf.toMercator method you can then convert [lng, lat] coordinates to projected Mercator ones.
Simple function for creating Mercator bounding box could then look something like this:
function getMercBbox(center, radius) {
function coordOffsetMerc(bearing) {
var centerPoint = turf.point(center);
return(turf.getCoord(turf.toMercator(turf.destination(centerPoint, radius, bearing))));
}
var centerUpMerc = coordOffsetMerc(0);
var centerRightMerc = coordOffsetMerc(90);
var centerDownMerc = coordOffsetMerc(180);
var centerLeftMerc = coordOffsetMerc(270);
return {
lowerLeft: [centerLeftMerc[0], centerDownMerc[1]],
upperRight: [centerRightMerc[0], centerUpMerc[1]]
};
}
You can then test this function with the following code:
var center = [12, 51];
var radius = 10 // kilometers
var bboxMerc = getMercBbox(center, radius);
var bboxMercWidth = Math.abs(bboxMerc.lowerLeft[0] - bboxMerc.upperRight[0]);
var bboxMercHeight = Math.abs(bboxMerc.lowerLeft[1] - bboxMerc.upperRight[1]);
var ratioWidth = (radius * 2 * 1000) / bboxMercWidth;
var ratioHeight = (radius * 2 * 1000) / bboxMercHeight;
var mercFactor = Math.cos(center[1] * Math.PI / 180);
console.log(ratioWidth, ratioHeight, mercFactor);
If you look at the ratio between actual bbox width and width (or height) in projected Mercator meters, you get value to one decimal equal with the value of cos() of bbox latitude, which is considered to be approx method for calculating Mercator length distortions (see Better Distance Measurements in Web Mercator Projection):
ratioWidth: 0.6286178504081783
ratioHeight: 0.6286178504081783
mercFactor: 0.6293203910498375
[lat, lng]coordinate to Mercator with theturf.toMercatormethod. – TomazicM Oct 07 '21 at 07:49