24QString QgsShortestPathPointToPointAlgorithm::name()
const
26 return QStringLiteral(
"shortestpathpointtopoint" );
29QString QgsShortestPathPointToPointAlgorithm::displayName()
const
31 return QObject::tr(
"Shortest path (point to point)" );
34QStringList QgsShortestPathPointToPointAlgorithm::tags()
const
36 return QObject::tr(
"network,path,shortest,fastest" ).split(
',' );
39QString QgsShortestPathPointToPointAlgorithm::shortHelpString()
const
41 return QObject::tr(
"This algorithm computes optimal (shortest or fastest) route between given start and end points." );
44QgsShortestPathPointToPointAlgorithm *QgsShortestPathPointToPointAlgorithm::createInstance()
const
46 return new QgsShortestPathPointToPointAlgorithm();
49void QgsShortestPathPointToPointAlgorithm::initAlgorithm(
const QVariantMap & )
57 std::unique_ptr<QgsProcessingParameterNumber> maxEndPointDistanceFromNetwork = std::make_unique<QgsProcessingParameterDistance>( QStringLiteral(
"POINT_TOLERANCE" ), QObject::tr(
"Maximum point distance from network" ), QVariant(), QStringLiteral(
"INPUT" ),
true, 0 );
59 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." ) );
60 addParameter( maxEndPointDistanceFromNetwork.release() );
67 loadCommonParams( parameters, context, feedback );
70 fields.
append(
QgsField( QStringLiteral(
"start" ), QMetaType::Type::QString ) );
71 fields.
append(
QgsField( QStringLiteral(
"end" ), QMetaType::Type::QString ) );
72 fields.
append(
QgsField( QStringLiteral(
"cost" ), QMetaType::Type::Double ) );
75 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, dest, fields,
Qgis::WkbType::LineString, mNetwork->sourceCrs() ) );
79 const QgsPointXY startPoint = parameterAsPoint( parameters, QStringLiteral(
"START_POINT" ), context, mNetwork->sourceCrs() );
80 const QgsPointXY endPoint = parameterAsPoint( parameters, QStringLiteral(
"END_POINT" ), context, mNetwork->sourceCrs() );
82 feedback->
pushInfo( QObject::tr(
"Building graph…" ) );
83 QVector<QgsPointXY> snappedPoints;
84 mDirector->makeGraph( mBuilder.get(), { startPoint, endPoint }, snappedPoints, feedback );
85 const QgsPointXY snappedStartPoint = snappedPoints[0];
86 const QgsPointXY snappedEndPoint = snappedPoints[1];
89 if ( parameters.value( QStringLiteral(
"POINT_TOLERANCE" ) ).isValid() )
91 const double pointDistanceThreshold = parameterAsDouble( parameters, QStringLiteral(
"POINT_TOLERANCE" ), context );
93 double distanceStartPointToNetwork = 0;
96 distanceStartPointToNetwork = mBuilder->distanceArea()->measureLine( startPoint, snappedStartPoint );
103 if ( distanceStartPointToNetwork > pointDistanceThreshold )
105 throw QgsProcessingException( QObject::tr(
"Start point is too far from the network layer (%1, maximum permitted is %2)" ).arg( distanceStartPointToNetwork ).arg( pointDistanceThreshold ) );
108 double distanceEndPointToNetwork = 0;
111 distanceEndPointToNetwork = mBuilder->distanceArea()->measureLine( endPoint, snappedEndPoint );
118 if ( distanceEndPointToNetwork > pointDistanceThreshold )
120 throw QgsProcessingException( QObject::tr(
"End point is too far from the network layer (%1, maximum permitted is %2)" ).arg( distanceEndPointToNetwork ).arg( pointDistanceThreshold ) );
124 feedback->
pushInfo( QObject::tr(
"Calculating shortest path…" ) );
125 std::unique_ptr<QgsGraph> graph( mBuilder->takeGraph() );
127 const int idxStart = graph->findVertex( snappedStartPoint );
128 int idxEnd = graph->findVertex( snappedEndPoint );
131 QVector<double> costs;
134 if ( tree.at( idxEnd ) == -1 )
139 QVector<QgsPointXY> route;
140 route.push_front( graph->vertex( idxEnd ).point() );
141 const double cost = costs.at( idxEnd );
142 while ( idxEnd != idxStart )
144 idxEnd = graph->edge( tree.at( idxEnd ) ).fromVertex();
145 route.push_front( graph->vertex( idxEnd ).point() );
148 feedback->
pushInfo( QObject::tr(
"Writing results…" ) );
153 attributes << startPoint.
toString() << endPoint.
toString() << cost / mMultiplier;
162 outputs.insert( QStringLiteral(
"OUTPUT" ), dest );
163 outputs.insert( QStringLiteral(
"TRAVEL_COST" ), 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.
A class to represent a 2D point.
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.