osgEarth 2.1.1
Static Public Member Functions

osgEarth::GeoMath Class Reference

List of all members.

Static Public Member Functions

static double distance (double lat1Rad, double lon1Rad, double lat2Rad, double lon2Rad, double radius=osg::WGS_84_RADIUS_EQUATOR)
static double distance (const std::vector< osg::Vec3d > &points, double radius=osg::WGS_84_RADIUS_EQUATOR)
static double distance (const osg::Vec3d &p1, const osg::Vec3d &p2, const SpatialReference *srs)
static double bearing (double lat1Rad, double lon1Rad, double lat2Rad, double lon2Rad)
static void midpoint (double lat1Rad, double lon1Rad, double lat2Rad, double lon2Rad, double &out_latRad, double &out_lonRad)
static void destination (double lat1Rad, double lon1Rad, double bearingRad, double distance, double &out_latRad, double &out_lonRad, double radius=osg::WGS_84_RADIUS_EQUATOR)
static double rhumbDistance (double lat1Rad, double lon1Rad, double lat2Rad, double lon2Rad, double radius=osg::WGS_84_RADIUS_EQUATOR)
static double rhumbDistance (const std::vector< osg::Vec3d > &points, double radius=osg::WGS_84_RADIUS_EQUATOR)
static double rhumbBearing (double lat1Rad, double lon1Rad, double lat2Rad, double lon2Rad)
static void rhumbDestination (double lat1Rad, double lon1Rad, double bearing, double distance, double &out_latRad, double &out_lonRad, double radius=osg::WGS_84_RADIUS_EQUATOR)

Detailed Description

Useful calculations for lat/long points. Converted from http://www.movable-type.co.uk/scripts/latlong.html

Definition at line 42 of file GeoMath.


Member Function Documentation

double GeoMath::bearing ( double  lat1Rad,
double  lon1Rad,
double  lat2Rad,
double  lon2Rad 
) [static]

Computes the initial bearing from one point to the next in radians

Parameters:
lat1RadThe start latitude in radians
lon1RadThe start longitude in radians
lat2RadThe end latitude in radians
lon2RadThe end longitude in radians
Returns:
The initial bearing in radians

Definition at line 72 of file GeoMath.cpp.

{
    double dLon = (lon2Rad-lon1Rad); 
    double y = sin(dLon) * cos(lat2Rad);
    double x = cos(lat1Rad)*sin(lat2Rad) - sin(lat1Rad)*cos(lat2Rad)*cos(dLon);
    double brng = atan2(y, x);
    return brng;
}

Here is the caller graph for this function:

void GeoMath::destination ( double  lat1Rad,
double  lon1Rad,
double  bearingRad,
double  distance,
double &  out_latRad,
double &  out_lonRad,
double  radius = osg::WGS_84_RADIUS_EQUATOR 
) [static]

Computes the destination point given a start point, a bearing and a distance

Parameters:
lat1RadThe latitude in radians
lon1RadThe longitude in radians
bearingRadThe bearing in radians
distanceThe distance in meters
out_latRadThe destination point's latitude in radians
out_lonRadThe destination points' longitude in radians
radiusThe radius of the earth in meters

Definition at line 103 of file GeoMath.cpp.

{
    double dR = distance / radius;
    out_latRad = asin( sin(lat1Rad)*cos(dR) + 
                       cos(lat1Rad)*sin(dR)*cos(bearingRad) );
    out_lonRad = lon1Rad + atan2(sin(bearingRad)*sin(dR)*cos(lat1Rad), 
                                 cos(dR)-sin(lat1Rad)*sin(out_latRad));
}

Here is the caller graph for this function:

double GeoMath::distance ( double  lat1Rad,
double  lon1Rad,
double  lat2Rad,
double  lon2Rad,
double  radius = osg::WGS_84_RADIUS_EQUATOR 
) [static]

Computes the distance between the given points in meters using the Haversine formula

Parameters:
lat1RadThe start latitude in radians
lon1RadThe start longitude in radians
lat2RadThe end latitude in radians
lon2RadThe end longitude in radians
radiusThe radius of the earth in meters
Returns:
The distance between the two points in meters

Definition at line 25 of file GeoMath.cpp.

{       
    double dLat = (lat2Rad-lat1Rad);
    double dLon = (lon2Rad-lon1Rad); 
    double a = sin(dLat/2.0) * sin(dLat/2.0) +
               cos(lat1Rad) *  cos(lat2Rad) * 
               sin(dLon/2.0) * sin(dLon/2.0); 
    double c = 2.0 * atan2(sqrt(a), sqrt(1.0-a)); 
    double d = radius * c;
    return d;
}

Here is the caller graph for this function:

double GeoMath::distance ( const std::vector< osg::Vec3d > &  points,
double  radius = osg::WGS_84_RADIUS_EQUATOR 
) [static]

Computes the distance covered by the given path in meters using the Haversine formula Assumes the points in Lon, Lat in degrees

Definition at line 38 of file GeoMath.cpp.

{
    double length = 0;

    if (points.size() > 1)
    {
        for (unsigned int i = 0; i < points.size()-1; ++i)
        {
            const osg::Vec3d& current = points[i];
            const osg::Vec3d& next    = points[i+1];
            length += GeoMath::distance(osg::DegreesToRadians(current.y()), osg::DegreesToRadians(current.x()),
                osg::DegreesToRadians(next.y()), osg::DegreesToRadians(next.x()), radius);
        }
    }
    return length;
}

Here is the call graph for this function:

double GeoMath::distance ( const osg::Vec3d &  p1,
const osg::Vec3d &  p2,
const SpatialReference srs 
) [static]

Computes the distance between two points, in meters.

Definition at line 56 of file GeoMath.cpp.

{
    if ( srs == 0L || srs->isProjected() )
    {
        return (p2-p1).length();
    }
    else
    {
        return distance(
            osg::DegreesToRadians( p1.y() ), osg::DegreesToRadians( p1.x() ),
            osg::DegreesToRadians( p2.y() ), osg::DegreesToRadians( p2.x() ),
            srs->getEllipsoid()->getRadiusEquator() );
    }
}

Here is the call graph for this function:

void GeoMath::midpoint ( double  lat1Rad,
double  lon1Rad,
double  lat2Rad,
double  lon2Rad,
double &  out_latRad,
double &  out_lonRad 
) [static]

Computes the midpoint between two points

Parameters:
lat1RadThe start latitude in radians
lon1RadThe start longitude in radians
lat2RadThe end latitude in radians
lon2RadThe end longitude in radians
out_latRadThe latitude of the midpoint in radians
out_lonRadThe longitude of the midpoint in radians

Definition at line 83 of file GeoMath.cpp.

{     
    double dLon = (lon2Rad-lon1Rad); 

    double cosLat1 = cos(lat1Rad);
    double cosLat2 = cos(lat2Rad);
    double sinLat1 = sin(lat1Rad);
    double sinLat2 = sin(lat2Rad);


    double Bx = cosLat2 * cos(dLon);
    double By = cosLat2 * sin(dLon);
    out_latRad = atan2(sinLat1+sinLat2,
                       sqrt( (cosLat1+Bx)*(cosLat1+Bx) + By*By) ); 
    out_lonRad = lon1Rad + atan2(By, cosLat1 + Bx);
}
double GeoMath::rhumbBearing ( double  lat1Rad,
double  lon1Rad,
double  lat2Rad,
double  lon2Rad 
) [static]

Computes the bearing of the rhumb line between two points in radians

Parameters:
lat1RadThe start latitude in radians
lon1RadThe start longitude in radians
lat2RadThe end latitude in radians
lon2RadThe end longitude in radians

Definition at line 150 of file GeoMath.cpp.

{
  double dLon = lon2Rad - lon1Rad;
  
  double dPhi = log(tan(lat2Rad/2.0+osg::PI/4.0)/tan(lat1Rad/2.0+osg::PI/4.0));
  if (osg::absolute(dLon) > osg::PI) dLon = dLon > 0.0 ? -(2.0*osg::PI-dLon) : (2.0*osg::PI+dLon);
  double brng = atan2(dLon, dPhi);
  return fmod(brng + 2.0 * osg::PI, 2.0 * osg::PI);

}

Here is the caller graph for this function:

void GeoMath::rhumbDestination ( double  lat1Rad,
double  lon1Rad,
double  bearing,
double  distance,
double &  out_latRad,
double &  out_lonRad,
double  radius = osg::WGS_84_RADIUS_EQUATOR 
) [static]

Computes the destination point given a start point, a bearing and a distance along a rhumb line

Parameters:
lat1RadThe latitude in radians
lon1RadThe longitude in radians
bearingRadThe bearing in radians
distanceThe distance in meters
out_latRadThe destination point's latitude in radians
out_lonRadThe destination points' longitude in radians
radiusThe radius of the earth in meters

Definition at line 163 of file GeoMath.cpp.

{ 
  double R = radius;
  double d = distance / R;

  double lat2Rad = lat1Rad + d*cos(bearing);
  double dLat = lat2Rad-lat1Rad;
  double dPhi = log(tan(lat2Rad/2.0+osg::PI/4.0)/tan(lat1Rad/2.0+osg::PI/4.0));
  double q = (!osg::isNaN(dLat/dPhi)) ? dLat/dPhi : cos(lat1Rad);  // E-W line gives dPhi=0
  double dLon = d*sin(bearing)/q;
  // check for some daft bugger going past the pole
  if (osg::absolute(lat2Rad) > osg::PI/2.0) lat2Rad = lat2Rad > 0.0 ? osg::PI-lat2Rad : -(osg::PI-lat2Rad);
  //double lon2Rad = (lon1Rad+dLon+3.0*Math.PI)%(2.0*osg::PI) - osg::PI;
  double lon2Rad = fmod((lon1Rad+dLon+3.0*osg::PI),(2.0*osg::PI)) - osg::PI;

  out_latRad = lat2Rad;
  out_lonRad = lon2Rad;
}

Here is the caller graph for this function:

double GeoMath::rhumbDistance ( double  lat1Rad,
double  lon1Rad,
double  lat2Rad,
double  lon2Rad,
double  radius = osg::WGS_84_RADIUS_EQUATOR 
) [static]

Computes the distance between two points in meters following a rhumb line

Parameters:
lat1RadThe start latitude in radians
lon1RadThe start longitude in radians
lat2RadThe end latitude in radians
lon2RadThe end longitude in radians
radiusThe radius of the earth in meters
Returns:
The distance between the two points in meters following a rhumb line

Definition at line 117 of file GeoMath.cpp.

{
    double dLat = (lat2Rad - lat1Rad);
    double dLon = osg::absolute(lon2Rad - lon1Rad);

    double dPhi = log(tan(lat2Rad/2.0+osg::PI/4.0)/tan(lat1Rad/2.0+osg::PI/4.0));
    double q = (!osg::isNaN(dLat/dPhi)) ? dLat/dPhi : cos(lat1Rad);  // E-W line gives dPhi=0
    // if dLon over 180° take shorter rhumb across 180° meridian:
    if (dLon > osg::PI) dLon = 2.0*osg::PI - dLon;
    double dist = sqrt(dLat*dLat + q*q*dLon*dLon) * radius; 
    return dist;
}

Here is the caller graph for this function:

double GeoMath::rhumbDistance ( const std::vector< osg::Vec3d > &  points,
double  radius = osg::WGS_84_RADIUS_EQUATOR 
) [static]

Computes the distance between two points in meters following a rhumb line Assumes the points are in Lon, Lat in degrees

Definition at line 133 of file GeoMath.cpp.

{
    double length = 0;
    if (points.size() > 1)
    {
        for (unsigned int i = 0; i < points.size()-1; ++i)
        {
            const osg::Vec3d& current = points[i];
            const osg::Vec3d& next    = points[i+1];
            length += GeoMath::rhumbDistance(osg::DegreesToRadians(current.y()), osg::DegreesToRadians(current.x()),
                                             osg::DegreesToRadians(next.y()), osg::DegreesToRadians(next.x()), radius);                                             
        }
    }
    return length;
}

Here is the call graph for this function:


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines