Skip to content

Instantly share code, notes, and snippets.

@cosinekitty
Created December 5, 2019 02:06
Show Gist options
  • Save cosinekitty/dc4a9a666c829eccf973e0de91dfd5f6 to your computer and use it in GitHub Desktop.
Save cosinekitty/dc4a9a666c829eccf973e0de91dfd5f6 to your computer and use it in GitHub Desktop.
Converting GPS coordinates to Cartesian coordinates and normal vector.
function LocationToPoint(c)
{
// Convert (lat, lon, elv) to (x, y, z).
var lat = c.lat * Math.PI / 180.0;
var lon = c.lon * Math.PI / 180.0;
var radius = EarthRadiusInMeters(lat);
var clat = GeocentricLatitude(lat);
var cosLon = Math.cos(lon);
var sinLon = Math.sin(lon);
var cosLat = Math.cos(clat);
var sinLat = Math.sin(clat);
var x = radius * cosLon * cosLat;
var y = radius * sinLon * cosLat;
var z = radius * sinLat;
// We used geocentric latitude to calculate (x,y,z) on the Earth's ellipsoid.
// Now we use geodetic latitude to calculate normal vector from the surface, to correct for elevation.
var cosGlat = Math.cos(lat);
var sinGlat = Math.sin(lat);
var nx = cosGlat * cosLon;
var ny = cosGlat * sinLon;
var nz = sinGlat;
x += c.elv * nx;
y += c.elv * ny;
z += c.elv * nz;
return {x:x, y:y, z:z, radius:radius, nx:nx, ny:ny, nz:nz};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment