Last active
June 18, 2017 20:33
-
-
Save stephanbogner/58a73ace1fe3fb21e5d88be4e202880c to your computer and use it in GitHub Desktop.
Create square around a centerpoint with a specified area
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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