LCOV - code coverage report
Current view: top level - analysis/processing - qgsalgorithmshortestpathpointtopoint.cpp (source / functions) Hit Total Coverage
Test: coverage.info.cleaned Lines: 0 64 0.0 %
Date: 2021-03-26 12:19:53 Functions: 0 0 -
Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : /***************************************************************************
       2                 :            :                          qgsalgorithmshortestpathpointtopoint.cpp
       3                 :            :                          ---------------------
       4                 :            :     begin                : July 2018
       5                 :            :     copyright            : (C) 2018 by Alexander Bruy
       6                 :            :     email                : alexander dot bruy at gmail dot com
       7                 :            :  ***************************************************************************/
       8                 :            : 
       9                 :            : /***************************************************************************
      10                 :            :  *                                                                         *
      11                 :            :  *   This program is free software; you can redistribute it and/or modify  *
      12                 :            :  *   it under the terms of the GNU General Public License as published by  *
      13                 :            :  *   the Free Software Foundation; either version 2 of the License, or     *
      14                 :            :  *   (at your option) any later version.                                   *
      15                 :            :  *                                                                         *
      16                 :            :  ***************************************************************************/
      17                 :            : 
      18                 :            : #include "qgsalgorithmshortestpathpointtopoint.h"
      19                 :            : 
      20                 :            : #include "qgsgraphanalyzer.h"
      21                 :            : 
      22                 :            : ///@cond PRIVATE
      23                 :            : 
      24                 :          0 : QString QgsShortestPathPointToPointAlgorithm::name() const
      25                 :            : {
      26                 :          0 :   return QStringLiteral( "shortestpathpointtopoint" );
      27                 :            : }
      28                 :            : 
      29                 :          0 : QString QgsShortestPathPointToPointAlgorithm::displayName() const
      30                 :            : {
      31                 :          0 :   return QObject::tr( "Shortest path (point to point)" );
      32                 :            : }
      33                 :            : 
      34                 :          0 : QStringList QgsShortestPathPointToPointAlgorithm::tags() const
      35                 :            : {
      36                 :          0 :   return QObject::tr( "network,path,shortest,fastest" ).split( ',' );
      37                 :          0 : }
      38                 :            : 
      39                 :          0 : QString QgsShortestPathPointToPointAlgorithm::shortHelpString() const
      40                 :            : {
      41                 :          0 :   return QObject::tr( "This algorithm computes optimal (shortest or fastest) route between given start and end points." );
      42                 :            : }
      43                 :            : 
      44                 :          0 : QgsShortestPathPointToPointAlgorithm *QgsShortestPathPointToPointAlgorithm::createInstance() const
      45                 :            : {
      46                 :          0 :   return new QgsShortestPathPointToPointAlgorithm();
      47                 :            : }
      48                 :            : 
      49                 :          0 : void QgsShortestPathPointToPointAlgorithm::initAlgorithm( const QVariantMap & )
      50                 :            : {
      51                 :          0 :   addCommonParams();
      52                 :          0 :   addParameter( new QgsProcessingParameterPoint( QStringLiteral( "START_POINT" ), QObject::tr( "Start point" ) ) );
      53                 :          0 :   addParameter( new QgsProcessingParameterPoint( QStringLiteral( "END_POINT" ), QObject::tr( "End point" ) ) );
      54                 :            : 
      55                 :          0 :   addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Shortest path" ), QgsProcessing::TypeVectorLine ) );
      56                 :          0 :   addOutput( new QgsProcessingOutputNumber( QStringLiteral( "TRAVEL_COST" ), QObject::tr( "Travel cost" ) ) );
      57                 :          0 : }
      58                 :            : 
      59                 :          0 : QVariantMap QgsShortestPathPointToPointAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
      60                 :            : {
      61                 :          0 :   loadCommonParams( parameters, context, feedback );
      62                 :            : 
      63                 :          0 :   QgsFields fields;
      64                 :          0 :   fields.append( QgsField( QStringLiteral( "start" ), QVariant::String ) );
      65                 :          0 :   fields.append( QgsField( QStringLiteral( "end" ), QVariant::String ) );
      66                 :          0 :   fields.append( QgsField( QStringLiteral( "cost" ), QVariant::Double ) );
      67                 :            : 
      68                 :          0 :   QString dest;
      69                 :          0 :   std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, fields, QgsWkbTypes::LineString, mNetwork->sourceCrs() ) );
      70                 :          0 :   if ( !sink )
      71                 :          0 :     throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
      72                 :            : 
      73                 :          0 :   QgsPointXY startPoint = parameterAsPoint( parameters, QStringLiteral( "START_POINT" ), context, mNetwork->sourceCrs() );
      74                 :          0 :   QgsPointXY endPoint = parameterAsPoint( parameters, QStringLiteral( "END_POINT" ), context, mNetwork->sourceCrs() );
      75                 :            : 
      76                 :          0 :   feedback->pushInfo( QObject::tr( "Building graph…" ) );
      77                 :          0 :   QVector< QgsPointXY > points;
      78                 :          0 :   points << startPoint << endPoint;
      79                 :          0 :   QVector< QgsPointXY > snappedPoints;
      80                 :          0 :   mDirector->makeGraph( mBuilder.get(), points, snappedPoints, feedback );
      81                 :            : 
      82                 :          0 :   feedback->pushInfo( QObject::tr( "Calculating shortest path…" ) );
      83                 :          0 :   QgsGraph *graph = mBuilder->graph();
      84                 :          0 :   int idxStart = graph->findVertex( snappedPoints[0] );
      85                 :          0 :   int idxEnd = graph->findVertex( snappedPoints[1] );
      86                 :            : 
      87                 :          0 :   QVector< int > tree;
      88                 :          0 :   QVector< double > costs;
      89                 :          0 :   QgsGraphAnalyzer::dijkstra( graph, idxStart, 0, &tree, &costs );
      90                 :            : 
      91                 :          0 :   if ( tree.at( idxEnd ) == -1 )
      92                 :            :   {
      93                 :          0 :     throw QgsProcessingException( QObject::tr( "There is no route from start point to end point." ) );
      94                 :            :   }
      95                 :            : 
      96                 :          0 :   QVector<QgsPointXY> route;
      97                 :          0 :   route.push_front( graph->vertex( idxEnd ).point() );
      98                 :          0 :   double cost = costs.at( idxEnd );
      99                 :          0 :   while ( idxEnd != idxStart )
     100                 :            :   {
     101                 :          0 :     idxEnd = graph->edge( tree.at( idxEnd ) ).fromVertex();
     102                 :          0 :     route.push_front( graph->vertex( idxEnd ).point() );
     103                 :            :   }
     104                 :            : 
     105                 :          0 :   feedback->pushInfo( QObject::tr( "Writing results…" ) );
     106                 :          0 :   QgsGeometry geom = QgsGeometry::fromPolylineXY( route );
     107                 :          0 :   QgsFeature feat;
     108                 :          0 :   feat.setFields( fields );
     109                 :          0 :   QgsAttributes attributes;
     110                 :          0 :   attributes << startPoint.toString() << endPoint.toString() << cost / mMultiplier;
     111                 :          0 :   feat.setGeometry( geom );
     112                 :          0 :   feat.setAttributes( attributes );
     113                 :          0 :   sink->addFeature( feat, QgsFeatureSink::FastInsert );
     114                 :            : 
     115                 :          0 :   QVariantMap outputs;
     116                 :          0 :   outputs.insert( QStringLiteral( "OUTPUT" ), dest );
     117                 :          0 :   outputs.insert( QStringLiteral( "TRAVEL_COST" ), cost / mMultiplier );
     118                 :          0 :   return outputs;
     119                 :          0 : }
     120                 :            : 
     121                 :            : ///@endcond

Generated by: LCOV version 1.14