-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
shortestpathwidget.cpp
355 lines (294 loc) · 11.5 KB
/
shortestpathwidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/***************************************************************************
* Copyright (C) 2009 by Sergey Yakushev *
* yakushevs@list.ru *
* *
* This is a plugin generated from the QGIS plugin template *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
/**
* \file shortestpathwidget.cpp
* \brief implemetation UI for find shotest path
*/
//qt includes
#include <qcombobox.h>
#include <qlayout.h>
#include <qpushbutton.h>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <qlabel.h>
#include <qlineedit.h>
#include <QToolButton>
#include <QMessageBox>
// Qgis includes
#include <qgsmapcanvas.h>
#include <qgsmaptoolemitpoint.h>
#include <qgisinterface.h>
#include <qgsrubberband.h>
#include <qgsmaptopixel.h>
#include <qgsmaprenderer.h>
#include <qgsfeature.h>
#include <qgsapplication.h>
#include <qgsvectorlayer.h>
// roadgraph plugin includes
#include "roadgraphplugin.h"
#include "shortestpathwidget.h"
#include "utils.h"
#include "simplegraphbuilder.h"
#include "graphdirector.h"
#include "exportdlg.h"
#include "units.h"
#include "settings.h"
//standard includes
RgShortestPathWidget::RgShortestPathWidget( QWidget* theParent, RoadGraphPlugin *thePlugin ) : QDockWidget( theParent ), mPlugin( thePlugin )
{
setWindowTitle( tr( "Shortest path" ) );
setObjectName( "ShortestPathDock" );
setAllowedAreas( Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea );
QWidget *myWidget = new QWidget( this );
setWidget( myWidget );
QVBoxLayout *v = new QVBoxLayout( myWidget );
QHBoxLayout *h = NULL;
QLabel *l = NULL;
l = new QLabel( tr( "Start" ), myWidget );
v->addWidget( l );
h = new QHBoxLayout();
mFrontPointLineEdit = new QLineEdit( myWidget );
mFrontPointLineEdit->setReadOnly( true );
QToolButton *selectFrontPoint = new QToolButton( myWidget );
selectFrontPoint->setCheckable( true );
selectFrontPoint->setIcon( QPixmap( ":/roadgraph/coordinate_capture.png" ) );
h->addWidget( mFrontPointLineEdit );
h->addWidget( selectFrontPoint );
v->addLayout( h );
l = new QLabel( tr( "Stop" ), myWidget );
v->addWidget( l );
h = new QHBoxLayout();
mBackPointLineEdit = new QLineEdit( myWidget );
mBackPointLineEdit->setReadOnly( true );
QToolButton *selectBackPoint = new QToolButton( myWidget );
selectBackPoint->setCheckable( true );
selectBackPoint->setIcon( QPixmap( ":/roadgraph/coordinate_capture.png" ) );
h->addWidget( mBackPointLineEdit );
h->addWidget( selectBackPoint );
v->addLayout( h );
h = new QHBoxLayout( this );
l = new QLabel( tr( "Criterion" ), myWidget );
mCriterionName = new QComboBox( myWidget );
mCriterionName->insertItem( 0, tr( "Length" ) );
mCriterionName->insertItem( 1, tr( "Time" ) );
h->addWidget( l );
h->addWidget( mCriterionName );
v->addLayout( h );
h = new QHBoxLayout( myWidget );
l = new QLabel( tr( "Length" ), myWidget );
mPathCostLineEdit = new QLineEdit( myWidget );
mPathCostLineEdit->setReadOnly( true );
h->addWidget( l );
h->addWidget( mPathCostLineEdit );
v->addLayout( h );
h = new QHBoxLayout( myWidget );
l = new QLabel( tr( "Time" ), myWidget );
mPathTimeLineEdit = new QLineEdit( myWidget );
mPathTimeLineEdit->setReadOnly( true );
h->addWidget( l );
h->addWidget( mPathTimeLineEdit );
v->addLayout( h );
h = new QHBoxLayout( myWidget );
mCalculate = new QPushButton( tr( "Calculate" ), myWidget );
h->addWidget( mCalculate );
QPushButton *pbExport = new QPushButton( tr( "Export" ), myWidget );
h->addWidget( pbExport );
connect( pbExport, SIGNAL( clicked( bool ) ), this, SLOT( exportPath() ) );
mClear = new QPushButton( tr( "Clear" ), myWidget );
h->addWidget( mClear );
v->addLayout( h );
v->addStretch();
mFrontPointMapTool = new QgsMapToolEmitPoint( mPlugin->iface()->mapCanvas() );
mFrontPointMapTool->setButton( selectFrontPoint );
mBackPointMapTool = new QgsMapToolEmitPoint( mPlugin->iface()->mapCanvas() );
mBackPointMapTool->setButton( selectBackPoint );
connect( selectFrontPoint, SIGNAL( clicked( bool ) ), this, SLOT( onSelectFrontPoint() ) );
connect( mFrontPointMapTool, SIGNAL( canvasClicked( const QgsPoint&, Qt::MouseButton ) ),
this, SLOT( setFrontPoint( const QgsPoint& ) ) );
connect( selectBackPoint, SIGNAL( clicked( bool ) ), this, SLOT( onSelectBackPoint() ) );
connect( mBackPointMapTool, SIGNAL( canvasClicked( const QgsPoint&, Qt::MouseButton ) ),
this, SLOT( setBackPoint( const QgsPoint& ) ) );
connect( mCalculate, SIGNAL( clicked( bool ) ), this, SLOT( findingPath() ) );
connect( mClear, SIGNAL( clicked( bool ) ), this, SLOT( clear() ) );
mrbFrontPoint = new QgsRubberBand( mPlugin->iface()->mapCanvas(), true );
mrbFrontPoint->setColor( Qt::green );
mrbFrontPoint->setWidth( 2 );
mrbBackPoint = new QgsRubberBand( mPlugin->iface()->mapCanvas(), true );
mrbBackPoint->setColor( Qt::red );
mrbBackPoint->setWidth( 2 );
mrbPath = new QgsRubberBand( mPlugin->iface()->mapCanvas(), false );
mrbPath->setWidth( 2 );
connect( mPlugin->iface()->mapCanvas(), SIGNAL( extentsChanged() ), this, SLOT( mapCanvasExtentsChanged() ) );
} //RgShortestPathWidget::RgShortestPathWidget()
RgShortestPathWidget::~RgShortestPathWidget()
{
delete mFrontPointMapTool;
delete mBackPointMapTool;
delete mrbFrontPoint;
delete mrbBackPoint;
delete mrbPath;
} //RgShortestPathWidget::~RgShortestPathWidget()
void RgShortestPathWidget::mapCanvasExtentsChanged()
{
// update rubberbands
if ( mFrontPointLineEdit->text().length() > 0 )
setFrontPoint( mFrontPoint );
if ( mBackPointLineEdit->text().length() > 0 )
setBackPoint( mBackPoint );
}
void RgShortestPathWidget::onSelectFrontPoint()
{
mPlugin->iface()->mapCanvas()->setMapTool( mFrontPointMapTool );
}
void RgShortestPathWidget::setFrontPoint( const QgsPoint& pt )
{
mPlugin->iface()->mapCanvas()->unsetMapTool( mFrontPointMapTool );
mFrontPointLineEdit->setText( QString( "(" ) + QString().setNum( pt.x() ) + QString( "," ) +
QString().setNum( pt.y() ) + QString( ")" ) );
mFrontPoint = pt;
double mupp = mPlugin->iface()->mapCanvas()->getCoordinateTransform()->mapUnitsPerPixel() * 2;
mrbFrontPoint->reset( true );
mrbFrontPoint->addPoint( QgsPoint( pt.x() - mupp, pt.y() - mupp ), false );
mrbFrontPoint->addPoint( QgsPoint( pt.x() + mupp, pt.y() - mupp ), false );
mrbFrontPoint->addPoint( QgsPoint( pt.x() + mupp, pt.y() + mupp ), false );
mrbFrontPoint->addPoint( QgsPoint( pt.x() - mupp, pt.y() + mupp ), true );
mrbFrontPoint->show();
} //RgShortestPathWidget::setFrontPoint( const QgsPoint& pt )
void RgShortestPathWidget::onSelectBackPoint()
{
mPlugin->iface()->mapCanvas()->setMapTool( mBackPointMapTool );
}
void RgShortestPathWidget::setBackPoint( const QgsPoint& pt )
{
mPlugin->iface()->mapCanvas()->unsetMapTool( mBackPointMapTool );
mBackPoint = pt;
mBackPointLineEdit->setText( QString( "(" ) + QString().setNum( pt.x() ) + QString( "," ) +
QString().setNum( pt.y() ) + QString( ")" ) );
double mupp = mPlugin->iface()->mapCanvas()->getCoordinateTransform()->mapUnitsPerPixel() * 2;
mrbBackPoint->reset( true );
mrbBackPoint->addPoint( QgsPoint( pt.x() - mupp, pt.y() - mupp ), false );
mrbBackPoint->addPoint( QgsPoint( pt.x() + mupp, pt.y() - mupp ), false );
mrbBackPoint->addPoint( QgsPoint( pt.x() + mupp, pt.y() + mupp ), false );
mrbBackPoint->addPoint( QgsPoint( pt.x() - mupp, pt.y() + mupp ), true );
mrbBackPoint->show();
}
bool RgShortestPathWidget::getPath( AdjacencyMatrix& matrix, QgsPoint& p1, QgsPoint& p2 )
{
if ( mFrontPointLineEdit->text().isNull() || mBackPointLineEdit->text().isNull() )
return false;
RgSimpleGraphBuilder builder( mPlugin->iface()->mapCanvas()->mapRenderer()->destinationSrs() );
{
const RgGraphDirector *director = mPlugin->director();
if ( director == NULL )
{
QMessageBox::critical( this, tr( "Plugin isn't configured" ), tr( "Plugin isn't configured!" ) );
return false;
}
QVector< QgsPoint > points;
QVector< QgsPoint > tiedPoint;
points.push_back( mFrontPoint );
points.push_back( mBackPoint );
director->makeGraph( &builder, points, tiedPoint );
p1 = tiedPoint[ 0 ];
p2 = tiedPoint[ 1 ];
// not need
delete director;
}
if ( p1 == QgsPoint( 0.0, 0.0 ) )
{
QMessageBox::critical( this, tr( "Tie point failed" ), tr( "Start point doesn't tie to the road!" ) );
return false;
}
if ( p1 == QgsPoint( 0.0, 0.0 ) )
{
QMessageBox::critical( this, tr( "Tie point failed" ), tr( "Stop point doesn't tie to the road!" ) );
return false;
}
AdjacencyMatrix m = builder.adjacencyMatrix();
DijkstraFinder::OptimizationCriterion criterion = DijkstraFinder::byCost;
if ( mCriterionName->currentIndex() == 1 )
criterion = DijkstraFinder::byTime;
DijkstraFinder f( m, criterion );
matrix = f.find( p1, p2 );
return true;
}
void RgShortestPathWidget::findingPath()
{
QgsPoint p1, p2;
AdjacencyMatrix path;
if ( !getPath( path, p1, p2 ) )
return;
mrbPath->reset( false );
double time = 0.0;
double cost = 0.0;
AdjacencyMatrix::iterator it = path.find( p1 );
if ( it == path.end() )
return;
mrbPath->addPoint( it->first );
while ( it != path.end() )
{
AdjacencyMatrixString::iterator it2 = it->second.begin();
if ( it2 == it->second.end() )
break;
mrbPath->addPoint( it2->first );
time += it2->second.mTime;
cost += it2->second.mCost;
it = path.find( it2->first );
}
Unit timeUnit = Unit::byName( mPlugin->timeUnitName() );
Unit distanceUnit = Unit::byName( mPlugin->distanceUnitName() );
mPathCostLineEdit->setText( QString().setNum( cost / distanceUnit.multipler() ) + distanceUnit.name() );
mPathTimeLineEdit->setText( QString().setNum( time / timeUnit.multipler() ) + timeUnit.name() );
mrbPath->setColor( Qt::red );
}
void RgShortestPathWidget::clear()
{
mFrontPointLineEdit->setText( QString() );
mrbFrontPoint->reset( true );
mBackPointLineEdit->setText( QString() );
mrbBackPoint->reset( true );
mrbPath->reset();
}
void RgShortestPathWidget::exportPath()
{
RgExportDlg dlg( this );
if ( !dlg.exec() )
return;
QgsPoint p1, p2;
AdjacencyMatrix path;
if ( !getPath( path, p1, p2 ) )
return;
QgsVectorLayer *vl = dlg.mapLayer();
if ( vl == NULL )
return;
QgsCoordinateTransform ct( mPlugin->iface()->mapCanvas()->mapRenderer()->destinationSrs(),
vl->crs() );
QVector< QgsPoint > points;
AdjacencyMatrix::iterator it = path.find( p1 );
if ( it == path.end() )
return;
points.append( ct.transform( it->first ) );
while ( it != path.end() )
{
AdjacencyMatrixString::iterator it2 = it->second.begin();
if ( it2 == it->second.end() )
break;
points.append( ct.transform( it2->first ) );
it = path.find( it2->first );
}
vl->startEditing();
QgsFeature f;
f.setGeometry( QgsGeometry::fromPolyline( points ) );
vl->addFeature( f );
vl->updateExtents();
mPlugin->iface()->mapCanvas()->update();
}