mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-17 00:04:02 -04:00
[processing] port shortest path (layer to point) alg to c++
This commit is contained in:
parent
d999923da9
commit
72d13ac0f8
@ -125,7 +125,7 @@ from .SetMValue import SetMValue
|
||||
from .SetRasterStyle import SetRasterStyle
|
||||
from .SetVectorStyle import SetVectorStyle
|
||||
from .SetZValue import SetZValue
|
||||
from .ShortestPathLayerToPoint import ShortestPathLayerToPoint
|
||||
#from .ShortestPathLayerToPoint import ShortestPathLayerToPoint
|
||||
#from .ShortestPathPointToLayer import ShortestPathPointToLayer
|
||||
#from .ShortestPathPointToPoint import ShortestPathPointToPoint
|
||||
from .SingleSidedBuffer import SingleSidedBuffer
|
||||
@ -239,7 +239,7 @@ class QgisAlgorithmProvider(QgsProcessingProvider):
|
||||
SetRasterStyle(),
|
||||
SetVectorStyle(),
|
||||
SetZValue(),
|
||||
ShortestPathLayerToPoint(),
|
||||
#ShortestPathLayerToPoint(),
|
||||
#ShortestPathPointToLayer(),
|
||||
#ShortestPathPointToPoint(),
|
||||
SingleSidedBuffer(),
|
||||
|
@ -3036,7 +3036,7 @@ tests:
|
||||
start: skip
|
||||
end: skip
|
||||
|
||||
- algorithm: qgis:shortestpathlayertopoint
|
||||
- algorithm: native:shortestpathlayertopoint
|
||||
name: Shortest path layer to point
|
||||
params:
|
||||
DEFAULT_DIRECTION: 2
|
||||
|
@ -77,6 +77,7 @@ SET(QGIS_ANALYSIS_SRCS
|
||||
processing/qgsalgorithmrotate.cpp
|
||||
processing/qgsalgorithmsaveselectedfeatures.cpp
|
||||
processing/qgsalgorithmsegmentize.cpp
|
||||
processing/qgsalgorithmshortestpathlayertopoint.cpp
|
||||
processing/qgsalgorithmshortestpathpointtolayer.cpp
|
||||
processing/qgsalgorithmshortestpathpointtopoint.cpp
|
||||
processing/qgsalgorithmsimplify.cpp
|
||||
|
158
src/analysis/processing/qgsalgorithmshortestpathlayertopoint.cpp
Normal file
158
src/analysis/processing/qgsalgorithmshortestpathlayertopoint.cpp
Normal file
@ -0,0 +1,158 @@
|
||||
/***************************************************************************
|
||||
qgsalgorithmshortestpathlayertopoint.cpp
|
||||
---------------------
|
||||
begin : July 2018
|
||||
copyright : (C) 2018 by Alexander Bruy
|
||||
email : alexander dot bruy at gmail dot com
|
||||
***************************************************************************/
|
||||
|
||||
/***************************************************************************
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
***************************************************************************/
|
||||
|
||||
#include "qgsalgorithmshortestpathlayertopoint.h"
|
||||
|
||||
#include "qgsgraphanalyzer.h"
|
||||
|
||||
#include "qgsmessagelog.h"
|
||||
|
||||
///@cond PRIVATE
|
||||
|
||||
QString QgsShortestPathLayerToPointAlgorithm::name() const
|
||||
{
|
||||
return QStringLiteral( "shortestpathlayertopoint" );
|
||||
}
|
||||
|
||||
QString QgsShortestPathLayerToPointAlgorithm::displayName() const
|
||||
{
|
||||
return QObject::tr( "Shortest path (layer to point)" );
|
||||
}
|
||||
|
||||
QStringList QgsShortestPathLayerToPointAlgorithm::tags() const
|
||||
{
|
||||
return QObject::tr( "network,path,shortest,fastest" ).split( ',' );
|
||||
}
|
||||
|
||||
QString QgsShortestPathLayerToPointAlgorithm::shortHelpString() const
|
||||
{
|
||||
return QObject::tr( "This algorithm computes optimal (shortest or fastest) route from multiple start points defined by vector layer and given end point." );
|
||||
}
|
||||
|
||||
QgsShortestPathLayerToPointAlgorithm *QgsShortestPathLayerToPointAlgorithm::createInstance() const
|
||||
{
|
||||
return new QgsShortestPathLayerToPointAlgorithm();
|
||||
}
|
||||
|
||||
void QgsShortestPathLayerToPointAlgorithm::initAlgorithm( const QVariantMap & )
|
||||
{
|
||||
addCommonParams();
|
||||
addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "START_POINTS" ), QObject::tr( "Vector layer with start points" ), QList< int >() << QgsProcessing::TypeVectorPoint ) );
|
||||
addParameter( new QgsProcessingParameterPoint( QStringLiteral( "END_POINT" ), QObject::tr( "End point" ) ) );
|
||||
|
||||
addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Shortest path" ), QgsProcessing::TypeVectorLine ) );
|
||||
}
|
||||
|
||||
QVariantMap QgsShortestPathLayerToPointAlgorithm::processAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
|
||||
{
|
||||
loadCommonParams( parameters, context, feedback );
|
||||
|
||||
QgsPointXY endPoint = parameterAsPoint( parameters, QStringLiteral( "END_POINT" ), context, mNetwork->sourceCrs() );
|
||||
|
||||
std::unique_ptr< QgsFeatureSource > startPoints( parameterAsSource( parameters, QStringLiteral( "START_POINTS" ), context ) );
|
||||
if ( !startPoints )
|
||||
throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "START_POINTS" ) ) );
|
||||
|
||||
QgsFields fields = startPoints->fields();
|
||||
fields.append( QgsField( QStringLiteral( "start" ), QVariant::String ) );
|
||||
fields.append( QgsField( QStringLiteral( "end" ), QVariant::String ) );
|
||||
fields.append( QgsField( QStringLiteral( "cost" ), QVariant::Double ) );
|
||||
|
||||
QString dest;
|
||||
std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, fields, QgsWkbTypes::LineString, mNetwork->sourceCrs() ) );
|
||||
if ( !sink )
|
||||
throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
|
||||
|
||||
QVector< QgsPointXY > points;
|
||||
points.push_front( endPoint );
|
||||
QHash< int, QgsAttributes > sourceAttributes;
|
||||
loadPoints( startPoints.get(), points, sourceAttributes, context, feedback );
|
||||
|
||||
feedback->pushInfo( QObject::tr( "Building graph…" ) );
|
||||
QVector< QgsPointXY > snappedPoints;
|
||||
mDirector->makeGraph( mBuilder.get(), points, snappedPoints, feedback );
|
||||
|
||||
feedback->pushInfo( QObject::tr( "Calculating shortest paths…" ) );
|
||||
QgsGraph *graph = mBuilder->graph();
|
||||
int idxEnd = graph->findVertex( snappedPoints[0] );
|
||||
int idxStart;
|
||||
int currentIdx;
|
||||
|
||||
QVector< int > tree;
|
||||
QVector< double > costs;
|
||||
|
||||
QVector<QgsPointXY> route;
|
||||
double cost;
|
||||
|
||||
QgsFeature feat;
|
||||
feat.setFields( fields );
|
||||
QgsAttributes attributes;
|
||||
|
||||
int step = points.size() > 0 ? 100.0 / points.size() : 1;
|
||||
for ( int i = 1; i < points.size(); i++ )
|
||||
{
|
||||
if ( feedback->isCanceled() )
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
idxStart = graph->findVertex( snappedPoints[i] );
|
||||
QgsGraphAnalyzer::dijkstra( graph, idxStart, 0, &tree, &costs );
|
||||
|
||||
if ( tree.at( idxEnd ) == -1 )
|
||||
{
|
||||
feedback->reportError( QObject::tr( "There is no route from start point (%1) to end point (%2)." )
|
||||
.arg( points[i].toString() )
|
||||
.arg( endPoint.toString() ) );
|
||||
feat.clearGeometry();
|
||||
attributes = sourceAttributes.value( i );
|
||||
attributes.append( points[i].toString() );
|
||||
feat.setAttributes( attributes );
|
||||
sink->addFeature( feat, QgsFeatureSink::FastInsert );
|
||||
continue;
|
||||
}
|
||||
|
||||
route.clear();
|
||||
route.push_front( graph->vertex( idxEnd ).point() );
|
||||
cost = costs.at( idxEnd );
|
||||
currentIdx = idxEnd;
|
||||
while ( currentIdx != idxStart )
|
||||
{
|
||||
currentIdx = graph->edge( tree.at( currentIdx ) ).fromVertex();
|
||||
route.push_front( graph->vertex( currentIdx ).point() );
|
||||
}
|
||||
|
||||
QgsGeometry geom = QgsGeometry::fromPolylineXY( route );
|
||||
QgsFeature feat;
|
||||
feat.setFields( fields );
|
||||
attributes = sourceAttributes.value( i );
|
||||
attributes.append( points[i].toString() );
|
||||
attributes.append( endPoint.toString() );
|
||||
attributes.append( cost / mMultiplier );
|
||||
feat.setAttributes( attributes );
|
||||
feat.setGeometry( geom );
|
||||
sink->addFeature( feat, QgsFeatureSink::FastInsert );
|
||||
|
||||
feedback->setProgress( i * step );
|
||||
}
|
||||
|
||||
QVariantMap outputs;
|
||||
outputs.insert( QStringLiteral( "OUTPUT" ), dest );
|
||||
return outputs;
|
||||
}
|
||||
|
||||
///@endcond
|
@ -0,0 +1,53 @@
|
||||
/***************************************************************************
|
||||
qgsalgorithmshortestpathlayertopoint.h
|
||||
---------------------
|
||||
begin : JUly 2018
|
||||
copyright : (C) 2018 by Alexander Bruy
|
||||
email : alexander dot bruy at gmail dot com
|
||||
***************************************************************************/
|
||||
|
||||
/***************************************************************************
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef QGSALGORITHMSHORTESTPATHLAYERTOPOINT_H
|
||||
#define QGSALGORITHMSHORTESTPATHLAYERTOPOINT_H
|
||||
|
||||
#define SIP_NO_FILE
|
||||
|
||||
#include "qgis.h"
|
||||
#include "qgsalgorithmnetworkanalysisbase.h"
|
||||
|
||||
///@cond PRIVATE
|
||||
|
||||
/**
|
||||
* Native shortest path (layer to point) algorithm.
|
||||
*/
|
||||
class QgsShortestPathLayerToPointAlgorithm : public QgsNetworkAnalysisAlgorithmBase
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
QgsShortestPathLayerToPointAlgorithm() = default;
|
||||
void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
|
||||
QString name() const override;
|
||||
QString displayName() const override;
|
||||
QStringList tags() const override;
|
||||
QString shortHelpString() const override;
|
||||
QgsShortestPathLayerToPointAlgorithm *createInstance() const override SIP_FACTORY;
|
||||
|
||||
protected:
|
||||
|
||||
QVariantMap processAlgorithm( const QVariantMap ¶meters,
|
||||
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
|
||||
|
||||
};
|
||||
|
||||
///@endcond PRIVATE
|
||||
|
||||
#endif // QGSALGORITHMSHORTESTPATHLAYERTOPOINT_H
|
@ -74,6 +74,7 @@
|
||||
#include "qgsalgorithmrotate.h"
|
||||
#include "qgsalgorithmsaveselectedfeatures.h"
|
||||
#include "qgsalgorithmsegmentize.h"
|
||||
#include "qgsalgorithmshortestpathlayertopoint.h"
|
||||
#include "qgsalgorithmshortestpathpointtolayer.h"
|
||||
#include "qgsalgorithmshortestpathpointtopoint.h"
|
||||
#include "qgsalgorithmsimplify.h"
|
||||
@ -198,6 +199,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
|
||||
addAlgorithm( new QgsSegmentizeByMaximumAngleAlgorithm() );
|
||||
addAlgorithm( new QgsSegmentizeByMaximumDistanceAlgorithm() );
|
||||
addAlgorithm( new QgsSelectByLocationAlgorithm() );
|
||||
addAlgorithm( new QgsShortestPathLayerToPointAlgorithm() );
|
||||
addAlgorithm( new QgsShortestPathPointToLayerAlgorithm() );
|
||||
addAlgorithm( new QgsShortestPathPointToPointAlgorithm() );
|
||||
addAlgorithm( new QgsSimplifyAlgorithm() );
|
||||
|
Loading…
x
Reference in New Issue
Block a user