Last active
July 16, 2018 22:38
-
-
Save pianosnake/e578b8ca5a0678c1138862cd8292499d to your computer and use it in GitHub Desktop.
Computes the area of a geographic bounding box assuming spherical Earth
This file contains hidden or 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
// adapted from http://mathforum.org/library/drmath/view/63767.html | |
const r = 6371.0072; //earth radius in KM | |
function toRadians(degrees) { | |
return degrees * Math.PI / 180; | |
}; | |
function computeArea(bbox){ | |
const [minLng, minLat, maxLng, maxLat] = bbox; | |
const h = r * ( Math.sin(toRadians(maxLat)) - Math.sin(toRadians(minLat))) | |
const latitudinalArea = 2 * Math.PI * r * h; | |
const longitudinalSection = (maxLng - minLng) / 360; | |
return latitudinalArea * longitudinalSection; | |
} | |
//example [west, south, east, north] | |
const colorado = [-109.060256958008, 36.9924240112305, -102.041580200195, 41.0023612976074]; | |
computeArea(colorado); //returns 270391.9194161927 square kilometers |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment