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