Skip to content

Commit b3b85d7

Browse files
author
stopa85
committedJan 24, 2011
Road-Graph plugin added
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@15068 c8812cc2-4d05-0410-92ff-de0c093fc19c

31 files changed

+3186
-0
lines changed
 

‎src/plugins/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ SUBDIRS (copyright_label
1414
point_displacement_renderer
1515
spatialquery
1616
sqlanywhere
17+
roadgraph
1718
)
1819

1920
IF (WITH_SPATIALITE)

‎src/plugins/roadgraph/CMakeLists.txt

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
########################################################
3+
# Files
4+
5+
SET (VRP_SRCS
6+
roadgraphplugin.cpp
7+
settingsdlg.cpp
8+
units.cpp
9+
utils.cpp
10+
shortestpathwidget.cpp
11+
linevectorlayersettings.cpp
12+
linevectorlayerwidget.cpp
13+
linevectorlayerdirector.cpp
14+
simplegraphbuilder.cpp
15+
exportdlg.cpp
16+
)
17+
18+
#SET ([pluginlcasename]_UIS [pluginlcasename]guibase.ui)
19+
20+
SET (VRP_MOC_HDRS
21+
roadgraphplugin.h
22+
settingsdlg.h
23+
shortestpathwidget.h
24+
linevectorlayerwidget.h
25+
exportdlg.h
26+
)
27+
SET (VRP_RCCS roadgraph.qrc)
28+
29+
INCLUDE_DIRECTORIES(
30+
${CMAKE_CURRENT_BINARY_DIR}
31+
../../core
32+
../../gui
33+
..
34+
)
35+
########################################################
36+
# Build
37+
38+
QT4_WRAP_CPP (VRP_MOC_SRCS ${VRP_MOC_HDRS})
39+
40+
QT4_ADD_RESOURCES(VRP_RCC_SRCS ${VRP_RCCS})
41+
42+
ADD_LIBRARY (roadgraphplugin MODULE ${VRP_SRCS} ${VRP_MOC_SRCS} ${VRP_RCC_SRCS})
43+
44+
45+
TARGET_LINK_LIBRARIES(roadgraphplugin
46+
qgis_core
47+
qgis_gui
48+
)
49+
50+
51+
########################################################
52+
# Install
53+
54+
INSTALL(TARGETS roadgraphplugin
55+
RUNTIME DESTINATION ${QGIS_PLUGIN_DIR}
56+
LIBRARY DESTINATION ${QGIS_PLUGIN_DIR})
57+

‎src/plugins/roadgraph/about.png

2.18 KB
Loading
1.64 KB
Loading

‎src/plugins/roadgraph/exportdlg.cpp

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/***************************************************************************
2+
* Copyright (C) 2010 by Sergey Yakushev *
3+
* yakushevs@list.ru *
4+
* *
5+
* This program is free software; you can redistribute it and/or modify *
6+
* it under the terms of the GNU General Public License as published by *
7+
* the Free Software Foundation; either version 2 of the License, or *
8+
* (at your option) any later version. *
9+
***************************************************************************/
10+
#include "exportdlg.h"
11+
#include <qgscontexthelp.h>
12+
13+
//qt includes
14+
#include <qlabel.h>
15+
#include <qcombobox.h>
16+
#include <QHBoxLayout>
17+
#include <QVBoxLayout>
18+
#include <qdialogbuttonbox.h>
19+
#include <qmessagebox.h>
20+
21+
22+
// Qgis includes
23+
#include <qgsvectorlayer.h>
24+
#include <qgsmaplayerregistry.h>
25+
#include <qgsproviderregistry.h>
26+
#include <qgsvectordataprovider.h>
27+
28+
//standard includes
29+
30+
RgExportDlg::RgExportDlg( QWidget* parent, Qt::WFlags fl )
31+
: QDialog( parent, fl )
32+
{
33+
// create base widgets;
34+
setWindowTitle( tr("Export feature") );
35+
QVBoxLayout *v = new QVBoxLayout( this );
36+
37+
QHBoxLayout *h = new QHBoxLayout();
38+
QLabel *l = new QLabel( tr("Select destination layer:"), this);
39+
h->addWidget(l);
40+
mcbLayers = new QComboBox( this );
41+
h->addWidget(mcbLayers);
42+
v->addLayout(h);
43+
44+
QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this);
45+
connect(bb, SIGNAL(accepted()), this, SLOT(on_buttonBox_accepted()) );
46+
connect(bb, SIGNAL(rejected()), this, SLOT(on_buttonBox_rejected()) );
47+
v->addWidget(bb);
48+
49+
//fill list of layers
50+
mcbLayers->insertItem( 0, tr("new temporary layer"), QVariant("-1") );
51+
52+
QMap<QString, QgsMapLayer*> mapLayers = QgsMapLayerRegistry::instance()->mapLayers();
53+
QMap<QString, QgsMapLayer*>::iterator layer_it = mapLayers.begin();
54+
55+
for ( ; layer_it != mapLayers.end(); ++layer_it )
56+
{
57+
QgsVectorLayer* vl = dynamic_cast<QgsVectorLayer*>( layer_it.value() );
58+
if ( !vl )
59+
continue;
60+
if ( vl->geometryType() != QGis::Line )
61+
continue;
62+
mcbLayers->insertItem( 0, vl->name(), QVariant( vl->getLayerID() ) );
63+
}
64+
65+
} // RgSettingsDlg::RgSettingsDlg()
66+
67+
RgExportDlg::~RgExportDlg()
68+
{
69+
}
70+
71+
QgsVectorLayer* RgExportDlg::mapLayer() const
72+
{
73+
QgsVectorLayer* myLayer = NULL;
74+
QString layerId = mcbLayers->itemData( mcbLayers->currentIndex() ).toString();
75+
76+
if ( layerId == QString("-1") )
77+
{
78+
// create a temporary layer
79+
myLayer = new QgsVectorLayer( "LineString", "shortest path", "memory" );
80+
81+
QgsVectorDataProvider *prov = myLayer->dataProvider();
82+
if ( prov == NULL)
83+
return NULL;
84+
85+
QList<QgsField> attrList;
86+
attrList.append( QgsField("one", QVariant::Int) );
87+
prov->addAttributes( attrList );
88+
QgsMapLayerRegistry::instance()->addMapLayer( myLayer );
89+
90+
}else
91+
{
92+
// retrun selected layer
93+
myLayer = dynamic_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( layerId ) );
94+
}
95+
96+
return myLayer;
97+
} // QgsVectorLayer* RgExportDlg::vectorLayer() const
98+
99+
void RgExportDlg::on_buttonBox_accepted()
100+
{
101+
accept();
102+
} // void RgExportDlg::on_buttonBox_accepted()
103+
104+
void RgExportDlg::on_buttonBox_rejected()
105+
{
106+
reject();
107+
}
108+
109+
void RgExportDlg::on_buttonBox_helpRequested()
110+
{
111+
QgsContextHelp::run( context_id );
112+
}

‎src/plugins/roadgraph/exportdlg.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/***************************************************************************
2+
exportdlg.h
3+
--------------------------------------
4+
Date : 2010-11-29
5+
Copyright : (C) 2010 by Yakushev Sergey
6+
Email : YakushevS <at> list.ru
7+
****************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
#ifndef ROADGRAPH_EXPORTDLG_H
16+
#define ROADGRAPH_EXPORTDLG_H
17+
18+
#include <QDialog>
19+
20+
// forward declaration QT-classes
21+
class QComboBox;
22+
23+
// forward declaration Qgis-classes
24+
25+
//forward declaration RoadGraph plugins classes
26+
class QgsVectorLayer;
27+
28+
/**
29+
@author Sergey Yakushev
30+
*/
31+
/**
32+
* \class RgSettingsDlg
33+
* \brief implement of export dialog
34+
*/
35+
class RgExportDlg : public QDialog
36+
{
37+
Q_OBJECT
38+
public:
39+
RgExportDlg( QWidget* parent = 0, Qt::WFlags fl = 0 );
40+
~RgExportDlg();
41+
public:
42+
QgsVectorLayer* mapLayer() const;
43+
private:
44+
static const int context_id = 0;
45+
46+
private slots:
47+
void on_buttonBox_accepted();
48+
void on_buttonBox_rejected();
49+
void on_buttonBox_helpRequested();
50+
51+
private:
52+
QComboBox *mcbLayers;
53+
};
54+
#endif

‎src/plugins/roadgraph/graphbuilder.h

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/***************************************************************************
2+
graphbuilder.h
3+
--------------------------------------
4+
Date : 2010-10-22
5+
Copyright : (C) 2010 by Yakushev Sergey
6+
Email : YakushevS <at> list.ru
7+
****************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
#ifndef ROADGRAPH_GRAPHBUILDER
16+
#define ROADGRAPH_GRAPHBUILDER
17+
18+
#include "utils.h"
19+
20+
//QT4 includes
21+
22+
//QGIS includes
23+
#include <qgspoint.h>
24+
#include <qgscoordinatereferencesystem.h>
25+
26+
//forward declarations
27+
28+
/**
29+
* \class RgGraphDirector
30+
* \brief Determine making the graph
31+
* contained the settings
32+
*/
33+
class RgGraphBuilder
34+
{
35+
public:
36+
//! Destructor
37+
virtual ~RgGraphBuilder()
38+
{};
39+
/**
40+
* set source CRS
41+
*/
42+
virtual void setSourceCrs( const QgsCoordinateReferenceSystem& crs ) = 0;
43+
44+
/**
45+
* set destionation CRS
46+
*/
47+
virtual void setDestinationCrs( const QgsCoordinateReferenceSystem& crs ) = 0;
48+
49+
/**
50+
* add vertex
51+
*/
52+
virtual void addVertex( const QgsPoint& pt ) = 0;
53+
54+
/**
55+
* add arc
56+
*/
57+
virtual void addArc( const QgsPoint& pt1, const QgsPoint& pt2, double speed ) = 0;
58+
59+
/**
60+
* tie point
61+
* @param pt maps point
62+
* @param pt ok = false if tiePoint failed.
63+
* @return Graph vertex corresponding pt.
64+
* @note: graph can be modified
65+
*/
66+
virtual QgsPoint tiePoint( const QgsPoint &pt, bool &ok ) = 0;
67+
68+
};
69+
#endif //GRAPHBUILDER

‎src/plugins/roadgraph/graphdirector.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/***************************************************************************
2+
graphdirector.h
3+
--------------------------------------
4+
Date : 2010-10-18
5+
Copyright : (C) 2010 by Yakushev Sergey
6+
Email : YakushevS <at> list.ru
7+
****************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
#ifndef ROADGRAPH_GRAPHDIRECTOR
16+
#define ROADGRAPH_GRAPHDIRECTOR
17+
18+
//QT4 includes
19+
20+
//QGIS includes
21+
#include <qgsrectangle.h>
22+
23+
//forward declarations
24+
class RgSettings;
25+
class RgGraphBuilder;
26+
27+
/**
28+
* \class RgGraphDirector
29+
* \brief Determine making the graph
30+
* contained the settings
31+
*/
32+
class RgGraphDirector
33+
{
34+
public:
35+
//! Destructor
36+
virtual ~RgGraphDirector() { };
37+
38+
/**
39+
* get adjacency matrix
40+
*/
41+
virtual void makeGraph( RgGraphBuilder * ) const = 0;
42+
43+
/**
44+
* return pointer to my Settings
45+
*/
46+
virtual RgSettings* settings() = 0;
47+
48+
/**
49+
* return Director name
50+
*/
51+
virtual QString name() const = 0;
52+
};
53+
#endif //GRAPHDIRECTOR
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/***************************************************************************
2+
* Copyright (C) 2010 by Sergey Yakushev *
3+
* yakushevs <at> list.ru *
4+
* *
5+
* *
6+
* This program is free software; you can redistribute it and/or modify *
7+
* it under the terms of the GNU General Public License as published by *
8+
* the Free Software Foundation; either version 2 of the License, or *
9+
* (at your option) any later version. *
10+
***************************************************************************/
11+
12+
/**
13+
* \file linevectorlayersettins.cpp
14+
* \brief implementation of RgLineVectorLayerDirector
15+
*/
16+
17+
#include "linevectorlayersettings.h"
18+
#include "linevectorlayerdirector.h"
19+
#include "graphbuilder.h"
20+
#include "units.h"
21+
22+
// Qgis includes
23+
#include <qgsvectorlayer.h>
24+
#include <qgsmaplayerregistry.h>
25+
#include <qgsvectordataprovider.h>
26+
#include <qgspoint.h>
27+
#include <qgsgeometry.h>
28+
29+
// QT includes
30+
#include <QString>
31+
32+
//standard includes
33+
#include <iostream>
34+
35+
RgLineVectorLayerDirector::RgLineVectorLayerDirector()
36+
{
37+
}
38+
RgLineVectorLayerDirector::~RgLineVectorLayerDirector()
39+
{
40+
}
41+
RgSettings* RgLineVectorLayerDirector::settings()
42+
{
43+
return &mSettings;
44+
}
45+
46+
QString RgLineVectorLayerDirector::name() const
47+
{
48+
return QString( "Vector line" );
49+
}
50+
51+
void RgLineVectorLayerDirector::makeGraph( RgGraphBuilder *builder ) const
52+
{
53+
QgsVectorLayer *vl = NULL;
54+
QMap< QString, QgsMapLayer*> m = QgsMapLayerRegistry::instance()->mapLayers();
55+
QMap< QString, QgsMapLayer*>::const_iterator it;
56+
for ( it = m.constBegin(); it != m.constEnd(); ++it )
57+
{
58+
if ( it.value()->name() == mSettings.mLayer )
59+
{
60+
vl = dynamic_cast<QgsVectorLayer*>( it.value() );
61+
break;
62+
}
63+
}
64+
if ( vl == NULL )
65+
return;
66+
67+
QgsVectorDataProvider *provider = dynamic_cast<QgsVectorDataProvider*>( vl->dataProvider() );
68+
if ( provider == NULL )
69+
return;
70+
71+
int directionFieldId = provider->fieldNameIndex( mSettings.mDirection );
72+
int speedFieldId = provider->fieldNameIndex( mSettings.mSpeed );
73+
74+
builder->setSourceCrs( vl->crs() );
75+
QgsAttributeList la;
76+
if ( directionFieldId > -1 )
77+
la.push_back( directionFieldId );
78+
if ( speedFieldId > -1 )
79+
la.push_back( speedFieldId );
80+
81+
SpeedUnit su = SpeedUnit::byName( mSettings.mSpeedUnitName );
82+
83+
vl->select( la );
84+
QgsFeature feature;
85+
while ( vl->nextFeature(feature) )
86+
{
87+
QgsAttributeMap attr = feature.attributeMap();
88+
RgLineVectorLayerSettings::DirectionType directionType = mSettings.mDefaultDirection;
89+
QgsAttributeMap::const_iterator it;
90+
// What direction have feature?
91+
for ( it = attr.constBegin(); it != attr.constEnd(); ++it )
92+
{
93+
if ( it.key() != directionFieldId )
94+
{
95+
continue;
96+
}
97+
QString str = it.value().toString();
98+
if ( str == mSettings.mBothDirectionVal )
99+
{
100+
directionType = RgLineVectorLayerSettings::Both;
101+
}
102+
else if ( str == mSettings.mFirstPointToLastPointDirectionVal )
103+
{
104+
directionType = RgLineVectorLayerSettings::FirstPointToLastPoint;
105+
}
106+
else if ( str == mSettings.mLastPointToFirstPointDirectionVal )
107+
{
108+
directionType = RgLineVectorLayerSettings::LastPointToFirstPoint;
109+
}
110+
}
111+
// What speed have feature?
112+
double speed = 0.0;
113+
for (it = attr.constBegin(); it != attr.constEnd(); ++it)
114+
{
115+
if ( it.key() != speedFieldId )
116+
{
117+
continue;
118+
}
119+
speed = it.value().toDouble();
120+
}
121+
if (speed <= 0.0)
122+
{
123+
speed = mSettings.mDefaultSpeed;
124+
}
125+
126+
// begin features segments and add arc to the Graph;
127+
QgsPoint pt1, pt2;
128+
129+
bool isFirstPoint=true;
130+
QgsPolyline pl = feature.geometry()->asPolyline();
131+
QgsPolyline::iterator pointIt;
132+
for ( pointIt = pl.begin(); pointIt != pl.end(); ++pointIt )
133+
{
134+
pt2 = *pointIt;
135+
if ( !isFirstPoint )
136+
{
137+
if ( directionType == RgLineVectorLayerSettings::FirstPointToLastPoint ||
138+
directionType == RgLineVectorLayerSettings::Both )
139+
{
140+
builder->addArc(pt1, pt2, speed*su.multipler() );
141+
}
142+
if (directionType == RgLineVectorLayerSettings::LastPointToFirstPoint ||
143+
directionType == RgLineVectorLayerSettings::Both )
144+
{
145+
builder->addArc( pt2, pt1, speed*su.multipler() );
146+
}
147+
}
148+
pt1 = pt2;
149+
isFirstPoint=false;
150+
} // for (it = pl.begin(); it != pl.end(); ++it)
151+
152+
} // while( vl->nextFeature(feature) )
153+
} // makeGraph( RgGraphBuilder *builder, const QgsRectangle& rt )
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/***************************************************************************
2+
linevectorlayerdirector.h
3+
--------------------------------------
4+
Date : 2010-10-20
5+
Copyright : (C) 2010 by Yakushev Sergey
6+
Email : YakushevS <at> list.ru
7+
****************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
#ifndef ROADGRAPH_LINEVECTORLAYERDIRECTOR
16+
#define ROADGRAPH_LINEVECTORLAYERDIRECTOR
17+
18+
//QT4 includes
19+
20+
//QGIS includes
21+
22+
// Road-graph plugin includes
23+
#include "graphdirector.h"
24+
#include "linevectorlayersettings.h"
25+
26+
//forward declarations
27+
class RgGraphBuilder;
28+
29+
/**
30+
* \class RgLineVectorLayerDirector
31+
* \brief Determine making the graph from vector line layer
32+
*/
33+
class RgLineVectorLayerDirector : public RgGraphDirector
34+
{
35+
public:
36+
RgLineVectorLayerDirector();
37+
//! Destructor
38+
virtual ~RgLineVectorLayerDirector();
39+
/**
40+
* MANDATORY DIRECTOR PROPERTY DECLARATION
41+
*/
42+
void makeGraph( RgGraphBuilder * ) const;
43+
44+
RgSettings* settings();
45+
46+
QString name() const;
47+
private:
48+
/**
49+
* settings of this director
50+
*/
51+
RgLineVectorLayerSettings mSettings;
52+
};
53+
#endif //GRAPHDIRECTOR
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/***************************************************************************
2+
* Copyright (C) 2009 by Sergey Yakushev *
3+
* yakushevs <at> list.ru *
4+
* *
5+
* *
6+
* This program is free software; you can redistribute it and/or modify *
7+
* it under the terms of the GNU General Public License as published by *
8+
* the Free Software Foundation; either version 2 of the License, or *
9+
* (at your option) any later version. *
10+
***************************************************************************/
11+
12+
/**
13+
* \file linevectorlayersettins.cpp
14+
* \brief implementation of RgLineVectorLayerSettings
15+
*/
16+
17+
#include "linevectorlayersettings.h"
18+
#include "linevectorlayerwidget.h"
19+
20+
// Qgis includes
21+
#include <qgsproject.h>
22+
23+
// QT includes
24+
#include <QLineEdit>
25+
#include <QComboBox>
26+
#include <QSpinBox>
27+
28+
//standard includes
29+
30+
RgLineVectorLayerSettings::RgLineVectorLayerSettings()
31+
{
32+
mLayer = "";
33+
mDirection = "";
34+
mDefaultDirection = Both;
35+
mSpeed = "";
36+
mDefaultSpeed = 40;
37+
}
38+
RgLineVectorLayerSettings::~RgLineVectorLayerSettings()
39+
{
40+
41+
}
42+
bool RgLineVectorLayerSettings::test()
43+
{
44+
// implement me
45+
46+
// check default speed
47+
if (mDefaultSpeed <= 0.0)
48+
{
49+
return false;
50+
}
51+
if (mLayer == "")
52+
{
53+
return false;
54+
}
55+
// implement me
56+
57+
return true;
58+
} // RgLineVectorLayerSettings::test()
59+
60+
void RgLineVectorLayerSettings::read( const QgsProject *project )
61+
{
62+
int dd = project->readNumEntry( "roadgraphplugin", "/defaultDirection" );
63+
mDirection = project->readEntry( "roadgraphplugin", "/directionField" );
64+
mFirstPointToLastPointDirectionVal =
65+
project->readEntry( "roadgraphplugin", "/FirstPointToLastPointDirectionVal" );
66+
mLastPointToFirstPointDirectionVal =
67+
project->readEntry( "roadgraphplugin", "/LastPointToFirstPointDirectionVal" );
68+
mBothDirectionVal = project->readEntry( "roadgraphplugin", "/BothDirectionVal" );
69+
mSpeed = project->readEntry( "roadgraphplugin", "/speedField" );
70+
mDefaultSpeed = project->readDoubleEntry( "roadgraphplugin", "/defaultSpeed" );
71+
mLayer = project->readEntry( "roadgraphplugin", "/layer" );
72+
mSpeedUnitName= project->readEntry( "roadgraphplugin","/speedUnitName" );
73+
74+
if (dd == 1)
75+
{
76+
mDefaultDirection = FirstPointToLastPoint;
77+
}
78+
else if (dd == 2)
79+
{
80+
mDefaultDirection = LastPointToFirstPoint;
81+
}
82+
else if (dd == 3)
83+
{
84+
mDefaultDirection = Both;
85+
}
86+
87+
} // RgLineVectorLayerSettings::read( const QgsProject *project )
88+
89+
void RgLineVectorLayerSettings::write( QgsProject *project )
90+
{
91+
project->writeEntry( "roadgraphplugin", "/defaultDirection", mDefaultDirection );
92+
project->writeEntry( "roadgraphplugin", "/directionField", mDirection );
93+
project->writeEntry( "roadgraphplugin", "/FirstPointToLastPointDirectionVal",
94+
mFirstPointToLastPointDirectionVal );
95+
project->writeEntry( "roadgraphplugin", "/LastPointToFirstPointDirectionVal",
96+
mLastPointToFirstPointDirectionVal );
97+
project->writeEntry( "roadgraphplugin", "/BothDirectionVal", mBothDirectionVal );
98+
project->writeEntry( "roadgraphplugin", "/speedField", mSpeed );
99+
project->writeEntry( "roadgraphplugin", "/defaultSpeed", mDefaultSpeed );
100+
project->writeEntry( "roadgraphplugin", "/layer", mLayer );
101+
project->writeEntry( "roadgraphplugin", "/speedUnitName", mSpeedUnitName );
102+
} // RgLineVectorLayerSettings::write( QgsProject *project )
103+
104+
QWidget* RgLineVectorLayerSettings::getGui( QWidget *parent )
105+
{
106+
return new RgLineVectorLayerSettingsWidget( this, parent );
107+
}
108+
109+
void RgLineVectorLayerSettings::setFromGui( QWidget *myGui )
110+
{
111+
RgLineVectorLayerSettingsWidget* w = dynamic_cast<RgLineVectorLayerSettingsWidget*>( myGui );
112+
if (w == NULL)
113+
return;
114+
115+
mFirstPointToLastPointDirectionVal = w->mleFirstPointToLastPointDirection->text();
116+
mLastPointToFirstPointDirectionVal = w->mleLastPointToFirstPointDirection->text();
117+
mBothDirectionVal = w->mleBothDirection->text();
118+
mDirection = w->mcbDirection->currentText();
119+
mLayer = w->mcbLayers->currentText();
120+
121+
if ( w->mcbDirectionDefault->currentIndex() == 0 )
122+
{
123+
mDefaultDirection = Both;
124+
}
125+
else if ( w->mcbDirectionDefault->currentIndex() == 1 )
126+
{
127+
mDefaultDirection = FirstPointToLastPoint;
128+
}
129+
else if ( w->mcbDirectionDefault->currentIndex() == 2 )
130+
{
131+
mDefaultDirection = LastPointToFirstPoint;
132+
}
133+
134+
mSpeed = w->mcbSpeed->currentText();
135+
mDefaultSpeed = w->msbSpeedDefault->value();
136+
137+
if ( w->mcbUnitOfSpeed->currentIndex() == 0 )
138+
{
139+
mSpeedUnitName = "m/s";
140+
}
141+
else if ( w->mcbUnitOfSpeed->currentIndex() == 1 )
142+
{
143+
mSpeedUnitName = "km/h";
144+
}
145+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/***************************************************************************
2+
* Copyright (C) 2010 by Sergey Yakushev *
3+
* yakushevs <at> list.ru *
4+
* *
5+
* This is file define Road graph plugins settings *
6+
* *
7+
* This program is free software; you can redistribute it and/or modify *
8+
* it under the terms of the GNU General Public License as published by *
9+
* the Free Software Foundation; either version 2 of the License, or *
10+
* (at your option) any later version. *
11+
***************************************************************************/
12+
#ifndef ROADGRAPH_LINEVECTOR_SETTINGS
13+
#define ROADGRAPH_LINEVECTOR_SETTINGS
14+
15+
#include "settings.h"
16+
17+
// QT includes
18+
#include <qstring.h>
19+
20+
// Qgis includes
21+
22+
// standart includes
23+
24+
// forward declaration Qgis-classes
25+
class QWidget;
26+
27+
/**
28+
@author Sergey Yakushev
29+
*/
30+
/**
31+
* \class RgSettings
32+
* \brief This class contained settings for RgLineVectorLayerDirector
33+
*/
34+
35+
class RgLineVectorLayerSettings: public RgSettings
36+
{
37+
public:
38+
/**
39+
* \enum DirectionType
40+
* \brief DirectionType enumeration discribe
41+
*/
42+
enum DirectionType { FirstPointToLastPoint=1, LastPointToFirstPoint=2, Both=3 };
43+
44+
public:
45+
/**
46+
* default constructor.
47+
*/
48+
RgLineVectorLayerSettings();
49+
50+
/**
51+
* destructor
52+
*/
53+
~RgLineVectorLayerSettings();
54+
public:
55+
/*
56+
* MANDATORY SETTINGS PROPERTY DECLARATIONS
57+
*/
58+
void write( QgsProject * );
59+
void read( const QgsProject * );
60+
bool test();
61+
QWidget *getGui( QWidget* Parent );
62+
void setFromGui( QWidget* );
63+
public:
64+
65+
/**
66+
* contained Layer name
67+
*/
68+
QString mLayer;
69+
70+
/**
71+
* contained direction field name
72+
*/
73+
QString mDirection;
74+
75+
/**
76+
* mDirection field value as first point to last point value
77+
*/
78+
QString mFirstPointToLastPointDirectionVal;
79+
80+
/**
81+
* mDirection field value as last point to first point value
82+
*/
83+
QString mLastPointToFirstPointDirectionVal;
84+
85+
/**
86+
* mDirection field value as both direction
87+
*/
88+
QString mBothDirectionVal;
89+
90+
/**
91+
* contained Default direction
92+
*/
93+
DirectionType mDefaultDirection;
94+
95+
/**
96+
* contained speed filed name
97+
*/
98+
QString mSpeed;
99+
100+
/**
101+
* сontained default speed value;
102+
*/
103+
double mDefaultSpeed;
104+
105+
/*
106+
* name of speed unit
107+
*/
108+
QString mSpeedUnitName;
109+
};
110+
#endif
Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
/***************************************************************************
2+
* Copyright (C) 2010 by Sergey Yakushev *
3+
* yakushevs@list.ru *
4+
* *
5+
* This is a plugin generated from the QGIS plugin template *
6+
* *
7+
* This program is free software; you can redistribute it and/or modify *
8+
* it under the terms of the GNU General Public License as published by *
9+
* the Free Software Foundation; either version 2 of the License, or *
10+
* (at your option) any later version. *
11+
***************************************************************************/
12+
#include "linevectorlayerwidget.h"
13+
#include "linevectorlayersettings.h"
14+
15+
//qt includes
16+
#include <qgscontexthelp.h>
17+
#include <qlabel.h>
18+
#include <qcombobox.h>
19+
#include <QHBoxLayout>
20+
#include <QVBoxLayout>
21+
#include <qdialogbuttonbox.h>
22+
#include <qtabwidget.h>
23+
#include <qspinbox.h>
24+
#include <qmessagebox.h>
25+
#include <qlineedit.h>
26+
27+
// Qgis includes
28+
#include "qgsfield.h"
29+
#include "qgsmaplayerregistry.h"
30+
#include "qgsvectordataprovider.h"
31+
#include "qgsvectorlayer.h"
32+
33+
//standard includes
34+
35+
36+
RgLineVectorLayerSettingsWidget::RgLineVectorLayerSettingsWidget( RgLineVectorLayerSettings *s, QWidget* parent )
37+
: QWidget( parent )
38+
{
39+
// create base widgets;
40+
QTabWidget *tab = new QTabWidget(this);
41+
QVBoxLayout *v= new QVBoxLayout(this);
42+
v->addWidget(tab);
43+
44+
// transportation layer
45+
QFrame *frame = new QFrame(this);
46+
tab->addTab( frame, tr("Transportation layer") );
47+
v = new QVBoxLayout(frame);
48+
QLabel *l = new QLabel(tr("Layer"), frame);
49+
mcbLayers = new QComboBox(frame);
50+
QHBoxLayout *h = new QHBoxLayout(this);
51+
52+
h->addWidget(l);
53+
h->addWidget(mcbLayers);
54+
v->addLayout(h);
55+
56+
h = new QHBoxLayout();
57+
l = new QLabel( tr("Direction field"), frame );
58+
mcbDirection = new QComboBox(frame);
59+
h->addWidget(l);
60+
h->addWidget(mcbDirection);
61+
v->addLayout(h);
62+
63+
h = new QHBoxLayout();
64+
h->addWidget( new QLabel( tr("Direct direction"), frame) );
65+
mleFirstPointToLastPointDirection = new QLineEdit( s->mFirstPointToLastPointDirectionVal, frame );
66+
h->addWidget( mleFirstPointToLastPointDirection );
67+
v->addLayout(h);
68+
69+
h = new QHBoxLayout();
70+
h->addWidget( new QLabel( tr("Reverse direction"), frame) );
71+
mleLastPointToFirstPointDirection = new QLineEdit( s->mLastPointToFirstPointDirectionVal, frame );
72+
h->addWidget( mleLastPointToFirstPointDirection );
73+
v->addLayout(h);
74+
75+
h = new QHBoxLayout();
76+
h->addWidget( new QLabel( tr("Both direction"), frame) );
77+
mleBothDirection = new QLineEdit( s->mBothDirectionVal, frame );
78+
h->addWidget( mleBothDirection );
79+
v->addLayout(h);
80+
81+
h = new QHBoxLayout();
82+
l = new QLabel( tr("Speed field"), frame );
83+
mcbSpeed = new QComboBox(frame);
84+
h->addWidget(l);
85+
h->addWidget(mcbSpeed);
86+
v->addLayout(h);
87+
88+
frame = new QFrame(tab);
89+
tab->addTab( frame, tr("Default settings") );
90+
v = new QVBoxLayout(frame);
91+
h = new QHBoxLayout();
92+
l = new QLabel( tr("Direction"), frame );
93+
mcbDirectionDefault = new QComboBox(frame);
94+
mcbDirectionDefault->insertItem( 0, tr("Both direction") );
95+
mcbDirectionDefault->insertItem( 1, tr("Direct direction") );
96+
mcbDirectionDefault->insertItem( 2, tr("Reverse direction") );
97+
connect( mcbLayers, SIGNAL(currentIndexChanged(int)), this, SLOT(on_mcbLayers_selectItem()) );
98+
99+
h->addWidget(l);
100+
h->addWidget(mcbDirectionDefault);
101+
v->addLayout(h);
102+
103+
h = new QHBoxLayout(frame);
104+
l = new QLabel( tr("Cost"), frame );
105+
h->addWidget(l);
106+
l = new QLabel( tr("lines length"), frame );
107+
h->addWidget(l);
108+
v->addLayout(h);
109+
110+
h = new QHBoxLayout(frame);
111+
l = new QLabel( tr("Speed"), frame );
112+
msbSpeedDefault = new QSpinBox( frame );
113+
msbSpeedDefault->setMinimum( 1 );
114+
msbSpeedDefault->setMaximum( 10000000 );
115+
h->addWidget( l );
116+
h->addWidget( msbSpeedDefault );
117+
v->addLayout( h );
118+
119+
frame = new QFrame(tab);
120+
tab->addTab( frame, tr("Units") );
121+
v = new QVBoxLayout( frame );
122+
h = new QHBoxLayout();
123+
l = new QLabel( tr("Unit of speed") );
124+
mcbUnitOfSpeed = new QComboBox(this);
125+
h->addWidget(l);
126+
h->addWidget( mcbUnitOfSpeed );
127+
v->addLayout( h );
128+
129+
mcbUnitOfSpeed->insertItem( 0, tr("km/h") );
130+
mcbUnitOfSpeed->insertItem( 0, tr("m/s") );
131+
if ( s->mSpeedUnitName == "km/h" )
132+
mcbUnitOfSpeed->setCurrentIndex( 1 );
133+
else if (s->mSpeedUnitName == "m/s" )
134+
mcbUnitOfSpeed->setCurrentIndex( 0 );
135+
136+
// fill list of layers
137+
QMap<QString, QgsMapLayer*> mapLayers = QgsMapLayerRegistry::instance()->mapLayers();
138+
QMap<QString, QgsMapLayer*>::iterator layer_it = mapLayers.begin();
139+
140+
for ( ; layer_it != mapLayers.end(); ++layer_it )
141+
{
142+
QgsVectorLayer* vl = dynamic_cast<QgsVectorLayer*>( layer_it.value() );
143+
if ( !vl )
144+
continue;
145+
if ( vl->geometryType() != QGis::Line )
146+
continue;
147+
mcbLayers->insertItem( 0, vl->name() );
148+
}
149+
150+
//sets current settings
151+
msbSpeedDefault->setValue( static_cast<int>( s->mDefaultSpeed ) );
152+
153+
int idx = mcbLayers->findText( s->mLayer );
154+
if (idx != -1)
155+
{
156+
mcbLayers->setCurrentIndex( idx );
157+
}
158+
159+
idx = mcbDirection->findText( s->mDirection );
160+
if (idx != -1)
161+
mcbDirection->setCurrentIndex( idx );
162+
163+
idx = mcbSpeed->findText( s->mSpeed );
164+
if (idx != -1)
165+
mcbSpeed->setCurrentIndex( idx );
166+
167+
168+
switch( s->mDefaultDirection )
169+
{
170+
case RgLineVectorLayerSettings::Both:
171+
mcbDirectionDefault->setCurrentIndex(0);
172+
break;
173+
case RgLineVectorLayerSettings::FirstPointToLastPoint:
174+
mcbDirectionDefault->setCurrentIndex(1);
175+
break;
176+
case RgLineVectorLayerSettings::LastPointToFirstPoint:
177+
mcbDirectionDefault->setCurrentIndex(2);
178+
break;
179+
}
180+
} // RgLineVectorLayerSettingsWidget::RgLineVectorLayerSettingsWidget()
181+
182+
QgsVectorLayer* RgLineVectorLayerSettingsWidget::selectedLayer()
183+
{
184+
QMap<QString, QgsMapLayer*> mapLayers = QgsMapLayerRegistry::instance()->mapLayers();
185+
QMap<QString, QgsMapLayer*>::iterator layer_it = mapLayers.begin();
186+
187+
for ( ; layer_it != mapLayers.end(); ++layer_it )
188+
{
189+
QgsVectorLayer* vl = dynamic_cast<QgsVectorLayer*>( layer_it.value() );
190+
if ( !vl )
191+
continue;
192+
if ( vl->geometryType() != QGis::Line )
193+
continue;
194+
if ( vl->name() == mcbLayers->currentText() )
195+
return vl;
196+
}
197+
198+
return NULL;
199+
} // RgLineVectorLayerSettingsWidget::setlectedLayer()
200+
201+
void RgLineVectorLayerSettingsWidget::on_mcbLayers_selectItem()
202+
{
203+
mcbDirection->clear();
204+
mcbSpeed->clear();
205+
206+
mcbDirection->insertItem( 0, tr("Always use default") );
207+
mcbSpeed->insertItem( 0, tr("Always use default") );
208+
209+
QgsVectorLayer* vl = selectedLayer();
210+
if ( !vl )
211+
return;
212+
213+
QgsVectorDataProvider* provider = vl->dataProvider();
214+
if ( !provider )
215+
return;
216+
217+
const QgsFieldMap& fields = provider->fields();
218+
QgsFieldMap::const_iterator it;
219+
for (it = fields.constBegin(); it != fields.constEnd(); ++it)
220+
{
221+
QgsField currentField = it.value();
222+
QVariant currentType = currentField.type();
223+
if ( currentType == QVariant::Int || currentType == QVariant::String )
224+
{
225+
mcbDirection->insertItem(1, currentField.name());
226+
}
227+
if ( currentType == QVariant::Int || currentType == QVariant::Double )
228+
{
229+
mcbSpeed->insertItem(1, currentField.name());
230+
}
231+
}
232+
233+
} // RgDSettingsDlg::on_mcbLayers_selectItem()
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/***************************************************************************
2+
roadgraphplugin.h
3+
--------------------------------------
4+
Date : 2010-10-19
5+
Copyright : (C) 2010 by Yakushev Sergey
6+
Email : YakushevS@list.ru
7+
****************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
#ifndef ROADGRAPH_LINEVECTORLAYERSETTINGSWIDGET_H
17+
#define ROADGRAPH_LINEVECTORLAYERSETTINGSWIDGET_H
18+
19+
#include <QWidget>
20+
21+
class RgLineVectorLayerSettings;
22+
23+
// forward declaration QT-classes
24+
class QComboBox;
25+
class QLineEdit;
26+
class QSpinBox;
27+
class QLineEdit;
28+
29+
// forward declaration Qgis-classes
30+
class QgsVectorLayer;
31+
32+
/**
33+
@author Sergey Yakushev
34+
*/
35+
/**
36+
* \class RgLineVectorLayerSettingsWidget
37+
* \brief
38+
*/
39+
class RgLineVectorLayerSettingsWidget : public QWidget
40+
{
41+
Q_OBJECT
42+
public:
43+
RgLineVectorLayerSettingsWidget( RgLineVectorLayerSettings *s, QWidget* parent = 0 );
44+
45+
private slots:
46+
void on_mcbLayers_selectItem();
47+
48+
private:
49+
QgsVectorLayer * selectedLayer();
50+
51+
public:
52+
/**
53+
* list of passible layers
54+
*/
55+
QComboBox *mcbLayers;
56+
57+
/**
58+
* list of possible fields for use as direction
59+
*/
60+
QComboBox *mcbDirection;
61+
62+
/**
63+
*
64+
*/
65+
QLineEdit *mleFirstPointToLastPointDirection;
66+
67+
/**
68+
*
69+
*/
70+
QLineEdit *mleLastPointToFirstPointDirection;
71+
72+
/**
73+
*
74+
*/
75+
QLineEdit *mleBothDirection;
76+
77+
/**
78+
* default direction value
79+
*/
80+
QComboBox *mcbDirectionDefault;
81+
82+
/**
83+
* list of possible fields for use as speed
84+
*/
85+
QComboBox *mcbSpeed;
86+
87+
/**
88+
* Default speed value
89+
*/
90+
QSpinBox *msbSpeedDefault;
91+
92+
/**
93+
* Unit of speed
94+
*/
95+
QComboBox *mcbUnitOfSpeed;
96+
};
97+
#endif

‎src/plugins/roadgraph/roadgraph.png

1.06 KB
Loading

‎src/plugins/roadgraph/roadgraph.qrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<RCC>
2+
<qresource prefix="/roadgraph/" >
3+
<file>roadgraph.png</file>
4+
<file>showdirect.png</file>
5+
<file>about.png</file>
6+
<file>coordinate_capture.png</file>
7+
</qresource>
8+
</RCC>
Lines changed: 359 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,359 @@
1+
/***************************************************************************
2+
roadgraphplugin.cpp - implemention of plugin
3+
--------------------------------------
4+
Date : 2010-10-10
5+
Copyright : (C) 2010 by Yakushev Sergey
6+
Email : YakushevS <at> list.ru
7+
****************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
17+
// QGIS Specific includes
18+
#include <qgsapplication.h>
19+
#include <qgisinterface.h>
20+
#include <qgisgui.h>
21+
#include <qgsmapcanvas.h>
22+
#include <qgsproject.h>
23+
#include <qgsmaptoolemitpoint.h>
24+
#include <qgsmaprenderer.h>
25+
26+
// Road grap plugin includes
27+
#include "roadgraphplugin.h"
28+
#include "shortestpathwidget.h"
29+
#include "settingsdlg.h"
30+
#include "graphdirector.h"
31+
#include "linevectorlayerdirector.h"
32+
#include "simplegraphbuilder.h"
33+
34+
//
35+
// Qt4 Related Includes
36+
//
37+
38+
#include <QAction>
39+
#include <QLabel>
40+
#include <QLocale>
41+
#include <QToolBar>
42+
#include <QPainter>
43+
#include <QPushButton>
44+
#include <QDockWidget>
45+
#include <QVBoxLayout>
46+
#include <QDebug>
47+
48+
// standart includes
49+
50+
static const char * const sIdent = "$Id: roadgraphplugin.cpp 9327 2009-04-20 10:09:44Z YEKST $";
51+
static const QString sName = QObject::tr( "Road graph plugin" );
52+
static const QString sDescription = QObject::tr( "It solve shortest path poblem." );
53+
static const QString sPluginVersion = QObject::tr( "Version 0.1" );
54+
static const QgisPlugin::PLUGINTYPE sPluginType = QgisPlugin::UI;
55+
56+
//////////////////////////////////////////////////////////////////////
57+
//
58+
// THE FOLLOWING METHODS ARE MANDATORY FOR ALL PLUGINS
59+
//
60+
//////////////////////////////////////////////////////////////////////
61+
62+
/**
63+
* Constructor for the plugin. The plugin is passed a pointer
64+
* an interface object that provides access to exposed functions in QGIS.
65+
* @param theQGisInterface - Pointer to the QGIS interface object
66+
*/
67+
RoadGraphPlugin::RoadGraphPlugin( QgisInterface * theQgisInterface ):
68+
QgisPlugin( sName, sDescription, sPluginVersion, sPluginType ),
69+
mQGisIface( theQgisInterface )
70+
{
71+
72+
mQShortestPathDock = NULL;
73+
mDirector = new RgLineVectorLayerDirector();
74+
mTimeUnitName = "h";
75+
mDistanceUnitName = "km";
76+
}
77+
78+
RoadGraphPlugin::~RoadGraphPlugin()
79+
{
80+
81+
}
82+
83+
/*
84+
* Initialize the GUI interface for the plugin - this is only called once when the plugin is
85+
* added to the plugin registry in the QGIS application.
86+
*/
87+
void RoadGraphPlugin::initGui()
88+
{
89+
// create shorttest path dock
90+
mQShortestPathDock = new RgShortestPathWidget( mQGisIface->mainWindow() , this );
91+
mQGisIface->addDockWidget( Qt::LeftDockWidgetArea, mQShortestPathDock );
92+
93+
// Create the action for tool
94+
mQSettingsAction = new QAction( QIcon( ":/roadgraph/road.png" ), tr( "Road graph settings" ), this );
95+
mQShowDirectionAction = new QAction( QIcon( ":/roadgraph/showdirect.png" ), tr("Show roads direction"), this );
96+
mInfoAction = new QAction( QIcon( ":/roadgraph/about.png" ), tr( "About" ), this );
97+
98+
// Set the what's this text
99+
mQSettingsAction->setWhatsThis( tr("Road graph plugin settings") );
100+
mQShowDirectionAction->setWhatsThis( tr("Roads direction viewer") );
101+
mInfoAction->setWhatsThis( tr("About Road graph plugin") );
102+
103+
mQShowDirectionAction->setCheckable( true );
104+
105+
setGuiElementsToDefault();
106+
107+
// Connect the action to slots
108+
connect( mQSettingsAction, SIGNAL( triggered() ), this, SLOT( property() ) );
109+
connect( mQShowDirectionAction, SIGNAL( triggered() ), this, SLOT( onShowDirection() ) );
110+
connect( mInfoAction, SIGNAL( triggered() ), SLOT( about() ) );
111+
112+
// Add the icons to the toolbar
113+
mQGisIface->addToolBarIcon( mQShowDirectionAction );
114+
115+
mQGisIface->addPluginToMenu( tr( "Road graph" ), mQSettingsAction );
116+
mQGisIface->addPluginToMenu( tr( "Road graph" ), mQShowDirectionAction );
117+
mQGisIface->addPluginToMenu( tr( "Road graph" ), mInfoAction );
118+
119+
connect( mQGisIface->mapCanvas(), SIGNAL( renderComplete(QPainter*) ), this, SLOT( render(QPainter*) ) );
120+
connect( mQGisIface, SIGNAL( projectRead() ), this, SLOT( projectRead() ) );
121+
connect( mQGisIface , SIGNAL( newProjectCreated() ), this, SLOT( newProject() ) );
122+
// load settings
123+
projectRead();
124+
} // RoadGraphPlugin::initGui()
125+
126+
// Unload the plugin by cleaning up the GUI
127+
void RoadGraphPlugin::unload()
128+
{
129+
// remove the GUI
130+
mQGisIface->removePluginMenu( tr("Road graph"), mQSettingsAction );
131+
mQGisIface->removePluginMenu( tr("Road graph"), mQShowDirectionAction );
132+
133+
mQGisIface->removeToolBarIcon( mQShowDirectionAction );
134+
135+
// disconnect
136+
disconnect( mQGisIface->mapCanvas(), SIGNAL( renderComplete(QPainter*) ), this, SLOT( render(QPainter*) ) );
137+
disconnect( mQGisIface->mainWindow(), SIGNAL( projectRead() ), this, SLOT( projectRead() ) );
138+
disconnect( mQGisIface->mainWindow(), SIGNAL( newProject() ), this, SLOT( newProject() ) );
139+
140+
delete mQSettingsAction;
141+
delete mQShowDirectionAction;
142+
delete mQShortestPathDock;
143+
} // RoadGraphPlugin::unload()
144+
145+
void RoadGraphPlugin::setGuiElementsToDefault()
146+
{
147+
148+
} // RoadGraphPlugin::setGuiElementsToDefault()
149+
150+
//method defined in interface
151+
void RoadGraphPlugin::help()
152+
{
153+
//implement me!
154+
} // RoadGraphPlugin::help()
155+
156+
void RoadGraphPlugin::onShowDirection()
157+
{
158+
mQGisIface->mapCanvas()->refresh();
159+
} // RoadGraphPlugin::onShowDirection()
160+
161+
162+
void RoadGraphPlugin::newProject()
163+
{
164+
setGuiElementsToDefault();
165+
}
166+
167+
void RoadGraphPlugin::property()
168+
{
169+
RgSettingsDlg dlg(mDirector, mQGisIface->mainWindow(), QgisGui::ModalDialogFlags );
170+
171+
dlg.setTimeUnitName( mTimeUnitName );
172+
dlg.setDistanceUnitName( mDistanceUnitName );
173+
174+
if ( !dlg.exec() )
175+
return;
176+
mTimeUnitName = dlg.timeUnitName();
177+
mDistanceUnitName = dlg.distanceUnitName();
178+
179+
mDirector->settings()->write( QgsProject::instance() );
180+
QgsProject::instance()->writeEntry( "roadgraphplugin", "/pluginTimeUnit", mTimeUnitName );
181+
QgsProject::instance()->writeEntry( "roadgraphplugin", "/pluginDistanceUnit", mDistanceUnitName );
182+
183+
setGuiElementsToDefault();
184+
} //RoadGraphPlugin::property()
185+
186+
void RoadGraphPlugin::about()
187+
{
188+
QDialog dlg( mQGisIface->mainWindow() );
189+
dlg.setWindowFlags( dlg.windowFlags() | Qt::MSWindowsFixedSizeDialogHint );
190+
dlg.setWindowFlags( dlg.windowFlags() &~ Qt::WindowContextHelpButtonHint );
191+
dlg.setWindowTitle( tr( "About RoadGraph" ) );
192+
QVBoxLayout *lines = new QVBoxLayout( &dlg );
193+
QLabel *title = new QLabel( "<b>RoadGraph plugin</b>" );
194+
title->setAlignment( Qt::AlignHCenter | Qt::AlignVCenter );
195+
QLabel *version = new QLabel( sPluginVersion );
196+
version->setAlignment( Qt::AlignHCenter | Qt::AlignVCenter );
197+
lines->addWidget( title );
198+
lines->addWidget( version );
199+
lines->addWidget( new QLabel( tr( "Find shortest path on roads graph" ) ) );
200+
lines->addWidget( new QLabel( tr( "<b>Developers:</b>" ) ) );
201+
lines->addWidget( new QLabel( " Sergey Yakushev" ) );
202+
lines->addWidget( new QLabel( tr( "<b>Homepage:</b>" ) ) );
203+
204+
QSettings settings;
205+
QString localeFullName, localeShortName;
206+
bool overrideLocale = settings.value( "locale/overrideFlag", QVariant( false ) ).toBool();
207+
if ( !overrideLocale )
208+
{
209+
localeFullName = QLocale().system().name();
210+
}
211+
else
212+
{
213+
localeFullName = settings.value( "locale/userLocale", QVariant( "" ) ).toString();
214+
}
215+
216+
localeShortName = localeFullName.left( 2 );
217+
QLabel *link = new QLabel();
218+
if ( localeShortName == "ru" || localeShortName == "uk" )
219+
{
220+
link->setText( "<a href=\"http://gis-lab.info/qa/road-graph.html\">http://gis-lab.info/qa/road-graph.html</a>" );
221+
}
222+
else
223+
{
224+
link->setText( "<a href=\"http://gis-lab.info/qa/road-graph-eng.html\">http://gis-lab.info/qa/road-graph-eng.html</a>" );
225+
}
226+
227+
link->setOpenExternalLinks( true );
228+
lines->addWidget( link );
229+
230+
QPushButton *btnClose = new QPushButton( tr( "Close" ) );
231+
lines->addWidget( btnClose );
232+
QObject::connect( btnClose, SIGNAL( clicked() ), &dlg, SLOT( close() ) );
233+
234+
dlg.exec();
235+
} //RoadGraphPlugin::about()
236+
237+
void RoadGraphPlugin::projectRead()
238+
{
239+
mDirector->settings()->read( QgsProject::instance() );
240+
mTimeUnitName = QgsProject::instance()->readEntry( "roadgraphplugin", "/pluginTimeUnit", "h" );
241+
mDistanceUnitName = QgsProject::instance()->readEntry( "roadgraphplugin", "/pluginDistanceUnit", "km" );
242+
setGuiElementsToDefault();
243+
}// RoadGraphplguin::projectRead()
244+
245+
QgisInterface* RoadGraphPlugin::iface()
246+
{
247+
return mQGisIface;
248+
}
249+
250+
const RgGraphDirector* RoadGraphPlugin::director() const
251+
{
252+
return mDirector;
253+
}
254+
void RoadGraphPlugin::render(QPainter *painter)
255+
{
256+
if ( mDirector == NULL )
257+
return;
258+
if ( !mQShowDirectionAction->isChecked() )
259+
return;
260+
RgSimpleGraphBuilder builder;
261+
builder.setDestinationCrs( mQGisIface->mapCanvas()->mapRenderer()->destinationSrs() );
262+
mDirector->makeGraph( &builder );
263+
AdjacencyMatrix m = builder.adjacencyMatrix();
264+
265+
AdjacencyMatrix::iterator it1;
266+
AdjacencyMatrixString::iterator it2;
267+
for ( it1 = m.begin(); it1 != m.end(); ++it1 )
268+
{
269+
for ( it2 = it1->second.begin(); it2 != it1->second.end(); ++it2 )
270+
{
271+
QgsPoint p1 = mQGisIface->mapCanvas()->getCoordinateTransform()->transform( it1->first );
272+
QgsPoint p2 = mQGisIface->mapCanvas()->getCoordinateTransform()->transform( it2->first );
273+
double x1 = p1.x(),
274+
y1 = p1.y(),
275+
x2 = p2.x(),
276+
y2 = p2.y();
277+
278+
double length = sqrt( pow( x2-x1, 2.0) + pow( y2-y1, 2.0 ) );
279+
double Cos = (x2-x1)/length;
280+
double Sin = (y2-y1)/length;
281+
double centerX = (x1+x2)/2;
282+
double centerY = (y1+y2)/2;
283+
double r = mArrowSize;
284+
285+
QPointF pt1(centerX - Sin*r, centerY + Cos*r);
286+
QPointF pt2(centerX + Sin*r, centerY - Cos*r);
287+
288+
QVector<QPointF> tmp;
289+
tmp.resize(3);
290+
tmp[0] = QPointF( centerX + Cos*r*2, centerY + Sin*r*2 );
291+
tmp[1] = pt1;
292+
tmp[2] = pt2;
293+
painter->drawPolygon(tmp);
294+
}
295+
}
296+
297+
}// RoadGraphPlugin::render()
298+
QString RoadGraphPlugin::timeUnitName()
299+
{
300+
return mTimeUnitName;
301+
}
302+
303+
QString RoadGraphPlugin::distanceUnitName()
304+
{
305+
return mDistanceUnitName;
306+
}
307+
308+
//////////////////////////////////////////////////////////////////////////
309+
//
310+
//
311+
// THE FOLLOWING CODE IS AUTOGENERATED BY THE PLUGIN BUILDER SCRIPT
312+
// YOU WOULD NORMALLY NOT NEED TO MODIFY THIS, AND YOUR PLUGIN
313+
// MAY NOT WORK PROPERLY IF YOU MODIFY THIS INCORRECTLY
314+
//
315+
//
316+
//////////////////////////////////////////////////////////////////////////
317+
318+
319+
/**
320+
* Required extern functions needed for every plugin
321+
* These functions can be called prior to creating an instance
322+
* of the plugin class
323+
*/
324+
// Class factory to return a new instance of the plugin class
325+
QGISEXTERN QgisPlugin * classFactory( QgisInterface * theQgisInterfacePointer )
326+
{
327+
return new RoadGraphPlugin( theQgisInterfacePointer );
328+
329+
}
330+
// Return the name of the plugin - note that we do not user class members as
331+
// the class may not yet be insantiated when this method is called.
332+
QGISEXTERN QString name()
333+
{
334+
return sName;
335+
}
336+
337+
// Return the description
338+
QGISEXTERN QString description()
339+
{
340+
return sDescription;
341+
}
342+
343+
// Return the type (either UI or MapLayer plugin)
344+
QGISEXTERN int type()
345+
{
346+
return sPluginType;
347+
}
348+
349+
// Return the version number for the plugin
350+
QGISEXTERN QString version()
351+
{
352+
return sPluginVersion;
353+
}
354+
355+
// Delete ourself
356+
QGISEXTERN void unload( QgisPlugin * thePluginPointer )
357+
{
358+
delete thePluginPointer;
359+
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/***************************************************************************
2+
roadgraphplugin.h
3+
--------------------------------------
4+
Date : 2010-10-10
5+
Copyright : (C) 2010 by Yakushev Sergey
6+
Email : YakushevS <at> list.ru
7+
****************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
#ifndef ROADGRAPHPLUGIN
16+
#define ROADGRAPHPLUGIN
17+
18+
//QT4 includes
19+
#include <QObject>
20+
21+
//QGIS includes
22+
#include <qgisplugin.h>
23+
#include <qgspoint.h>
24+
25+
//forward declarations
26+
class QAction;
27+
class QToolBar;
28+
class QPainter;
29+
class QgisInterface;
30+
class QDockWidget;
31+
32+
//forward declarations RoadGraph plugins classes
33+
class RgGraphDirector;
34+
class RgShortestPathWidget;
35+
36+
/**
37+
* \class RoadGraphPlugin
38+
* \brief Road graph plugin for QGIS
39+
* This plugin can solve the shotrest path problem and etc...
40+
*/
41+
class RoadGraphPlugin: public QObject, public QgisPlugin
42+
{
43+
Q_OBJECT
44+
public:
45+
/**
46+
* Constructor for a plugin. The QgisInterface pointer is passed by
47+
* QGIS when it attempts to instantiate the plugin.
48+
* @param theInterface Pointer to the QgisInterface object.
49+
*/
50+
RoadGraphPlugin( QgisInterface * theInterface );
51+
//! Destructor
52+
virtual ~RoadGraphPlugin();
53+
/**
54+
* return pointer to my Interface
55+
*/
56+
QgisInterface *iface();
57+
58+
/**
59+
* return pointer to my Graph director
60+
*/
61+
const RgGraphDirector* director() const;
62+
63+
/**
64+
* get time unit name
65+
*/
66+
QString timeUnitName();
67+
68+
/**
69+
* get distance unit name
70+
*/
71+
QString distanceUnitName();
72+
73+
public slots:
74+
void render(QPainter *painter);
75+
//! init the gui
76+
virtual void initGui();
77+
78+
//!set values onthe gui when a project is read or the gui first loaded
79+
virtual void projectRead();
80+
81+
//!set default values for new project
82+
void newProject();
83+
84+
//! Show the property dialog box
85+
void property();
86+
87+
//! unload the plugin
88+
void unload();
89+
90+
//! show the help document
91+
void help();
92+
93+
//! show about window
94+
void about();
95+
private slots:
96+
/**
97+
* set show roads direction
98+
*/
99+
void onShowDirection();
100+
101+
private:
102+
/**
103+
* set all gui elements to default status
104+
*/
105+
void setGuiElementsToDefault( );
106+
107+
private:
108+
109+
////////////////////////////////////////////////////////////////////
110+
//
111+
// MANDATORY PLUGIN PROPERTY DECLARATIONS .....
112+
//
113+
////////////////////////////////////////////////////////////////////
114+
int mPluginType;
115+
116+
//! Pointer to the QGIS interface object
117+
QgisInterface *mQGisIface;
118+
119+
////////////////////////////////////////////////////////////////////
120+
// ADD YOUR OWN PROPERTY DECLARATIONS AFTER THIS POINT.....
121+
//
122+
////////////////////////////////////////////////////////////////////
123+
/**
124+
* on show settings
125+
*/
126+
QAction * mQSettingsAction;
127+
128+
/**
129+
* pointer ot the direction show action
130+
*/
131+
QAction * mQShowDirectionAction;
132+
133+
/**
134+
* pointer ot the about action
135+
*/
136+
QAction * mInfoAction;
137+
/**
138+
* GUI for use shortest path finder
139+
*/
140+
RgShortestPathWidget *mQShortestPathDock;
141+
142+
/**
143+
* My Graph Director
144+
*/
145+
RgGraphDirector *mDirector;
146+
147+
/**
148+
* time unit for results presentation
149+
*/
150+
QString mTimeUnitName;
151+
152+
/**
153+
* distance unit for results presentation
154+
*/
155+
QString mDistanceUnitName;
156+
private:
157+
static const int mArrowSize = 5;
158+
};
159+
160+
#endif //ROADGRAPHPLUGIN

‎src/plugins/roadgraph/settings.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/***************************************************************************
2+
settings.h
3+
--------------------------------------
4+
Date : 2010-10-18
5+
Copyright : (C) 2010 by Yakushev Sergey
6+
Email : YakushevS <at> list.ru
7+
****************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
#ifndef ROADGRAPH_SETTINGS
16+
#define ROADGRAPH_SETTINGS
17+
18+
//QT4 includes
19+
20+
//QGIS includes
21+
22+
//forward declarations
23+
class QWidget;
24+
class QgsProject;
25+
26+
/**
27+
* \class RgGraphDirector
28+
* \brief Determine making the graph
29+
* contained the settings
30+
*/
31+
class RgSettings
32+
{
33+
public:
34+
//! Destructor
35+
virtual ~RgSettings() { };
36+
37+
/**
38+
* write settings to the poject file
39+
*/
40+
virtual void write( QgsProject * ) = 0;
41+
/**
42+
* read settings form project file
43+
*/
44+
virtual void read( const QgsProject * ) = 0;
45+
/**
46+
* This function test settings and return true if setting correct
47+
*/
48+
virtual bool test() = 0;
49+
/**
50+
* Make settings widget
51+
* use it for GUI setting
52+
*/
53+
virtual QWidget* getGui( QWidget* parent ) = 0;
54+
/**
55+
* Load settings from widget
56+
*/
57+
virtual void setFromGui( QWidget * ) = 0;
58+
};
59+
#endif //ROADGRAPH_SETTIGNS

‎src/plugins/roadgraph/settingsdlg.cpp

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/***************************************************************************
2+
* Copyright (C) 2010 by Sergey Yakushev *
3+
* yakushevs <at> list.ru *
4+
* *
5+
* This program is free software; you can redistribute it and/or modify *
6+
* it under the terms of the GNU General Public License as published by *
7+
* the Free Software Foundation; either version 2 of the License, or *
8+
* (at your option) any later version. *
9+
***************************************************************************/
10+
//road-graph plugin includes
11+
#include "settingsdlg.h"
12+
#include <qgscontexthelp.h>
13+
14+
//qt includes
15+
#include <qlabel.h>
16+
#include <qcombobox.h>
17+
#include <QHBoxLayout>
18+
#include <QVBoxLayout>
19+
#include <qdialogbuttonbox.h>
20+
#include <qmessagebox.h>
21+
22+
23+
// Qgis includes
24+
#include "graphdirector.h"
25+
#include "settings.h"
26+
27+
//standard includes
28+
29+
RgSettingsDlg::RgSettingsDlg( RgGraphDirector *director, QWidget* parent, Qt::WFlags fl )
30+
: mDirector(director), QDialog( parent, fl )
31+
{
32+
// create base widgets;
33+
setWindowTitle( tr("Road graph plugins settings") );
34+
QVBoxLayout *v = new QVBoxLayout( this );
35+
36+
QHBoxLayout *h = new QHBoxLayout();
37+
QLabel *l = new QLabel( tr("Plugins time unit:"), this );
38+
h->addWidget( l );
39+
mcbPluginsTimeUnit = new QComboBox( this );
40+
h->addWidget( mcbPluginsTimeUnit );
41+
v->addLayout( h );
42+
43+
h = new QHBoxLayout();
44+
l = new QLabel( tr("Plugins distance unit:"), this );
45+
h->addWidget( l );
46+
mcbPluginsDistanceUnit = new QComboBox( this );
47+
h->addWidget( mcbPluginsDistanceUnit );
48+
v->addLayout( h );
49+
50+
/*
51+
h = new QHBoxLayout();
52+
l = new QLabel( tr("Select graph source:"), this);
53+
h->addWidget(l);
54+
mcbGraphDirector = new QComboBox( this );
55+
h->addWidget(mcbGraphDirector);
56+
v->addLayout(h);
57+
*/
58+
mSettingsWidget = director->settings()->getGui(this);
59+
v->addWidget( mSettingsWidget );
60+
61+
QDialogButtonBox *bb = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this );
62+
connect( bb, SIGNAL(accepted()), this, SLOT(on_buttonBox_accepted()) );
63+
connect( bb, SIGNAL(rejected()), this, SLOT(on_buttonBox_rejected()) );
64+
v->addWidget( bb );
65+
66+
mcbPluginsTimeUnit->addItem( tr("second"), QVariant( "s" ) );
67+
mcbPluginsTimeUnit->addItem( tr("hour"), QVariant( "h" ) );
68+
mcbPluginsDistanceUnit->addItem( tr("meter"), QVariant( "m" ) );
69+
mcbPluginsDistanceUnit->addItem( tr("kilometer"), QVariant( "km" ) );
70+
71+
} // RgSettingsDlg::RgSettingsDlg()
72+
73+
RgSettingsDlg::~RgSettingsDlg()
74+
{
75+
}
76+
77+
void RgSettingsDlg::on_buttonBox_accepted()
78+
{
79+
mDirector->settings()->setFromGui( mSettingsWidget );
80+
accept();
81+
}
82+
83+
void RgSettingsDlg::on_buttonBox_rejected()
84+
{
85+
reject();
86+
}
87+
88+
void RgSettingsDlg::on_buttonBox_helpRequested()
89+
{
90+
QgsContextHelp::run( context_id );
91+
}
92+
93+
QString RgSettingsDlg::timeUnitName()
94+
{
95+
return mcbPluginsTimeUnit->itemData( mcbPluginsTimeUnit->currentIndex() ).toString();
96+
}
97+
98+
void RgSettingsDlg::setTimeUnitName( const QString& name)
99+
{
100+
int i = mcbPluginsTimeUnit->findData( QVariant( name ) );
101+
if (i != -1)
102+
{
103+
mcbPluginsTimeUnit->setCurrentIndex( i );
104+
}
105+
}
106+
107+
QString RgSettingsDlg::distanceUnitName()
108+
{
109+
return mcbPluginsDistanceUnit->itemData( mcbPluginsDistanceUnit->currentIndex() ).toString();
110+
}
111+
112+
void RgSettingsDlg::setDistanceUnitName( const QString& name)
113+
{
114+
int i = mcbPluginsDistanceUnit->findData( QVariant( name ) );
115+
if (i != -1)
116+
{
117+
mcbPluginsDistanceUnit->setCurrentIndex( i );
118+
}
119+
}

‎src/plugins/roadgraph/settingsdlg.h

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

‎src/plugins/roadgraph/showdirect.png

639 Bytes
Loading

‎src/plugins/roadgraph/showdirect.svg

Lines changed: 85 additions & 0 deletions
Loading
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/***************************************************************************
2+
* Copyright (C) 2010 by Sergey Yakushev *
3+
* yakushevs@list.ru *
4+
* *
5+
* *
6+
* This program is free software; you can redistribute it and/or modify *
7+
* it under the terms of the GNU General Public License as published by *
8+
* the Free Software Foundation; either version 2 of the License, or *
9+
* (at your option) any later version. *
10+
***************************************************************************/
11+
12+
/**
13+
* \file simplegraphbuilder.cpp
14+
* \brief implementation of RgSimpleGraphBuilder
15+
*/
16+
17+
#include "simplegraphbuilder.h"
18+
#include "utils.h"
19+
20+
// Qgis includes
21+
#include <qgscoordinatetransform.h>
22+
#include <qgsdistancearea.h>
23+
24+
#include <iostream>
25+
26+
RgSimpleGraphBuilder::RgSimpleGraphBuilder()
27+
{
28+
mCoordinateTransform = new QgsCoordinateTransform();
29+
mDistanceArea = new QgsDistanceArea();
30+
mDistanceArea->setProjectionsEnabled( true );
31+
}
32+
33+
RgSimpleGraphBuilder::~RgSimpleGraphBuilder()
34+
{
35+
delete mCoordinateTransform;
36+
delete mDistanceArea;
37+
}
38+
39+
void RgSimpleGraphBuilder::setSourceCrs( const QgsCoordinateReferenceSystem& crs )
40+
{
41+
mCoordinateTransform->setSourceCrs( crs );
42+
}
43+
44+
void RgSimpleGraphBuilder::setDestinationCrs( const QgsCoordinateReferenceSystem& crs )
45+
{
46+
mCoordinateTransform->setDestCRS( crs );
47+
mDistanceArea->setSourceCrs( crs.srsid() );
48+
}
49+
50+
void RgSimpleGraphBuilder::addVertex( const QgsPoint& pt )
51+
{
52+
mMatrix[ mCoordinateTransform->transform(pt) ];
53+
}
54+
55+
void RgSimpleGraphBuilder::addArc( const QgsPoint& pt1, const QgsPoint& pt2, double speed )
56+
{
57+
QgsPoint p1 = mCoordinateTransform->transform(pt1);
58+
QgsPoint p2 = mCoordinateTransform->transform(pt2);
59+
60+
double distance = mDistanceArea->measureLine( p1, p2 );
61+
double time = distance/speed;
62+
mMatrix[ p1 ][ p2 ] = ArcAttributes(distance, time, 0);
63+
}
64+
65+
QgsPoint RgSimpleGraphBuilder::tiePoint( const QgsPoint &pt, bool &b )
66+
{
67+
b = false;
68+
AdjacencyMatrix::iterator it1;
69+
AdjacencyMatrixString::iterator it2;
70+
71+
double min = infinity();
72+
QgsPoint minP1, minP2;
73+
QgsPoint center;
74+
for ( it1 = mMatrix.begin(); it1 != mMatrix.end(); ++it1 )
75+
{
76+
for ( it2 = it1->second.begin(); it2 != it1->second.end(); ++it2 )
77+
{
78+
QgsPoint c;
79+
double d = distance(it1->first, it2->first, pt, c);
80+
if (d < min)
81+
{
82+
minP1 = it1->first;
83+
minP2 = it2->first;
84+
min = d;
85+
center = c;
86+
}
87+
}
88+
}
89+
if ( min >= infinity() )
90+
return center;
91+
92+
double c = mMatrix[ minP1 ][ minP2 ].mCost;
93+
double t = mMatrix[ minP1 ][ minP2 ].mTime;
94+
95+
double newArcLength = mDistanceArea->measureLine( minP1, center );
96+
mMatrix[ minP1 ][ center ] = ArcAttributes( newArcLength, t*newArcLength/c, 0);
97+
newArcLength = mDistanceArea->measureLine( center, minP2 );
98+
mMatrix[ center ][ minP2 ] = ArcAttributes( newArcLength, t*newArcLength/c, 0 );
99+
100+
if ( mMatrix[ minP2 ].find( minP1 ) != mMatrix[ minP2 ].end() )
101+
{
102+
c = mMatrix[ minP2 ][ minP1 ].mCost;
103+
t = mMatrix[ minP2 ][ minP1 ].mTime;
104+
newArcLength = mDistanceArea->measureLine( center, minP1 );
105+
mMatrix[ center ][ minP1 ] = ArcAttributes( newArcLength, t*newArcLength/c, 0);
106+
newArcLength = mDistanceArea->measureLine( minP2, center );
107+
mMatrix[ minP2 ][ center ] = ArcAttributes( newArcLength, t*newArcLength/c, 0);
108+
}
109+
110+
mMatrix[minP1].erase( minP2 );
111+
mMatrix[minP2].erase( minP1 );
112+
b = true;
113+
return center;
114+
}
115+
116+
AdjacencyMatrix RgSimpleGraphBuilder::adjacencyMatrix()
117+
{
118+
return mMatrix;
119+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/***************************************************************************
2+
simplegraphbuilder.h
3+
--------------------------------------
4+
Date : 2010-10-25
5+
Copyright : (C) 2010 by Yakushev Sergey
6+
Email : YakushevS@list.ru
7+
****************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
#ifndef ROADGRAPH_SIMPLEGRAPHBUILDER
16+
#define ROADGRAPH_SIMPLEGRAPHBUILDER
17+
18+
#include "graphbuilder.h"
19+
#include "utils.h"
20+
21+
//QT4 includes
22+
23+
//QGIS includes
24+
25+
//forward declarations
26+
class QgsDistanceArea;
27+
class QgsCoordinateTransform;
28+
29+
/**
30+
* \class RgSimpleGraphBuilder
31+
* \brief This class making the simple graph for visualisation
32+
* contained the settings
33+
*/
34+
35+
class RgSimpleGraphBuilder : public RgGraphBuilder
36+
{
37+
public:
38+
/**
39+
* default constructor
40+
*/
41+
RgSimpleGraphBuilder();
42+
/**
43+
* MANDATORY DIRECTOR PROPERTY DECLARATION
44+
*/
45+
~RgSimpleGraphBuilder();
46+
void setSourceCrs( const QgsCoordinateReferenceSystem& crs );
47+
void setDestinationCrs( const QgsCoordinateReferenceSystem& crs );
48+
void addVertex( const QgsPoint& pt );
49+
void addArc( const QgsPoint& pt1, const QgsPoint& pt2, double speed );
50+
QgsPoint tiePoint( const QgsPoint& pt, bool& ok );
51+
52+
/**
53+
* return Adjacecncy matrix;
54+
*/
55+
AdjacencyMatrix adjacencyMatrix();
56+
private:
57+
AdjacencyMatrix mMatrix;
58+
59+
QgsDistanceArea* mDistanceArea;
60+
61+
QgsCoordinateTransform* mCoordinateTransform;
62+
};
63+
#endif //SIMPLEGRAPHBUILDER

‎src/plugins/roadgraph/units.cpp

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/***************************************************************************
2+
* Copyright (C) 2009 by Sergey Yakushev *
3+
* yakushevs@list.ru *
4+
* *
5+
* This is file implements Units classes *
6+
* *
7+
* This program is free software; you can redistribute it and/or modify *
8+
* it under the terms of the GNU General Public License as published by *
9+
* the Free Software Foundation; either version 2 of the License, or *
10+
* (at your option) any later version. *
11+
***************************************************************************/
12+
13+
/**
14+
* \file units.cpp
15+
* \brief implementation of VRP plugins utils
16+
*/
17+
18+
#include "units.h"
19+
20+
21+
Unit::Unit()
22+
{
23+
mMultipler = 1.0;
24+
}
25+
26+
Unit::Unit(const QString& name, double multipler) :
27+
mName(name), mMultipler(multipler)
28+
{
29+
}
30+
31+
QString Unit::name() const
32+
{
33+
return mName;
34+
}
35+
36+
double Unit::multipler() const
37+
{
38+
return mMultipler;
39+
}
40+
Unit Unit::byName( const QString& name)
41+
{
42+
if ( name == "h" )
43+
return Unit( name, 60*60 );
44+
else if ( name == "km" )
45+
return Unit( name, 1000 );
46+
else if ( name == "s" )
47+
return Unit( name, 1 );
48+
else if ( name == "m" )
49+
return Unit( name, 1 );
50+
return Unit();
51+
}
52+
53+
SpeedUnit::SpeedUnit() :
54+
mTimeUnit("",1), mDistanceUnit("",1)
55+
{
56+
57+
}
58+
59+
SpeedUnit::SpeedUnit( const Unit& distanceUnit, const Unit& timeUnit) :
60+
mTimeUnit( timeUnit ), mDistanceUnit( distanceUnit )
61+
{
62+
}
63+
64+
QString SpeedUnit::name() const
65+
{
66+
if ( mDistanceUnit.name().isNull() || mTimeUnit.name().isNull() )
67+
return QString();
68+
return mDistanceUnit.name() + QString("/") + mTimeUnit.name();
69+
}
70+
71+
SpeedUnit SpeedUnit::byName( const QString& name )
72+
{
73+
if ( name=="km/h" )
74+
return SpeedUnit( Unit::byName("km"), Unit::byName("h") );
75+
else if ( name=="m/s" )
76+
return SpeedUnit( Unit::byName("m"), Unit::byName("s") );
77+
return SpeedUnit();
78+
}
79+
80+
double SpeedUnit::multipler() const
81+
{
82+
return mDistanceUnit.multipler()/mTimeUnit.multipler();
83+
}
84+
85+
Unit SpeedUnit::timeUnit() const
86+
{
87+
return mTimeUnit;
88+
}
89+
90+
Unit SpeedUnit::distanceUnit() const
91+
{
92+
return mDistanceUnit;
93+
}

‎src/plugins/roadgraph/units.h

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/***************************************************************************
2+
* Copyright (C) 2010 by Sergey Yakushev *
3+
* yakushevs@list.ru *
4+
* *
5+
* This is file define vrp plugins time, distance and speed units *
6+
* classes *
7+
* *
8+
* This program is free software; you can redistribute it and/or modify *
9+
* it under the terms of the GNU General Public License as published by *
10+
* the Free Software Foundation; either version 2 of the License, or *
11+
* (at your option) any later version. *
12+
***************************************************************************/
13+
14+
#ifndef ROADGRAPH_UNITS_H
15+
#define ROADGRAPH_UNITS_H
16+
17+
//#include <vrpguibase.h>
18+
// QT includes
19+
#include <qstring.h>
20+
21+
// Qgis includes
22+
23+
// forward declaration Qgis-classes
24+
25+
/**
26+
@author Sergey Yakushev
27+
*/
28+
/**
29+
* \class Unit
30+
* \brief This class provide interface to access unit name and multipler.
31+
* You can use it for convert units to metric system
32+
*/
33+
class Unit
34+
{
35+
public:
36+
/**
37+
* default constructor
38+
*/
39+
Unit();
40+
41+
/**
42+
* constructor
43+
*/
44+
Unit( const QString& name, double multipler );
45+
46+
/**
47+
* return unit name
48+
*/
49+
QString name() const;
50+
51+
/**
52+
* return unit multipler. You can use multipler to conver unit to metric system
53+
*/
54+
double multipler() const;
55+
56+
/**
57+
* return unit by name
58+
*/
59+
static Unit byName( const QString& name );
60+
private:
61+
62+
/**
63+
* units name
64+
*/
65+
QString mName;
66+
67+
/**
68+
* units multipler
69+
*/
70+
double mMultipler;
71+
};
72+
73+
class SpeedUnit
74+
{
75+
public:
76+
SpeedUnit();
77+
SpeedUnit( const Unit& distanceUnit, const Unit& timeUnit );
78+
79+
QString name() const;
80+
double multipler() const;
81+
82+
Unit timeUnit() const;
83+
Unit distanceUnit() const;
84+
85+
static SpeedUnit byName( const QString& name );
86+
protected:
87+
Unit mTimeUnit;
88+
Unit mDistanceUnit;
89+
};
90+
#endif

‎src/plugins/roadgraph/utils.cpp

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/***************************************************************************
2+
* Copyright (C) 2009 by Sergey Yakushev *
3+
* yakushevs <at> list.ru *
4+
* *
5+
* This is a plugin generated from the QGIS plugin template *
6+
* *
7+
* This program is free software; you can redistribute it and/or modify *
8+
* it under the terms of the GNU General Public License as published by *
9+
* the Free Software Foundation; either version 2 of the License, or *
10+
* (at your option) any later version. *
11+
***************************************************************************/
12+
13+
/**
14+
* \file utils.cpp
15+
* \brief implementation of Road-graph plugins utils
16+
*/
17+
18+
#include "utils.h"
19+
20+
//qt includes
21+
22+
// Qgis includes
23+
#include <qgspoint.h>
24+
25+
// standart includes
26+
#include <set>
27+
#include <cmath>
28+
29+
double infinity()
30+
{
31+
return 1e12;
32+
}
33+
34+
// return distance between line of two points and center
35+
double distance( const QgsPoint& p1, const QgsPoint& p2, const QgsPoint& p, QgsPoint& center )
36+
{
37+
38+
// first line
39+
double A1,B1,C1;
40+
A1 = p1.y() - p2.y();
41+
B1 = p2.x() - p1.x();
42+
C1 = p1.x()*(-A1) + p1.y()*(-B1);
43+
44+
// second line. First and second line is perpendicular.
45+
double A2,B2,C2;
46+
A2 = B1;
47+
B2 = -A1;
48+
C2 = -p.x()*A2 - p.y()*B2;
49+
50+
// union point
51+
double x,y,det;
52+
det = A1*B2 - B1*A2;
53+
x = (C2*B1 - B2*C1)/det;
54+
y = (-A1*C2 + C1*A2)/det;
55+
56+
center = QgsPoint(x,y);
57+
58+
det = sqrt(A1*A1+B1*B1);
59+
A1 /= det;
60+
B1 /= det;
61+
C1 /= det;
62+
if ( std::min( p1.x(), p2.x() ) <= x && std::max( p1.x(), p2.x() ) >= x &&
63+
std::min( p1.y(), p2.y() ) <= y && std::max( p1.y(), p2.y() ) >= y )
64+
return std::abs( A1*p.x()+B1*p.y()+C1 );
65+
66+
return infinity();
67+
}// RoadGraphPlugin::distance()
68+
69+
70+
bool QgsPointCompare::operator()( const QgsPoint& a, const QgsPoint& b ) const
71+
{
72+
return a.x() == b.x() ? a.y() < b.y() : a.x() < b.x();
73+
}
74+
75+
ArcAttributes::ArcAttributes()
76+
{
77+
mCost = infinity();
78+
mTime = infinity();
79+
}
80+
ArcAttributes::ArcAttributes( double cost, double time, int featureId ) :
81+
mCost( cost ), mTime( time ), mFeatureId( featureId )
82+
{
83+
84+
}
85+
86+
87+
DijkstraFinder::DijkstraFinder( const AdjacencyMatrix& m, DijkstraFinder::OptimizationCriterion c ):
88+
mAdjacencyMatrix (m), mCriterion(c)
89+
{
90+
}
91+
92+
std::map< QgsPoint , DijkstraFinder::DijkstraIterator, QgsPointCompare> DijkstraFinder::find( const QgsPoint& p )
93+
{
94+
CompareDijkstraIterator ci( mCriterion );
95+
std::set< DijkstraIterator, CompareDijkstraIterator > not_begin( ci );
96+
std::set< DijkstraIterator, CompareDijkstraIterator >::iterator it;
97+
std::map< QgsPoint, DijkstraIterator, QgsPointCompare> res;
98+
if (mAdjacencyMatrix.find( p ) == mAdjacencyMatrix.end() )
99+
{
100+
return res;
101+
}
102+
103+
AdjacencyMatrixString::const_iterator arcIt;
104+
AdjacencyMatrixString::const_iterator end = mAdjacencyMatrix.find( p )->second.end();
105+
106+
DijkstraIterator f;
107+
f.mCost = 0;
108+
f.mTime = 0;
109+
f.mFrontPoint = p;
110+
f.mBackPoint = p;
111+
res[ p ] = f;
112+
not_begin.insert( f );
113+
114+
while ( !not_begin.empty() )
115+
{
116+
it = not_begin.begin();
117+
DijkstraIterator i = *it;
118+
not_begin.erase( it );
119+
120+
if ( mAdjacencyMatrix.find( i.mBackPoint ) == mAdjacencyMatrix.end() )
121+
{
122+
continue;
123+
}
124+
end = mAdjacencyMatrix.find( i.mBackPoint )->second.end();
125+
for (arcIt = mAdjacencyMatrix.find( i.mBackPoint )->second.begin(); arcIt != end; ++arcIt)
126+
{
127+
DijkstraIterator di = i;
128+
di.mCost += arcIt->second.mCost;
129+
di.mTime += arcIt->second.mTime;
130+
131+
if ( ci(di, res[ arcIt->first ] ) )
132+
{
133+
di.mFrontPoint = di.mBackPoint;
134+
di.mBackPoint = arcIt->first;
135+
not_begin.insert( di );
136+
res[ arcIt->first ] = di;
137+
}
138+
}
139+
}
140+
return res;
141+
} // DijkstraFinder::find( const QgsPoint& p )
142+
143+
AdjacencyMatrix DijkstraFinder::find( const QgsPoint& frontPoint, const QgsPoint& backPoint )
144+
{
145+
std::map< QgsPoint , DijkstraFinder::DijkstraIterator, QgsPointCompare> r = find( frontPoint );
146+
std::map< QgsPoint , DijkstraFinder::DijkstraIterator, QgsPointCompare>::iterator it;
147+
if ( r.find( backPoint ) == r.end() )
148+
{
149+
return AdjacencyMatrix();
150+
}
151+
152+
AdjacencyMatrix m;
153+
QgsPoint nextPoint = backPoint;
154+
QgsPoint firstPoint = backPoint;
155+
while ( true )
156+
{
157+
if ( firstPoint != nextPoint )
158+
m[ nextPoint ][ firstPoint ] = mAdjacencyMatrix.find( nextPoint )->second.find( firstPoint )->second;
159+
160+
if ( r[ nextPoint ].mFrontPoint == r[ nextPoint ].mBackPoint )
161+
break;
162+
firstPoint = nextPoint;
163+
nextPoint = r[ nextPoint ].mFrontPoint;
164+
}
165+
return m;
166+
} // DijkstraFinder::find( const QgsPoint& frontPoint, const QgsPoint& backPoint )

‎src/plugins/roadgraph/utils.h

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/***************************************************************************
2+
* Copyright (C) 2009 by Sergey Yakushev *
3+
* yakushevs <at> list.ru *
4+
* *
5+
* This is file define road-graph plugins utils *
6+
* *
7+
* This program is free software; you can redistribute it and/or modify *
8+
* it under the terms of the GNU General Public License as published by *
9+
* the Free Software Foundation; either version 2 of the License, or *
10+
* (at your option) any later version. *
11+
***************************************************************************/
12+
#ifndef ROADGRAPHPLUGIN_UTILS_H
13+
#define ROADGRAPHPLUGIN_UTILS_H
14+
15+
// QT includes
16+
#include <qlist.h>
17+
18+
// Qgis includes
19+
#include "qgspoint.h"
20+
21+
// standart includes
22+
#include <map>
23+
#include <set>
24+
25+
// forward declaration Qgis-classes
26+
27+
/**
28+
@author Sergey Yakushev
29+
*/
30+
31+
/**
32+
* \function infinity()
33+
* \brief return big const double value
34+
*/
35+
double infinity();
36+
37+
/**
38+
* return distance from lenght <x1,y1><x2,y2> to point <x,y> or infinity() value
39+
* in union point
40+
*/
41+
double distance( const QgsPoint& p1, const QgsPoint& p2, const QgsPoint& p, QgsPoint& center );
42+
43+
/**
44+
* \class QgsPointCompare
45+
* \brief This class implement operation "<", ">", "==" and etc. for use QgsPoint type in STL containers
46+
*
47+
*/
48+
class QgsPointCompare
49+
{
50+
public:
51+
bool operator()( const QgsPoint& a, const QgsPoint& b ) const;
52+
};
53+
54+
/**
55+
* \class ArcAttributes
56+
* \brief This class contained arcs attributes.
57+
*/
58+
class ArcAttributes
59+
{
60+
public:
61+
ArcAttributes();
62+
ArcAttributes( double cost, double time, int mFeatureId );
63+
public:
64+
double mCost;
65+
double mTime;
66+
int mFeatureId;
67+
};
68+
69+
typedef std::map< QgsPoint, ArcAttributes, QgsPointCompare > AdjacencyMatrixString;
70+
typedef std::map< QgsPoint, AdjacencyMatrixString, QgsPointCompare > AdjacencyMatrix;
71+
72+
73+
/**
74+
* \class DijkstraFinder
75+
* \brief This class find shortest path via two points using Dijkstra's algorithm
76+
*/
77+
class DijkstraFinder
78+
{
79+
public:
80+
enum OptimizationCriterion { byTime=1, byCost=2 };
81+
private:
82+
class DijkstraIterator {
83+
public:
84+
DijkstraIterator()
85+
{
86+
mCost = infinity();
87+
mTime = infinity();
88+
}
89+
double mCost;
90+
double mTime;
91+
QgsPoint mBackPoint;
92+
QgsPoint mFrontPoint;
93+
};
94+
class CompareDijkstraIterator {
95+
public:
96+
CompareDijkstraIterator( OptimizationCriterion criterion = byCost ) :
97+
mCriterion( criterion )
98+
{ }
99+
bool operator()( const DijkstraIterator& a, const DijkstraIterator& b ) const
100+
{
101+
if ( mCriterion == byCost )
102+
{
103+
return a.mCost < b.mCost;
104+
}
105+
return a.mTime < b.mTime;
106+
}
107+
bool operator==( const CompareDijkstraIterator& a ) const
108+
{
109+
return mCriterion == a.mCriterion;
110+
}
111+
private:
112+
OptimizationCriterion mCriterion;
113+
};
114+
public:
115+
/**
116+
* constructor.
117+
* m is adjacency matrix of graph, criterion is a citerion of shortest path
118+
*/
119+
DijkstraFinder( const AdjacencyMatrix &m, OptimizationCriterion criterion );
120+
121+
/**
122+
* find all shortest path from point frontPoint to all points of graph.
123+
* return tree.
124+
*/
125+
std::map< QgsPoint , DijkstraIterator, QgsPointCompare > find( const QgsPoint& p );
126+
127+
/**
128+
* return shortest path form point frontPoint to endPoint
129+
*/
130+
AdjacencyMatrix find( const QgsPoint& frontPoint, const QgsPoint& endPoint );
131+
132+
private:
133+
const AdjacencyMatrix& mAdjacencyMatrix;
134+
OptimizationCriterion mCriterion;
135+
};
136+
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.