26 QString QgsShortestPathPointToLayerAlgorithm::name()
const
28 return QStringLiteral(
"shortestpathpointtolayer" );
31 QString QgsShortestPathPointToLayerAlgorithm::displayName()
const
33 return QObject::tr(
"Shortest path (point to layer)" );
36 QStringList QgsShortestPathPointToLayerAlgorithm::tags()
const
38 return QObject::tr(
"network,path,shortest,fastest" ).split(
',' );
41 QString QgsShortestPathPointToLayerAlgorithm::shortHelpString()
const
43 return QObject::tr(
"This algorithm computes optimal (shortest or fastest) route between given start point and multiple end points defined by point vector layer." );
46 QgsShortestPathPointToLayerAlgorithm *QgsShortestPathPointToLayerAlgorithm::createInstance()
const
48 return new QgsShortestPathPointToLayerAlgorithm();
51 void QgsShortestPathPointToLayerAlgorithm::initAlgorithm(
const QVariantMap & )
62 loadCommonParams( parameters, context, feedback );
64 QgsPointXY startPoint = parameterAsPoint( parameters, QStringLiteral(
"START_POINT" ), context, mNetwork->sourceCrs() );
66 std::unique_ptr< QgsFeatureSource > endPoints( parameterAsSource( parameters, QStringLiteral(
"END_POINTS" ), context ) );
71 fields.
append(
QgsField( QStringLiteral(
"start" ), QVariant::String ) );
72 fields.
append(
QgsField( QStringLiteral(
"end" ), QVariant::String ) );
73 fields.
append(
QgsField( QStringLiteral(
"cost" ), QVariant::Double ) );
80 QVector< QgsPointXY > points;
81 points.push_front( startPoint );
82 QHash< int, QgsAttributes > sourceAttributes;
83 loadPoints( endPoints.get(), points, sourceAttributes, context, feedback );
85 feedback->
pushInfo( QObject::tr(
"Building graph…" ) );
86 QVector< QgsPointXY > snappedPoints;
87 mDirector->makeGraph( mBuilder.get(), points, snappedPoints, feedback );
89 feedback->
pushInfo( QObject::tr(
"Calculating shortest paths…" ) );
91 int idxStart = graph->
findVertex( snappedPoints[0] );
95 QVector< double > costs;
98 QVector<QgsPointXY> route;
105 int step = points.size() > 0 ? 100.0 / points.size() : 1;
106 for (
int i = 1; i < points.size(); i++ )
113 idxEnd = graph->
findVertex( snappedPoints[i] );
114 if ( tree.at( idxEnd ) == -1 )
116 feedback->
reportError( QObject::tr(
"There is no route from start point (%1) to end point (%2)." )
118 points[i].toString() ) );
120 attributes = sourceAttributes.value( i );
121 attributes.append( QVariant() );
122 attributes.append( points[i].toString() );
129 route.push_front( graph->
vertex( idxEnd ).
point() );
130 cost = costs.at( idxEnd );
131 while ( idxEnd != idxStart )
134 route.push_front( graph->
vertex( idxEnd ).
point() );
140 attributes = sourceAttributes.value( i );
141 attributes.append( startPoint.
toString() );
142 attributes.append( points[i].toString() );
143 attributes.append( cost / mMultiplier );
152 outputs.insert( QStringLiteral(
"OUTPUT" ), dest );