• 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
  • monav
MonavRunner.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 "MonavRunner.h"
12 #include "MonavPlugin.h"
13 #include "signals.h"
14 
15 #include "MarbleDebug.h"
16 #include "MarbleDirs.h"
17 #include "routing/RouteRequest.h"
18 #include "routing/instructions/InstructionTransformation.h"
19 #include "GeoDataDocument.h"
20 #include "GeoDataData.h"
21 #include "GeoDataExtendedData.h"
22 
23 #include <QProcess>
24 #include <QTime>
25 #include <QLocalSocket>
26 
27 using namespace MoNav;
28 
29 namespace Marble
30 {
31 
32 class MonavRunnerPrivate
33 {
34 public:
35  const MonavPlugin* m_plugin;
36 
37  MonavRunnerPrivate( const MonavPlugin* plugin );
38 
39  bool retrieveData( const RouteRequest *route, RoutingResult* result ) const;
40 
41  bool retrieveData( const RouteRequest *route, const QString &mapDir, RoutingResult* result ) const;
42 
43  GeoDataLineString* retrieveRoute( const RouteRequest *route, QVector<GeoDataPlacemark*> *instructions ) const;
44 
45  GeoDataDocument* createDocument( GeoDataLineString* geometry, const QVector<GeoDataPlacemark*> &instructions ) const;
46 };
47 
48 MonavRunnerPrivate::MonavRunnerPrivate( const MonavPlugin* plugin ) :
49  m_plugin( plugin )
50 {
51  // nothing to do
52 }
53 
54 bool MonavRunnerPrivate::retrieveData( const RouteRequest *route, RoutingResult* reply ) const
55 {
56  QString mapDir = m_plugin->mapDirectoryForRequest( route );
57  if ( mapDir.isEmpty() ) {
58  return false;
59  }
60 
61  if ( retrieveData( route, mapDir, reply ) ) {
62  return true;
63  }
64 
65  // The separation into two different methods to determine a first country candidate
66  // and a list of alternative ones if the first candidate fails is intentional
67  // for performance reasons. Do not merge both.
68  QStringList alternatives = m_plugin->mapDirectoriesForRequest( route );
69  alternatives.removeOne( mapDir );
70  foreach( const QString &mapDir, alternatives ) {
71  if ( retrieveData( route, mapDir, reply ) ) {
72  return true;
73  }
74  }
75 
76  return false;
77 }
78 
79 bool MonavRunnerPrivate::retrieveData( const RouteRequest *route, const QString &mapDir, RoutingResult* reply ) const
80 {
81  QLocalSocket socket;
82  socket.connectToServer( "MoNavD" );
83  if ( socket.waitForConnected() ) {
84  if ( m_plugin->monavVersion() == MonavPlugin::Monav_0_3 ) {
85  CommandType commandType;
86  commandType.value = CommandType::RoutingCommand;
87  commandType.post( &socket );
88  }
89 
90  RoutingCommand command;
91  QVector<Node> waypoints;
92 
93  for ( int i = 0; i < route->size(); ++i ) {
94  Node coordinate;
95  coordinate.longitude = route->at( i ).longitude( GeoDataCoordinates::Degree );
96  coordinate.latitude = route->at( i ).latitude( GeoDataCoordinates::Degree );
97  waypoints << coordinate;
98  }
99 
100  command.dataDirectory = mapDir;
101  command.lookupRadius = 1500;
102  command.waypoints = waypoints;
103  command.lookupStrings = true;
104 
105  command.post( &socket );
106  socket.flush();
107 
108  if ( reply->read( &socket ) ) {
109  switch ( reply->type ) {
110  case RoutingResult::LoadFailed:
111  mDebug() << "failed to load monav map from " << mapDir;
112  return false;
113  break;
114  case RoutingResult::RouteFailed:
115  mDebug() << "failed to retrieve route from monav daemon";
116  return false;
117  break;
118  case RoutingResult::TypeLookupFailed:
119  mDebug() << "failed to lookup type from monav daemon";
120  return false;
121  break;
122  case RoutingResult::NameLookupFailed:
123  mDebug() << "failed to lookup name from monav daemon";
124  return false;
125  break;
126  case RoutingResult::Success:
127  return true;
128  }
129  } else {
130  mDebug() << "Failed to read reply";
131  }
132  } else {
133  mDebug() << "No connection to MoNavD";
134  }
135 
136  return false;
137 }
138 
139 GeoDataLineString* MonavRunnerPrivate::retrieveRoute( const RouteRequest *route, QVector<GeoDataPlacemark*> *instructions ) const
140 {
141  GeoDataLineString* geometry = new GeoDataLineString;
142  RoutingResult reply;
143  if ( retrieveData( route, &reply ) ) {
145  for ( int i = 0; i < reply.pathNodes.size(); ++i ) {
146  qreal lon = reply.pathNodes[i].longitude;
147  qreal lat = reply.pathNodes[i].latitude;
148  GeoDataCoordinates coordinates( lon, lat, 0, GeoDataCoordinates::Degree );
149  geometry->append( coordinates );
150  }
151 
152  RoutingWaypoints waypoints;
153  int k = 0;
154  for ( int i = 0; i < reply.pathEdges.size(); ++i ) {
155  QString road = reply.nameStrings[reply.pathEdges[i].name];
156  QString type = reply.typeStrings[reply.pathEdges[i].type];
157  RoutingWaypoint::JunctionType junction = RoutingWaypoint::Other;
158  if ( type == "roundabout" && reply.pathEdges[i].branchingPossible ) {
159  junction = RoutingWaypoint::Roundabout;
160  }
161  for ( unsigned int l = 0; l < reply.pathEdges[i].length; ++k, ++l ) {
162  qreal lon = reply.pathNodes[k].longitude;
163  qreal lat = reply.pathNodes[k].latitude;
164  RoutingPoint point( lon, lat );
165  bool const last = l == reply.pathEdges[i].length - 1;
166  RoutingWaypoint::JunctionType finalJunction = last ? junction : ( reply.pathEdges[i].branchingPossible ? RoutingWaypoint::Other : RoutingWaypoint::None );
167  RoutingWaypoint waypoint( point, finalJunction, "", type, -1, road );
168  waypoints.push_back( waypoint );
169  }
170  }
171 
172  RoutingInstructions directions = InstructionTransformation::process( waypoints );
173  for ( int i = 0; i < directions.size(); ++i ) {
174  GeoDataPlacemark* placemark = new GeoDataPlacemark( directions[i].instructionText() );
175  GeoDataExtendedData extendedData;
176  GeoDataData turnType;
177  turnType.setName( "turnType" );
178  turnType.setValue( qVariantFromValue<int>( int( directions[i].turnType() ) ) );
179  extendedData.addValue( turnType );
180  GeoDataData roadName;
181  roadName.setName( "roadName" );
182  roadName.setValue( directions[i].roadName() );
183  extendedData.addValue( roadName );
184  placemark->setExtendedData( extendedData );
185  Q_ASSERT( !directions[i].points().isEmpty() );
186  GeoDataLineString* geometry = new GeoDataLineString;
187  QVector<RoutingWaypoint> items = directions[i].points();
188  for ( int j = 0; j < items.size(); ++j ) {
189  RoutingPoint point = items[j].point();
190  GeoDataCoordinates coordinates( point.lon(), point.lat(), 0.0, GeoDataCoordinates::Degree );
191  geometry->append( coordinates );
192  }
193  placemark->setGeometry( geometry );
194  instructions->push_back( placemark );
195  }
196  }
197 
198  return geometry;
199 }
200 
201 GeoDataDocument* MonavRunnerPrivate::createDocument( GeoDataLineString *geometry, const QVector<GeoDataPlacemark*> &instructions ) const
202 {
203  if ( !geometry || geometry->isEmpty() ) {
204  return 0;
205  }
206 
207  GeoDataDocument* result = new GeoDataDocument;
208  GeoDataPlacemark* routePlacemark = new GeoDataPlacemark;
209  routePlacemark->setName( "Route" );
210  routePlacemark->setGeometry( geometry );
211  result->append( routePlacemark );
212 
213  QString name = "%1 %2 (Monav)";
214  QString unit = QLatin1String( "m" );
215  qreal length = geometry->length( EARTH_RADIUS );
216  if ( length >= 1000 ) {
217  length /= 1000.0;
218  unit = "km";
219  }
220 
221  foreach( GeoDataPlacemark* placemark, instructions ) {
222  result->append( placemark );
223  }
224 
225  result->setName( name.arg( length, 0, 'f', 1 ).arg( unit ) );
226  return result;
227 }
228 
229 MonavRunner::MonavRunner( const MonavPlugin* plugin, QObject *parent ) :
230  RoutingRunner( parent ),
231  d( new MonavRunnerPrivate( plugin ) )
232 {
233  // nothing to do
234 }
235 
236 MonavRunner::~MonavRunner()
237 {
238  delete d;
239 }
240 
241 void MonavRunner::retrieveRoute( const RouteRequest *route )
242 {
243  QVector<GeoDataPlacemark*> instructions;
244  GeoDataLineString* waypoints = d->retrieveRoute( route, &instructions );
245  GeoDataDocument* result = d->createDocument( waypoints, instructions );
246  emit routeCalculated( result );
247 }
248 
249 #if 0
250 void MonavRunner::reverseGeocoding( const GeoDataCoordinates &coordinates )
251 {
252  GeoDataPlacemark placemark;
253  placemark.setCoordinate( GeoDataPoint( coordinates ) );
254 
255  RouteRequest route;
256  route.append( coordinates );
257  route.append( coordinates );
258  RoutingResult reply;
259 
260  if ( d->retrieveData( &route, &reply ) && !reply.pathEdges.isEmpty() ) {
261  QString road = reply.nameStrings[reply.pathEdges[0].name];
262  placemark.setAddress( road );
263  GeoDataExtendedData extendedData;
264  extendedData.addValue( GeoDataData( "road", road ) );
265  placemark.setExtendedData( extendedData );
266  }
267 
268  emit reverseGeocodingFinished( coordinates, placemark );
269 }
270 #endif
271 
272 } // namespace Marble
273 
274 #include "MonavRunner.moc"
GeoDataDocument.h
Marble::GeoDataPoint
A Geometry object representing a 3d point.
Definition: GeoDataPoint.h:47
Marble::GeoDataCoordinates
A 3d point representation.
Definition: GeoDataCoordinates.h:52
MoNav::CommandType::post
void post(QIODevice *out)
Definition: signals.h:43
Marble::GeoDataDocument
A container for Features, Styles and in the future Schemas.
Definition: GeoDataDocument.h:64
MoNav::CommandType
Definition: signals.h:37
MoNav::RoutingResult::read
bool read(QLocalSocket *in)
Definition: signals.h:202
MoNav::RoutingResult::nameStrings
QStringList nameStrings
Definition: signals.h:184
Marble::MonavRunner::~MonavRunner
~MonavRunner()
Definition: MonavRunner.cpp:236
Marble::MonavPlugin
Definition: MonavPlugin.h:23
Marble::GeoDataFeature::setExtendedData
void setExtendedData(const GeoDataExtendedData &extendedData)
Sets the ExtendedData of the feature.
Definition: GeoDataFeature.cpp:658
Marble::RouteRequest::append
void append(const GeoDataCoordinates &coordinates, const QString &name=QString())
Add the given element to the end.
Definition: RouteRequest.cpp:218
GeoDataExtendedData.h
Marble::GeoDataPlacemark::setCoordinate
void setCoordinate(qreal longitude, qreal latitude, qreal altitude=0, GeoDataCoordinates::Unit _unit=GeoDataCoordinates::Radian)
Set the coordinate of the placemark in longitude and latitude.
Definition: GeoDataPlacemark.cpp:121
Marble::RoutingInstructions
QVector< RoutingInstruction > RoutingInstructions
Definition: RoutingInstruction.h:157
MonavPlugin.h
QObject
InstructionTransformation.h
MarbleDebug.h
Marble::RoutingWaypoint::None
Definition: RoutingWaypoint.h:33
Marble::GeoDataCoordinates::Degree
Definition: GeoDataCoordinates.h:66
Marble::GeoDataExtendedData
a class which allows to add custom data to KML Feature.
Definition: GeoDataExtendedData.h:35
MoNav::RoutingResult
Definition: signals.h:173
MoNav::Node::latitude
double latitude
Definition: signals.h:66
Marble::RouteRequest
Points to be included in a route.
Definition: RouteRequest.h:31
Marble::MonavRunner::MonavRunner
MonavRunner(const MonavPlugin *plugin, QObject *parent=0)
Definition: MonavRunner.cpp:229
Marble::EARTH_RADIUS
const qreal EARTH_RADIUS
Definition: MarbleGlobal.h:238
Marble::InstructionTransformation::process
static RoutingInstructions process(const RoutingWaypoints &waypoints)
Transforms waypoints and metadata into driving directions.
Definition: InstructionTransformation.cpp:18
MarbleDirs.h
MoNav::CommandType::value
enum MoNav::CommandType::Type value
MoNav::RoutingCommand::dataDirectory
QString dataDirectory
Definition: signals.h:127
MoNav::Node::longitude
double longitude
Definition: signals.h:67
Marble::GeoDataLineString
A LineString that allows to store a contiguous set of line segments.
Definition: GeoDataLineString.h:75
Marble::MonavPlugin::Monav_0_3
Definition: MonavPlugin.h:33
MoNav::RoutingResult::typeStrings
QStringList typeStrings
Definition: signals.h:185
MoNav::RoutingCommand
Definition: signals.h:112
Marble::GeoDataFeature::setAddress
void setAddress(const QString &value)
Set the address of this feature to value.
Definition: GeoDataFeature.cpp:496
MoNav::RoutingCommand::post
void post(QIODevice *out)
Definition: signals.h:131
MoNav::RoutingCommand::lookupRadius
double lookupRadius
Definition: signals.h:123
MoNav::RoutingCommand::lookupStrings
bool lookupStrings
Definition: signals.h:125
Marble::RoutingRunner::routeCalculated
void routeCalculated(GeoDataDocument *route)
Route download/calculation is finished, result in the given route object.
MoNav::RoutingResult::pathEdges
QVector< Edge > pathEdges
Definition: signals.h:183
Marble::GeoDataData
Definition: GeoDataData.h:26
GeoDataData.h
MoNav::RoutingResult::type
enum MoNav::RoutingResult::ResultType type
Marble::RoutingRunner
Definition: RoutingRunner.h:27
Marble::GeoDataExtendedData::addValue
void addValue(const GeoDataData &data)
add a data object to the GeoDataExtendedData with the key
Definition: GeoDataExtendedData.cpp:59
signals.h
Marble::RoutingWaypoint::Roundabout
Definition: RoutingWaypoint.h:31
RouteRequest.h
Marble::RoutingWaypoint::Other
Definition: RoutingWaypoint.h:32
MoNav::Node
Definition: signals.h:65
MoNav::RoutingResult::pathNodes
QVector< Node > pathNodes
Definition: signals.h:182
MonavRunner.h
MoNav::RoutingCommand::waypoints
QVector< Node > waypoints
Definition: signals.h:129
Marble::RoutingWaypoints
QVector< RoutingWaypoint > RoutingWaypoints
Definition: RoutingWaypoint.h:75
Marble::GeoDataPlacemark
a class representing a point of interest on the map
Definition: GeoDataPlacemark.h:54
Marble::mDebug
QDebug mDebug()
a function to replace qDebug() in Marble library code
Definition: MarbleDebug.cpp:31
Marble::MonavRunner::retrieveRoute
virtual void retrieveRoute(const RouteRequest *request)
Start a route download orw calculation.
Definition: MonavRunner.cpp:241
Marble::RoutingWaypoint::JunctionType
JunctionType
Junction types that affect instructions.
Definition: RoutingWaypoint.h:29
coordinate
Coordinate coordinate
Definition: tools/osm-addresses/OsmParser.cpp:38
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:38:51 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