24 QString QgsShortestPathPointToPointAlgorithm::name()
const 26 return QStringLiteral(
"shortestpathpointtopoint" );
29 QString QgsShortestPathPointToPointAlgorithm::displayName()
const 31 return QObject::tr(
"Shortest path (point to point)" );
34 QStringList QgsShortestPathPointToPointAlgorithm::tags()
const 36 return QObject::tr(
"network,path,shortest,fastest" ).split(
',' );
39 QString QgsShortestPathPointToPointAlgorithm::shortHelpString()
const 41 return QObject::tr(
"This algorithm computes optimal (shortest or fastest) route between given start and end points." );
44 QgsShortestPathPointToPointAlgorithm *QgsShortestPathPointToPointAlgorithm::createInstance()
const 46 return new QgsShortestPathPointToPointAlgorithm();
49 void QgsShortestPathPointToPointAlgorithm::initAlgorithm(
const QVariantMap & )
61 loadCommonParams( parameters, context, feedback );
64 fields.
append(
QgsField( QStringLiteral(
"start" ), QVariant::String ) );
65 fields.
append(
QgsField( QStringLiteral(
"end" ), QVariant::String ) );
66 fields.
append(
QgsField( QStringLiteral(
"cost" ), QVariant::Double ) );
69 std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, dest, fields,
QgsWkbTypes::LineString, mNetwork->sourceCrs() ) );
73 QgsPointXY startPoint = parameterAsPoint( parameters, QStringLiteral(
"START_POINT" ), context, mNetwork->sourceCrs() );
74 QgsPointXY endPoint = parameterAsPoint( parameters, QStringLiteral(
"END_POINT" ), context, mNetwork->sourceCrs() );
76 feedback->
pushInfo( QObject::tr(
"Building graph…" ) );
77 QVector< QgsPointXY > points;
78 points << startPoint << endPoint;
79 QVector< QgsPointXY > snappedPoints;
80 mDirector->makeGraph( mBuilder.get(), points, snappedPoints, feedback );
82 feedback->
pushInfo( QObject::tr(
"Calculating shortest path…" ) );
84 int idxStart = graph->
findVertex( snappedPoints[0] );
85 int idxEnd = graph->
findVertex( snappedPoints[1] );
88 QVector< double > costs;
91 if ( tree.at( idxEnd ) == -1 )
96 QVector<QgsPointXY> route;
97 route.push_front( graph->
vertex( idxEnd ).
point() );
98 double cost = costs.at( idxEnd );
99 while ( idxEnd != idxStart )
101 idxEnd = graph->
edge( tree.at( idxEnd ) ).fromVertex();
102 route.push_front( graph->
vertex( idxEnd ).
point() );
105 feedback->
pushInfo( QObject::tr(
"Writing results…" ) );
110 attributes << startPoint.
toString() << endPoint.toString() << cost / mMultiplier;
116 outputs.insert( QStringLiteral(
"OUTPUT" ), dest );
117 outputs.insert( QStringLiteral(
"TRAVEL_COST" ), cost / mMultiplier );
const QgsGraphEdge & edge(int idx) const
Returns edge at given index.
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
static QgsGeometry fromPolylineXY(const QgsPolylineXY &polyline)
Creates a new LineString geometry from a list of QgsPointXY points.
Base class for providing feedback from a processing algorithm.
void setFields(const QgsFields &fields, bool initAttributes=false)
Assign a field map with the feature to allow attribute access by attribute name.
A class to represent a 2D point.
Container of fields for a vector layer.
QgsPointXY point() const
Returns point associated with graph vertex.
A geometry is the spatial representation of a feature.
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
QString toString(int precision=-1) const
Returns a string representation of the point (x, y) with a preset precision.
A numeric output for processing algorithms.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
A feature sink output for processing algorithms.
Custom exception class for processing related exceptions.
bool append(const QgsField &field, FieldOrigin origin=OriginProvider, int originIndex=-1)
Appends a field. The field must have unique name, otherwise it is rejected (returns false) ...
int findVertex(const QgsPointXY &pt) const
Find vertex by associated point.
Encapsulate a field in an attribute table or data source.
Mathematical graph representation.
A point parameter for processing algorithms.
const QgsGraphVertex & vertex(int idx) const
Returns vertex at given index.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Contains information about the context in which a processing algorithm is executed.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
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.