25using namespace Qt::StringLiterals;
29QString QgsShortestPathLayerToPointAlgorithm::name()
const
31 return u
"shortestpathlayertopoint"_s;
34QString QgsShortestPathLayerToPointAlgorithm::displayName()
const
36 return QObject::tr(
"Shortest path (layer to point)" );
39QStringList QgsShortestPathLayerToPointAlgorithm::tags()
const
41 return QObject::tr(
"network,path,shortest,fastest" ).split(
',' );
44QString QgsShortestPathLayerToPointAlgorithm::shortHelpString()
const
47 "This algorithm computes optimal (shortest or fastest) routes "
48 "from multiple start points defined by a vector layer and a given end point."
52QString QgsShortestPathLayerToPointAlgorithm::shortDescription()
const
55 "Computes optimal (shortest or fastest) routes "
56 "from multiple start points defined by a vector layer and a given end point."
60QgsShortestPathLayerToPointAlgorithm *QgsShortestPathLayerToPointAlgorithm::createInstance()
const
62 return new QgsShortestPathLayerToPointAlgorithm();
65void QgsShortestPathLayerToPointAlgorithm::initAlgorithm(
const QVariantMap & )
71 std::unique_ptr<QgsProcessingParameterNumber> maxEndPointDistanceFromNetwork
72 = std::make_unique<QgsProcessingParameterDistance>( u
"POINT_TOLERANCE"_s, QObject::tr(
"Maximum point distance from network" ), QVariant(), u
"INPUT"_s,
true, 0 );
74 maxEndPointDistanceFromNetwork->setHelp(
76 "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 "
77 "non-routable. If the end point is further from the network than this distance an error will be raised."
80 addParameter( maxEndPointDistanceFromNetwork.release() );
84 auto outputNonRoutable = std::make_unique<QgsProcessingParameterFeatureSink>( u
"OUTPUT_NON_ROUTABLE"_s, QObject::tr(
"Non-routable features" ),
Qgis::ProcessingSourceType::VectorPoint, QVariant(),
true );
85 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)." ) );
86 outputNonRoutable->setCreateByDefault(
false );
87 addParameter( outputNonRoutable.release() );
92 loadCommonParams( parameters, context, feedback );
94 const QgsPointXY endPoint = parameterAsPoint( parameters, u
"END_POINT"_s, context, mNetwork->sourceCrs() );
96 std::unique_ptr<QgsFeatureSource> startPoints( parameterAsSource( parameters, u
"START_POINTS"_s, context ) );
101 newFields.
append(
QgsField( u
"start"_s, QMetaType::Type::QString ) );
102 newFields.
append(
QgsField( u
"end"_s, QMetaType::Type::QString ) );
103 newFields.
append(
QgsField( u
"cost"_s, QMetaType::Type::Double ) );
107 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u
"OUTPUT"_s, context, dest, fields,
Qgis::WkbType::LineString, mNetwork->sourceCrs() ) );
111 QString nonRoutableSinkId;
112 std::unique_ptr<QgsFeatureSink> nonRoutableSink( parameterAsSink( parameters, u
"OUTPUT_NON_ROUTABLE"_s, context, nonRoutableSinkId, startPoints->fields(),
Qgis::WkbType::Point, mNetwork->sourceCrs() ) );
114 const double pointDistanceThreshold = parameters.value( u
"POINT_TOLERANCE"_s ).isValid() ? parameterAsDouble( parameters, u
"POINT_TOLERANCE"_s, context ) : -1;
116 QVector<QgsPointXY> points;
117 points.push_front( endPoint );
118 QHash<int, QgsAttributes> sourceAttributes;
119 loadPoints( startPoints.get(), &points, &sourceAttributes, context, feedback,
nullptr );
121 feedback->
pushInfo( QObject::tr(
"Building graph…" ) );
122 QVector<QgsPointXY> snappedPoints;
123 mDirector->makeGraph( mBuilder.get(), points, snappedPoints, feedback );
125 const QgsPointXY snappedEndPoint = snappedPoints[0];
127 if ( pointDistanceThreshold >= 0 )
129 double distanceEndPointToNetwork = 0;
132 distanceEndPointToNetwork = mBuilder->distanceArea()->measureLine( endPoint, snappedEndPoint );
139 if ( distanceEndPointToNetwork > pointDistanceThreshold )
141 throw QgsProcessingException( QObject::tr(
"End point is too far from the network layer (%1, maximum permitted is %2)" ).arg( distanceEndPointToNetwork ).arg( pointDistanceThreshold ) );
145 feedback->
pushInfo( QObject::tr(
"Calculating shortest paths…" ) );
146 std::unique_ptr<QgsGraph> graph( mBuilder->takeGraph() );
147 const int idxEnd = graph->findVertex( snappedEndPoint );
152 QVector<double> costs;
154 QVector<QgsPointXY> route;
161 const double step = points.size() > 0 ? 100.0 / points.size() : 1;
162 for (
int i = 1; i < points.size(); i++ )
169 const QgsPointXY snappedPoint = snappedPoints.at( i );
170 const QgsPointXY originalPoint = points.at( i );
172 if ( pointDistanceThreshold >= 0 )
174 double distancePointToNetwork = 0;
177 distancePointToNetwork = mBuilder->distanceArea()->measureLine( originalPoint, snappedPoint );
184 if ( distancePointToNetwork > pointDistanceThreshold )
186 feedback->
pushWarning( QObject::tr(
"Point is too far from the network layer (%1, maximum permitted is %2)" ).arg( distancePointToNetwork ).arg( pointDistanceThreshold ) );
187 if ( nonRoutableSink )
190 attributes = sourceAttributes.value( i );
193 throw QgsProcessingException( writeFeatureError( nonRoutableSink.get(), parameters, u
"OUTPUT_NON_ROUTABLE"_s ) );
201 idxStart = graph->findVertex( snappedPoint );
204 if ( tree.at( idxEnd ) == -1 )
206 feedback->
reportError( QObject::tr(
"There is no route from start point (%1) to end point (%2)." ).arg( originalPoint.
toString(), endPoint.
toString() ) );
208 attributes = sourceAttributes.value( i );
209 attributes.append( originalPoint.
toString() );
217 route.push_front( graph->vertex( idxEnd ).point() );
218 cost = costs.at( idxEnd );
220 while ( currentIdx != idxStart )
222 currentIdx = graph->edge( tree.at( currentIdx ) ).fromVertex();
223 route.push_front( graph->vertex( currentIdx ).point() );
229 attributes = sourceAttributes.value( i );
230 attributes.append( originalPoint.
toString() );
231 attributes.append( endPoint.
toString() );
232 attributes.append( cost / mMultiplier );
245 outputs.insert( u
"OUTPUT"_s, dest );
246 if ( nonRoutableSink )
248 nonRoutableSink->finalize();
249 outputs.insert( u
"OUTPUT_NON_ROUTABLE"_s, 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.
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.
static QgsFields combineFields(const QgsFields &fieldsA, const QgsFields &fieldsB, const QString &fieldsBPrefix=QString())
Combines two field lists, avoiding duplicate field names (in a case-insensitive manner).