Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[processing] Port reverse line direction alg to c++
  • Loading branch information
nyalldawson committed Jul 23, 2018
1 parent e4e7b2a commit 6554843
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 73 deletions.
2 changes: 0 additions & 2 deletions python/plugins/processing/algs/qgis/QgisAlgorithmProvider.py
Expand Up @@ -117,7 +117,6 @@
from .RectanglesOvalsDiamondsVariable import RectanglesOvalsDiamondsVariable
from .RegularPoints import RegularPoints
from .Relief import Relief
from .ReverseLineDirection import ReverseLineDirection
from .Ruggedness import Ruggedness
from .SelectByAttribute import SelectByAttribute
from .SelectByExpression import SelectByExpression
Expand Down Expand Up @@ -233,7 +232,6 @@ def getAlgs(self):
RectanglesOvalsDiamondsVariable(),
RegularPoints(),
Relief(),
ReverseLineDirection(),
Ruggedness(),
SelectByAttribute(),
SelectByExpression(),
Expand Down
70 changes: 0 additions & 70 deletions python/plugins/processing/algs/qgis/ReverseLineDirection.py

This file was deleted.

Expand Up @@ -959,7 +959,7 @@ tests:
name: expected/point_on_line.gml
type: vector

- algorithm: qgis:reverselinedirection
- algorithm: native:reverselinedirection
name: Reverse line direction
params:
INPUT:
Expand Down
1 change: 1 addition & 0 deletions src/analysis/CMakeLists.txt
Expand Up @@ -68,6 +68,7 @@ SET(QGIS_ANALYSIS_SRCS
processing/qgsalgorithmremoveholes.cpp
processing/qgsalgorithmremovenullgeometry.cpp
processing/qgsalgorithmrenamelayer.cpp
processing/qgsalgorithmreverselinedirection.cpp
processing/qgsalgorithmrotate.cpp
processing/qgsalgorithmsaveselectedfeatures.cpp
processing/qgsalgorithmsegmentize.cpp
Expand Down
106 changes: 106 additions & 0 deletions src/analysis/processing/qgsalgorithmreverselinedirection.cpp
@@ -0,0 +1,106 @@
/***************************************************************************
qgsalgorithmreverselinedirection.cpp
------------------------------------
begin : July 2018
copyright : (C) 2018 by Nyall Dawson
email : nyall dot dawson 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 "qgsalgorithmreverselinedirection.h"
#include "qgscurve.h"

///@cond PRIVATE

QString QgsReverseLineDirectionAlgorithm ::name() const
{
return QStringLiteral( "reverselinedirection" );
}

QString QgsReverseLineDirectionAlgorithm ::displayName() const
{
return QObject::tr( "Reverse line direction" );
}

QStringList QgsReverseLineDirectionAlgorithm ::tags() const
{
return QObject::tr( "swap,reverse,switch,flip,linestring,orientation" ).split( ',' );
}

QString QgsReverseLineDirectionAlgorithm ::group() const
{
return QObject::tr( "Vector geometry" );
}

QString QgsReverseLineDirectionAlgorithm ::groupId() const
{
return QStringLiteral( "vectorgeometry" );
}

QString QgsReverseLineDirectionAlgorithm ::outputName() const
{
return QObject::tr( "Reversed" );
}

QString QgsReverseLineDirectionAlgorithm ::shortHelpString() const
{
return QObject::tr( "This algorithm reverses the direction of curve or LineString geometries." );
}

QString QgsReverseLineDirectionAlgorithm::shortDescription() const
{
return QObject::tr( "Reverses the direction of curve or LineString geometries." );
}

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

QgsProcessing::SourceType QgsReverseLineDirectionAlgorithm::outputLayerType() const
{
return QgsProcessing::TypeVectorLine;
}

QList<int> QgsReverseLineDirectionAlgorithm::inputLayerTypes() const
{
return QList<int>() << QgsProcessing::TypeVectorLine;
}

QgsProcessingFeatureSource::Flag QgsReverseLineDirectionAlgorithm ::sourceFlags() const
{
// this algorithm doesn't care about invalid geometries
return QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks;
}

QgsFeatureList QgsReverseLineDirectionAlgorithm ::processFeature( const QgsFeature &f, QgsProcessingContext &, QgsProcessingFeedback * )
{
QgsFeature feature = f;
if ( feature.hasGeometry() )
{
const QgsGeometry geom = feature.geometry();
const QgsCurve *curve = qgsgeometry_cast< const QgsCurve * >( geom.constGet() );
if ( curve )
{
std::unique_ptr< QgsCurve > reversed( curve->reversed() );
if ( !reversed )
{
// can this even happen?
throw QgsProcessingException( QObject::tr( "Error reversing line" ) );
}
const QgsGeometry outGeom( std::move( reversed ) );
feature.setGeometry( outGeom );
}
}
return QgsFeatureList() << feature;
}

///@endcond
60 changes: 60 additions & 0 deletions src/analysis/processing/qgsalgorithmreverselinedirection.h
@@ -0,0 +1,60 @@
/***************************************************************************
qgsalgorithmreverselinedirection.h
----------------------------------
begin : July 2018
copyright : (C) 2018 by Nyall Dawson
email : nyall dot dawson 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 QGSALGORITHMREVERSELINEDIRECTION_H
#define QGSALGORITHMREVERSELINEDIRECTION_H

#define SIP_NO_FILE

#include "qgis.h"
#include "qgsprocessingalgorithm.h"

///@cond PRIVATE

/**
* Native reverse line direction algorithm.
*/
class QgsReverseLineDirectionAlgorithm : public QgsProcessingFeatureBasedAlgorithm
{

public:

QgsReverseLineDirectionAlgorithm() = default;
QString name() const override;
QString displayName() const override;
QStringList tags() const override;
QString group() const override;
QString groupId() const override;
QString shortHelpString() const override;
QString shortDescription() const override;
QgsReverseLineDirectionAlgorithm *createInstance() const override SIP_FACTORY;
QgsProcessing::SourceType outputLayerType() const override;
QList<int> inputLayerTypes() const override;

protected:

QString outputName() const override;
QgsProcessingFeatureSource::Flag sourceFlags() const override;
QgsFeatureList processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;

};

///@endcond PRIVATE

#endif // QGSALGORITHMREVERSELINEDIRECTION_H


2 changes: 2 additions & 0 deletions src/analysis/processing/qgsnativealgorithms.cpp
Expand Up @@ -65,6 +65,7 @@
#include "qgsalgorithmremoveholes.h"
#include "qgsalgorithmremovenullgeometry.h"
#include "qgsalgorithmrenamelayer.h"
#include "qgsalgorithmreverselinedirection.h"
#include "qgsalgorithmrotate.h"
#include "qgsalgorithmsaveselectedfeatures.h"
#include "qgsalgorithmsegmentize.h"
Expand Down Expand Up @@ -177,6 +178,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
addAlgorithm( new QgsRemoveHolesAlgorithm() );
addAlgorithm( new QgsRemoveNullGeometryAlgorithm() );
addAlgorithm( new QgsRenameLayerAlgorithm() );
addAlgorithm( new QgsReverseLineDirectionAlgorithm() );
addAlgorithm( new QgsRotateFeaturesAlgorithm() );
addAlgorithm( new QgsSaveSelectedFeatures() );
addAlgorithm( new QgsSegmentizeByMaximumAngleAlgorithm() );
Expand Down

0 comments on commit 6554843

Please sign in to comment.