• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

marble

  • sources
  • kde-4.12
  • kdeedu
  • marble
  • src
  • plugins
  • runner
  • yours
YoursRunner.cpp
Go to the documentation of this file.
1 //
2 // This file is part of the Marble Virtual Globe.
3 //
4 // This program is free software licensed under the GNU LGPL. You can
5 // find a copy of this license in LICENSE.txt in the top directory of
6 // the source code.
7 //
8 // Copyright 2010 Dennis Nienhüser <earthwings@gentoo.org>
9 //
10 
11 #include "YoursRunner.h"
12 
13 #include "MarbleDebug.h"
14 #include "MarbleLocale.h"
15 #include "GeoDataDocument.h"
16 #include "GeoDataPlacemark.h"
17 #include "TinyWebBrowser.h"
18 #include "GeoDataParser.h"
19 #include "GeoDataFolder.h"
20 #include "routing/RouteRequest.h"
21 
22 #include <QString>
23 #include <QVector>
24 #include <QUrl>
25 #include <QTime>
26 #include <QNetworkAccessManager>
27 #include <QNetworkReply>
28 #include <QDomDocument>
29 #include <QBuffer>
30 #include <QTimer>
31 
32 namespace Marble
33 {
34 
35 YoursRunner::YoursRunner( QObject *parent ) :
36  RoutingRunner( parent ),
37  m_networkAccessManager()
38 {
39  connect( &m_networkAccessManager, SIGNAL(finished(QNetworkReply*)),
40  this, SLOT(retrieveData(QNetworkReply*)) );
41 }
42 
43 YoursRunner::~YoursRunner()
44 {
45  // nothing to do
46 }
47 
48 void YoursRunner::retrieveRoute( const RouteRequest *route )
49 {
50  if ( route->size() != 2 ) {
51  return;
52  }
53 
54  GeoDataCoordinates source = route->source();
55  GeoDataCoordinates destination = route->destination();
56 
57  double fLon = source.longitude( GeoDataCoordinates::Degree );
58  double fLat = source.latitude( GeoDataCoordinates::Degree );
59 
60  double tLon = destination.longitude( GeoDataCoordinates::Degree );
61  double tLat = destination.latitude( GeoDataCoordinates::Degree );
62 
63  QString base = "http://www.yournavigation.org/api/1.0/gosmore.php";
64  //QString base = "http://nroets.dev.openstreetmap.org/demo/gosmore.php";
65  QString args = "?flat=%1&flon=%2&tlat=%3&tlon=%4";
66  args = args.arg( fLat, 0, 'f', 6 ).arg( fLon, 0, 'f', 6 ).arg( tLat, 0, 'f', 6 ).arg( tLon, 0, 'f', 6 );
67  QString preferences = "&v=motorcar&fast=1&layer=mapnik";
68  QString request = base + args + preferences;
69  // mDebug() << "GET: " << request;
70 
71  m_request = QNetworkRequest( QUrl( request ) );
72 
73  QEventLoop eventLoop;
74 
75  QTimer timer;
76  timer.setSingleShot( true );
77  timer.setInterval( 15000 );
78 
79  connect( &timer, SIGNAL(timeout()),
80  &eventLoop, SLOT(quit()));
81  connect( this, SIGNAL(routeCalculated(GeoDataDocument*)),
82  &eventLoop, SLOT(quit()) );
83 
84  // @todo FIXME Must currently be done in the main thread, see bug 257376
85  QTimer::singleShot( 0, this, SLOT(get()) );
86  timer.start();
87 
88  eventLoop.exec();
89 }
90 
91 void YoursRunner::get()
92 {
93  QNetworkReply *reply = m_networkAccessManager.get( m_request );
94  connect( reply, SIGNAL(error(QNetworkReply::NetworkError)),
95  this, SLOT(handleError(QNetworkReply::NetworkError)) );
96 }
97 
98 void YoursRunner::retrieveData( QNetworkReply *reply )
99 {
100  if ( reply->isFinished() ) {
101  QByteArray data = reply->readAll();
102  reply->deleteLater();
103  //mDebug() << "Download completed: " << data;
104  GeoDataDocument* result = parse( data );
105  if ( result ) {
106  QString name = "%1 %2 (Yours)";
107  QString unit = QLatin1String( "m" );
108  qreal length = distance( result );
109  if ( length == 0.0 ) {
110  delete result;
111  emit routeCalculated( 0 );
112  return;
113  } else if ( length >= 1000 ) {
114  length /= 1000.0;
115  unit = "km";
116  }
117  result->setName( name.arg( length, 0, 'f', 1 ).arg( unit ) );
118  }
119  emit routeCalculated( result );
120  }
121 }
122 
123 void YoursRunner::handleError( QNetworkReply::NetworkError error )
124 {
125  mDebug() << " Error when retrieving yournavigation.org route: " << error;
126  emit routeCalculated( 0 );
127 }
128 
129 GeoDataDocument* YoursRunner::parse( const QByteArray &content ) const
130 {
131  GeoDataParser parser( GeoData_UNKNOWN );
132 
133  // Open file in right mode
134  QBuffer buffer;
135  buffer.setData( content );
136  buffer.open( QIODevice::ReadOnly );
137 
138  if ( !parser.read( &buffer ) ) {
139  mDebug() << "Cannot parse kml data! Input is " << content ;
140  return 0;
141  }
142  GeoDataDocument* document = static_cast<GeoDataDocument*>( parser.releaseDocument() );
143  return document;
144 }
145 
146 qreal YoursRunner::distance( const GeoDataDocument* document ) const
147 {
148  QVector<GeoDataFolder*> folders = document->folderList();
149  foreach( const GeoDataFolder *folder, folders ) {
150  foreach( const GeoDataPlacemark *placemark, folder->placemarkList() ) {
151  GeoDataGeometry* geometry = placemark->geometry();
152  if ( geometry->geometryId() == GeoDataLineStringId ) {
153  GeoDataLineString* lineString = dynamic_cast<GeoDataLineString*>( geometry );
154  Q_ASSERT( lineString && "Internal error: geometry ID does not match class type" );
155  return lineString->length( EARTH_RADIUS );
156  }
157  }
158  }
159 
160  return 0.0;
161 }
162 
163 } // namespace Marble
164 
165 #include "YoursRunner.moc"
GeoDataDocument.h
Marble::RouteRequest::size
int size() const
Number of points in the route.
Definition: RouteRequest.cpp:126
Marble::GeoDataCoordinates
A 3d point representation.
Definition: GeoDataCoordinates.h:52
TinyWebBrowser.h
Marble::GeoDataDocument
A container for Features, Styles and in the future Schemas.
Definition: GeoDataDocument.h:64
Marble::RouteRequest::destination
GeoDataCoordinates destination() const
The last point, or a default constructed if empty.
Definition: RouteRequest.cpp:140
GeoDataParser.h
Marble::GeoDataCoordinates::latitude
qreal latitude(GeoDataCoordinates::Unit unit=GeoDataCoordinates::Radian) const
retrieves the latitude of the GeoDataCoordinates object use the unit parameter to switch between Radi...
Definition: GeoDataCoordinates.cpp:751
QObject
MarbleDebug.h
Marble::GeoDataCoordinates::Degree
Definition: GeoDataCoordinates.h:66
Marble::RouteRequest
Points to be included in a route.
Definition: RouteRequest.h:31
Marble::EARTH_RADIUS
const qreal EARTH_RADIUS
Definition: MarbleGlobal.h:238
MarbleLocale.h
GeoDataPlacemark.h
Marble::YoursRunner::~YoursRunner
~YoursRunner()
Definition: YoursRunner.cpp:43
Marble::GeoDataCoordinates::longitude
qreal longitude(GeoDataCoordinates::Unit unit=GeoDataCoordinates::Radian) const
retrieves the longitude of the GeoDataCoordinates object use the unit parameter to switch between Rad...
Definition: GeoDataCoordinates.cpp:739
Marble::RoutingRunner::routeCalculated
void routeCalculated(GeoDataDocument *route)
Route download/calculation is finished, result in the given route object.
GeoDataFolder.h
Marble::YoursRunner::YoursRunner
YoursRunner(QObject *parent=0)
Definition: YoursRunner.cpp:35
Marble::GeoData_UNKNOWN
Definition: GeoDataParser.h:35
Marble::RoutingRunner
Definition: RoutingRunner.h:27
Marble::YoursRunner::retrieveRoute
virtual void retrieveRoute(const RouteRequest *request)
Start a route download orw calculation.
Definition: YoursRunner.cpp:48
RouteRequest.h
Marble::GeoDataLineStringId
Definition: Serializable.h:43
Marble::mDebug
QDebug mDebug()
a function to replace qDebug() in Marble library code
Definition: MarbleDebug.cpp:31
Marble::RouteRequest::source
GeoDataCoordinates source() const
The first point, or a default constructed if empty.
Definition: RouteRequest.cpp:131
YoursRunner.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:38:53 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

marble

Skip menu "marble"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal