[processing] Port reverse line direction alg to c++

This commit is contained in:
Nyall Dawson 2018-07-21 15:51:20 +10:00
parent e4e7b2a48b
commit 65548436ad
7 changed files with 170 additions and 73 deletions

View File

@ -117,7 +117,6 @@ from .RectanglesOvalsDiamondsFixed import RectanglesOvalsDiamondsFixed
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
@ -233,7 +232,6 @@ class QgisAlgorithmProvider(QgsProcessingProvider):
RectanglesOvalsDiamondsVariable(),
RegularPoints(),
Relief(),
ReverseLineDirection(),
Ruggedness(),
SelectByAttribute(),
SelectByExpression(),

View File

@ -1,70 +0,0 @@
# -*- coding: utf-8 -*-
"""
***************************************************************************
ReverseLineDirection.py
-----------------------
Date : November 2015
Copyright : (C) 2015 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. *
* *
***************************************************************************
"""
__author__ = 'Nyall Dawson'
__date__ = 'November 2015'
__copyright__ = '(C) 2015, Nyall Dawson'
# This will get replaced with a git SHA1 when you do a git archive323
__revision__ = '$Format:%H$'
from qgis.core import (QgsGeometry,
QgsProcessingException,
QgsProcessing)
from processing.algs.qgis.QgisAlgorithm import QgisFeatureBasedAlgorithm
class ReverseLineDirection(QgisFeatureBasedAlgorithm):
def group(self):
return self.tr('Vector geometry')
def groupId(self):
return 'vectorgeometry'
def __init__(self):
super().__init__()
def name(self):
return 'reverselinedirection'
def displayName(self):
return self.tr('Reverse line direction')
def outputName(self):
return self.tr('Reversed')
def outputType(self):
return QgsProcessing.TypeVectorLine
def inputLayerTypes(self):
return [QgsProcessing.TypeVectorLine]
def processFeature(self, feature, context, feedback):
if feature.geometry():
inGeom = feature.geometry()
reversedLine = inGeom.constGet().reversed()
if not reversedLine:
raise QgsProcessingException(
self.tr('Error reversing line'))
outGeom = QgsGeometry(reversedLine)
feature.setGeometry(outGeom)
return [feature]

View File

@ -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:

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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"
@ -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() );