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

marble

  • sources
  • kde-4.14
  • 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  int retrieveRoute( const RouteRequest *route, QVector<GeoDataPlacemark*> *instructions, GeoDataLineString* geometry ) const;
44 
45  static GeoDataDocument* createDocument( GeoDataLineString *geometry, const QVector<GeoDataPlacemark*> &instructions, const QString &name, const GeoDataExtendedData &data );
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 int MonavRunnerPrivate::retrieveRoute( const Marble::RouteRequest* route, QVector< Marble::GeoDataPlacemark* >* instructions, Marble::GeoDataLineString* geometry ) const
140 {
141  RoutingResult reply;
142  if ( retrieveData( route, &reply ) ) {
144  for ( int i = 0; i < reply.pathNodes.size(); ++i ) {
145  qreal lon = reply.pathNodes[i].longitude;
146  qreal lat = reply.pathNodes[i].latitude;
147  GeoDataCoordinates coordinates( lon, lat, 0, GeoDataCoordinates::Degree );
148  geometry->append( coordinates );
149  }
150 
151  RoutingWaypoints waypoints;
152  int k = 0;
153  for ( int i = 0; i < reply.pathEdges.size(); ++i ) {
154  QString road = reply.nameStrings[reply.pathEdges[i].name];
155  QString type = reply.typeStrings[reply.pathEdges[i].type];
156  RoutingWaypoint::JunctionType junction = RoutingWaypoint::Other;
157  if ( type == "roundabout" && reply.pathEdges[i].branchingPossible ) {
158  junction = RoutingWaypoint::Roundabout;
159  }
160  for ( unsigned int l = 0; l < reply.pathEdges[i].length; ++k, ++l ) {
161  qreal lon = reply.pathNodes[k].longitude;
162  qreal lat = reply.pathNodes[k].latitude;
163  RoutingPoint point( lon, lat );
164  bool const last = l == reply.pathEdges[i].length - 1;
165  RoutingWaypoint::JunctionType finalJunction = last ? junction : ( reply.pathEdges[i].branchingPossible ? RoutingWaypoint::Other : RoutingWaypoint::None );
166  RoutingWaypoint waypoint( point, finalJunction, "", type, -1, road );
167  waypoints.push_back( waypoint );
168  }
169  }
170 
171  RoutingInstructions directions = InstructionTransformation::process( waypoints );
172  for ( int i = 0; i < directions.size(); ++i ) {
173  GeoDataPlacemark* placemark = new GeoDataPlacemark( directions[i].instructionText() );
174  GeoDataExtendedData extendedData;
175  GeoDataData turnType;
176  turnType.setName( "turnType" );
177  turnType.setValue( qVariantFromValue<int>( int( directions[i].turnType() ) ) );
178  extendedData.addValue( turnType );
179  GeoDataData roadName;
180  roadName.setName( "roadName" );
181  roadName.setValue( directions[i].roadName() );
182  extendedData.addValue( roadName );
183  placemark->setExtendedData( extendedData );
184  Q_ASSERT( !directions[i].points().isEmpty() );
185  GeoDataLineString* geometry = new GeoDataLineString;
186  QVector<RoutingWaypoint> items = directions[i].points();
187  for ( int j = 0; j < items.size(); ++j ) {
188  RoutingPoint point = items[j].point();
189  GeoDataCoordinates coordinates( point.lon(), point.lat(), 0.0, GeoDataCoordinates::Degree );
190  geometry->append( coordinates );
191  }
192  placemark->setGeometry( geometry );
193  instructions->push_back( placemark );
194  }
195  int duration = (int) reply.seconds;
196  return duration;
197  }
198  return 0;
199 }
200 
201 GeoDataDocument* MonavRunnerPrivate::createDocument( Marble::GeoDataLineString* geometry, const QVector< Marble::GeoDataPlacemark* >& instructions, const QString& name, const Marble::GeoDataExtendedData& data )
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  routePlacemark->setExtendedData( data );
212  result->append( routePlacemark );
213 
214  foreach( GeoDataPlacemark* placemark, instructions ) {
215  result->append( placemark );
216  }
217 
218  result->setName( name );
219  return result;
220 }
221 
222 MonavRunner::MonavRunner( const MonavPlugin* plugin, QObject *parent ) :
223  RoutingRunner( parent ),
224  d( new MonavRunnerPrivate( plugin ) )
225 {
226  // nothing to do
227 }
228 
229 MonavRunner::~MonavRunner()
230 {
231  delete d;
232 }
233 
234 void MonavRunner::retrieveRoute( const RouteRequest *route )
235 {
236  QVector<GeoDataPlacemark*> instructions;
237  QTime time;
238  GeoDataLineString* waypoints = new GeoDataLineString();
239  int duration = d->retrieveRoute( route, &instructions, waypoints );
240  time = time.addSecs( duration );
241  qreal length = waypoints->length( EARTH_RADIUS );
242  const QString name = nameString( "Monav", length, time );
243  const GeoDataExtendedData data = routeData( length, time );
244  GeoDataDocument *result = d->createDocument( waypoints, instructions, name, data );
245  emit routeCalculated( result );
246 }
247 
248 #if 0
249 void MonavRunner::reverseGeocoding( const GeoDataCoordinates &coordinates )
250 {
251  GeoDataPlacemark placemark;
252  placemark.setCoordinate( GeoDataPoint( coordinates ) );
253 
254  RouteRequest route;
255  route.append( coordinates );
256  route.append( coordinates );
257  RoutingResult reply;
258 
259  if ( d->retrieveData( &route, &reply ) && !reply.pathEdges.isEmpty() ) {
260  QString road = reply.nameStrings[reply.pathEdges[0].name];
261  placemark.setAddress( road );
262  GeoDataExtendedData extendedData;
263  extendedData.addValue( GeoDataData( "road", road ) );
264  placemark.setExtendedData( extendedData );
265  }
266 
267  emit reverseGeocodingFinished( coordinates, placemark );
268 }
269 #endif
270 
271 } // namespace Marble
272 
273 #include "MonavRunner.moc"
GeoDataDocument.h
Marble::GeoDataPoint
A Geometry object representing a 3d point.
Definition: GeoDataPoint.h:47
Marble::GeoDataLineString::length
virtual qreal length(qreal planetRadius, int offset=0) const
Returns the length of LineString across a sphere starting from a coordinate in LineString This method...
Definition: GeoDataLineString.cpp:594
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:65
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
QVector::append
void append(const T &value)
Marble::MonavRunner::~MonavRunner
~MonavRunner()
Definition: MonavRunner.cpp:229
Marble::MonavPlugin
Definition: MonavPlugin.h:23
Marble::GeoDataFeature::setExtendedData
void setExtendedData(const GeoDataExtendedData &extendedData)
Sets the ExtendedData of the feature.
Definition: GeoDataFeature.cpp:748
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:215
Marble::RoutingInstructions
QVector< RoutingInstruction > RoutingInstructions
Definition: RoutingInstruction.h:157
MonavPlugin.h
QTime
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
Marble::RoutingRunner::nameString
const QString nameString(const QString &name, qreal length, const QTime &duration) const
Definition: RoutingRunner.cpp:55
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:222
Marble::EARTH_RADIUS
const qreal EARTH_RADIUS
Definition: MarbleGlobal.h:257
Marble::InstructionTransformation::process
static RoutingInstructions process(const RoutingWaypoints &waypoints)
Transforms waypoints and metadata into driving directions.
Definition: InstructionTransformation.cpp:18
QObject
MarbleDirs.h
QString::isEmpty
bool isEmpty() const
MoNav::CommandType::value
enum MoNav::CommandType::Type value
MoNav::RoutingCommand::dataDirectory
QString dataDirectory
Definition: signals.h:127
QTime::addSecs
QTime addSecs(int s) const
Marble::RoutingRunner::routeData
const GeoDataExtendedData routeData(qreal length, const QTime &duration) const
Definition: RoutingRunner.cpp:61
MoNav::Node::longitude
double longitude
Definition: signals.h:67
QString
Marble::GeoDataLineString::append
void append(const GeoDataCoordinates &position)
Appends a given geodesic position as a new node to the LineString.
Definition: GeoDataLineString.cpp:225
Marble::GeoDataLineString
A LineString that allows to store a contiguous set of line segments.
Definition: GeoDataLineString.h:75
QStringList
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:571
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
QLocalSocket::flush
bool flush()
MoNav::RoutingResult::type
enum MoNav::RoutingResult::ResultType type
QVector
Marble::GeoDataLineString::isEmpty
bool isEmpty() const
Returns whether the LineString has no nodes at all.
Definition: GeoDataLineString.cpp:133
Marble::RoutingRunner
Definition: RoutingRunner.h:28
Marble::GeoDataExtendedData::addValue
void addValue(const GeoDataData &data)
add a data object to the GeoDataExtendedData with the key
Definition: GeoDataExtendedData.cpp:71
signals.h
Marble::RoutingWaypoint::Roundabout
Definition: RoutingWaypoint.h:31
QLocalSocket::connectToServer
void connectToServer(const QString &name, QFlags< QIODevice::OpenModeFlag > openMode)
QVector::push_back
void push_back(const T &value)
RouteRequest.h
QLocalSocket::waitForConnected
bool waitForConnected(int msecs)
Marble::RoutingWaypoint::Other
Definition: RoutingWaypoint.h:32
MoNav::Node
Definition: signals.h:65
MoNav::RoutingResult::pathNodes
QVector< Node > pathNodes
Definition: signals.h:182
QVector::size
int size() const
MoNav::RoutingResult::seconds
double seconds
Definition: signals.h:181
MonavRunner.h
QLocalSocket
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:36
QList::removeOne
bool removeOne(const T &value)
Marble::MonavRunner::retrieveRoute
virtual void retrieveRoute(const RouteRequest *request)
Start a route download orw calculation.
Definition: MonavRunner.cpp:234
Marble::RoutingWaypoint::JunctionType
JunctionType
Junction types that affect instructions.
Definition: RoutingWaypoint.h:29
coordinate
Coordinate coordinate
Definition: tools/osm-addresses/OsmParser.cpp:39
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:13:41 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
  • 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