turf.square needs mercator coordinates to work correctly
Author: vin-niCreated Jul 7, 2019Updated May 13, 2026
Labels@turf/square
I've been struggling with getting a correct turf.square using real world coordinates until I realized I needed to pass mercator coordinates to make it work correctly on earth.
Maybe we could just add a hint in the docs or add a function that detects wgs84 coordinates and makes the correct calculation / conversion.
This is the result doing it with Wgs84 lat long:

And the expected result with conversion to mercator and back:

Quick code for the second result:
var originalbboxx = [-2.440916, 49.4627643, -2.4579404, 49.4828877]
var point1 = [originalbboxx[0], originalbboxx[1]]
var point2 = [originalbboxx[2], originalbboxx[3]]
var converted1 = turf.toMercator(point1);
var converted2 = turf.toMercator(point2);
var squared = turf.square([converted1[0],converted1[1],converted2[0],converted2[1]] )
var squarept1 = [squared[0], squared[1]]
var squarept2 = [squared[2], squared[3]]
var wsg1 = turf.toWgs84(squarept1);
var wsg2 = turf.toWgs84(squarept2);
var newSquaredBbox = [wsg1[0],wsg1[1],wsg2[0],wsg2[1] ]Source: Turfjs/turf