24using namespace Qt::StringLiterals;
28QString QgsShortestPathPointToPointAlgorithm::name()
const
30 return u
"shortestpathpointtopoint"_s;
33QString QgsShortestPathPointToPointAlgorithm::displayName()
const
35 return QObject::tr(
"Shortest path (point to point)" );
38QStringList QgsShortestPathPointToPointAlgorithm::tags()
const
40 return QObject::tr(
"network,path,shortest,fastest" ).split(
',' );
43QString QgsShortestPathPointToPointAlgorithm::shortHelpString()
const
45 return QObject::tr(
"This algorithm computes optimal (shortest or fastest) route between given start and end points." );
48QString QgsShortestPathPointToPointAlgorithm::shortDescription()
const
50 return QObject::tr(
"Computes optimal (shortest or fastest) route between given start and end points." );
53QgsShortestPathPointToPointAlgorithm *QgsShortestPathPointToPointAlgorithm::createInstance()
const
55 return new QgsShortestPathPointToPointAlgorithm();
58void QgsShortestPathPointToPointAlgorithm::initAlgorithm(
const QVariantMap & )
66 std::unique_ptr<QgsProcessingParameterNumber> maxEndPointDistanceFromNetwork
67 = std::make_unique<QgsProcessingParameterDistance>( u
"POINT_TOLERANCE"_s, QObject::tr(
"Maximum point distance from network" ), QVariant(), u
"INPUT"_s,
true, 0 );
69 maxEndPointDistanceFromNetwork->setHelp(
70 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." )
72 addParameter( maxEndPointDistanceFromNetwork.release() );
79 QGS_MARK_ALGORITHM_SOURCE
81 loadCommonParams( parameters, context, feedback );
89 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u
"OUTPUT"_s, context, dest, fields,
Qgis::WkbType::LineString, mNetwork->sourceCrs() ) );
93 const QgsPointXY startPoint = parameterAsPoint( parameters, u
"START_POINT"_s, context, mNetwork->sourceCrs() );
94 const QgsPointXY endPoint = parameterAsPoint( parameters, u
"END_POINT"_s, context, mNetwork->sourceCrs() );
96 feedback->
pushInfo( QObject::tr(
"Building graph…" ) );
97 QVector<QgsPointXY> snappedPoints;
98 mDirector->makeGraph( mBuilder.get(), { startPoint, endPoint }, snappedPoints, feedback );
99 const QgsPointXY snappedStartPoint = snappedPoints[0];
100 const QgsPointXY snappedEndPoint = snappedPoints[1];
103 if ( parameters.value( u
"POINT_TOLERANCE"_s ).isValid() )
105 const double pointDistanceThreshold = parameterAsDouble( parameters, u
"POINT_TOLERANCE"_s, context );
107 double distanceStartPointToNetwork = 0;
110 distanceStartPointToNetwork = mBuilder->distanceArea()->measureLine( startPoint, snappedStartPoint );
117 if ( distanceStartPointToNetwork > pointDistanceThreshold )
119 throw QgsProcessingException( QObject::tr(
"Start point is too far from the network layer (%1, maximum permitted is %2)" ).arg( distanceStartPointToNetwork ).arg( pointDistanceThreshold ) );
122 double distanceEndPointToNetwork = 0;
125 distanceEndPointToNetwork = mBuilder->distanceArea()->measureLine( endPoint, snappedEndPoint );
132 if ( distanceEndPointToNetwork > pointDistanceThreshold )
134 throw QgsProcessingException( QObject::tr(
"End point is too far from the network layer (%1, maximum permitted is %2)" ).arg( distanceEndPointToNetwork ).arg( pointDistanceThreshold ) );
138 feedback->
pushInfo( QObject::tr(
"Calculating shortest path…" ) );
139 std::unique_ptr<QgsGraph> graph( mBuilder->takeGraph() );
141 const int idxStart = graph->findVertex( snappedStartPoint );
142 int idxEnd = graph->findVertex( snappedEndPoint );
145 QVector<double> costs;
148 if ( tree.at( idxEnd ) == -1 )
153 QVector<QgsPointXY> route;
154 route.push_front( graph->vertex( idxEnd ).point() );
155 const double cost = costs.at( idxEnd );
156 while ( idxEnd != idxStart )
158 idxEnd = graph->edge( tree.at( idxEnd ) ).fromVertex();
159 route.push_front( graph->vertex( idxEnd ).point() );
162 feedback->
pushInfo( QObject::tr(
"Writing results…" ) );
167 attributes << startPoint.
toString() << endPoint.
toString() << cost / mMultiplier;
179 outputs.insert( u
"OUTPUT"_s, dest );
180 outputs.insert( u
"TRAVEL_COST"_s, cost / mMultiplier );
@ VectorLine
Vector line layers.
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
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...
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.
Container of fields for a vector layer.
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
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.
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.
void featureAddedToSink(const QString &output)
Reports that a feature was added to the the sink associated with the specified algorithm output.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
void featureSinkFinalized(const QString &output)
Reports that a feature sink has been finalized.
A numeric output for processing algorithms.
A feature sink output for processing algorithms.
A point parameter for processing algorithms.