Skip to content

Instantly share code, notes, and snippets.

@stephanbogner
Last active June 18, 2017 20:33
Show Gist options
  • Save stephanbogner/58a73ace1fe3fb21e5d88be4e202880c to your computer and use it in GitHub Desktop.
Save stephanbogner/58a73ace1fe3fb21e5d88be4e202880c to your computer and use it in GitHub Desktop.
Create square around a centerpoint with a specified area
// Note: Not sure how well it works with very large squares due to projection distortion
var turf = require('turf'); // For NodeJS
function getSquareAroundPointWithArea(point, area) {
// Area in m2
var coordinates = [];
var areaInKm = area / 1000 / 1000;
var edgeLength = Math.sqrt(areaInKm);
var diagonal = Math.sqrt(Math.pow(edgeLength, 2) + Math.pow(edgeLength, 2));
for (var i = 0; i < 4; i++) {
var bearing = i * 90 + 45;
var distance = diagonal / 2;
var destination = turf.destination(point, distance, bearing);
coordinates.push(destination.geometry.coordinates);
}
coordinates.push(coordinates[0]); // Close Polygon
var square = turf.polygon([coordinates]);
return square;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment