My altitude calculation algorithm

Topics: Algorithms, Italiano, SharpMap Project
Apr 7, 2014 at 1:25 PM
Edited Apr 7, 2014 at 1:34 PM
Hi all, here's my code to calculate viewer's current altitude in meters, using the Haversine formula revisited by me. It works for me, hope it'll work for you. It's in line with google earth, don't know if it's the right source for this variable
Coordinate Reproject(Coordinate Point, ProjectionInfo From,ProjectionInfo To)
{
    double[] xy = new double[] { Point.X, Point.Y };
    DotSpatial.Projections.Reproject.ReprojectPoints(xy, new double[] { 0 }, From,To, 0, 1);
    return new Coordinate(xy[0], xy[1]);
}

double Haversine(double lat1, double long1, double lat2, double long2)
{
    double radiusE = 6378135; //Equator radius
    double radiusP = 6356750; //Pole radius

    double nr = Math.Pow(radiusE * radiusE * Math.Cos(lat1 / 180 * Math.PI), 2) + Math.Pow(radiusP * radiusP * Math.Sin(lat1 / 180 * Math.PI), 2);
    double dr = Math.Pow(radiusE * Math.Cos(lat1 / 180 * Math.PI), 2) + Math.Pow(radiusP * Math.Sin(lat1 / 180 * Math.PI), 2);
    double rad = Math.Sqrt(nr / dr);

    double p1X = lat1 / 180 * Math.PI;
    double p1Y = long1 / 180 * Math.PI;
    double p2X = lat2 / 180 * Math.PI;
    double p2Y = long2 / 180 * Math.PI;
    return Math.Acos(Math.Sin(p1Y) * Math.Sin(p2Y) + Math.Cos(p1Y) * Math.Cos(p2Y) * Math.Cos(p2X - p1X)) * rad;
}

ProjectionInfo ProjectionMap; //map projection. In my project, first layer added determines this variable, or it's set by user interaction
ProjectionInfo WGS84; //the WGS84 coordinate system

Envelope E=Map.Envelope;
Coordinate Pa = Reproject(E.TopLeft(), ProjectionMap, WGS84);
Coordinate Pb = Reproject(E.BottomRight(), ProjectionMap, WGS84);
double Height = Haversine(Pa.X, Pa.Y, Pb.X, Pb.Y) / 2.0;