• 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
  • lib
  • marble
  • routing
RoutingModel.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 "RoutingModel.h"
12 
13 #include "MarbleDebug.h"
14 #include "MarbleDirs.h"
15 #include "MarbleMath.h"
16 #include "GeoDataCoordinates.h"
17 #include "GeoDataDocument.h"
18 #include "GeoDataGeometry.h"
19 #include "GeoDataFolder.h"
20 #include "GeoDataPlacemark.h"
21 #include "RouteRequest.h"
22 #include "PositionTracking.h"
23 #include "MarbleModel.h"
24 #include "MarbleWidget.h"
25 #include "MarbleGlobal.h"
26 #include "GeoDataExtendedData.h"
27 
28 #include <QBuffer>
29 #include <QPointer>
30 #include <QRegExp>
31 #include <QVector>
32 #include <QHash>
33 #include <QByteArray>
34 #include <QMessageBox>
35 #include <QPixmap>
36 #include <QDomDocument>
37 
38 namespace Marble
39 {
40 
41 class RoutingModelPrivate
42 {
43 public:
44  enum RouteDeviation
45  {
46  Unknown,
47  OnRoute,
48  OffRoute
49  };
50 
51  RoutingModelPrivate( RouteRequest* request );
52 
53  Route m_route;
54 
55  RouteDeviation m_deviation;
56  PositionTracking* m_positionTracking;
57  RouteRequest* const m_request;
58  GeoDataCoordinates m_position;
59 #if QT_VERSION >= 0x050000
60  QHash<int, QByteArray> m_roleNames;
61 #endif
62 
63  void importPlacemark( RouteSegment &outline, QVector<RouteSegment> &segments, const GeoDataPlacemark *placemark );
64 
65  void updateViaPoints( const GeoDataCoordinates &position );
66 };
67 
68 RoutingModelPrivate::RoutingModelPrivate( RouteRequest* request )
69  : m_deviation( Unknown ),
70  m_positionTracking( 0 ),
71  m_request( request )
72 {
73  // nothing to do
74 }
75 
76 void RoutingModelPrivate::updateViaPoints( const GeoDataCoordinates &position )
77 {
78  // Mark via points visited after approaching them in a range of 500m or less
79  qreal const threshold = 500 / EARTH_RADIUS;
80  for( int i=0; i<m_request->size(); ++i ) {
81  if ( !m_request->visited( i ) ) {
82  if ( distanceSphere( position, m_request->at( i ) ) < threshold ) {
83  m_request->setVisited( i, true );
84  }
85  }
86  }
87 }
88 
89 void RoutingModelPrivate::importPlacemark( RouteSegment &outline, QVector<RouteSegment> &segments, const GeoDataPlacemark *placemark )
90 {
91  GeoDataGeometry* geometry = placemark->geometry();
92  GeoDataLineString* lineString = dynamic_cast<GeoDataLineString*>( geometry );
93  QStringList blacklist = QStringList() << "" << "Route" << "Tessellated";
94  RouteSegment segment;
95  bool isOutline = true;
96  if ( !blacklist.contains( placemark->name() ) ) {
97  if( lineString ) {
98  Maneuver maneuver;
99  maneuver.setInstructionText( placemark->name() );
100  maneuver.setPosition( lineString->at( 0 ) );
101 
102  if ( placemark->extendedData().contains( "turnType" ) ) {
103  QVariant turnType = placemark->extendedData().value( "turnType" ).value();
104  // The enum value is converted to/from an int in the QVariant
105  // because only a limited set of data types can be serialized with QVariant's
106  // toString() method (which is used to serialize <ExtendedData>/<Data> values)
107  maneuver.setDirection( Maneuver::Direction( turnType.toInt() ) );
108  }
109 
110  if ( placemark->extendedData().contains( "roadName" ) ) {
111  QVariant roadName = placemark->extendedData().value( "roadName" ).value();
112  maneuver.setRoadName( roadName.toString() );
113  }
114 
115  segment.setManeuver( maneuver );
116  isOutline = false;
117  }
118  }
119 
120  if ( lineString ) {
121  segment.setPath( *lineString );
122 
123  if ( isOutline ) {
124  outline = segment;
125  } else {
126  segments.push_back( segment );
127  }
128  }
129 }
130 
131 RoutingModel::RoutingModel( RouteRequest* request, MarbleModel *model, QObject *parent ) :
132  QAbstractListModel( parent ), d( new RoutingModelPrivate( request ) )
133 {
134  if( model )
135  {
136  d->m_positionTracking = model->positionTracking();
137  QObject::connect( d->m_positionTracking, SIGNAL(gpsLocation(GeoDataCoordinates,qreal)),
138  this, SLOT(updatePosition(GeoDataCoordinates,qreal)) );
139  }
140 
141  QHash<int, QByteArray> roles;
142  roles.insert( Qt::DisplayRole, "display" );
143  roles.insert( RoutingModel::TurnTypeIconRole, "turnTypeIcon" );
144  roles.insert( RoutingModel::LongitudeRole, "longitude" );
145  roles.insert( RoutingModel::LatitudeRole, "latitude" );
146 #if QT_VERSION < 0x050000
147  setRoleNames( roles );
148 #else
149  d->m_roleNames = roles;
150 #endif
151 }
152 
153 RoutingModel::~RoutingModel()
154 {
155  delete d;
156 }
157 
158 int RoutingModel::rowCount ( const QModelIndex &parent ) const
159 {
160  return parent.isValid() ? 0 : d->m_route.turnPoints().size();
161 }
162 
163 QVariant RoutingModel::headerData ( int section, Qt::Orientation orientation, int role ) const
164 {
165  if ( orientation == Qt::Horizontal && role == Qt::DisplayRole && section == 0 ) {
166  return QString( "Instruction" );
167  }
168 
169  return QAbstractListModel::headerData( section, orientation, role );
170 }
171 
172 QVariant RoutingModel::data ( const QModelIndex & index, int role ) const
173 {
174  if ( !index.isValid() ) {
175  return QVariant();
176  }
177 
178  if ( index.row() < d->m_route.turnPoints().size() && index.column() == 0 ) {
179  const RouteSegment &segment = d->m_route.at( index.row() );
180  switch ( role ) {
181  case Qt::DisplayRole:
182  case Qt::ToolTipRole:
183  return segment.maneuver().instructionText();
184  break;
185  case Qt::DecorationRole:
186  {
187  bool const smallScreen = MarbleGlobal::getInstance()->profiles() & MarbleGlobal::SmallScreen;
188  if ( segment.maneuver().hasWaypoint() ) {
189  int const size = smallScreen ? 64 : 32;
190  return d->m_request->pixmap( segment.maneuver().waypointIndex(), size, size/4 );
191  } else {
192  QPixmap const pixmap = segment.maneuver().directionPixmap();
193  return smallScreen ? pixmap : pixmap.scaled( 32, 32 );
194  }
195  }
196  break;
197  case Qt::SizeHintRole:
198  {
199  bool const smallScreen = MarbleGlobal::getInstance()->profiles() & MarbleGlobal::SmallScreen;
200  int const size = smallScreen ? 64 : 32;
201  return QSize( size, size );
202  }
203  break;
204  case RoutingModel::CoordinateRole:
205  return QVariant::fromValue( segment.maneuver().position() );
206  break;
207  case RoutingModel::LongitudeRole:
208  return QVariant::fromValue( segment.maneuver().position().longitude( GeoDataCoordinates::Degree ) );
209  break;
210  case RoutingModel::LatitudeRole:
211  return QVariant::fromValue( segment.maneuver().position().latitude( GeoDataCoordinates::Degree ) );
212  break;
213  case RoutingModel::TurnTypeIconRole:
214  return segment.maneuver().directionPixmap();
215  break;
216  default:
217  return QVariant();
218  }
219  }
220 
221  return QVariant();
222 }
223 
224 #if QT_VERSION >= 0x050000
225 QHash<int, QByteArray> RoutingModel::roleNames() const
226 {
227  return d->m_roleNames;
228 }
229 #endif
230 
231 bool RoutingModel::setCurrentRoute( GeoDataDocument* document )
232 {
233  d->m_route = Route();
234  QVector<RouteSegment> segments;
235  RouteSegment outline;
236 
237  QVector<GeoDataFolder*> folders = document->folderList();
238  foreach( const GeoDataFolder *folder, folders ) {
239  foreach( const GeoDataPlacemark *placemark, folder->placemarkList() ) {
240  d->importPlacemark( outline, segments, placemark );
241  }
242  }
243 
244  foreach( const GeoDataPlacemark *placemark, document->placemarkList() ) {
245  d->importPlacemark( outline, segments, placemark );
246  }
247 
248  if ( segments.isEmpty() ) {
249  segments << outline;
250  }
251 
252  // Map via points onto segments
253  if ( d->m_request->size() > 1 && segments.size() > 1 ) {
254  int index = 0;
255  for ( int j=0; j<d->m_request->size(); ++j ) {
256  QPair<int, qreal> minimum( -1, -1.0 );
257  int viaIndex = -1;
258  for ( int i=index; i<segments.size(); ++i ) {
259  RouteSegment const & segment = segments[i];
260  GeoDataCoordinates closest;
261  qreal const distance = segment.distanceTo( d->m_request->at( j ), closest, closest );
262  if ( minimum.first < 0 || distance < minimum.second ) {
263  minimum.first = i;
264  minimum.second = distance;
265  viaIndex = j;
266  }
267  }
268 
269  if ( minimum.first >= 0 ) {
270  index = minimum.first;
271  Maneuver viaPoint = segments[ minimum.first ].maneuver();
272  viaPoint.setWaypoint( d->m_request->at( viaIndex ), viaIndex );
273  segments[ minimum.first ].setManeuver( viaPoint );
274  }
275  }
276  }
277 
278  if ( segments.size() > 0 ) {
279  foreach( const RouteSegment &segment, segments ) {
280  d->m_route.addRouteSegment( segment );
281  }
282  }
283 
284  d->m_deviation = RoutingModelPrivate::Unknown;
285 
286  beginResetModel();
287  endResetModel();
288  emit currentRouteChanged();
289  return true;
290 }
291 
292 void RoutingModel::exportGpx( QIODevice *device ) const
293 {
294  QString content( "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>\n" );
295  content += "<gpx xmlns=\"http://www.topografix.com/GPX/1/1\" creator=\"Marble\" version=\"1.1\" ";
296  content += "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ";
297  content += "xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 ";
298  content += "http://www.topografix.com/GPX/1/1/gpx.xsd\">\n";
299  content += "<metadata>\n <link href=\"http://edu.kde.org/marble\">\n ";
300  content += "<text>Marble Virtual Globe</text>\n </link>\n</metadata>\n";
301 
302  content += " <rte>\n <name>Route</name>\n";
303  for ( int i=0; i<d->m_route.size(); ++i ) {
304  const RouteSegment &segment = d->m_route.at( i );
305  qreal lon = segment.maneuver().position().longitude( GeoDataCoordinates::Degree );
306  qreal lat = segment.maneuver().position().latitude( GeoDataCoordinates::Degree );
307  QString const text = segment.maneuver().instructionText();
308  content += QString( " <rtept lat=\"%1\" lon=\"%2\"><name>%3</name></rtept>\n" ).arg( lat, 0, 'f', 7 ).arg( lon, 0, 'f', 7 ).arg( text );
309  }
310  content += " </rte>\n";
311 
312  content += "<trk>\n <name>Route</name>\n <trkseg>\n";
313  GeoDataLineString points = d->m_route.path();
314  for ( int i=0; i<points.size(); ++i ) {
315  qreal lon = points[i].longitude( GeoDataCoordinates::Degree );
316  qreal lat = points[i].latitude( GeoDataCoordinates::Degree );
317  content += QString( " <trkpt lat=\"%1\" lon=\"%2\"></trkpt>\n" ).arg( lat, 0, 'f', 7 ).arg( lon, 0, 'f', 7 );
318  }
319  content += " </trkseg>\n </trk>\n";
320  content += "</gpx>\n";
321 
322  device->write( content.toUtf8() );
323 }
324 
325 void RoutingModel::clear()
326 {
327  d->m_route = Route();
328  beginResetModel();
329  endResetModel();
330  emit currentRouteChanged();
331 }
332 
333 int RoutingModel::rightNeighbor( const GeoDataCoordinates &position, RouteRequest const *const route ) const
334 {
335  Q_ASSERT( route && "Must not pass a null route ");
336 
337  // Quick result for trivial cases
338  if ( route->size() < 3 ) {
339  return route->size() - 1;
340  }
341 
342  // Generate an ordered list of all waypoints
343  GeoDataLineString points = d->m_route.path();
344  QMap<int,int> mapping;
345 
346  // Force first mapping point to match the route start
347  mapping[0] = 0;
348 
349  // Calculate the mapping between waypoints and via points
350  // Need two for loops to avoid getting stuck in local minima
351  for ( int j=1; j<route->size()-1; ++j ) {
352  qreal minDistance = -1.0;
353  for ( int i=mapping[j-1]; i<points.size(); ++i ) {
354  qreal distance = distanceSphere( points[i], route->at(j) );
355  if (minDistance < 0.0 || distance < minDistance ) {
356  mapping[j] = i;
357  minDistance = distance;
358  }
359  }
360  }
361 
362  // Determine waypoint with minimum distance to the provided position
363  qreal minWaypointDistance = -1.0;
364  int waypoint=0;
365  for ( int i=0; i<points.size(); ++i ) {
366  qreal waypointDistance = distanceSphere( points[i], position );
367  if ( minWaypointDistance < 0.0 || waypointDistance < minWaypointDistance ) {
368  minWaypointDistance = waypointDistance;
369  waypoint = i;
370  }
371  }
372 
373  // Force last mapping point to match the route destination
374  mapping[route->size()-1] = points.size()-1;
375 
376  // Determine neighbor based on the mapping
377  QMap<int, int>::const_iterator iter = mapping.constBegin();
378  for ( ; iter != mapping.constEnd(); ++iter ) {
379  if ( iter.value() > waypoint ) {
380  int index = iter.key();
381  Q_ASSERT( index >= 0 && index <= route->size() );
382  return index;
383  }
384  }
385 
386  return route->size()-1;
387 }
388 
389 void RoutingModel::updatePosition( GeoDataCoordinates location, qreal /*speed*/ )
390 {
391  d->m_position = location;
392  d->m_route.setPosition( location );
393 
394  d->updateViaPoints( d->m_position );
395  qreal distance = EARTH_RADIUS * distanceSphere( location, d->m_route.positionOnRoute() );
396  emit positionChanged();
397 
398  qreal deviation = 0.0;
399  if ( d->m_positionTracking && d->m_positionTracking->accuracy().vertical > 0.0 ) {
400  deviation = qMax<qreal>( d->m_positionTracking->accuracy().vertical, d->m_positionTracking->accuracy().horizontal );
401  }
402  qreal const threshold = deviation + 100.0;
403 
404  RoutingModelPrivate::RouteDeviation const deviated = distance < threshold ? RoutingModelPrivate::OnRoute : RoutingModelPrivate::OffRoute;
405  if ( d->m_deviation != deviated ) {
406  d->m_deviation = deviated;
407  emit deviatedFromRoute( deviated == RoutingModelPrivate::OffRoute );
408  }
409 }
410 
411 bool RoutingModel::deviatedFromRoute() const
412 {
413  return d->m_deviation == RoutingModelPrivate::OffRoute;
414 }
415 
416 const Route & RoutingModel::route() const
417 {
418  return d->m_route;
419 }
420 
421 } // namespace Marble
422 
423 #include "RoutingModel.moc"
GeoDataDocument.h
Marble::RouteRequest::size
int size() const
Number of points in the route.
Definition: RouteRequest.cpp:126
Marble::RoutingModel::exportGpx
void exportGpx(QIODevice *device) const
Export waypoints and instructions in gpx format.
Definition: RoutingModel.cpp:292
GeoDataCoordinates.h
Marble::RoutingModel::~RoutingModel
~RoutingModel()
Destructor.
Definition: RoutingModel.cpp:153
Marble::GeoDataCoordinates
A 3d point representation.
Definition: GeoDataCoordinates.h:52
RoutingModel.h
Marble::GeoDataDocument
A container for Features, Styles and in the future Schemas.
Definition: GeoDataDocument.h:64
MarbleMath.h
MarbleModel.h
This file contains the headers for MarbleModel.
Marble::GeoDataLineString::size
int size() const
Returns the number of nodes in a LineString.
Definition: GeoDataLineString.cpp:134
Marble::RoutingModel::data
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Overload of QAbstractListModel.
Definition: RoutingModel.cpp:172
Marble::distanceSphere
qreal distanceSphere(qreal lon1, qreal lat1, qreal lon2, qreal lat2)
This method calculates the shortest distance between two points on a sphere.
Definition: MarbleMath.h:52
Marble::RoutingModel::route
const Route & route() const
Definition: RoutingModel.cpp:416
Marble::RoutingModel::updatePosition
void updatePosition(GeoDataCoordinates, qreal)
Definition: RoutingModel.cpp:389
Marble::RoutingModel::rowCount
int rowCount(const QModelIndex &parent=QModelIndex()) const
Overload of QAbstractListModel.
Definition: RoutingModel.cpp:158
GeoDataExtendedData.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::RoutingModel::headerData
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Overload of QAbstractListModel.
Definition: RoutingModel.cpp:163
Marble::RoutingModel::setCurrentRoute
bool setCurrentRoute(GeoDataDocument *document)
Old data in the model is discarded, the parsed content of the provided document is used as the new mo...
Definition: RoutingModel.cpp:231
Marble::Maneuver
Definition: Maneuver.h:22
Marble::RouteRequest
Points to be included in a route.
Definition: RouteRequest.h:31
Marble::Maneuver::directionPixmap
QString directionPixmap() const
Definition: Maneuver.cpp:47
Marble::Maneuver::setWaypoint
void setWaypoint(const GeoDataCoordinates &waypoint, int index)
Definition: Maneuver.cpp:91
Marble::RoutingModel::positionChanged
void positionChanged()
emits a signal regarding information about total time( seconds ) and distance( metres ) remaining to ...
Marble::EARTH_RADIUS
const qreal EARTH_RADIUS
Definition: MarbleGlobal.h:238
MarbleDirs.h
Marble::GeoDataContainer::folderList
QVector< GeoDataFolder * > folderList() const
A convenience function that returns all folders in this container.
Definition: GeoDataContainer.cpp:90
Marble::MarbleModel::positionTracking
PositionTracking * positionTracking() const
Definition: MarbleModel.cpp:442
Marble::Maneuver::hasWaypoint
bool hasWaypoint() const
Definition: Maneuver.cpp:86
MarbleGlobal.h
Marble::GeoDataFolder
Definition: GeoDataFolder.h:50
GeoDataPlacemark.h
Marble::RoutingModel::clear
void clear()
Clear any data held in the model.
Definition: RoutingModel.cpp:325
Marble::GeoDataLineString
A LineString that allows to store a contiguous set of line segments.
Definition: GeoDataLineString.h:75
Marble::Maneuver::instructionText
QString instructionText() const
Definition: Maneuver.cpp:102
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::MarbleGlobal::SmallScreen
Definition: MarbleGlobal.h:268
Marble::MarbleGlobal::getInstance
static MarbleGlobal * getInstance()
Definition: MarbleGlobal.cpp:37
Marble::RoutingModel::currentRouteChanged
void currentRouteChanged()
A different route was loaded.
GeoDataFolder.h
Marble::MarbleModel
The data model (not based on QAbstractModel) for a MarbleWidget.
Definition: MarbleModel.h:96
Marble::RoutingModel::deviatedFromRoute
bool deviatedFromRoute() const
returns whether the gps location is on route
Marble::MarbleGlobal::profiles
Profiles profiles() const
Definition: MarbleGlobal.cpp:48
Marble::RouteSegment::distanceTo
qreal distanceTo(const GeoDataCoordinates &point, GeoDataCoordinates &closest, GeoDataCoordinates &interpolated) const
Definition: RouteSegment.cpp:149
Marble::RoutingModel::LatitudeRole
Definition: RoutingModel.h:45
MarbleWidget.h
This file contains the headers for MarbleWidget.
Marble::Route
Definition: Route.h:20
RouteRequest.h
Marble::RoutingModel::CoordinateRole
Definition: RoutingModel.h:42
GeoDataGeometry.h
Marble::RoutingModel::rightNeighbor
int rightNeighbor(const GeoDataCoordinates &position, RouteRequest const *const route) const
Maps points from the provided route request to waypoints in the model according to their global minim...
Definition: RoutingModel.cpp:333
PositionTracking.h
Marble::RouteSegment
Definition: RouteSegment.h:23
Marble::Maneuver::waypointIndex
int waypointIndex() const
Definition: Maneuver.cpp:97
Marble::RoutingModel::TurnTypeIconRole
Definition: RoutingModel.h:43
Marble::Maneuver::position
GeoDataCoordinates position() const
Definition: Maneuver.cpp:71
Marble::GeoDataPlacemark
a class representing a point of interest on the map
Definition: GeoDataPlacemark.h:54
Marble::RouteSegment::maneuver
const Maneuver & maneuver() const
Definition: RouteSegment.cpp:32
Marble::RoutingModel::LongitudeRole
Definition: RoutingModel.h:44
Marble::RoutingModel::RoutingModel
RoutingModel(RouteRequest *request, MarbleModel *model, QObject *parent=0)
Constructor.
Definition: RoutingModel.cpp:131
Marble::Maneuver::Direction
Direction
Definition: Maneuver.h:26
Marble::GeoDataContainer::placemarkList
QVector< GeoDataPlacemark * > placemarkList() const
A convenience function that returns all placemarks in this container.
Definition: GeoDataContainer.cpp:107
Marble::RouteRequest::at
GeoDataCoordinates at(int index) const
Accessor for the n-th position.
Definition: RouteRequest.cpp:149
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:38:52 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