Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

turf.square needs mercator coordinates to work correctly #1727

Open
vin-ni opened this issue Jul 7, 2019 · 1 comment
Open

turf.square needs mercator coordinates to work correctly #1727

vin-ni opened this issue Jul 7, 2019 · 1 comment
Assignees

Comments

@vin-ni
Copy link

vin-ni commented Jul 7, 2019

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:
Bildschirmfoto 2019-07-07 um 03 12 39

And the expected result with conversion to mercator and back:
Bildschirmfoto 2019-07-07 um 14 39 12

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] ]
@vin-ni
Copy link
Author

vin-ni commented Jul 7, 2019

Actually I needed to do the aspectratio comparison with lat long, but calculate the values with mercato. So I rewrote the square function like that:

unction square(bbox) {
    var west = bbox[0];
    var south = bbox[1];
    var east = bbox[2];
    var north = bbox[3];

    var horizontalDistance = turf.distance(bbox.slice(0, 2), [east, south]);
	var verticalDistance = turf.distance(bbox.slice(0, 2), [west, north]);
	
	var converted1 = turf.toMercator([bbox[0],bbox[1]]);
	var converted2 = turf.toMercator([bbox[2],bbox[3]]);

	var westM = converted1[0];
    var southM = converted1[1];
    var eastM = converted2[0];
	var northM = converted2[1];
	
	var mercatorResult = []

    if (horizontalDistance >= verticalDistance) {
        var verticalMidpoint = (southM + northM) / 2;
        mercatorResult = [
            westM,
            verticalMidpoint - ((eastM - westM) / 2),
            eastM,
            verticalMidpoint + ((eastM - westM) / 2)
        ];
    } else {
        var horizontalMidpoint = (westM + eastM) / 2;
        mercatorResult = [
            horizontalMidpoint - ((northM - southM) / 2),
            southM,
            horizontalMidpoint + ((northM - southM) / 2),
            northM
        ];
	}
	
	// convert back
	var wsg1 = turf.toWgs84([mercatorResult[0],mercatorResult[1]]);
	var wsg2 = turf.toWgs84([mercatorResult[2],mercatorResult[3]]);

	return [wsg1[0],wsg1[1],wsg2[0],wsg2[1]]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants