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