Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[network analysis] expose speed properter in C++ and Python API
This allows users to calculate shortest path using travel time
as optimization criteria in addition to travel distance.
  • Loading branch information
alexbruy committed Nov 21, 2016
1 parent ee71077 commit 3c3e17a
Show file tree
Hide file tree
Showing 19 changed files with 197 additions and 111 deletions.
7 changes: 4 additions & 3 deletions python/analysis/network/qgsarcproperter.sip
Expand Up @@ -2,14 +2,15 @@
// fix to allow compilation with sip 4.7 that for some reason
// doesn't add these includes to the file where the code from
// ConvertToSubClassCode goes.
#include <qgsspeedarcproperter.h>
#include <qgsdistancearcproperter.h>
%End

/**
* \ingroup networkanalysis
* \class QgsEdgeProperter
* \brief QgsEdgeProperter is a strategy pattern.
* You can use it for customize arc property. For example look at QgsDistanceArcProperter or src/plugins/roadgraph/speedproperter.h
* \class QgsArcProperter
* \brief QgsArcProperter is a strategy pattern.
* You can use it for customize arc property. For example look at QgsDistanceArcProperter or QgsSpeedArcProperter
*/
class QgsArcProperter
{
Expand Down
1 change: 0 additions & 1 deletion python/analysis/network/qgsdistancearcproperter.sip
@@ -1,4 +1,3 @@

class QgsDistanceArcProperter : QgsArcProperter
{
%TypeHeaderCode
Expand Down
4 changes: 2 additions & 2 deletions python/analysis/network/qgsgraph.sip
@@ -1,7 +1,7 @@
/**
* \ingroup networkanalysis
* \class QgsGraphArc
* \brief This class implement a graph edge
* \brief This class implements a graph edge
*/
class QgsGraphArc
{
Expand Down Expand Up @@ -40,7 +40,7 @@ typedef QList< int > QgsGraphArcIdList;
/**
* \ingroup networkanalysis
* \class QgsGraphVertex
* \brief This class implement a graph vertex
* \brief This class implements a graph vertex
*/
class QgsGraphVertex
{
Expand Down
13 changes: 13 additions & 0 deletions python/analysis/network/qgsspeedarcproperter.sip
@@ -0,0 +1,13 @@
class QgsSpeedArcProperter : QgsArcProperter
{
%TypeHeaderCode
#include <qgsspeedarcproperter.h>
%End

public:
QgsSpeedArcProperter( int attributeId, double defaultValue, double toMetricFactor );

QVariant property( double distance, const QgsFeature& f ) const;

QgsAttributeList requiredAttributes() const;
};
2 changes: 2 additions & 0 deletions src/analysis/network/CMakeLists.txt
Expand Up @@ -6,6 +6,7 @@
SET(QGIS_NETWORK_ANALYSIS_SRCS
qgsgraph.cpp
qgsgraphbuilder.cpp
qgsspeedarcproperter.cpp
qgsdistancearcproperter.cpp
qgslinevectorlayerdirector.cpp
qgsgraphanalyzer.cpp
Expand All @@ -27,6 +28,7 @@ SET(QGIS_NETWORK_ANALYSIS_HDRS
qgsgraphbuilderintr.h
qgsgraphbuilder.h
qgsarcproperter.h
qgsspeedarcproperter.h
qgsdistancearcproperter.h
qgsgraphdirector.h
qgslinevectorlayerdirector.h
Expand Down
12 changes: 5 additions & 7 deletions src/analysis/network/qgsarcproperter.h
@@ -1,5 +1,5 @@
/***************************************************************************
qgsedgeproperter.h
qgsarcproperter.h
--------------------------------------
Date : 2011-04-01
Copyright : (C) 2010 by Yakushev Sergey
Expand All @@ -13,21 +13,19 @@
* *
***************************************************************************/

#ifndef QGSEDGEPROPERTERH
#define QGSEDGEPROPERTERH
#ifndef QGSARCROPERTER_H
#define QGSARCROPERTER_H

// QT4 includes
#include <QVariant>

// QGIS includes
#include <qgsfeature.h>
#include <qgsfeaturerequest.h>

/**
* \ingroup networkanalysis
* \class QgsArcProperter
* \brief QgsArcProperter is a strategy pattern.
* You can use it for customize arc property. For example look at QgsDistanceArcProperter or src/plugins/roadgraph/speedproperter.h
* You can use it for customize arc property. For example look at QgsDistanceArcProperter and QgsSpeedArcProperter
*/
class ANALYSIS_EXPORT QgsArcProperter
{
Expand Down Expand Up @@ -56,4 +54,4 @@ class ANALYSIS_EXPORT QgsArcProperter
return QVariant();
}
};
#endif //QGSEDGEPROPERTYH
#endif // QGSARCROPERTER_H
23 changes: 13 additions & 10 deletions src/analysis/network/qgsdistancearcproperter.cpp
@@ -1,15 +1,18 @@
/***************************************************************************
* Copyright (C) 2011 by Sergey Yakushev *
* yakushevs <at >list.ru *
* *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
qgsdistancearcproperter.h
--------------------------------------
Date : 2011-04-01
Copyright : (C) 2010 by Yakushev Sergey
Email : YakushevS <at> list.ru
****************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

//QGIS includes
#include "qgsdistancearcproperter.h"

QVariant QgsDistanceArcProperter::property( double distance, const QgsFeature& f ) const
Expand Down
9 changes: 5 additions & 4 deletions src/analysis/network/qgsdistancearcproperter.h
@@ -1,5 +1,5 @@
/***************************************************************************
qgsedgeproperter.h
qgsdistancearcproperter.h
--------------------------------------
Date : 2011-04-01
Copyright : (C) 2010 by Yakushev Sergey
Expand All @@ -13,8 +13,8 @@
* *
***************************************************************************/

#ifndef QGSEDGEDISTANCEPROPERTERH
#define QGSEDGEDISTANCEPROPERTERH
#ifndef QGSDISTANCEARCPROPERTER_H
#define QGSDISTANCEARCPROPERTER_H

// QT4 includes
#include <QVariant>
Expand All @@ -30,4 +30,5 @@ class ANALYSIS_EXPORT QgsDistanceArcProperter : public QgsArcProperter
public:
virtual QVariant property( double distance, const QgsFeature& ) const override;
};
#endif //QGSEDGEDISTANCEPROPERTYH

#endif // QGSDISTANCEARCPROPERTER_H
16 changes: 7 additions & 9 deletions src/analysis/network/qgsgraph.h
Expand Up @@ -23,23 +23,21 @@
* \file qgsgraph.h
*/

#ifndef QGSGRAPHH
#define QGSGRAPHH
#ifndef QGSGRAPH_H
#define QGSGRAPH_H

// QT4 includes
#include <QList>
#include <QVector>
#include <QVariant>

// QGIS includes
#include "qgspoint.h"

class QgsGraphVertex;

/**
* \ingroup networkanalysis
* \class QgsGraphArc
* \brief This class implement a graph edge
* \brief This class implements a graph edge
*/
class ANALYSIS_EXPORT QgsGraphArc
{
Expand Down Expand Up @@ -83,14 +81,14 @@ typedef QList< int > QgsGraphArcIdList;
/**
* \ingroup networkanalysis
* \class QgsGraphVertex
* \brief This class implement a graph vertex
* \brief This class implements a graph vertex
*/
class ANALYSIS_EXPORT QgsGraphVertex
{
public:

/**
* default constructor. It need for QT's container, e.g. QVector
* default constructor. It needed for Qt's container, e.g. QVector
*/
QgsGraphVertex() {}

Expand Down Expand Up @@ -137,7 +135,7 @@ class ANALYSIS_EXPORT QgsGraph
// begin graph constructing methods

/**
* add vertex to a grap
* add vertex to a graph
*/
int addVertex( const QgsPoint& pt );

Expand Down Expand Up @@ -178,4 +176,4 @@ class ANALYSIS_EXPORT QgsGraph
QVector<QgsGraphArc> mGraphArc;
};

#endif //QGSGRAPHH
#endif // QGSGRAPH_H
8 changes: 3 additions & 5 deletions src/analysis/network/qgsgraphanalyzer.cpp
Expand Up @@ -14,15 +14,13 @@
* (at your option) any later version. *
* *
***************************************************************************/
// C++ standard includes

#include <limits>

// QT includes
#include <QMap>
#include <QVector>
#include <QPair>

//QGIS-uncludes
#include "qgsgraph.h"
#include "qgsgraphanalyzer.h"

Expand All @@ -49,7 +47,7 @@ void QgsGraphAnalyzer::dijkstra( const QgsGraph* source, int startPointIdx, int
}

// QMultiMap< cost, vertexIdx > not_begin
// I use it and not create any struct or class.
// I use it and don't create any struct or class
QMultiMap< double, int > not_begin;
QMultiMap< double, int >::iterator it;

Expand Down Expand Up @@ -108,7 +106,7 @@ QgsGraph* QgsGraphAnalyzer::shortestTree( const QgsGraph* source, int startVerte
}
}

// Add arcs to result
// Add arcs to the result
for ( i = 0; i < source->vertexCount(); ++i )
{
if ( tree[ i ] != -1 )
Expand Down
25 changes: 12 additions & 13 deletions src/analysis/network/qgsgraphanalyzer.h
Expand Up @@ -15,17 +15,15 @@
* *
***************************************************************************/

#ifndef QGSGRAPHANALYZERH
#define QGSGRAPHANALYZERH
#ifndef QGSGRAPHANALYZER_H
#define QGSGRAPHANALYZER_H

//QT-includes
#include <QVector>

// forward-declaration
class QgsGraph;

/** \ingroup networkanalysis
* The QGis class provides graph analysis functions
* QGIS class with graph analysis functions
*/

class ANALYSIS_EXPORT QgsGraphAnalyzer
Expand All @@ -34,20 +32,21 @@ class ANALYSIS_EXPORT QgsGraphAnalyzer

/**
* solve shortest path problem using dijkstra algorithm
* @param source The source graph
* @param startVertexIdx index of start vertex
* @param criterionNum index of arc property as optimization criterion
* @param resultTree array represents the shortest path tree. resultTree[ vertexIndex ] == inboundingArcIndex if vertex reacheble and resultTree[ vertexIndex ] == -1 others.
* @param source source graph
* @param startVertexIdx index of the start vertex
* @param criterionNum index of the arc property as optimization criterion
* @param resultTree array represents the shortest path tree. resultTree[ vertexIndex ] == inboundingArcIndex if vertex reachable, otherwise resultTree[ vertexIndex ] == -1
* @param resultCost array of cost paths
*/
static void dijkstra( const QgsGraph* source, int startVertexIdx, int criterionNum, QVector<int>* resultTree = nullptr, QVector<double>* resultCost = nullptr );

/**
* return shortest path tree with root-node in startVertexIdx
* @param source The source graph
* @param startVertexIdx index of start vertex
* @param criterionNum index of edge property as optimization criterion
* @param source source graph
* @param startVertexIdx index of the start vertex
* @param criterionNum index of the edge property as optimization criterion
*/
static QgsGraph* shortestTree( const QgsGraph* source, int startVertexIdx, int criterionNum );
};
#endif //QGSGRAPHANALYZERH

#endif // QGSGRAPHANALYZER_H
23 changes: 13 additions & 10 deletions src/analysis/network/qgsgraphbuilder.cpp
@@ -1,13 +1,17 @@
/***************************************************************************
* Copyright (C) 2010 by Sergey Yakushev *
* yakushevs <at> list.ru *
* *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
qgsgraphbuilder.cpp
--------------------------------------
Date : 2010-10-25
Copyright : (C) 2010 by Yakushev Sergey
Email : YakushevS@list.ru
****************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

/**
* \file qgsgraphbuilder.cpp
Expand All @@ -17,7 +21,6 @@
#include "qgsgraphbuilder.h"
#include "qgsgraph.h"

// Qgis includes
#include <qgsfeature.h>
#include <qgsgeometry.h>

Expand Down
14 changes: 6 additions & 8 deletions src/analysis/network/qgsgraphbuilder.h
Expand Up @@ -12,25 +12,22 @@
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSGRAPHBUILDERH
#define QGSGRAPHBUILDERH

#include "qgsgraphbuilderintr.h"
#ifndef QGSGRAPHBUILDER_H
#define QGSGRAPHBUILDER_H

//QT4 includes
#include "qgsgraphbuilderintr.h"

//QGIS includes
#include <qgsspatialindex.h>

//forward declarations
class QgsDistanceArea;
class QgsCoordinateTransform;
class QgsGraph;

/**
* \ingroup networkanalysis
* \class QgsGraphBuilder
* \brief This class making the QgsGraph object
* \brief This class is used for making the QgsGraph object
*/

class ANALYSIS_EXPORT QgsGraphBuilder : public QgsGraphBuilderInterface
Expand Down Expand Up @@ -60,4 +57,5 @@ class ANALYSIS_EXPORT QgsGraphBuilder : public QgsGraphBuilderInterface

QgsGraph *mGraph;
};
#endif //QGSGRAPHBUILDERH

#endif // QGSGRAPHBUILDER_H

0 comments on commit 3c3e17a

Please sign in to comment.