Created
December 5, 2019 02:06
-
-
Save cosinekitty/dc4a9a666c829eccf973e0de91dfd5f6 to your computer and use it in GitHub Desktop.
Converting GPS coordinates to Cartesian coordinates and normal vector.
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
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