Skip to content

Commit

Permalink
[processing] port shortest path (point to layer) alg to c++
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Jul 30, 2018
1 parent be3b2c8 commit d999923
Show file tree
Hide file tree
Showing 8 changed files with 250 additions and 37 deletions.
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/qgis/QgisAlgorithmProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
from .SetVectorStyle import SetVectorStyle
from .SetZValue import SetZValue
from .ShortestPathLayerToPoint import ShortestPathLayerToPoint
from .ShortestPathPointToLayer import ShortestPathPointToLayer
#from .ShortestPathPointToLayer import ShortestPathPointToLayer
#from .ShortestPathPointToPoint import ShortestPathPointToPoint
from .SingleSidedBuffer import SingleSidedBuffer
from .Slope import Slope
Expand Down Expand Up @@ -240,7 +240,7 @@ def getAlgs(self):
SetVectorStyle(),
SetZValue(),
ShortestPathLayerToPoint(),
ShortestPathPointToLayer(),
#ShortestPathPointToLayer(),
#ShortestPathPointToPoint(),
SingleSidedBuffer(),
Slope(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3066,7 +3066,7 @@ tests:
start: skip
end: skip

- algorithm: qgis:shortestpathpointtolayer
- algorithm: native:shortestpathpointtolayer
name: Shortest path point to layer
params:
DEFAULT_DIRECTION: 2
Expand Down
2 changes: 2 additions & 0 deletions src/analysis/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ SET(QGIS_ANALYSIS_SRCS
processing/qgsalgorithmrotate.cpp
processing/qgsalgorithmsaveselectedfeatures.cpp
processing/qgsalgorithmsegmentize.cpp
processing/qgsalgorithmshortestpathpointtolayer.cpp
processing/qgsalgorithmshortestpathpointtopoint.cpp
processing/qgsalgorithmsimplify.cpp
processing/qgsalgorithmsmooth.cpp
Expand All @@ -95,6 +96,7 @@ SET(QGIS_ANALYSIS_SRCS
processing/qgsalgorithmvectorize.cpp
processing/qgsalgorithmwedgebuffers.cpp
processing/qgsalgorithmzonalhistogram.cpp

processing/qgsalgorithmnetworkanalysisbase.cpp

processing/qgsnativealgorithms.cpp
Expand Down
66 changes: 33 additions & 33 deletions src/analysis/processing/qgsalgorithmnetworkanalysisbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
// QgsNetworkAnalysisAlgorithmBase
//


QString QgsNetworkAnalysisAlgorithmBase::group() const
{
return QObject::tr( "Network analysis" );
Expand Down Expand Up @@ -130,37 +129,38 @@ void QgsNetworkAnalysisAlgorithmBase::loadCommonParams( const QVariantMap &param
mBuilder = qgis::make_unique< QgsGraphBuilder >( mNetwork->sourceCrs(), true, tolerance );
}

//~ QVector< QgsPointXY > QgsNetworkAnalysisAlgorithmBase::loadPoints( QgsFeatureSource *source, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
//~ {
//~ feedback.pushInfo( QObject::tr( "Loading points…" ) );

//~ QVector< QgsPointXY > points;
//~ QgsFeature feat;
//~ int i = 0;
//~ double step = source->featureCount() > 0 ? 100.0 / source->featureCount() : 0;
//~ QgsFeatureIterator features = source->getFeatures( QgsFeatureRequest().setDestinationCrs( mNetwork->sourceCrs(), context.transformContext() ) );

//~ while ( features.nextFeature( feat ) )
//~ {
//~ i++;
//~ if ( feedback->isCanceled() )
//~ {
//~ break;
//~ }

//~ feedback->setProgress( i * step );
//~ if ( !feat.hasGeometry() )
//~ continue;

//~ QgsGeometry geom = feat.geometry();
//~ QgsAbstractGeometry::vertex_iterator it = geom.vertices_begin();
//~ while ( it != geom.vertices_end() )
//~ {
//~ points.push_back( QgsPointXY( it ) )
//~ }
//~ }

//~ return points
//~ }
void QgsNetworkAnalysisAlgorithmBase::loadPoints( QgsFeatureSource *source, QVector< QgsPointXY > &points, QHash< int, QgsAttributes > &attributes, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
{
feedback->pushInfo( QObject::tr( "Loading points…" ) );

QgsFeature feat;
int i = 0;
int pointId = 1;
double step = source->featureCount() > 0 ? 100.0 / source->featureCount() : 0;
QgsFeatureIterator features = source->getFeatures( QgsFeatureRequest().setDestinationCrs( mNetwork->sourceCrs(), context.transformContext() ) );

while ( features.nextFeature( feat ) )
{
i++;
if ( feedback->isCanceled() )
{
break;
}

feedback->setProgress( i * step );
if ( !feat.hasGeometry() )
continue;

QgsGeometry geom = feat.geometry();
QgsAbstractGeometry::vertex_iterator it = geom.vertices_begin();
while ( it != geom.vertices_end() )
{
points.push_back( QgsPointXY( *it ) );
attributes.insert( pointId, feat.attributes() );
it++;
pointId++;
}
}
}

///@endcond
2 changes: 1 addition & 1 deletion src/analysis/processing/qgsalgorithmnetworkanalysisbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class QgsNetworkAnalysisAlgorithmBase : public QgsProcessingAlgorithm
/**
* Loads point from the feature source for further processing.
*/
//~ void loadPoints( QgsFeatureSource *source, QgsProcessingContext &context, QgsProcessingFeedback *feedback );
void loadPoints( QgsFeatureSource *source, QVector< QgsPointXY > &points, QHash< int, QgsAttributes > &attributes, QgsProcessingContext &context, QgsProcessingFeedback *feedback );

std::unique_ptr< QgsFeatureSource > mNetwork;
QgsVectorLayerDirector *mDirector = nullptr;
Expand Down
156 changes: 156 additions & 0 deletions src/analysis/processing/qgsalgorithmshortestpathpointtolayer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/***************************************************************************
qgsalgorithmshortestpathpointtolayer.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 "qgsalgorithmshortestpathpointtolayer.h"

#include "qgsgraphanalyzer.h"

#include "qgsmessagelog.h"

///@cond PRIVATE

QString QgsShortestPathPointToLayerAlgorithm::name() const
{
return QStringLiteral( "shortestpathpointtolayer" );
}

QString QgsShortestPathPointToLayerAlgorithm::displayName() const
{
return QObject::tr( "Shortest path (point to layer)" );
}

QStringList QgsShortestPathPointToLayerAlgorithm::tags() const
{
return QObject::tr( "network,path,shortest,fastest" ).split( ',' );
}

QString QgsShortestPathPointToLayerAlgorithm::shortHelpString() const
{
return QObject::tr( "This algorithm computes optimal (shortest or fastest) route between given start point and multiple end points defined by point vector layer." );
}

QgsShortestPathPointToLayerAlgorithm *QgsShortestPathPointToLayerAlgorithm::createInstance() const
{
return new QgsShortestPathPointToLayerAlgorithm();
}

void QgsShortestPathPointToLayerAlgorithm::initAlgorithm( const QVariantMap & )
{
addCommonParams();
addParameter( new QgsProcessingParameterPoint( QStringLiteral( "START_POINT" ), QObject::tr( "Start point" ) ) );
addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "END_POINTS" ), QObject::tr( "Vector layer with end points" ), QList< int >() << QgsProcessing::TypeVectorPoint ) );

addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Shortest path" ), QgsProcessing::TypeVectorLine ) );
}

QVariantMap QgsShortestPathPointToLayerAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
{
loadCommonParams( parameters, context, feedback );

QgsPointXY startPoint = parameterAsPoint( parameters, QStringLiteral( "START_POINT" ), context, mNetwork->sourceCrs() );

std::unique_ptr< QgsFeatureSource > endPoints( parameterAsSource( parameters, QStringLiteral( "END_POINTS" ), context ) );
if ( !endPoints )
throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "END_POINTS" ) ) );

QgsFields fields = endPoints->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( startPoint );
QHash< int, QgsAttributes > sourceAttributes;
loadPoints( endPoints.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 idxStart = graph->findVertex( snappedPoints[0] );
int idxEnd;

QVector< int > tree;
QVector< double > costs;
QgsGraphAnalyzer::dijkstra( graph, idxStart, 0, &tree, &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;
}

idxEnd = graph->findVertex( snappedPoints[i] );
if ( tree.at( idxEnd ) == -1 )
{
feedback->reportError( QObject::tr( "There is no route from start point (%1) to end point (%2)." )
.arg( startPoint.toString() )
.arg( points[i].toString() ) );
feat.clearGeometry();
attributes = sourceAttributes.value( i );
attributes.append( QVariant() );
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 );
while ( idxEnd != idxStart )
{
idxEnd = graph->edge( tree.at( idxEnd ) ).fromVertex();
route.push_front( graph->vertex( idxEnd ).point() );
}

QgsGeometry geom = QgsGeometry::fromPolylineXY( route );
QgsFeature feat;
feat.setFields( fields );
attributes = sourceAttributes.value( i );
attributes.append( startPoint.toString() );
attributes.append( points[i].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
53 changes: 53 additions & 0 deletions src/analysis/processing/qgsalgorithmshortestpathpointtolayer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/***************************************************************************
qgsalgorithmshortestpathpointtolayer.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 QGSALGORITHMSHORTESTPATHPOINTTOLAYER_H
#define QGSALGORITHMSHORTESTPATHPOINTTOLAYER_H

#define SIP_NO_FILE

#include "qgis.h"
#include "qgsalgorithmnetworkanalysisbase.h"

///@cond PRIVATE

/**
* Native shortest path (point to layer) algorithm.
*/
class QgsShortestPathPointToLayerAlgorithm : public QgsNetworkAnalysisAlgorithmBase
{

public:

QgsShortestPathPointToLayerAlgorithm() = default;
void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
QString name() const override;
QString displayName() const override;
QStringList tags() const override;
QString shortHelpString() const override;
QgsShortestPathPointToLayerAlgorithm *createInstance() const override SIP_FACTORY;

protected:

QVariantMap processAlgorithm( const QVariantMap &parameters,
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;

};

///@endcond PRIVATE

#endif // QGSALGORITHMSHORTESTPATHPOINTTOLAYER_H
2 changes: 2 additions & 0 deletions src/analysis/processing/qgsnativealgorithms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
#include "qgsalgorithmrotate.h"
#include "qgsalgorithmsaveselectedfeatures.h"
#include "qgsalgorithmsegmentize.h"
#include "qgsalgorithmshortestpathpointtolayer.h"
#include "qgsalgorithmshortestpathpointtopoint.h"
#include "qgsalgorithmsimplify.h"
#include "qgsalgorithmsmooth.h"
Expand Down Expand Up @@ -197,6 +198,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
addAlgorithm( new QgsSegmentizeByMaximumAngleAlgorithm() );
addAlgorithm( new QgsSegmentizeByMaximumDistanceAlgorithm() );
addAlgorithm( new QgsSelectByLocationAlgorithm() );
addAlgorithm( new QgsShortestPathPointToLayerAlgorithm() );
addAlgorithm( new QgsShortestPathPointToPointAlgorithm() );
addAlgorithm( new QgsSimplifyAlgorithm() );
addAlgorithm( new QgsSmoothAlgorithm() );
Expand Down

0 comments on commit d999923

Please sign in to comment.