24 QString QgsShortestPathPointToPointAlgorithm::name()
const
26 return QStringLiteral(
"shortestpathpointtopoint" );
29 QString QgsShortestPathPointToPointAlgorithm::displayName()
const
31 return QObject::tr(
"Shortest path (point to point)" );
34 QStringList QgsShortestPathPointToPointAlgorithm::tags()
const
36 return QObject::tr(
"network,path,shortest,fastest" ).split(
',' );
39 QString QgsShortestPathPointToPointAlgorithm::shortHelpString()
const
41 return QObject::tr(
"This algorithm computes optimal (shortest or fastest) route between given start and end points." );
44 QgsShortestPathPointToPointAlgorithm *QgsShortestPathPointToPointAlgorithm::createInstance()
const
46 return new QgsShortestPathPointToPointAlgorithm();
49 void QgsShortestPathPointToPointAlgorithm::initAlgorithm(
const QVariantMap & )
61 loadCommonParams( parameters, context, feedback );
64 fields.
append(
QgsField( QStringLiteral(
"start" ), QVariant::String ) );
65 fields.
append(
QgsField( QStringLiteral(
"end" ), QVariant::String ) );
66 fields.
append(
QgsField( QStringLiteral(
"cost" ), QVariant::Double ) );
69 std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, dest, fields,
QgsWkbTypes::LineString, mNetwork->sourceCrs() ) );
73 const QgsPointXY startPoint = parameterAsPoint( parameters, QStringLiteral(
"START_POINT" ), context, mNetwork->sourceCrs() );
74 const QgsPointXY endPoint = parameterAsPoint( parameters, QStringLiteral(
"END_POINT" ), context, mNetwork->sourceCrs() );
76 feedback->
pushInfo( QObject::tr(
"Building graph…" ) );
77 QVector< QgsPointXY > points;
78 points << startPoint << endPoint;
79 QVector< QgsPointXY > snappedPoints;
80 mDirector->makeGraph( mBuilder.get(), points, snappedPoints, feedback );
82 feedback->
pushInfo( QObject::tr(
"Calculating shortest path…" ) );
83 std::unique_ptr< QgsGraph > graph( mBuilder->takeGraph() );
84 const int idxStart = graph->findVertex( snappedPoints[0] );
85 int idxEnd = graph->findVertex( snappedPoints[1] );
88 QVector< double > costs;
91 if ( tree.at( idxEnd ) == -1 )
96 QVector<QgsPointXY> route;
97 route.push_front( graph->vertex( idxEnd ).point() );
98 const double cost = costs.at( idxEnd );
99 while ( idxEnd != idxStart )
101 idxEnd = graph->edge( tree.at( idxEnd ) ).fromVertex();
102 route.push_front( graph->vertex( idxEnd ).point() );
105 feedback->
pushInfo( QObject::tr(
"Writing results…" ) );
110 attributes << startPoint.
toString() << endPoint.toString() << cost / mMultiplier;
117 outputs.insert( QStringLiteral(
"OUTPUT" ), dest );
118 outputs.insert( QStringLiteral(
"TRAVEL_COST" ), cost / mMultiplier );