26QString QgsShortestPathLayerToPointAlgorithm::name()
const
28 return QStringLiteral(
"shortestpathlayertopoint" );
31QString QgsShortestPathLayerToPointAlgorithm::displayName()
const
33 return QObject::tr(
"Shortest path (layer to point)" );
36QStringList QgsShortestPathLayerToPointAlgorithm::tags()
const
38 return QObject::tr(
"network,path,shortest,fastest" ).split(
',' );
41QString 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." );
46QgsShortestPathLayerToPointAlgorithm *QgsShortestPathLayerToPointAlgorithm::createInstance()
const
48 return new QgsShortestPathLayerToPointAlgorithm();
51void QgsShortestPathLayerToPointAlgorithm::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 the start feature is further from the network than this distance it will be treated as non-routable. If the end point is further from the network than this distance an error will be raised." ) );
60 addParameter( maxEndPointDistanceFromNetwork.release() );
64 std::unique_ptr< QgsProcessingParameterFeatureSink > outputNonRoutable = std::make_unique< QgsProcessingParameterFeatureSink >( QStringLiteral(
"OUTPUT_NON_ROUTABLE" ), QObject::tr(
"Non-routable features" ),
66 outputNonRoutable->setHelp( QObject::tr(
"An optional output which will be used to store any input features which could not be routed (e.g. those which are too far from the network layer)." ) );
67 outputNonRoutable->setCreateByDefault(
false );
68 addParameter( outputNonRoutable.release() );
73 loadCommonParams( parameters, context, feedback );
75 const QgsPointXY endPoint = parameterAsPoint( parameters, QStringLiteral(
"END_POINT" ), context, mNetwork->sourceCrs() );
77 std::unique_ptr< QgsFeatureSource > startPoints( parameterAsSource( parameters, QStringLiteral(
"START_POINTS" ), context ) );
82 fields.
append(
QgsField( QStringLiteral(
"start" ), QMetaType::Type::QString ) );
83 fields.
append(
QgsField( QStringLiteral(
"end" ), QMetaType::Type::QString ) );
84 fields.
append(
QgsField( QStringLiteral(
"cost" ), QMetaType::Type::Double ) );
87 std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, dest, fields,
Qgis::WkbType::LineString, mNetwork->sourceCrs() ) );
91 QString nonRoutableSinkId;
92 std::unique_ptr< QgsFeatureSink > nonRoutableSink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT_NON_ROUTABLE" ), context, nonRoutableSinkId, startPoints->fields(),
95 const double pointDistanceThreshold = parameters.value( QStringLiteral(
"POINT_TOLERANCE" ) ).isValid() ? parameterAsDouble( parameters, QStringLiteral(
"POINT_TOLERANCE" ), context ) : -1;
97 QVector< QgsPointXY > points;
98 points.push_front( endPoint );
99 QHash< int, QgsAttributes > sourceAttributes;
100 loadPoints( startPoints.get(), points, sourceAttributes, context, feedback );
102 feedback->
pushInfo( QObject::tr(
"Building graph…" ) );
103 QVector< QgsPointXY > snappedPoints;
104 mDirector->makeGraph( mBuilder.get(), points, snappedPoints, feedback );
106 const QgsPointXY snappedEndPoint = snappedPoints[0];
108 if ( pointDistanceThreshold >= 0 )
110 double distanceEndPointToNetwork = 0;
113 distanceEndPointToNetwork = mBuilder->distanceArea()->measureLine( endPoint, snappedEndPoint );
120 if ( distanceEndPointToNetwork > pointDistanceThreshold )
122 throw QgsProcessingException( QObject::tr(
"End point is too far from the network layer (%1, maximum permitted is %2)" ).arg( distanceEndPointToNetwork ).arg( pointDistanceThreshold ) );
126 feedback->
pushInfo( QObject::tr(
"Calculating shortest paths…" ) );
127 std::unique_ptr< QgsGraph > graph( mBuilder->takeGraph() );
128 const int idxEnd = graph->findVertex( snappedEndPoint );
133 QVector< double > costs;
135 QVector<QgsPointXY> route;
142 const double step = points.size() > 0 ? 100.0 / points.size() : 1;
143 for (
int i = 1; i < points.size(); i++ )
150 const QgsPointXY snappedPoint = snappedPoints.at( i );
151 const QgsPointXY originalPoint = points.at( i );
153 if ( pointDistanceThreshold >= 0 )
155 double distancePointToNetwork = 0;
158 distancePointToNetwork = mBuilder->distanceArea()->measureLine( originalPoint, snappedPoint );
165 if ( distancePointToNetwork > pointDistanceThreshold )
167 feedback->
pushWarning( QObject::tr(
"Point is too far from the network layer (%1, maximum permitted is %2)" ).arg( distancePointToNetwork ).arg( pointDistanceThreshold ) );
168 if ( nonRoutableSink )
171 attributes = sourceAttributes.value( i );
174 throw QgsProcessingException( writeFeatureError( nonRoutableSink.get(), parameters, QStringLiteral(
"OUTPUT_NON_ROUTABLE" ) ) );
182 idxStart = graph->findVertex( snappedPoint );
185 if ( tree.at( idxEnd ) == -1 )
187 feedback->
reportError( QObject::tr(
"There is no route from start point (%1) to end point (%2)." )
191 attributes = sourceAttributes.value( i );
192 attributes.append( originalPoint.
toString() );
200 route.push_front( graph->vertex( idxEnd ).point() );
201 cost = costs.at( idxEnd );
203 while ( currentIdx != idxStart )
205 currentIdx = graph->edge( tree.at( currentIdx ) ).fromVertex();
206 route.push_front( graph->vertex( currentIdx ).point() );
212 attributes = sourceAttributes.value( i );
213 attributes.append( originalPoint.
toString() );
214 attributes.append( endPoint.
toString() );
215 attributes.append( cost / mMultiplier );
225 outputs.insert( QStringLiteral(
"OUTPUT" ), dest );
226 if ( nonRoutableSink )
228 outputs.insert( QStringLiteral(
"OUTPUT_NON_ROUTABLE" ), nonRoutableSinkId );
@ VectorPoint
Vector point layers.
@ 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 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, 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 QgsGeometry fromPointXY(const QgsPointXY &point)
Creates a new geometry from a QgsPointXY object.
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 pushWarning(const QString &warning)
Pushes a warning 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.