3 #include "location_p.h"
9 #include <QNetworkAccessManager>
11 #include <QNetworkRequest>
12 #include <QNetworkReply>
13 #include <QXmlStreamReader>
15 #include <QXmlStreamAttributes>
17 #include <QGeoPositionInfo>
21 const double Location::KkjZoneInfo[6][2] = {
32 Location::Location( const QString &x, const QString &y, const QString &label ) :
33 q( new LocationPrivate( x, y, label ) ),
34 manager( new QNetworkAccessManager(this) )
36 qDebug() << "Location::Location(" << x << "," << y << "," << label <<")";
38 manager, SIGNAL( finished(QNetworkReply*) ),
39 this, SLOT( replyFinished(QNetworkReply*) )
43 Location::Location( const QGeoPositionInfo &positionInfo, const QString &label ) :
44 q( new LocationPrivate( label ) ),
47 qDebug() << "Location::Location( QGeoPositionInfo, label=" << label << " )";
48 qreal latitude = positionInfo.coordinate().latitude();
49 qreal longitude = positionInfo.coordinate().longitude();
54 WGS84lola_to_KKJxy( longitude, latitude, &outX, &outY);
61 Location::Location( const Location &from ) :
63 q( new LocationPrivate( from.label() ) ),
66 qDebug() << "Location::Location( const Location [" << from.label() << "] )";
67 q->setAddress( from.address() );
70 q->setValid( from.isValid() );
71 if ( from.manager != 0 ) {
72 manager = new QNetworkAccessManager(this);
73 connect( manager, SIGNAL( finished(QNetworkReply*) ), this, SLOT( replyFinished(QNetworkReply*) ) );
77 Location::Location( const QString &label ) :
78 q( new LocationPrivate( label ) ),
79 manager( new QNetworkAccessManager(this) )
81 qDebug() << "Location::Location( const QString &label=" << label << " )";
82 connect( manager, SIGNAL( finished(QNetworkReply*) ), this, SLOT( replyFinished(QNetworkReply*) ) );
93 Location &Location::operator=( const Location &from )
95 qDebug() << "Location::Location( const Location &from )";
96 q = new LocationPrivate( from.label() );
97 q->setAddress( from.address() );
100 q->setValid( from.isValid() );
102 if ( from.manager != 0 ) {
103 manager = new QNetworkAccessManager(this);
104 connect( manager, SIGNAL( finished(QNetworkReply*) ), this, SLOT( replyFinished(QNetworkReply*) ) );
112 void Location::resolveAddress( const QString &address )
114 qDebug() << "resolving address";
117 q->setAddress( address );
118 q->setValid( false );
120 QUrl fullUrl( Ytv::Url );
122 fullUrl.addEncodedQueryItem( "key", address.toAscii().toPercentEncoding() );
123 fullUrl.addQueryItem( "user", Ytv::Username );
124 fullUrl.addQueryItem( "pass", Ytv::Password );
126 manager->get( QNetworkRequest( fullUrl ) );
127 qDebug() << "waiting for reply from Ytv";
130 void Location::replyFinished( QNetworkReply * reply )
132 qDebug() << "address resolved";
133 q->parseReply( reply->readAll() );
136 qDebug() << label() << "becomeValid";
137 emit( becomeValid() );
141 QString Location::x() const
146 QString Location::y() const
151 void Location::setLabel( const QString &label ) const
153 q->setLabel( label );
156 QString Location::label() const
161 void Location::setAddress( const QString &address ) const
163 qDebug() << "setting address to" << address;
164 q->setAddress( address );
167 QString Location::address() const
172 bool Location::isValid() const
177 // Degrees to radians
178 double Location::radians(double deg)
180 return deg * M_PI / 180.0;
183 // Radians to degrees
184 double Location::degrees(double rad)
186 return rad * 180.0 / M_PI;
189 // Function: KKJ_Zone_I
190 int Location::KKJ_Zone_I(KKJ easting)
192 int zoneNumber = floor(easting / 1000000.0);
193 if (zoneNumber < 0 || zoneNumber > 5) {
200 // Function: KKJ_Zone_Lo
201 int Location::KKJ_Zone_Lo(double kkjlo)
203 // determine the zonenumber from KKJ easting
204 // takes KKJ zone which has center meridian
205 // longitude nearest (in math value) to
206 // the given KKJ longitude
208 while (zoneNumber >= 0) {
209 if (fabs(kkjlo - KkjZoneInfo[zoneNumber][0]) <= 1.5) {
219 // Function: KKJlalo_to_WGS84lalo
220 void Location::KKJlola_to_WGS84lola(double kkjlo, double kkjla, double *outLongitude, double *outLatitude)
222 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;
223 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;
225 *outLatitude = degrees(radians(kkjla) + dLa);
226 *outLongitude = degrees(radians(kkjlo) + dLo);
230 // Function: WGS84lalo_to_KKJlalo
231 void Location::WGS84lola_to_KKJlola(double longitude, double latitude, double *outLongitude, double *outLatitude)
233 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;
234 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;
236 *outLatitude = degrees(radians(latitude) + dLa);
237 *outLongitude = degrees(radians(longitude) + dLo);
241 // Function: KKJlalo_to_KKJxy
242 void Location::KKJlola_to_KKJxy(double lon, double lat, int zoneNumber, KKJ *outX, KKJ *outY)
245 double a = 6378388.0;
246 double f = 1.0 / 297.0;
247 double b = (1.0 - f) * a;
249 double c = (a / b) * a;
250 double ee = (a * a - bb) / bb;
251 double n = (a - b) / (a + b);
254 double Lo = radians(lon) - radians(KkjZoneInfo[zoneNumber][0]);
255 double cosLa = cos(radians(lat));
256 double NN = ee * cosLa * cosLa;
257 double LaF = atan(tan(radians(lat)) / cos(Lo * sqrt(1.0 + NN)));
258 double cosLaF = cos(LaF);
259 double t = (tan(Lo) * cosLaF) / sqrt(1.0 + ee * cosLaF * cosLaF);
260 double A = a / (1.0 + n);
261 double A1 = A * (1.0 + nn / 4.0 + nn * nn / 64.0);
262 double A2 = A * 1.5 * n * (1.0 - nn / 8.0);
263 double A3 = A * 0.9375 * nn * (1.0 - nn / 4.0);
264 double A4 = A * 35.0 / 48.0 * nn * n;
266 *outY = A1 * LaF - A2 * sin(2.0 * LaF) + A3 * sin(4.0 * LaF) - A4 * sin(6.0 * LaF);
267 *outX = c * log(t + sqrt(1.0 + t * t)) + 500000.0 + zoneNumber * 1000000.0;
270 // Function: KKJxy_to_KKJlalo
271 void Location::KKJxy_to_KKJlola(KKJ x, KKJ y, double *outLongitude, double *outLatitude)
273 // Scan iteratively the target area, until find matching
274 // KKJ coordinate value. Area is defined with Hayford Ellipsoid.
275 int zoneNumber = KKJ_Zone_I(x);
276 double minLo = radians(18.5);
277 double maxLo = radians(32.0);
278 double minLa = radians(59.0);
279 double maxLa = radians(70.5);
285 double deltaLo = maxLo - minLo;
286 double deltaLa = maxLa - minLa;
287 *outLongitude = degrees(minLo + 0.5 * deltaLo);
288 *outLatitude = degrees(minLa + 0.5 * deltaLa);
289 KKJlola_to_KKJxy(*outLongitude, *outLatitude, zoneNumber, &tmpX, &tmpY);
291 minLa = minLa + 0.45 * deltaLa;
293 maxLa = minLa + 0.55 * deltaLa;
297 minLo = minLo + 0.45 * deltaLo;
299 maxLo = minLo + 0.55 * deltaLo;
306 void Location::WGS84lola_to_KKJxy(double longitude, double latitude, KKJ *outX, KKJ *outY)
310 WGS84lola_to_KKJlola(longitude, latitude, &kkjlo, &kkjla);
311 int zoneNumber = KKJ_Zone_Lo(kkjlo);
312 KKJlola_to_KKJxy(kkjlo, kkjla, zoneNumber, outX, outY);
315 void Location::KKJxy_to_WGS84lola(KKJ x, KKJ y, double *outLongitude, double *outLatitude)
319 KKJxy_to_KKJlola(x, y, &kkjlo, &kkjla);
320 KKJlola_to_WGS84lola(kkjlo, kkjla, outLongitude, outLatitude);