roadgraphplugin.diff

plugin source code - Sergey Yakushev, 2011-01-17 04:29 AM

Download (101 KB)

View differences:

src/plugins/roadgraph/utils.cpp (revision 0)
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/shortestpathwidget.h (revision 0)
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/graphdirector.h (revision 0)
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
src/plugins/roadgraph/units.cpp (revision 0)
1
/***************************************************************************
2
 *   Copyright (C) 2009 by Sergey Yakushev                                 *
3
 *   [email protected]                                                     *
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/linevectorlayerdirector.cpp (revision 0)
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 )
src/plugins/roadgraph/settings.h (revision 0)
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/utils.h (revision 0)
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
src/plugins/roadgraph/units.h (revision 0)
1
/***************************************************************************
2
 *   Copyright (C) 2010 by Sergey Yakushev                                 *
3
 *   [email protected]                                                     *
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/linevectorlayerdirector.h (revision 0)
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
src/plugins/roadgraph/exportdlg.cpp (revision 0)
1
/***************************************************************************
2
 *   Copyright (C) 2010 by Sergey Yakushev                                 *
3
 *   [email protected]                                                     *
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/roadgraph.qrc (revision 0)
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>
src/plugins/roadgraph/linevectorlayerwidget.cpp (revision 0)
1
/***************************************************************************
2
 *   Copyright (C) 2010 by Sergey Yakushev                                 *
3
 *   [email protected]                                                     *
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()
src/plugins/roadgraph/graphbuilder.h (revision 0)
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
... This diff was truncated because it exceeds the maximum size that can be displayed.