Skip to content

Commit d999923

Browse files
committedJul 30, 2018
[processing] port shortest path (point to layer) alg to c++
1 parent be3b2c8 commit d999923

File tree

8 files changed

+250
-37
lines changed

8 files changed

+250
-37
lines changed
 

‎python/plugins/processing/algs/qgis/QgisAlgorithmProvider.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@
126126
from .SetVectorStyle import SetVectorStyle
127127
from .SetZValue import SetZValue
128128
from .ShortestPathLayerToPoint import ShortestPathLayerToPoint
129-
from .ShortestPathPointToLayer import ShortestPathPointToLayer
129+
#from .ShortestPathPointToLayer import ShortestPathPointToLayer
130130
#from .ShortestPathPointToPoint import ShortestPathPointToPoint
131131
from .SingleSidedBuffer import SingleSidedBuffer
132132
from .Slope import Slope
@@ -240,7 +240,7 @@ def getAlgs(self):
240240
SetVectorStyle(),
241241
SetZValue(),
242242
ShortestPathLayerToPoint(),
243-
ShortestPathPointToLayer(),
243+
#ShortestPathPointToLayer(),
244244
#ShortestPathPointToPoint(),
245245
SingleSidedBuffer(),
246246
Slope(),

‎python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3066,7 +3066,7 @@ tests:
30663066
start: skip
30673067
end: skip
30683068

3069-
- algorithm: qgis:shortestpathpointtolayer
3069+
- algorithm: native:shortestpathpointtolayer
30703070
name: Shortest path point to layer
30713071
params:
30723072
DEFAULT_DIRECTION: 2

‎src/analysis/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ SET(QGIS_ANALYSIS_SRCS
7777
processing/qgsalgorithmrotate.cpp
7878
processing/qgsalgorithmsaveselectedfeatures.cpp
7979
processing/qgsalgorithmsegmentize.cpp
80+
processing/qgsalgorithmshortestpathpointtolayer.cpp
8081
processing/qgsalgorithmshortestpathpointtopoint.cpp
8182
processing/qgsalgorithmsimplify.cpp
8283
processing/qgsalgorithmsmooth.cpp
@@ -95,6 +96,7 @@ SET(QGIS_ANALYSIS_SRCS
9596
processing/qgsalgorithmvectorize.cpp
9697
processing/qgsalgorithmwedgebuffers.cpp
9798
processing/qgsalgorithmzonalhistogram.cpp
99+
98100
processing/qgsalgorithmnetworkanalysisbase.cpp
99101

100102
processing/qgsnativealgorithms.cpp

‎src/analysis/processing/qgsalgorithmnetworkanalysisbase.cpp

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
// QgsNetworkAnalysisAlgorithmBase
2828
//
2929

30-
3130
QString QgsNetworkAnalysisAlgorithmBase::group() const
3231
{
3332
return QObject::tr( "Network analysis" );
@@ -130,37 +129,38 @@ void QgsNetworkAnalysisAlgorithmBase::loadCommonParams( const QVariantMap &param
130129
mBuilder = qgis::make_unique< QgsGraphBuilder >( mNetwork->sourceCrs(), true, tolerance );
131130
}
132131

133-
//~ QVector< QgsPointXY > QgsNetworkAnalysisAlgorithmBase::loadPoints( QgsFeatureSource *source, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
134-
//~ {
135-
//~ feedback.pushInfo( QObject::tr( "Loading points…" ) );
136-
137-
//~ QVector< QgsPointXY > points;
138-
//~ QgsFeature feat;
139-
//~ int i = 0;
140-
//~ double step = source->featureCount() > 0 ? 100.0 / source->featureCount() : 0;
141-
//~ QgsFeatureIterator features = source->getFeatures( QgsFeatureRequest().setDestinationCrs( mNetwork->sourceCrs(), context.transformContext() ) );
142-
143-
//~ while ( features.nextFeature( feat ) )
144-
//~ {
145-
//~ i++;
146-
//~ if ( feedback->isCanceled() )
147-
//~ {
148-
//~ break;
149-
//~ }
150-
151-
//~ feedback->setProgress( i * step );
152-
//~ if ( !feat.hasGeometry() )
153-
//~ continue;
154-
155-
//~ QgsGeometry geom = feat.geometry();
156-
//~ QgsAbstractGeometry::vertex_iterator it = geom.vertices_begin();
157-
//~ while ( it != geom.vertices_end() )
158-
//~ {
159-
//~ points.push_back( QgsPointXY( it ) )
160-
//~ }
161-
//~ }
162-
163-
//~ return points
164-
//~ }
132+
void QgsNetworkAnalysisAlgorithmBase::loadPoints( QgsFeatureSource *source, QVector< QgsPointXY > &points, QHash< int, QgsAttributes > &attributes, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
133+
{
134+
feedback->pushInfo( QObject::tr( "Loading points…" ) );
135+
136+
QgsFeature feat;
137+
int i = 0;
138+
int pointId = 1;
139+
double step = source->featureCount() > 0 ? 100.0 / source->featureCount() : 0;
140+
QgsFeatureIterator features = source->getFeatures( QgsFeatureRequest().setDestinationCrs( mNetwork->sourceCrs(), context.transformContext() ) );
141+
142+
while ( features.nextFeature( feat ) )
143+
{
144+
i++;
145+
if ( feedback->isCanceled() )
146+
{
147+
break;
148+
}
149+
150+
feedback->setProgress( i * step );
151+
if ( !feat.hasGeometry() )
152+
continue;
153+
154+
QgsGeometry geom = feat.geometry();
155+
QgsAbstractGeometry::vertex_iterator it = geom.vertices_begin();
156+
while ( it != geom.vertices_end() )
157+
{
158+
points.push_back( QgsPointXY( *it ) );
159+
attributes.insert( pointId, feat.attributes() );
160+
it++;
161+
pointId++;
162+
}
163+
}
164+
}
165165

166166
///@endcond

‎src/analysis/processing/qgsalgorithmnetworkanalysisbase.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class QgsNetworkAnalysisAlgorithmBase : public QgsProcessingAlgorithm
5656
/**
5757
* Loads point from the feature source for further processing.
5858
*/
59-
//~ void loadPoints( QgsFeatureSource *source, QgsProcessingContext &context, QgsProcessingFeedback *feedback );
59+
void loadPoints( QgsFeatureSource *source, QVector< QgsPointXY > &points, QHash< int, QgsAttributes > &attributes, QgsProcessingContext &context, QgsProcessingFeedback *feedback );
6060

6161
std::unique_ptr< QgsFeatureSource > mNetwork;
6262
QgsVectorLayerDirector *mDirector = nullptr;
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/***************************************************************************
2+
qgsalgorithmshortestpathpointtolayer.cpp
3+
---------------------
4+
begin : July 2018
5+
copyright : (C) 2018 by Alexander Bruy
6+
email : alexander dot bruy at gmail dot com
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
18+
#include "qgsalgorithmshortestpathpointtolayer.h"
19+
20+
#include "qgsgraphanalyzer.h"
21+
22+
#include "qgsmessagelog.h"
23+
24+
///@cond PRIVATE
25+
26+
QString QgsShortestPathPointToLayerAlgorithm::name() const
27+
{
28+
return QStringLiteral( "shortestpathpointtolayer" );
29+
}
30+
31+
QString QgsShortestPathPointToLayerAlgorithm::displayName() const
32+
{
33+
return QObject::tr( "Shortest path (point to layer)" );
34+
}
35+
36+
QStringList QgsShortestPathPointToLayerAlgorithm::tags() const
37+
{
38+
return QObject::tr( "network,path,shortest,fastest" ).split( ',' );
39+
}
40+
41+
QString QgsShortestPathPointToLayerAlgorithm::shortHelpString() const
42+
{
43+
return QObject::tr( "This algorithm computes optimal (shortest or fastest) route between given start point and multiple end points defined by point vector layer." );
44+
}
45+
46+
QgsShortestPathPointToLayerAlgorithm *QgsShortestPathPointToLayerAlgorithm::createInstance() const
47+
{
48+
return new QgsShortestPathPointToLayerAlgorithm();
49+
}
50+
51+
void QgsShortestPathPointToLayerAlgorithm::initAlgorithm( const QVariantMap & )
52+
{
53+
addCommonParams();
54+
addParameter( new QgsProcessingParameterPoint( QStringLiteral( "START_POINT" ), QObject::tr( "Start point" ) ) );
55+
addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "END_POINTS" ), QObject::tr( "Vector layer with end points" ), QList< int >() << QgsProcessing::TypeVectorPoint ) );
56+
57+
addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Shortest path" ), QgsProcessing::TypeVectorLine ) );
58+
}
59+
60+
QVariantMap QgsShortestPathPointToLayerAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
61+
{
62+
loadCommonParams( parameters, context, feedback );
63+
64+
QgsPointXY startPoint = parameterAsPoint( parameters, QStringLiteral( "START_POINT" ), context, mNetwork->sourceCrs() );
65+
66+
std::unique_ptr< QgsFeatureSource > endPoints( parameterAsSource( parameters, QStringLiteral( "END_POINTS" ), context ) );
67+
if ( !endPoints )
68+
throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "END_POINTS" ) ) );
69+
70+
QgsFields fields = endPoints->fields();
71+
fields.append( QgsField( QStringLiteral( "start" ), QVariant::String ) );
72+
fields.append( QgsField( QStringLiteral( "end" ), QVariant::String ) );
73+
fields.append( QgsField( QStringLiteral( "cost" ), QVariant::Double ) );
74+
75+
QString dest;
76+
std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, fields, QgsWkbTypes::LineString, mNetwork->sourceCrs() ) );
77+
if ( !sink )
78+
throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
79+
80+
QVector< QgsPointXY > points;
81+
points.push_front( startPoint );
82+
QHash< int, QgsAttributes > sourceAttributes;
83+
loadPoints( endPoints.get(), points, sourceAttributes, context, feedback );
84+
85+
feedback->pushInfo( QObject::tr( "Building graph…" ) );
86+
QVector< QgsPointXY > snappedPoints;
87+
mDirector->makeGraph( mBuilder.get(), points, snappedPoints, feedback );
88+
89+
feedback->pushInfo( QObject::tr( "Calculating shortest paths…" ) );
90+
QgsGraph *graph = mBuilder->graph();
91+
int idxStart = graph->findVertex( snappedPoints[0] );
92+
int idxEnd;
93+
94+
QVector< int > tree;
95+
QVector< double > costs;
96+
QgsGraphAnalyzer::dijkstra( graph, idxStart, 0, &tree, &costs );
97+
98+
QVector<QgsPointXY> route;
99+
double cost;
100+
101+
QgsFeature feat;
102+
feat.setFields( fields );
103+
QgsAttributes attributes;
104+
105+
int step = points.size() > 0 ? 100.0 / points.size() : 1;
106+
for ( int i = 1; i < points.size(); i++ )
107+
{
108+
if ( feedback->isCanceled() )
109+
{
110+
break;
111+
}
112+
113+
idxEnd = graph->findVertex( snappedPoints[i] );
114+
if ( tree.at( idxEnd ) == -1 )
115+
{
116+
feedback->reportError( QObject::tr( "There is no route from start point (%1) to end point (%2)." )
117+
.arg( startPoint.toString() )
118+
.arg( points[i].toString() ) );
119+
feat.clearGeometry();
120+
attributes = sourceAttributes.value( i );
121+
attributes.append( QVariant() );
122+
attributes.append( points[i].toString() );
123+
feat.setAttributes( attributes );
124+
sink->addFeature( feat, QgsFeatureSink::FastInsert );
125+
continue;
126+
}
127+
128+
route.clear();
129+
route.push_front( graph->vertex( idxEnd ).point() );
130+
cost = costs.at( idxEnd );
131+
while ( idxEnd != idxStart )
132+
{
133+
idxEnd = graph->edge( tree.at( idxEnd ) ).fromVertex();
134+
route.push_front( graph->vertex( idxEnd ).point() );
135+
}
136+
137+
QgsGeometry geom = QgsGeometry::fromPolylineXY( route );
138+
QgsFeature feat;
139+
feat.setFields( fields );
140+
attributes = sourceAttributes.value( i );
141+
attributes.append( startPoint.toString() );
142+
attributes.append( points[i].toString() );
143+
attributes.append( cost / mMultiplier );
144+
feat.setAttributes( attributes );
145+
feat.setGeometry( geom );
146+
sink->addFeature( feat, QgsFeatureSink::FastInsert );
147+
148+
feedback->setProgress( i * step );
149+
}
150+
151+
QVariantMap outputs;
152+
outputs.insert( QStringLiteral( "OUTPUT" ), dest );
153+
return outputs;
154+
}
155+
156+
///@endcond
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/***************************************************************************
2+
qgsalgorithmshortestpathpointtolayer.h
3+
---------------------
4+
begin : JUly 2018
5+
copyright : (C) 2018 by Alexander Bruy
6+
email : alexander dot bruy at gmail dot com
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
18+
#ifndef QGSALGORITHMSHORTESTPATHPOINTTOLAYER_H
19+
#define QGSALGORITHMSHORTESTPATHPOINTTOLAYER_H
20+
21+
#define SIP_NO_FILE
22+
23+
#include "qgis.h"
24+
#include "qgsalgorithmnetworkanalysisbase.h"
25+
26+
///@cond PRIVATE
27+
28+
/**
29+
* Native shortest path (point to layer) algorithm.
30+
*/
31+
class QgsShortestPathPointToLayerAlgorithm : public QgsNetworkAnalysisAlgorithmBase
32+
{
33+
34+
public:
35+
36+
QgsShortestPathPointToLayerAlgorithm() = default;
37+
void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
38+
QString name() const override;
39+
QString displayName() const override;
40+
QStringList tags() const override;
41+
QString shortHelpString() const override;
42+
QgsShortestPathPointToLayerAlgorithm *createInstance() const override SIP_FACTORY;
43+
44+
protected:
45+
46+
QVariantMap processAlgorithm( const QVariantMap &parameters,
47+
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
48+
49+
};
50+
51+
///@endcond PRIVATE
52+
53+
#endif // QGSALGORITHMSHORTESTPATHPOINTTOLAYER_H

‎src/analysis/processing/qgsnativealgorithms.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
#include "qgsalgorithmrotate.h"
7575
#include "qgsalgorithmsaveselectedfeatures.h"
7676
#include "qgsalgorithmsegmentize.h"
77+
#include "qgsalgorithmshortestpathpointtolayer.h"
7778
#include "qgsalgorithmshortestpathpointtopoint.h"
7879
#include "qgsalgorithmsimplify.h"
7980
#include "qgsalgorithmsmooth.h"
@@ -197,6 +198,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
197198
addAlgorithm( new QgsSegmentizeByMaximumAngleAlgorithm() );
198199
addAlgorithm( new QgsSegmentizeByMaximumDistanceAlgorithm() );
199200
addAlgorithm( new QgsSelectByLocationAlgorithm() );
201+
addAlgorithm( new QgsShortestPathPointToLayerAlgorithm() );
200202
addAlgorithm( new QgsShortestPathPointToPointAlgorithm() );
201203
addAlgorithm( new QgsSimplifyAlgorithm() );
202204
addAlgorithm( new QgsSmoothAlgorithm() );

0 commit comments

Comments
 (0)
Please sign in to comment.