Skip to content

Instantly share code, notes, and snippets.

@trevorstarick
Created December 11, 2014 06:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trevorstarick/d31a61fc70b9f1aba6c6 to your computer and use it in GitHub Desktop.
Save trevorstarick/d31a61fc70b9f1aba6c6 to your computer and use it in GitHub Desktop.
// degrees to radians
var deg2rad = function(degrees) {
return Math.PI * degrees / 180.0
}
// radians to degrees
var rad2deg = function(radians) {
return 180.0 * radians / Math.PI
}
// Semi-axes of WGS-84 geoidal reference
var WGS84_a = 6378137.0 // Major semiaxis [m]
var WGS84_b = 6356752.3 // Minor semiaxis [m]
// Earth radius at a given latitude, according to the WGS-84 ellipsoid [m]
var WGS84EarthRadius = function(lat) {
// http://en.wikipedia.org/wiki/Earth_radius
var An = WGS84_a * WGS84_a * Math.cos(lat)
var Bn = WGS84_b * WGS84_b * Math.sin(lat)
var Ad = WGS84_a * Math.cos(lat)
var Bd = WGS84_b * Math.sin(lat)
return Math.sqrt((An * An + Bn * Bn) / (Ad * Ad + Bd * Bd))
}
// Bounding box surrounding the point at given coordinates,
// assuming local approximation of Earth surface as a sphere
// of radius given by WGS84
var boundingBox = function(latitudeInDegrees, longitudeInDegrees, halfSideInKm) {
var lat = deg2rad(latitudeInDegrees)
var lon = deg2rad(longitudeInDegrees)
var halfSide = 1000 * halfSideInKm
// Radius of Earth at given latitude
var radius = WGS84EarthRadius(lat)
// Radius of the parallel at given latitude
var pradius = radius * Math.cos(lat)
var latMin = lat - halfSide / radius
var latMax = lat + halfSide / radius
var lonMin = lon - halfSide / pradius
var lonMax = lon + halfSide / pradius
var bounds = [
[rad2deg(latMin), rad2deg(lonMin)],
[rad2deg(latMax), rad2deg(lonMax)]
];
return bounds;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment