QGIS API Documentation 3.99.0-Master (357b655ed83)
Loading...
Searching...
No Matches
qgsalgorithmshortestpathpointtopoint.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmshortestpathpointtopoint.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
19
20#include "qgsgraphanalyzer.h"
21
22#include <QString>
23
24using namespace Qt::StringLiterals;
25
27
28QString QgsShortestPathPointToPointAlgorithm::name() const
29{
30 return u"shortestpathpointtopoint"_s;
31}
32
33QString QgsShortestPathPointToPointAlgorithm::displayName() const
34{
35 return QObject::tr( "Shortest path (point to point)" );
36}
37
38QStringList QgsShortestPathPointToPointAlgorithm::tags() const
39{
40 return QObject::tr( "network,path,shortest,fastest" ).split( ',' );
41}
42
43QString QgsShortestPathPointToPointAlgorithm::shortHelpString() const
44{
45 return QObject::tr( "This algorithm computes optimal (shortest or fastest) route between given start and end points." );
46}
47
48QString QgsShortestPathPointToPointAlgorithm::shortDescription() const
49{
50 return QObject::tr( "Computes optimal (shortest or fastest) route between given start and end points." );
51}
52
53QgsShortestPathPointToPointAlgorithm *QgsShortestPathPointToPointAlgorithm::createInstance() const
54{
55 return new QgsShortestPathPointToPointAlgorithm();
56}
57
58void QgsShortestPathPointToPointAlgorithm::initAlgorithm( const QVariantMap & )
59{
60 addCommonParams();
61 addParameter( new QgsProcessingParameterPoint( u"START_POINT"_s, QObject::tr( "Start point" ) ) );
62 addParameter( new QgsProcessingParameterPoint( u"END_POINT"_s, QObject::tr( "End point" ) ) );
63
64 addParameter( new QgsProcessingParameterFeatureSink( u"OUTPUT"_s, QObject::tr( "Shortest path" ), Qgis::ProcessingSourceType::VectorLine ) );
65
66 std::unique_ptr<QgsProcessingParameterNumber> maxEndPointDistanceFromNetwork = std::make_unique<QgsProcessingParameterDistance>( u"POINT_TOLERANCE"_s, QObject::tr( "Maximum point distance from network" ), QVariant(), u"INPUT"_s, true, 0 );
67 maxEndPointDistanceFromNetwork->setFlags( maxEndPointDistanceFromNetwork->flags() | Qgis::ProcessingParameterFlag::Advanced );
68 maxEndPointDistanceFromNetwork->setHelp( QObject::tr( "Specifies an optional limit on the distance from the start and end points to the network layer. If either point is further from the network than this distance an error will be raised." ) );
69 addParameter( maxEndPointDistanceFromNetwork.release() );
70
71 addOutput( new QgsProcessingOutputNumber( u"TRAVEL_COST"_s, QObject::tr( "Travel cost" ) ) );
72}
73
74QVariantMap QgsShortestPathPointToPointAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
75{
76 loadCommonParams( parameters, context, feedback );
77
78 QgsFields fields;
79 fields.append( QgsField( u"start"_s, QMetaType::Type::QString ) );
80 fields.append( QgsField( u"end"_s, QMetaType::Type::QString ) );
81 fields.append( QgsField( u"cost"_s, QMetaType::Type::Double ) );
82
83 QString dest;
84 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u"OUTPUT"_s, context, dest, fields, Qgis::WkbType::LineString, mNetwork->sourceCrs() ) );
85 if ( !sink )
86 throw QgsProcessingException( invalidSinkError( parameters, u"OUTPUT"_s ) );
87
88 const QgsPointXY startPoint = parameterAsPoint( parameters, u"START_POINT"_s, context, mNetwork->sourceCrs() );
89 const QgsPointXY endPoint = parameterAsPoint( parameters, u"END_POINT"_s, context, mNetwork->sourceCrs() );
90
91 feedback->pushInfo( QObject::tr( "Building graph…" ) );
92 QVector<QgsPointXY> snappedPoints;
93 mDirector->makeGraph( mBuilder.get(), { startPoint, endPoint }, snappedPoints, feedback );
94 const QgsPointXY snappedStartPoint = snappedPoints[0];
95 const QgsPointXY snappedEndPoint = snappedPoints[1];
96
97 // check distance for the snapped start/end points
98 if ( parameters.value( u"POINT_TOLERANCE"_s ).isValid() )
99 {
100 const double pointDistanceThreshold = parameterAsDouble( parameters, u"POINT_TOLERANCE"_s, context );
101
102 double distanceStartPointToNetwork = 0;
103 try
104 {
105 distanceStartPointToNetwork = mBuilder->distanceArea()->measureLine( startPoint, snappedStartPoint );
106 }
107 catch ( QgsCsException & )
108 {
109 throw QgsProcessingException( QObject::tr( "An error occurred while calculating length" ) );
110 }
111
112 if ( distanceStartPointToNetwork > pointDistanceThreshold )
113 {
114 throw QgsProcessingException( QObject::tr( "Start point is too far from the network layer (%1, maximum permitted is %2)" ).arg( distanceStartPointToNetwork ).arg( pointDistanceThreshold ) );
115 }
116
117 double distanceEndPointToNetwork = 0;
118 try
119 {
120 distanceEndPointToNetwork = mBuilder->distanceArea()->measureLine( endPoint, snappedEndPoint );
121 }
122 catch ( QgsCsException & )
123 {
124 throw QgsProcessingException( QObject::tr( "An error occurred while calculating length" ) );
125 }
126
127 if ( distanceEndPointToNetwork > pointDistanceThreshold )
128 {
129 throw QgsProcessingException( QObject::tr( "End point is too far from the network layer (%1, maximum permitted is %2)" ).arg( distanceEndPointToNetwork ).arg( pointDistanceThreshold ) );
130 }
131 }
132
133 feedback->pushInfo( QObject::tr( "Calculating shortest path…" ) );
134 std::unique_ptr<QgsGraph> graph( mBuilder->takeGraph() );
135
136 const int idxStart = graph->findVertex( snappedStartPoint );
137 int idxEnd = graph->findVertex( snappedEndPoint );
138
139 QVector<int> tree;
140 QVector<double> costs;
141 QgsGraphAnalyzer::dijkstra( graph.get(), idxStart, 0, &tree, &costs );
142
143 if ( tree.at( idxEnd ) == -1 )
144 {
145 throw QgsProcessingException( QObject::tr( "There is no route from start point to end point." ) );
146 }
147
148 QVector<QgsPointXY> route;
149 route.push_front( graph->vertex( idxEnd ).point() );
150 const double cost = costs.at( idxEnd );
151 while ( idxEnd != idxStart )
152 {
153 idxEnd = graph->edge( tree.at( idxEnd ) ).fromVertex();
154 route.push_front( graph->vertex( idxEnd ).point() );
155 }
156
157 feedback->pushInfo( QObject::tr( "Writing results…" ) );
158 const QgsGeometry geom = QgsGeometry::fromPolylineXY( route );
159 QgsFeature feat;
160 feat.setFields( fields );
161 QgsAttributes attributes;
162 attributes << startPoint.toString() << endPoint.toString() << cost / mMultiplier;
163 feat.setGeometry( geom );
164 feat.setAttributes( attributes );
165 if ( !sink->addFeature( feat, QgsFeatureSink::FastInsert ) )
166 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, u"OUTPUT"_s ) );
167
168 sink->finalize();
169
170 QVariantMap outputs;
171 outputs.insert( u"OUTPUT"_s, dest );
172 outputs.insert( u"TRAVEL_COST"_s, cost / mMultiplier );
173 return outputs;
174}
175
@ VectorLine
Vector line layers.
Definition qgis.h:3606
@ LineString
LineString.
Definition qgis.h:283
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
Definition qgis.h:3834
A vector of attributes.
Custom exception class for Coordinate Reference System related exceptions.
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
void setFields(const QgsFields &fields, bool initAttributes=false)
Assigns a field map with the feature to allow attribute access by attribute name.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:56
Container of fields for a vector layer.
Definition qgsfields.h:46
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
Definition qgsfields.cpp:76
A geometry is the spatial representation of a feature.
static QgsGeometry fromPolylineXY(const QgsPolylineXY &polyline)
Creates a new LineString geometry from a list of QgsPointXY points.
static void dijkstra(const QgsGraph *source, int startVertexIdx, int criterionNum, QVector< int > *resultTree=nullptr, QVector< double > *resultCost=nullptr)
Solve shortest path problem using Dijkstra algorithm.
Represents a 2D point.
Definition qgspointxy.h:62
QString toString(int precision=-1) const
Returns a string representation of the point (x, y) with a preset precision.
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
A numeric output for processing algorithms.
A feature sink output for processing algorithms.
A point parameter for processing algorithms.