Skip to content

Instantly share code, notes, and snippets.

@pianosnake
Last active July 16, 2018 22:38
Show Gist options
  • Save pianosnake/e578b8ca5a0678c1138862cd8292499d to your computer and use it in GitHub Desktop.
Save pianosnake/e578b8ca5a0678c1138862cd8292499d to your computer and use it in GitHub Desktop.
Computes the area of a geographic bounding box assuming spherical Earth
// 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