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 = std::make_unique<QgsProcessingParameterDistance>( u
"POINT_TOLERANCE"_s, QObject::tr(
"Maximum point distance from network" ), QVariant(), u
"INPUT"_s,
true, 0 );
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() );
76 loadCommonParams( parameters, context, feedback );
84 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u
"OUTPUT"_s, context, dest, fields,
Qgis::WkbType::LineString, mNetwork->sourceCrs() ) );
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() );
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];
98 if ( parameters.value( u
"POINT_TOLERANCE"_s ).isValid() )
100 const double pointDistanceThreshold = parameterAsDouble( parameters, u
"POINT_TOLERANCE"_s, context );
102 double distanceStartPointToNetwork = 0;
105 distanceStartPointToNetwork = mBuilder->distanceArea()->measureLine( startPoint, snappedStartPoint );
112 if ( distanceStartPointToNetwork > pointDistanceThreshold )
114 throw QgsProcessingException( QObject::tr(
"Start point is too far from the network layer (%1, maximum permitted is %2)" ).arg( distanceStartPointToNetwork ).arg( pointDistanceThreshold ) );
117 double distanceEndPointToNetwork = 0;
120 distanceEndPointToNetwork = mBuilder->distanceArea()->measureLine( endPoint, snappedEndPoint );
127 if ( distanceEndPointToNetwork > pointDistanceThreshold )
129 throw QgsProcessingException( QObject::tr(
"End point is too far from the network layer (%1, maximum permitted is %2)" ).arg( distanceEndPointToNetwork ).arg( pointDistanceThreshold ) );
133 feedback->
pushInfo( QObject::tr(
"Calculating shortest path…" ) );
134 std::unique_ptr<QgsGraph> graph( mBuilder->takeGraph() );
136 const int idxStart = graph->findVertex( snappedStartPoint );
137 int idxEnd = graph->findVertex( snappedEndPoint );
140 QVector<double> costs;
143 if ( tree.at( idxEnd ) == -1 )
148 QVector<QgsPointXY> route;
149 route.push_front( graph->vertex( idxEnd ).point() );
150 const double cost = costs.at( idxEnd );
151 while ( idxEnd != idxStart )
153 idxEnd = graph->edge( tree.at( idxEnd ) ).fromVertex();
154 route.push_front( graph->vertex( idxEnd ).point() );
157 feedback->
pushInfo( QObject::tr(
"Writing results…" ) );
162 attributes << startPoint.
toString() << endPoint.
toString() << cost / mMultiplier;
171 outputs.insert( u
"OUTPUT"_s, dest );
172 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.
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.