3b289ca8d08daef88049cc5780cf02e5fbbb174a
[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( const QString &x, const QString &y, const QString &label ) :
33   q( new LocationPrivate( x, y, label ) ),
34   manager( new QNetworkAccessManager(this) )
35 {
36   connect(
37       manager, SIGNAL( finished(QNetworkReply*) ),
38       this, SLOT( replyFinished(QNetworkReply*) )
39       );
40 }
41
42 Location::Location( const QGeoPositionInfo &positionInfo, const QString &label ) :
43   q( new LocationPrivate( label ) ),
44   manager(0)
45 {
46   qreal latitude = positionInfo.coordinate().latitude();
47   qreal longitude = positionInfo.coordinate().longitude();
48
49   KKJ outX(0);
50   KKJ outY(0);
51
52   WGS84lola_to_KKJxy( longitude, latitude, &outX, &outY);
53
54   q->setX( outX );
55   q->setY( outY );
56   q->setValid( true );
57 }
58
59 Location::Location( const Location &from ) :
60   QObject(0),
61   q( new LocationPrivate( from.label() ) ),
62   manager(0)
63 {
64   q->setAddress( from.address() );
65   q->setX( from.x() );
66   q->setY( from.y() );
67   q->setValid( from.isValid() );
68   if ( from.manager != 0 ) {
69     manager = new QNetworkAccessManager(this);
70     connect( manager, SIGNAL( finished(QNetworkReply*) ), this, SLOT( replyFinished(QNetworkReply*) ) );
71   }
72 }
73
74 Location::Location( const QString &label ) :
75   q( new LocationPrivate( label ) ),
76   manager( new QNetworkAccessManager(this) )
77 {
78   connect( manager, SIGNAL( finished(QNetworkReply*) ), this, SLOT( replyFinished(QNetworkReply*) ) );
79 }
80
81 Location::~Location()
82 {
83   delete q;
84   q=0;
85   delete manager;
86   manager=0;
87 }
88
89 Location &Location::operator=( const Location &from )
90 {
91   q = new LocationPrivate( from.label() );
92   q->setAddress( from.address() );
93   q->setX( from.x() );
94   q->setY( from.y() );
95   q->setValid( from.isValid() );
96   if ( from.manager != 0 ) {
97     manager = new QNetworkAccessManager(this);
98     connect( manager, SIGNAL( finished(QNetworkReply*) ), this, SLOT( replyFinished(QNetworkReply*) ) );
99   } else {
100     manager = 0;
101   }
102   
103   return *this;
104 }
105
106 void Location::resolveAddress( const QString &address )
107 {
108   qDebug() << "resolving address";
109   qDebug() << address;
110
111   q->setAddress( address );
112
113   QUrl fullUrl( Ytv::Url );
114
115   fullUrl.addEncodedQueryItem( "key", address.toAscii().toPercentEncoding() );
116   fullUrl.addQueryItem( "user", Ytv::Username );
117   fullUrl.addQueryItem( "pass", Ytv::Password );
118
119   manager->get( QNetworkRequest( fullUrl ) );
120   qDebug() << "waiting for reply from Ytv";
121 }
122
123 void Location::replyFinished( QNetworkReply * reply )
124 {
125   qDebug() << "address resolved";
126   q->parseReply( reply->readAll() );
127
128   if ( isValid() ) {
129     qDebug() << label() << "becomeValid" << this;
130     emit( becomeValid() );
131   }
132 }
133
134 QString Location::x() const
135 {
136   return q->x();
137 }
138
139 QString Location::y() const
140 {
141   return q->y();
142 }
143
144 void Location::setLabel( const QString &label ) const
145 {
146   q->setLabel( label );
147 }
148
149 QString Location::label() const
150 {
151   return q->label();
152 }
153
154 void Location::setAddress( const QString &address ) const
155 {
156   q->setAddress( address );
157 }
158
159 QString Location::address() const
160 {
161   return q->address();
162 }
163
164 bool Location::isValid() const
165 {
166   return q->isValid();
167 }
168
169
170 // Degrees to radians
171 double Location::radians(double deg)
172 {
173   return deg * M_PI / 180.0;
174 }
175
176 // Radians to degrees
177 double Location::degrees(double rad)
178 {
179   return rad * 180.0 / M_PI;
180 }
181
182 // Function:  KKJ_Zone_I
183 int Location::KKJ_Zone_I(KKJ easting)
184 {
185   int zoneNumber = floor(easting / 1000000.0);
186   if (zoneNumber < 0 || zoneNumber > 5) {
187     zoneNumber = -1;
188   }
189
190   return zoneNumber;
191 }
192
193 // Function:  KKJ_Zone_Lo
194 int Location::KKJ_Zone_Lo(double kkjlo)
195 {
196   // determine the zonenumber from KKJ easting
197   // takes KKJ zone which has center meridian
198   // longitude nearest (in math value) to
199   // the given KKJ longitude
200   int zoneNumber = 5;
201   while (zoneNumber >= 0) {
202     if (fabs(kkjlo - KkjZoneInfo[zoneNumber][0]) <= 1.5) {
203       break;
204     }
205     zoneNumber--;
206   }
207
208   return zoneNumber;
209 }
210
211
212 // Function:  KKJlalo_to_WGS84lalo
213 void Location::KKJlola_to_WGS84lola(double kkjlo, double kkjla, double *outLongitude, double *outLatitude)
214 {
215   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;
216   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;
217
218   *outLatitude = degrees(radians(kkjla) + dLa);
219   *outLongitude = degrees(radians(kkjlo) + dLo);
220 }
221
222
223 // Function:  WGS84lalo_to_KKJlalo
224 void Location::WGS84lola_to_KKJlola(double longitude, double latitude, double *outLongitude, double *outLatitude) 
225 {
226   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;
227   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;
228
229   *outLatitude = degrees(radians(latitude) + dLa);
230   *outLongitude = degrees(radians(longitude) + dLo);
231 }
232
233
234 // Function:  KKJlalo_to_KKJxy
235 void Location::KKJlola_to_KKJxy(double lon, double lat, int zoneNumber, KKJ *outX, KKJ *outY) 
236 {
237   // Hayford ellipsoid
238   double a = 6378388.0;
239   double f  = 1.0 / 297.0;
240   double b  = (1.0 - f) * a;
241   double bb = b * b;
242   double c  = (a / b) * a;
243   double ee = (a * a - bb) / bb;
244   double n = (a - b) / (a + b);
245   double nn = n * n;
246
247   double Lo = radians(lon) - radians(KkjZoneInfo[zoneNumber][0]);
248   double cosLa = cos(radians(lat));
249   double NN = ee * cosLa * cosLa;
250   double LaF = atan(tan(radians(lat)) / cos(Lo * sqrt(1.0 + NN)));
251   double cosLaF = cos(LaF);
252   double t = (tan(Lo) * cosLaF) / sqrt(1.0 + ee * cosLaF * cosLaF);
253   double A = a / (1.0 + n);
254   double A1 = A * (1.0 + nn / 4.0 + nn * nn / 64.0);
255   double A2 = A * 1.5 * n * (1.0 - nn / 8.0);
256   double A3 = A * 0.9375 * nn * (1.0 - nn / 4.0);
257   double A4 = A * 35.0 / 48.0 * nn * n;
258
259   *outY = A1 * LaF - A2 * sin(2.0 * LaF) + A3 * sin(4.0 * LaF) - A4 * sin(6.0 * LaF);
260   *outX = c * log(t + sqrt(1.0 + t * t)) + 500000.0 + zoneNumber * 1000000.0;
261 }
262
263 // Function:  KKJxy_to_KKJlalo
264 void Location::KKJxy_to_KKJlola(KKJ x, KKJ y, double *outLongitude, double *outLatitude)
265 {
266   // Scan iteratively the target area, until find matching
267   // KKJ coordinate value.  Area is defined with Hayford Ellipsoid.
268   int zoneNumber = KKJ_Zone_I(x);
269   double minLo = radians(18.5);
270   double maxLo = radians(32.0);
271   double minLa = radians(59.0);
272   double maxLa = radians(70.5);
273
274   int i = 1;
275   KKJ tmpX, tmpY;
276
277   while (i < 35) {
278     double deltaLo = maxLo - minLo;
279     double deltaLa = maxLa - minLa;
280     *outLongitude = degrees(minLo + 0.5 * deltaLo);
281     *outLatitude = degrees(minLa + 0.5 * deltaLa);
282     KKJlola_to_KKJxy(*outLongitude, *outLatitude, zoneNumber, &tmpX, &tmpY);
283     if (tmpY < y) {
284       minLa = minLa + 0.45 * deltaLa;
285     } else {
286       maxLa = minLa + 0.55 * deltaLa;
287     }
288
289     if (tmpX < x) {
290       minLo = minLo + 0.45 * deltaLo;
291     } else {
292       maxLo = minLo + 0.55 * deltaLo;
293     }
294
295     i++;
296   }
297 }
298
299 void Location::WGS84lola_to_KKJxy(double longitude, double latitude, KKJ *outX, KKJ *outY)
300 {
301   double kkjlo, kkjla;
302
303   WGS84lola_to_KKJlola(longitude, latitude, &kkjlo, &kkjla);
304   int zoneNumber = KKJ_Zone_Lo(kkjlo);
305   KKJlola_to_KKJxy(kkjlo, kkjla, zoneNumber, outX, outY);
306 }
307
308 void Location::KKJxy_to_WGS84lola(KKJ x, KKJ y, double *outLongitude, double *outLatitude)
309 {
310   double kkjlo, kkjla;
311
312   KKJxy_to_KKJlola(x, y, &kkjlo, &kkjla);
313   KKJlola_to_WGS84lola(kkjlo, kkjla, outLongitude, outLatitude);
314
315 }
316