00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <PalmOS.h>
00023 #include <PalmCompatibility.h>
00024 #include "latlong.h"
00025 #include "MathLib.h"
00026
00027
00028
00029
00030 static double PI = 3.14159265358979323846;
00031
00032 static double WGSa = 6378137.0;
00033 static double WGSinvf = 298.257223563;
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 void
00044 translate (short fromWGS84, double *latitude, double *longitude,
00045 short datumID)
00046 {
00047
00048 double dx = gDatum[datumID].dx;
00049 double dy = gDatum[datumID].dy;
00050 double dz = gDatum[datumID].dz;
00051
00052 double phi = *latitude * PI / 180.0;
00053 double lambda = *longitude * PI / 180.0;
00054 double a0,
00055 b0,
00056 es0,
00057 f0;
00058 double a1,
00059 b1,
00060 es1,
00061 f1;
00062 double psi;
00063 double x,
00064 y,
00065 z;
00066 double psi1;
00067
00068 if (datumID == WGS84Index)
00069 return;
00070
00071 if (fromWGS84)
00072 {
00073 a0 = WGSa;
00074 f0 = 1.0 / WGSinvf;
00075 a1 = gEllipsoid[gDatum[datumID].ellipsoid].a;
00076 f1 = 1.0 / gEllipsoid[gDatum[datumID].ellipsoid].invf;
00077 }
00078 else
00079 {
00080 a0 = gEllipsoid[gDatum[datumID].ellipsoid].a;
00081 f0 = 1.0 / gEllipsoid[gDatum[datumID].ellipsoid].invf;
00082 a1 = WGSa;
00083 f1 = 1 / WGSinvf;
00084 dx = -dx;
00085 dy = -dy;
00086 dz = -dz;
00087 }
00088
00089 b0 = a0 * (1 - f0);
00090 es0 = 2 * f0 - f0 * f0;
00091
00092 b1 = a1 * (1 - f1);
00093 es1 = 2 * f1 - f1 * f1;
00094
00095
00096 if (*latitude == 0.0 || *latitude == 90.0 || *latitude == -90.0)
00097 psi = phi;
00098 else
00099 psi = atan ((1 - es0) * tan (phi));
00100
00101
00102 if (*longitude == 90.0 || *longitude == -90.0)
00103 {
00104 x = 0.0;
00105 y = fabs (a0 * b0 / sqrt (b0 * b0 + a0 * a0 * pow (tan (psi), 2.0)));
00106 }
00107 else
00108 {
00109 x = fabs ((a0 * b0) /
00110 sqrt ((1 + pow (tan (lambda), 2.0)) *
00111 (b0 * b0 + a0 * a0 * pow (tan (psi), 2.0))));
00112 y = fabs (x * tan (lambda));
00113 }
00114
00115 if (*longitude < -90.0 || *longitude > 90.0)
00116 x = -x;
00117 if (*longitude < 0.0)
00118 y = -y;
00119
00120
00121 if (*latitude == 90.0)
00122 z = b0;
00123 else if (*latitude == -90.0)
00124 z = -b0;
00125 else
00126 z = tan (psi) * sqrt ((a0 * a0 * b0 * b0) / (b0 * b0 + a0 * a0 * pow (tan (psi), 2.0)));
00127
00128
00129 psi1 = atan ((z - dz) / sqrt ((x - dx) * (x - dx) + (y - dy) * (y - dy)));
00130
00131
00132 *latitude = atan (tan (psi1) / (1 - es1)) * 180.0 / PI;
00133
00134
00135 *longitude = atan ((y - dy) / (x - dx)) * 180.0 / PI;
00136
00137
00138 if (x - dx < 0.0)
00139 {
00140 if (y - dy > 0.0)
00141 *longitude = 180.0 + *longitude;
00142 else
00143 *longitude = -180.0 + *longitude;
00144 }
00145 }