24QString QgsShortestPathPointToPointAlgorithm::name()
 const 
   26  return QStringLiteral( 
"shortestpathpointtopoint" );
 
   29QString QgsShortestPathPointToPointAlgorithm::displayName()
 const 
   31  return QObject::tr( 
"Shortest path (point to point)" );
 
   34QStringList QgsShortestPathPointToPointAlgorithm::tags()
 const 
   36  return QObject::tr( 
"network,path,shortest,fastest" ).split( 
',' );
 
   39QString QgsShortestPathPointToPointAlgorithm::shortHelpString()
 const 
   41  return QObject::tr( 
"This algorithm computes optimal (shortest or fastest) route between given start and end points." );
 
   44QString QgsShortestPathPointToPointAlgorithm::shortDescription()
 const 
   46  return QObject::tr( 
"Computes optimal (shortest or fastest) route between given start and end points." );
 
   49QgsShortestPathPointToPointAlgorithm *QgsShortestPathPointToPointAlgorithm::createInstance()
 const 
   51  return new QgsShortestPathPointToPointAlgorithm();
 
   54void QgsShortestPathPointToPointAlgorithm::initAlgorithm( 
const QVariantMap & )
 
   62  std::unique_ptr<QgsProcessingParameterNumber> maxEndPointDistanceFromNetwork = std::make_unique<QgsProcessingParameterDistance>( QStringLiteral( 
"POINT_TOLERANCE" ), QObject::tr( 
"Maximum point distance from network" ), QVariant(), QStringLiteral( 
"INPUT" ), 
true, 0 );
 
   64  maxEndPointDistanceFromNetwork->setHelp( QObject::tr( 
"Specifies an optional limit on the distance from the start and end points to the network layer. If either point is further from the network than this distance an error will be raised." ) );
 
   65  addParameter( maxEndPointDistanceFromNetwork.release() );
 
   72  loadCommonParams( parameters, context, feedback );
 
   75  fields.
append( 
QgsField( QStringLiteral( 
"start" ), QMetaType::Type::QString ) );
 
   76  fields.
append( 
QgsField( QStringLiteral( 
"end" ), QMetaType::Type::QString ) );
 
   77  fields.
append( 
QgsField( QStringLiteral( 
"cost" ), QMetaType::Type::Double ) );
 
   80  std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, QStringLiteral( 
"OUTPUT" ), context, dest, fields, 
Qgis::WkbType::LineString, mNetwork->sourceCrs() ) );
 
   84  const QgsPointXY startPoint = parameterAsPoint( parameters, QStringLiteral( 
"START_POINT" ), context, mNetwork->sourceCrs() );
 
   85  const QgsPointXY endPoint = parameterAsPoint( parameters, QStringLiteral( 
"END_POINT" ), context, mNetwork->sourceCrs() );
 
   87  feedback->
pushInfo( QObject::tr( 
"Building graph…" ) );
 
   88  QVector<QgsPointXY> snappedPoints;
 
   89  mDirector->makeGraph( mBuilder.get(), { startPoint, endPoint }, snappedPoints, feedback );
 
   90  const QgsPointXY snappedStartPoint = snappedPoints[0];
 
   91  const QgsPointXY snappedEndPoint = snappedPoints[1];
 
   94  if ( parameters.value( QStringLiteral( 
"POINT_TOLERANCE" ) ).isValid() )
 
   96    const double pointDistanceThreshold = parameterAsDouble( parameters, QStringLiteral( 
"POINT_TOLERANCE" ), context );
 
   98    double distanceStartPointToNetwork = 0;
 
  101      distanceStartPointToNetwork = mBuilder->distanceArea()->measureLine( startPoint, snappedStartPoint );
 
  108    if ( distanceStartPointToNetwork > pointDistanceThreshold )
 
  110      throw QgsProcessingException( QObject::tr( 
"Start point is too far from the network layer (%1, maximum permitted is %2)" ).arg( distanceStartPointToNetwork ).arg( pointDistanceThreshold ) );
 
  113    double distanceEndPointToNetwork = 0;
 
  116      distanceEndPointToNetwork = mBuilder->distanceArea()->measureLine( endPoint, snappedEndPoint );
 
  123    if ( distanceEndPointToNetwork > pointDistanceThreshold )
 
  125      throw QgsProcessingException( QObject::tr( 
"End point is too far from the network layer (%1, maximum permitted is %2)" ).arg( distanceEndPointToNetwork ).arg( pointDistanceThreshold ) );
 
  129  feedback->
pushInfo( QObject::tr( 
"Calculating shortest path…" ) );
 
  130  std::unique_ptr<QgsGraph> graph( mBuilder->takeGraph() );
 
  132  const int idxStart = graph->findVertex( snappedStartPoint );
 
  133  int idxEnd = graph->findVertex( snappedEndPoint );
 
  136  QVector<double> costs;
 
  139  if ( tree.at( idxEnd ) == -1 )
 
  144  QVector<QgsPointXY> route;
 
  145  route.push_front( graph->vertex( idxEnd ).point() );
 
  146  const double cost = costs.at( idxEnd );
 
  147  while ( idxEnd != idxStart )
 
  149    idxEnd = graph->edge( tree.at( idxEnd ) ).fromVertex();
 
  150    route.push_front( graph->vertex( idxEnd ).point() );
 
  153  feedback->
pushInfo( QObject::tr( 
"Writing results…" ) );
 
  158  attributes << startPoint.
toString() << endPoint.
toString() << cost / mMultiplier;
 
  167  outputs.insert( QStringLiteral( 
"OUTPUT" ), dest );
 
  168  outputs.insert( QStringLiteral( 
"TRAVEL_COST" ), cost / mMultiplier );
 
@ 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 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, 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 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.
 
A numeric output for processing algorithms.
 
A feature sink output for processing algorithms.
 
A point parameter for processing algorithms.