80a665c7c5a5a38d513409692487f37d5215616a
[ptas] / zouba / location.cpp
1 #include "location.h"
2
3 #include "location_p.h"
4
5 #include "ytv.h"
6
7 #include <QString>
8 #include <QObject>
9 #include <QNetworkAccessManager>
10 #include <QUrl>
11 #include <QNetworkRequest>
12 #include <QNetworkReply>
13 #include <QXmlStreamReader>
14 #include <QDebug>
15 #include <QXmlStreamAttributes>
16 #include <QStringRef>
17 #include <QGeoPositionInfo>
18
19 #include <math.h>
20
21 const double Location::KkjZoneInfo[6][2] = {
22   {18.0,  500000.0},
23   {21.0, 1500000.0},
24   {24.0, 2500000.0},
25   {27.0, 3500000.0},
26   {30.0, 4500000.0},
27   {33.0, 5500000.0}
28 };
29
30 QTM_USE_NAMESPACE
31
32 Location::Location( QString x, QString y ) :
33   q( new LocationPrivate( x, y ) ),
34   manager( new QNetworkAccessManager(this) )
35 {
36   connect( manager, SIGNAL( finished(QNetworkReply*) ), this, SLOT( replyFinished(QNetworkReply*) ) );
37 }
38
39 Location::Location( const QGeoPositionInfo &positionInfo ) :
40   q( new LocationPrivate() ),
41   manager(0)
42 {
43   qreal latitude = positionInfo.coordinate().latitude();
44   qreal longitude = positionInfo.coordinate().longitude();
45
46   KKJ outX(0);
47   KKJ outY(0);
48
49   WGS84lola_to_KKJxy( longitude, latitude, &outX, &outY);
50
51   q->setX( outX );
52   q->setY( outY );
53   q->setValid( true );
54 }
55
56 Location::Location( const Location &from ) :
57   QObject(0),
58   q( new LocationPrivate() ),
59   manager(0)
60 {
61   q->setX( from.x() );
62   q->setY( from.y() );
63   q->setValid( from.isValid() );
64   if ( from.manager != 0 ) {
65     manager = new QNetworkAccessManager(this);
66     connect( manager, SIGNAL( finished(QNetworkReply*) ), this, SLOT( replyFinished(QNetworkReply*) ) );
67   }
68 }
69
70 Location::Location() :
71   q( new LocationPrivate() ),
72   manager( new QNetworkAccessManager(this) )
73 {
74   connect( manager, SIGNAL( finished(QNetworkReply*) ), this, SLOT( replyFinished(QNetworkReply*) ) );
75 }
76
77 Location::~Location()
78 {
79   delete q;
80   q=0;
81   delete manager;
82   manager=0;
83 }
84
85 Location &Location::operator=( const Location &from )
86 {
87   q = new LocationPrivate();
88   q->setX( from.x() );
89   q->setY( from.y() );
90   q->setValid( from.isValid() );
91   if ( from.manager != 0 ) {
92     manager = new QNetworkAccessManager(this);
93     connect( manager, SIGNAL( finished(QNetworkReply*) ), this, SLOT( replyFinished(QNetworkReply*) ) );
94   } else {
95     manager = 0;
96   }
97   
98   return *this;
99 }
100
101 void Location::resolveAddress( QString address )
102 {
103   qDebug() << "resolving address";
104   qDebug() << address;
105
106   QUrl fullUrl( Ytv::Url );
107
108   fullUrl.addEncodedQueryItem( "key", address.toAscii().toPercentEncoding() );
109   fullUrl.addQueryItem( "user", Ytv::Username );
110   fullUrl.addQueryItem( "pass", Ytv::Password );
111
112   manager->get( QNetworkRequest( fullUrl ) );
113   qDebug() << "waiting for reply from Ytv";
114 }
115
116 void Location::replyFinished( QNetworkReply * reply )
117 {
118   qDebug() << "address resolved";
119   q->parseReply( reply->readAll() );
120
121   if ( isValid() ) {
122     emit( becomeValid() );
123   }
124 }
125
126 QString Location::x() const
127 {
128   return q->x();
129 }
130
131 QString Location::y() const
132 {
133   return q->y();
134 }
135
136 bool Location::isValid() const
137 {
138   return q->isValid();
139 }
140
141
142 // Degrees to radians
143 double Location::radians(double deg)
144 {
145   return deg * M_PI / 180.0;
146 }
147
148 // Radians to degrees
149 double Location::degrees(double rad)
150 {
151   return rad * 180.0 / M_PI;
152 }
153
154 // Function:  KKJ_Zone_I
155 int Location::KKJ_Zone_I(KKJ easting)
156 {
157   int zoneNumber = floor(easting / 1000000.0);
158   if (zoneNumber < 0 || zoneNumber > 5) {
159     zoneNumber = -1;
160   }
161
162   return zoneNumber;
163 }
164
165 // Function:  KKJ_Zone_Lo
166 int Location::KKJ_Zone_Lo(double kkjlo)
167 {
168   // determine the zonenumber from KKJ easting
169   // takes KKJ zone which has center meridian
170   // longitude nearest (in math value) to
171   // the given KKJ longitude
172   int zoneNumber = 5;
173   while (zoneNumber >= 0) {
174     if (fabs(kkjlo - KkjZoneInfo[zoneNumber][0]) <= 1.5) {
175       break;
176     }
177     zoneNumber--;
178   }
179
180   return zoneNumber;
181 }
182
183
184 // Function:  KKJlalo_to_WGS84lalo
185 void Location::KKJlola_to_WGS84lola(double kkjlo, double kkjla, double *outLongitude, double *outLatitude)
186 {
187   double dLa = radians(0.124867E+01 + -0.269982E+00 * kkjla + 0.191330E+00 * kkjlo + 0.356119E-02 * kkjla * kkjla + -0.122312E-02 * kkjla * kkjlo + -0.335514E-03 * kkjlo * kkjlo) / 3600.0;
188   double dLo = radians(-0.286111E+02 + 0.114183E+01 * kkjla + -0.581428E+00 * kkjlo + -0.152421E-01 * kkjla * kkjla + 0.118177E-01 * kkjla * kkjlo + 0.826646E-03 * kkjlo * kkjlo) / 3600.0;
189
190   *outLatitude = degrees(radians(kkjla) + dLa);
191   *outLongitude = degrees(radians(kkjlo) + dLo);
192 }
193
194
195 // Function:  WGS84lalo_to_KKJlalo
196 void Location::WGS84lola_to_KKJlola(double longitude, double latitude, double *outLongitude, double *outLatitude) 
197 {
198   double dLa = radians(-0.124766E+01 + 0.269941E+00 * latitude + -0.191342E+00 * longitude + -0.356086E-02 * latitude * latitude + 0.122353E-02 * latitude * longitude + 0.335456E-03 * longitude * longitude) / 3600.0;
199   double dLo = radians(0.286008E+02 + -0.114139E+01 * latitude + 0.581329E+00 * longitude + 0.152376E-01 * latitude * latitude + -0.118166E-01 * latitude * longitude + -0.826201E-03 * longitude * longitude) / 3600.0;
200
201   *outLatitude = degrees(radians(latitude) + dLa);
202   *outLongitude = degrees(radians(longitude) + dLo);
203 }
204
205
206 // Function:  KKJlalo_to_KKJxy
207 void Location::KKJlola_to_KKJxy(double lon, double lat, int zoneNumber, KKJ *outX, KKJ *outY) 
208 {
209   // Hayford ellipsoid
210   double a = 6378388.0;
211   double f  = 1.0 / 297.0;
212   double b  = (1.0 - f) * a;
213   double bb = b * b;
214   double c  = (a / b) * a;
215   double ee = (a * a - bb) / bb;
216   double n = (a - b) / (a + b);
217   double nn = n * n;
218
219   double Lo = radians(lon) - radians(KkjZoneInfo[zoneNumber][0]);
220   double cosLa = cos(radians(lat));
221   double NN = ee * cosLa * cosLa;
222   double LaF = atan(tan(radians(lat)) / cos(Lo * sqrt(1.0 + NN)));
223   double cosLaF = cos(LaF);
224   double t = (tan(Lo) * cosLaF) / sqrt(1.0 + ee * cosLaF * cosLaF);
225   double A = a / (1.0 + n);
226   double A1 = A * (1.0 + nn / 4.0 + nn * nn / 64.0);
227   double A2 = A * 1.5 * n * (1.0 - nn / 8.0);
228   double A3 = A * 0.9375 * nn * (1.0 - nn / 4.0);
229   double A4 = A * 35.0 / 48.0 * nn * n;
230
231   *outY = A1 * LaF - A2 * sin(2.0 * LaF) + A3 * sin(4.0 * LaF) - A4 * sin(6.0 * LaF);
232   *outX = c * log(t + sqrt(1.0 + t * t)) + 500000.0 + zoneNumber * 1000000.0;
233 }
234
235 // Function:  KKJxy_to_KKJlalo
236 void Location::KKJxy_to_KKJlola(KKJ x, KKJ y, double *outLongitude, double *outLatitude)
237 {
238   // Scan iteratively the target area, until find matching
239   // KKJ coordinate value.  Area is defined with Hayford Ellipsoid.
240   int zoneNumber = KKJ_Zone_I(x);
241   double minLo = radians(18.5);
242   double maxLo = radians(32.0);
243   double minLa = radians(59.0);
244   double maxLa = radians(70.5);
245
246   int i = 1;
247   KKJ tmpX, tmpY;
248
249   while (i < 35) {
250     double deltaLo = maxLo - minLo;
251     double deltaLa = maxLa - minLa;
252     *outLongitude = degrees(minLo + 0.5 * deltaLo);
253     *outLatitude = degrees(minLa + 0.5 * deltaLa);
254     KKJlola_to_KKJxy(*outLongitude, *outLatitude, zoneNumber, &tmpX, &tmpY);
255     if (tmpY < y) {
256       minLa = minLa + 0.45 * deltaLa;
257     } else {
258       maxLa = minLa + 0.55 * deltaLa;
259     }
260
261     if (tmpX < x) {
262       minLo = minLo + 0.45 * deltaLo;
263     } else {
264       maxLo = minLo + 0.55 * deltaLo;
265     }
266
267     i++;
268   }
269 }
270
271 void Location::WGS84lola_to_KKJxy(double longitude, double latitude, KKJ *outX, KKJ *outY)
272 {
273   double kkjlo, kkjla;
274
275   WGS84lola_to_KKJlola(longitude, latitude, &kkjlo, &kkjla);
276   int zoneNumber = KKJ_Zone_Lo(kkjlo);
277   KKJlola_to_KKJxy(kkjlo, kkjla, zoneNumber, outX, outY);
278 }
279
280 void Location::KKJxy_to_WGS84lola(KKJ x, KKJ y, double *outLongitude, double *outLatitude)
281 {
282   double kkjlo, kkjla;
283
284   KKJxy_to_KKJlola(x, y, &kkjlo, &kkjla);
285   KKJlola_to_WGS84lola(kkjlo, kkjla, outLongitude, outLatitude);
286
287 }
288