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 const QgsPointXY startPoint = parameterAsPoint( parameters, QStringLiteral(
"START_POINT" ), context, mNetwork->sourceCrs() );
74 const 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…" ) );
83 std::unique_ptr< QgsGraph > graph( mBuilder->takeGraph() );
84 const 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 const 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;
117 outputs.insert( QStringLiteral(
"OUTPUT" ), dest );
118 outputs.insert( QStringLiteral(
"TRAVEL_COST" ), cost / mMultiplier );
@ 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, FieldOrigin origin=OriginProvider, int originIndex=-1)
Appends a field. The field must have unique name, otherwise it is rejected (returns false)
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.
@ TypeVectorLine
Vector line layers.