1 #ifndef _SG_GEODESY_HXX
2 #define _SG_GEODESY_HXX
4 #include <simgear/math/point3d.hxx>
8 * Convert from geocentric coordinates to geodetic coordinates
9 * @param lat_geoc (in) Geocentric latitude, radians, + = North
10 * @param radius (in) C.G. radius to earth center (meters)
11 * @param lat_geod (out) Geodetic latitude, radians, + = North
12 * @param alt (out) C.G. altitude above mean sea level (meters)
13 * @param sea_level_r (out) radius from earth center to sea level at
14 * local vertical (surface normal) of C.G. (meters)
16 inline void sgGeocToGeod(double lat_geoc, double radius,
17 double *lat_geod, double *alt, double *sea_level_r)
20 SGGeodesy::SGGeocToCart(SGGeoc::fromRadM(0, lat_geoc, radius), cart);
22 SGGeodesy::SGCartToGeod(cart, geod);
23 *lat_geod = geod.getLatitudeRad();
24 *alt = geod.getElevationM();
25 *sea_level_r = SGGeodesy::SGGeodToSeaLevelRadius(geod);
29 * Convert from geodetic coordinates to geocentric coordinates.
30 * WARNING: this function is non-reversible. Due to the fact that
31 * "up" is a different direction for geocentric and geodetic frames,
32 * you can not simply add your "alt" parameter to the "sl_radius"
33 * result and get back (via sgGeodToGeoc()) to the coordinates you
34 * started with. The error under normal conditions will be of
35 * centimeter order; whether that is important or not is application
36 * dependent. Consider using sgGeodToCart() instead.
38 * @param lat_geod (in) Geodetic latitude, radians, + = North
39 * @param alt (in) C.G. altitude above mean sea level (meters)
40 * @param sl_radius (out) SEA LEVEL radius to earth center (meters)
41 * @param lat_geoc (out) Geocentric latitude, radians, + = North
43 inline void sgGeodToGeoc(double lat_geod, double alt,
44 double *sl_radius, double *lat_geoc)
47 SGGeod geod = SGGeod::fromRadM(0, lat_geod, alt);
48 SGGeodesy::SGGeodToCart(geod, cart);
50 SGGeodesy::SGCartToGeoc(cart, geoc);
51 *lat_geoc = geoc.getLatitudeRad();
52 *sl_radius = SGGeodesy::SGGeodToSeaLevelRadius(geod);
57 * Convert a cartesian point to a geodetic lat/lon/altitude.
59 * @param xyz (in) Pointer to cartesian point.
60 * @param lat (out) Latitude, in radians
61 * @param lon (out) Longitude, in radians
62 * @param alt (out) Altitude, in meters above the WGS84 ellipsoid
64 inline void sgCartToGeod(const double* xyz, double* lat, double* lon, double* alt)
67 SGGeodesy::SGCartToGeod(SGVec3<double>(xyz), geod);
68 *lat = geod.getLatitudeRad();
69 *lon = geod.getLongitudeRad();
70 *alt = geod.getElevationM();
74 * Convert a cartesian point to a geodetic lat/lon/altitude.
75 * Alternate form using Point3D objects.
77 * @param cartesian point
78 * @return geodetic point
80 inline Point3D sgCartToGeod(const Point3D& p)
83 SGGeodesy::SGCartToGeod(SGVec3<double>(p.x(), p.y(), p.z()), geod);
84 return Point3D::fromSGGeod(geod);
89 * Convert a geodetic lat/lon/altitude to a cartesian point.
91 * @param lat (in) Latitude, in radians
92 * @param lon (in) Longitude, in radians
93 * @param alt (in) Altitude, in meters above the WGS84 ellipsoid
94 * @param xyz (out) Pointer to cartesian point.
96 inline void sgGeodToCart(double lat, double lon, double alt, double* xyz)
99 SGGeodesy::SGGeodToCart(SGGeod::fromRadM(lon, lat, alt), cart);
106 * Convert a geodetic lat/lon/altitude to a cartesian point.
107 * Alternate form using Point3D objects.
109 * @param geodetic point
110 * @return cartesian point
112 inline Point3D sgGeodToCart(const Point3D& geod)
115 SGGeodesy::SGGeodToCart(SGGeod::fromRadM(geod.lon(), geod.lat(), geod.elev()), cart);
116 return Point3D::fromSGVec3(cart);
120 * Given a starting position and an offset radial and distance,
121 * calculate an ending positon on a wgs84 ellipsoid.
122 * @param alt (in) meters (unused)
123 * @param lat1 (in) degrees
124 * @param lon1 (in) degrees
125 * @param az1 (in) degrees
126 * @param s (in) distance in meters
127 * @param lat2 (out) degrees
128 * @param lon2 (out) degrees
129 * @param az2 (out) return course in degrees
131 int geo_direct_wgs_84 ( double lat1, double lon1, double az1,
132 double s, double *lat2, double *lon2,
134 inline int geo_direct_wgs_84 ( double alt, double lat1,
135 double lon1, double az1,
136 double s, double *lat2, double *lon2,
138 { return geo_direct_wgs_84(lat1, lon1, az1, s, lat2, lon2, az2); }
141 * Given a starting position and an offset radial and distance,
142 * calculate an ending positon on a wgs84 ellipsoid.
143 * @param p1 (in) geodetic position
144 * @param az1 (in) degrees
145 * @param s (in) distance in meters
146 * @param p2 (out) geodetic position
147 * @param az2 (out) return course in degrees
149 inline int geo_direct_wgs_84(const SGGeod& p1, double az1,
150 double s, SGGeod& p2, double *az2 )
153 int ret = geo_direct_wgs_84(p1.getLatitudeDeg(), p1.getLongitudeDeg(),
154 az1, s, &lat2, &lon2, az2);
155 p2.setLatitudeDeg(lat2);
156 p2.setLongitudeDeg(lon2);
161 * Given an altitude and two sets of (lat, lon) calculate great circle
162 * distance between them as well as the starting and ending azimuths.
163 * @param alt (in) meters (unused)
164 * @param lat1 (in) degrees
165 * @param lon1 (in) degrees
166 * @param lat2 (in) degrees
167 * @param lon2 (in) degrees
168 * @param az1 (out) start heading degrees
169 * @param az2 (out) end heading degrees
170 * @param s (out) distance meters
172 int geo_inverse_wgs_84( double lat1, double lon1, double lat2,
173 double lon2, double *az1, double *az2,
175 inline int geo_inverse_wgs_84( double alt, double lat1,
176 double lon1, double lat2,
177 double lon2, double *az1, double *az2,
179 { return geo_inverse_wgs_84(lat1, lon1, lat2, lon2, az1, az2, s); }
183 * Given an altitude and two sets of (lat, lon) calculate great circle
184 * distance between them as well as the starting and ending azimuths.
185 * @param p1 (in) first position
186 * @param p2 (in) fsecond position
187 * @param az1 (out) start heading degrees
188 * @param az2 (out) end heading degrees
189 * @param s (out) distance meters
191 inline int geo_inverse_wgs_84(const SGGeod& p1, const SGGeod& p2,
192 double *az1, double *az2, double *s )
194 return geo_inverse_wgs_84(p1.getLatitudeDeg(), p1.getLongitudeDeg(),
195 p2.getLatitudeDeg(), p2.getLongitudeDeg(),
199 #endif // _SG_GEODESY_HXX