26 QString QgsShortestPathLayerToPointAlgorithm::name()
const 28 return QStringLiteral(
"shortestpathlayertopoint" );
31 QString QgsShortestPathLayerToPointAlgorithm::displayName()
const 33 return QObject::tr(
"Shortest path (layer to point)" );
36 QStringList QgsShortestPathLayerToPointAlgorithm::tags()
const 38 return QObject::tr(
"network,path,shortest,fastest" ).split(
',' );
41 QString QgsShortestPathLayerToPointAlgorithm::shortHelpString()
const 43 return QObject::tr(
"This algorithm computes optimal (shortest or fastest) route from multiple start points defined by vector layer and given end point." );
46 QgsShortestPathLayerToPointAlgorithm *QgsShortestPathLayerToPointAlgorithm::createInstance()
const 48 return new QgsShortestPathLayerToPointAlgorithm();
51 void QgsShortestPathLayerToPointAlgorithm::initAlgorithm(
const QVariantMap & )
62 loadCommonParams( parameters, context, feedback );
64 QgsPointXY endPoint = parameterAsPoint( parameters, QStringLiteral(
"END_POINT" ), context, mNetwork->sourceCrs() );
66 std::unique_ptr< QgsFeatureSource > startPoints( parameterAsSource( parameters, QStringLiteral(
"START_POINTS" ), context ) );
71 fields.
append(
QgsField( QStringLiteral(
"start" ), QVariant::String ) );
72 fields.
append(
QgsField( QStringLiteral(
"end" ), QVariant::String ) );
73 fields.
append(
QgsField( QStringLiteral(
"cost" ), QVariant::Double ) );
76 std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, dest, fields,
QgsWkbTypes::LineString, mNetwork->sourceCrs() ) );
80 QVector< QgsPointXY > points;
81 points.push_front( endPoint );
82 QHash< int, QgsAttributes > sourceAttributes;
83 loadPoints( startPoints.get(), points, sourceAttributes, context, feedback );
85 feedback->
pushInfo( QObject::tr(
"Building graph…" ) );
86 QVector< QgsPointXY > snappedPoints;
87 mDirector->makeGraph( mBuilder.get(), points, snappedPoints, feedback );
89 feedback->
pushInfo( QObject::tr(
"Calculating shortest paths…" ) );
91 int idxEnd = graph->
findVertex( snappedPoints[0] );
96 QVector< double > costs;
98 QVector<QgsPointXY> route;
105 int step = points.size() > 0 ? 100.0 / points.size() : 1;
106 for (
int i = 1; i < points.size(); i++ )
113 idxStart = graph->
findVertex( snappedPoints[i] );
116 if ( tree.at( idxEnd ) == -1 )
118 feedback->
reportError( QObject::tr(
"There is no route from start point (%1) to end point (%2)." )
119 .arg( points[i].toString(),
122 attributes = sourceAttributes.value( i );
123 attributes.append( points[i].toString() );
130 route.push_front( graph->
vertex( idxEnd ).
point() );
131 cost = costs.at( idxEnd );
133 while ( currentIdx != idxStart )
135 currentIdx = graph->
edge( tree.at( currentIdx ) ).fromVertex();
136 route.push_front( graph->
vertex( currentIdx ).
point() );
142 attributes = sourceAttributes.value( i );
143 attributes.append( points[i].toString() );
144 attributes.append( endPoint.
toString() );
145 attributes.append( cost / mMultiplier );
154 outputs.insert( QStringLiteral(
"OUTPUT" ), dest );
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.
void setProgress(double progress)
Sets the current progress for the feedback object.
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.
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.
void clearGeometry()
Removes any geometry associated with the feature.
A point parameter for processing algorithms.
const QgsGraphVertex & vertex(int idx) const
Returns vertex at given index.
bool isCanceled() const
Tells whether the operation has been canceled already.
An input feature source (such as vector layers) parameter for processing algorithms.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Contains information about the context in which a processing algorithm is executed.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
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.