[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.
This commit is contained in:
Alexander Bruy 2016-11-16 13:27:57 +02:00
parent ee710771e7
commit 3c3e17ac90
19 changed files with 197 additions and 111 deletions

View File

@ -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
{

View File

@ -1,4 +1,3 @@
class QgsDistanceArcProperter : QgsArcProperter
{
%TypeHeaderCode

View File

@ -1,7 +1,7 @@
/**
* \ingroup networkanalysis
* \class QgsGraphArc
* \brief This class implement a graph edge
* \brief This class implements a graph edge
*/
class QgsGraphArc
{
@ -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
{

View File

@ -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;
};

View File

@ -6,6 +6,7 @@
SET(QGIS_NETWORK_ANALYSIS_SRCS
qgsgraph.cpp
qgsgraphbuilder.cpp
qgsspeedarcproperter.cpp
qgsdistancearcproperter.cpp
qgslinevectorlayerdirector.cpp
qgsgraphanalyzer.cpp
@ -27,6 +28,7 @@ SET(QGIS_NETWORK_ANALYSIS_HDRS
qgsgraphbuilderintr.h
qgsgraphbuilder.h
qgsarcproperter.h
qgsspeedarcproperter.h
qgsdistancearcproperter.h
qgsgraphdirector.h
qgslinevectorlayerdirector.h

View File

@ -1,5 +1,5 @@
/***************************************************************************
qgsedgeproperter.h
qgsarcproperter.h
--------------------------------------
Date : 2011-04-01
Copyright : (C) 2010 by Yakushev Sergey
@ -13,13 +13,11 @@
* *
***************************************************************************/
#ifndef QGSEDGEPROPERTERH
#define QGSEDGEPROPERTERH
#ifndef QGSARCROPERTER_H
#define QGSARCROPERTER_H
// QT4 includes
#include <QVariant>
// QGIS includes
#include <qgsfeature.h>
#include <qgsfeaturerequest.h>
@ -27,7 +25,7 @@
* \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
{
@ -56,4 +54,4 @@ class ANALYSIS_EXPORT QgsArcProperter
return QVariant();
}
};
#endif //QGSEDGEPROPERTYH
#endif // QGSARCROPERTER_H

View File

@ -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

View File

@ -1,5 +1,5 @@
/***************************************************************************
qgsedgeproperter.h
qgsdistancearcproperter.h
--------------------------------------
Date : 2011-04-01
Copyright : (C) 2010 by Yakushev Sergey
@ -13,8 +13,8 @@
* *
***************************************************************************/
#ifndef QGSEDGEDISTANCEPROPERTERH
#define QGSEDGEDISTANCEPROPERTERH
#ifndef QGSDISTANCEARCPROPERTER_H
#define QGSDISTANCEARCPROPERTER_H
// QT4 includes
#include <QVariant>
@ -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

View File

@ -23,15 +23,13 @@
* \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;
@ -39,7 +37,7 @@ class QgsGraphVertex;
/**
* \ingroup networkanalysis
* \class QgsGraphArc
* \brief This class implement a graph edge
* \brief This class implements a graph edge
*/
class ANALYSIS_EXPORT QgsGraphArc
{
@ -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() {}
@ -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 );
@ -178,4 +176,4 @@ class ANALYSIS_EXPORT QgsGraph
QVector<QgsGraphArc> mGraphArc;
};
#endif //QGSGRAPHH
#endif // QGSGRAPH_H

View File

@ -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"
@ -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;
@ -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 )

View File

@ -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
@ -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

View File

@ -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
@ -17,7 +21,6 @@
#include "qgsgraphbuilder.h"
#include "qgsgraph.h"
// Qgis includes
#include <qgsfeature.h>
#include <qgsgeometry.h>

View File

@ -12,17 +12,14 @@
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSGRAPHBUILDERH
#define QGSGRAPHBUILDERH
#ifndef QGSGRAPHBUILDER_H
#define QGSGRAPHBUILDER_H
#include "qgsgraphbuilderintr.h"
//QT4 includes
//QGIS includes
#include <qgsspatialindex.h>
//forward declarations
class QgsDistanceArea;
class QgsCoordinateTransform;
class QgsGraph;
@ -30,7 +27,7 @@ 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
@ -60,4 +57,5 @@ class ANALYSIS_EXPORT QgsGraphBuilder : public QgsGraphBuilderInterface
QgsGraph *mGraph;
};
#endif //QGSGRAPHBUILDERH
#endif // QGSGRAPHBUILDER_H

View File

@ -1,5 +1,5 @@
/***************************************************************************
qgsgraphbuilder.h
qgsgraphbuilderintr.h
--------------------------------------
Date : 2010-10-22
Copyright : (C) 2010 by Yakushev Sergey
@ -12,24 +12,21 @@
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSGRAPHBUILDERINTERFACE
#define QGSGRAPHBUILDERINTERFACE
//QT4 includes
#ifndef QGSGRAPHBUILDERINTERFACE_H
#define QGSGRAPHBUILDERINTERFACE_H
#include <QVector>
#include <QVariant>
//QGIS includes
#include <qgspoint.h>
#include <qgscoordinatereferencesystem.h>
#include <qgsdistancearea.h>
//forward declarations
/**
* \ingroup networkanalysis
* \class QgsGraphBuilderInterface
* \brief Determine interface for creating a graph. Contains the settings of the graph. QgsGraphBuilder and QgsGraphDirector is a Builder pattern
* \brief Determine interface for creating a graph. Contains the settings of the graph. QgsGraphBuilder and QgsGraphDirector both use a Builder pattern
*/
class ANALYSIS_EXPORT QgsGraphBuilderInterface
{
@ -56,7 +53,7 @@ class ANALYSIS_EXPORT QgsGraphBuilderInterface
virtual ~QgsGraphBuilderInterface()
{ }
//! get destinaltion Crs
//! get destinaltion CRS
QgsCoordinateReferenceSystem destinationCrs() const
{
return mCrs;
@ -120,4 +117,4 @@ class ANALYSIS_EXPORT QgsGraphBuilderInterface
double mTopologyTolerance;
};
#endif //QGSGRAPHBUILDERINTERFACE
#endif // QGSGRAPHBUILDERINTERFACE_H

View File

@ -12,19 +12,17 @@
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSGRAPHDIRECTORH
#define QGSGRAPHDIRECTORH
//QT4 includes
#ifndef QGSGRAPHDIRECTOR_H
#define QGSGRAPHDIRECTOR_H
#include <QObject>
#include <QVector>
#include <QList>
//QGIS includes
#include <qgspoint.h>
#include "qgsarcproperter.h"
//forward declarations
class QgsGraphBuilderInterface;
/**
@ -48,11 +46,8 @@ class ANALYSIS_EXPORT QgsGraphDirector : public QObject
* Make a graph using RgGraphBuilder
*
* @param builder The graph builder
*
* @param additionalPoints Vector of points that must be tied to the graph
*
* @param tiedPoints Vector of tied points
*
* @note if tiedPoints[i]==QgsPoint(0.0,0.0) then tied failed.
*/
virtual void makeGraph( QgsGraphBuilderInterface *builder,
@ -77,4 +72,5 @@ class ANALYSIS_EXPORT QgsGraphDirector : public QObject
protected:
QList<QgsArcProperter*> mProperterList;
};
#endif //QGSGRAPHDIRECTORH
#endif // QGSGRAPHDIRECTOR_H

View File

@ -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. *
***************************************************************************/
qgslinevectorlayerdirector.cpp
--------------------------------------
Date : 2010-10-20
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 qgslinevectorlayerdirector.cpp
@ -17,7 +21,6 @@
#include "qgslinevectorlayerdirector.h"
#include "qgsgraphbuilderintr.h"
// Qgis includes
#include "qgsfeatureiterator.h"
#include <qgsvectorlayer.h>
#include <qgsvectordataprovider.h>
@ -26,7 +29,6 @@
#include <qgsdistancearea.h>
#include <qgswkbtypes.h>
// QT includes
#include <QString>
#include <QtAlgorithms>
@ -393,4 +395,3 @@ void QgsLineVectorLayerDirector::makeGraph( QgsGraphBuilderInterface *builder, c
emit buildProgress( ++step, featureCount );
} // while( vl->nextFeature(feature) )
} // makeGraph( QgsGraphBuilderInterface *builder, const QVector< QgsPoint >& additionalPoints, QVector< QgsPoint >& tiedPoint )

View File

@ -12,17 +12,12 @@
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSLINEVECTORLAYERDIRECTORH
#define QGSLINEVECTORLAYERDIRECTORH
//QT4 includes
#ifndef QGSLINEVECTORLAYERDIRECTOR_H
#define QGSLINEVECTORLAYERDIRECTOR_H
//QGIS includes
// Road-graph plugin includes
#include "qgsgraphdirector.h"
//forward declarations
class QgsGraphBuilderInterface;
class QgsVectorLayer;
@ -80,4 +75,4 @@ class ANALYSIS_EXPORT QgsLineVectorLayerDirector : public QgsGraphDirector
int mDefaultDirection;
};
#endif //QGSLINEVECTORLAYERGRAPHDIRECTORH
#endif // QGSLINEVECTORLAYERDIRECTOR_H

View File

@ -0,0 +1,44 @@
/***************************************************************************
qgsspeedarcproperter.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. *
* *
***************************************************************************/
#include "qgsspeedarcproperter.h"
QgsSpeedArcProperter::QgsSpeedArcProperter( int attributeId, double defaultValue, double toMetricFactor )
{
mAttributeId = attributeId;
mDefaultValue = defaultValue;
mToMetricFactor = toMetricFactor;
}
QVariant QgsSpeedArcProperter::property( double distance, const QgsFeature& f ) const
{
QgsAttributes attrs = f.attributes();
if ( mAttributeId < 0 || mAttributeId >= attrs.count() )
return QVariant( distance / ( mDefaultValue*mToMetricFactor ) );
double val = distance / ( attrs.at( mAttributeId ).toDouble() * mToMetricFactor );
if ( val <= 0.0 )
return QVariant( distance / ( mDefaultValue / mToMetricFactor ) );
return QVariant( val );
}
QgsAttributeList QgsSpeedArcProperter::requiredAttributes() const
{
QgsAttributeList l;
l.push_back( mAttributeId );
return l;
}

View File

@ -0,0 +1,40 @@
/***************************************************************************
qgsspeedarcproperter.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. *
* *
***************************************************************************/
#ifndef QGSSPEEDARCPROPERTER_H
#define QGSSPEEDARCPROPERTER_H
#include <qgsarcproperter.h>
/** \ingroup networkanalysis
* \class QgsSpeedArcProperter
*/
class ANALYSIS_EXPORT QgsSpeedArcProperter : public QgsArcProperter
{
public:
QgsSpeedArcProperter( int attributeId, double defaultValue, double toMetricFactor );
QVariant property( double distance, const QgsFeature& f ) const override;
QgsAttributeList requiredAttributes() const override;
private:
int mAttributeId;
double mDefaultValue;
double mToMetricFactor;
};
#endif // QGSSPEEDARCPROPERTER_H