1 #ifndef _SG_GEODESY_HXX
2 #define _SG_GEODESY_HXX
4 #include <simgear/math/point3d.hxx>
7 // Returns the insersection of the line joining the center of the
8 // earth and the specified cylindrical point with the surface of the
9 // WGS84 ellipsoid. Works by finding a normalization constant (in
10 // squashed space) that places the squashed point on the surface of
12 inline double seaLevelRadius(double r, double z)
14 double sr = r * SGGeodesy::SQUASH;
16 return SGGeodesy::POLRAD*sqrt((r*r + zz)/(sr*sr + zz));
20 * Convert from geocentric coordinates to geodetic coordinates
21 * @param lat_geoc (in) Geocentric latitude, radians, + = North
22 * @param radius (in) C.G. radius to earth center (meters)
23 * @param lat_geod (out) Geodetic latitude, radians, + = North
24 * @param alt (out) C.G. altitude above mean sea level (meters)
25 * @param sea_level_r (out) radius from earth center to sea level at
26 * local vertical (surface normal) of C.G. (meters)
28 inline void sgGeocToGeod(double lat_geoc, double radius,
29 double *lat_geod, double *alt, double *sea_level_r)
32 SGGeodesy::SGGeocToCart(SGGeoc::fromRadM(0, lat_geoc, radius), cart);
34 SGGeodesy::SGCartToGeod(cart, geod);
35 *lat_geod = geod.getLatitudeRad();
36 *alt = geod.getElevationM();
37 *sea_level_r = SGGeodesy::SGGeodToSeaLevelRadius(geod);
41 * Convert from geodetic coordinates to geocentric coordinates.
42 * WARNING: this function is non-reversible. Due to the fact that
43 * "up" is a different direction for geocentric and geodetic frames,
44 * you can not simply add your "alt" parameter to the "sl_radius"
45 * result and get back (via sgGeodToGeoc()) to the coordinates you
46 * started with. The error under normal conditions will be of
47 * centimeter order; whether that is important or not is application
48 * dependent. Consider using sgGeodToCart() instead.
50 * @param lat_geod (in) Geodetic latitude, radians, + = North
51 * @param alt (in) C.G. altitude above mean sea level (meters)
52 * @param sl_radius (out) SEA LEVEL radius to earth center (meters)
53 * @param lat_geoc (out) Geocentric latitude, radians, + = North
55 inline void sgGeodToGeoc(double lat_geod, double alt,
56 double *sl_radius, double *lat_geoc)
59 SGGeodesy::SGGeodToCart(SGGeod::fromRadM(0, lat_geod, alt), cart);
61 SGGeodesy::SGCartToGeoc(cart, geoc);
62 *lat_geoc = geoc.getLatitudeRad();
63 *sl_radius = seaLevelRadius(cart(0), cart(2));
68 * Convert a cartesian point to a geodetic lat/lon/altitude.
70 * @param xyz (in) Pointer to cartesian point.
71 * @param lat (out) Latitude, in radians
72 * @param lon (out) Longitude, in radians
73 * @param alt (out) Altitude, in meters above the WGS84 ellipsoid
75 inline void sgCartToGeod(const double* xyz, double* lat, double* lon, double* alt)
78 SGGeodesy::SGCartToGeod(SGVec3<double>(xyz), geod);
79 *lat = geod.getLatitudeRad();
80 *lon = geod.getLongitudeRad();
81 *alt = geod.getElevationM();
85 * Convert a cartesian point to a geodetic lat/lon/altitude.
86 * Alternate form using Point3D objects.
88 * @param cartesian point
89 * @return geodetic point
91 inline Point3D sgCartToGeod(const Point3D& p)
94 SGGeodesy::SGCartToGeod(SGVec3<double>(p.x(), p.y(), p.z()), geod);
95 return Point3D::fromSGGeod(geod);
100 * Convert a geodetic lat/lon/altitude to a cartesian point.
102 * @param lat (in) Latitude, in radians
103 * @param lon (in) Longitude, in radians
104 * @param alt (in) Altitude, in meters above the WGS84 ellipsoid
105 * @param xyz (out) Pointer to cartesian point.
107 inline void sgGeodToCart(double lat, double lon, double alt, double* xyz)
110 SGGeodesy::SGGeodToCart(SGGeod::fromRadM(lon, lat, alt), cart);
117 * Convert a geodetic lat/lon/altitude to a cartesian point.
118 * Alternate form using Point3D objects.
120 * @param geodetic point
121 * @return cartesian point
123 inline Point3D sgGeodToCart(const Point3D& geod)
126 SGGeodesy::SGGeodToCart(SGGeod::fromRadM(geod.lon(), geod.lat(), geod.elev()), cart);
127 return Point3D::fromSGVec3(cart);
131 * Given a starting position and an offset radial and distance,
132 * calculate an ending positon on a wgs84 ellipsoid.
133 * @param alt (in) meters
134 * @param lat1 (in) degrees
135 * @param lon1 (in) degrees
136 * @param az1 (in) degrees
137 * @param s (in) distance in meters
138 * @param lat2 (out) degrees
139 * @param lon2 (out) degrees
140 * @param az2 (out) return course in degrees
142 int geo_direct_wgs_84 ( double alt, double lat1,
143 double lon1, double az1,
144 double s, double *lat2, double *lon2,
149 * Given an altitude and two sets of (lat, lon) calculate great circle
150 * distance between them as well as the starting and ending azimuths.
151 * @param alt (in) meters
152 * @param lat1 (in) degrees
153 * @param lon1 (in) degrees
154 * @param lat2 (in) degrees
155 * @param lon2 (in) degrees
156 * @param az1 (out) start heading degrees
157 * @param az2 (out) end heading degrees
158 * @param s (out) distance meters
160 int geo_inverse_wgs_84( double alt, double lat1,
161 double lon1, double lat2,
162 double lon2, double *az1, double *az2,
165 #endif // _SG_GEODESY_HXX