26QString QgsShortestPathPointToLayerAlgorithm::name()
const
28 return QStringLiteral(
"shortestpathpointtolayer" );
31QString QgsShortestPathPointToLayerAlgorithm::displayName()
const
33 return QObject::tr(
"Shortest path (point to layer)" );
36QStringList QgsShortestPathPointToLayerAlgorithm::tags()
const
38 return QObject::tr(
"network,path,shortest,fastest" ).split(
',' );
41QString QgsShortestPathPointToLayerAlgorithm::shortHelpString()
const
43 return QObject::tr(
"This algorithm computes optimal (shortest or fastest) route between given start point and multiple end points defined by point vector layer." );
46QgsShortestPathPointToLayerAlgorithm *QgsShortestPathPointToLayerAlgorithm::createInstance()
const
48 return new QgsShortestPathPointToLayerAlgorithm();
51void QgsShortestPathPointToLayerAlgorithm::initAlgorithm(
const QVariantMap & )
62 loadCommonParams( parameters, context, feedback );
64 const QgsPointXY startPoint = parameterAsPoint( parameters, QStringLiteral(
"START_POINT" ), context, mNetwork->sourceCrs() );
66 std::unique_ptr< QgsFeatureSource > endPoints( parameterAsSource( parameters, QStringLiteral(
"END_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 ) );
80 QVector< QgsPointXY > points;
81 points.push_front( startPoint );
82 QHash< int, QgsAttributes > sourceAttributes;
83 loadPoints( endPoints.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…" ) );
90 std::unique_ptr< QgsGraph > graph( mBuilder->takeGraph() );
91 const int idxStart = graph->findVertex( snappedPoints[0] );
95 QVector< double > costs;
98 QVector<QgsPointXY> route;
105 const double step = points.size() > 0 ? 100.0 / points.size() : 1;
106 for (
int i = 1; i < points.size(); i++ )
113 idxEnd = graph->findVertex( snappedPoints[i] );
114 if ( tree.at( idxEnd ) == -1 )
116 feedback->
reportError( QObject::tr(
"There is no route from start point (%1) to end point (%2)." )
118 points[i].toString() ) );
120 attributes = sourceAttributes.value( i );
121 attributes.append( QVariant() );
122 attributes.append( points[i].toString() );
130 route.push_front( graph->vertex( idxEnd ).point() );
131 cost = costs.at( idxEnd );
132 while ( idxEnd != idxStart )
134 idxEnd = graph->edge( tree.at( idxEnd ) ).fromVertex();
135 route.push_front( graph->vertex( idxEnd ).point() );
141 attributes = sourceAttributes.value( i );
142 attributes.append( startPoint.
toString() );
143 attributes.append( points[i].toString() );
144 attributes.append( cost / mMultiplier );
154 outputs.insert( QStringLiteral(
"OUTPUT" ), dest );
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
@ RegeneratePrimaryKey
This flag indicates, that a primary key field cannot be guaranteed to be unique and the sink should i...
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 clearGeometry()
Removes any geometry associated with the feature.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
bool isCanceled() const
Tells whether the operation has been canceled already.
void setProgress(double progress)
Sets the current progress for the feedback object.
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.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
A feature sink output for processing algorithms.
An input feature source (such as vector layers) parameter for processing algorithms.
A point parameter for processing algorithms.
@ TypeVectorLine
Vector line layers.
@ TypeVectorPoint
Vector point layers.