[processing][FEATURE] Port extend lines algorithm to c++, allow dynamic start/end distance

Allows the start and end distance to be data defined
This commit is contained in:
Nyall Dawson 2018-07-23 08:40:03 +10:00
parent 8a1964fb85
commit 59ec2e0bce
8 changed files with 218 additions and 90 deletions

View File

@ -157,9 +157,6 @@ qgis:exportaddgeometrycolumns: >
Depending on the geometry type of the vector layer, the attributes added to the table will be different.
qgis:extendlines: >
This algorithm extends line geometries by a specified amount at the start and end of the line. Lines are extended using the bearing of the first and last segment in the line.
qgis:extractbyattribute: >
This algorithm creates a new vector layer that only contains matching features from an input layer. The criteria for adding features to the resulting layer is defined based on the values of an attribute from the input layer.

View File

@ -1,83 +0,0 @@
# -*- coding: utf-8 -*-
"""
***************************************************************************
ExtendLines.py
--------------------
Date : October 2016
Copyright : (C) 2016 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__ = 'October 2016'
__copyright__ = '(C) 2016, Nyall Dawson'
# This will get replaced with a git SHA1 when you do a git archive323
__revision__ = '$Format:%H$'
from qgis.core import (QgsProcessingParameterDistance,
QgsProcessingException,
QgsProcessing)
from processing.algs.qgis.QgisAlgorithm import QgisFeatureBasedAlgorithm
class ExtendLines(QgisFeatureBasedAlgorithm):
START_DISTANCE = 'START_DISTANCE'
END_DISTANCE = 'END_DISTANCE'
def group(self):
return self.tr('Vector geometry')
def groupId(self):
return 'vectorgeometry'
def __init__(self):
super().__init__()
self.start_distance = None
self.end_distance = None
def initParameters(self, config=None):
self.addParameter(QgsProcessingParameterDistance(self.START_DISTANCE,
self.tr('Start distance'), defaultValue=0.0, parentParameterName='INPUT'))
self.addParameter(QgsProcessingParameterDistance(self.END_DISTANCE,
self.tr('End distance'), defaultValue=0.0, parentParameterName='INPUT'))
def name(self):
return 'extendlines'
def displayName(self):
return self.tr('Extend lines')
def outputName(self):
return self.tr('Extended')
def inputLayerTypes(self):
return [QgsProcessing.TypeVectorLine]
def prepareAlgorithm(self, parameters, context, feedback):
self.start_distance = self.parameterAsDouble(parameters, self.START_DISTANCE, context)
self.end_distance = self.parameterAsDouble(parameters, self.END_DISTANCE, context)
return True
def processFeature(self, feature, context, feedback):
input_geometry = feature.geometry()
if input_geometry:
output_geometry = input_geometry.extendLine(self.start_distance, self.end_distance)
if not output_geometry:
raise QgsProcessingException(
self.tr('Error calculating extended line'))
feature.setGeometry(output_geometry)
return [feature]

View File

@ -65,7 +65,6 @@ from .DensifyGeometriesInterval import DensifyGeometriesInterval
from .EliminateSelection import EliminateSelection
from .ExecuteSQL import ExecuteSQL
from .ExportGeometryInfo import ExportGeometryInfo
from .ExtendLines import ExtendLines
from .ExtentFromLayer import ExtentFromLayer
from .ExtractSpecificVertices import ExtractSpecificVertices
from .FieldPyculator import FieldsPyculator
@ -182,7 +181,6 @@ class QgisAlgorithmProvider(QgsProcessingProvider):
EliminateSelection(),
ExecuteSQL(),
ExportGeometryInfo(),
ExtendLines(),
ExtentFromLayer(),
ExtractSpecificVertices(),
FieldsCalculator(),

View File

@ -1835,7 +1835,7 @@ tests:
name: expected/extract_expression.gml
type: vector
- algorithm: qgis:extendlines
- algorithm: native:extendlines
name: Extend lines
params:
END_DISTANCE: 0.2
@ -1850,7 +1850,7 @@ tests:
compare:
geometry:
precision: 7
- algorithm: qgis:extendlines
- algorithm: native:extendlines
name: Extend multilines
params:
END_DISTANCE: 0.4

View File

@ -33,6 +33,7 @@ SET(QGIS_ANALYSIS_SRCS
processing/qgsalgorithmdropgeometry.cpp
processing/qgsalgorithmdropmzvalues.cpp
processing/qgsalgorithmexplode.cpp
processing/qgsalgorithmextendlines.cpp
processing/qgsalgorithmextenttolayer.cpp
processing/qgsalgorithmextractbyattribute.cpp
processing/qgsalgorithmextractbyexpression.cpp

View File

@ -0,0 +1,142 @@
/***************************************************************************
qgsalgorithmextendlines.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 "qgsalgorithmextendlines.h"
///@cond PRIVATE
QString QgsExtendLinesAlgorithm::name() const
{
return QStringLiteral( "extendlines" );
}
QString QgsExtendLinesAlgorithm::displayName() const
{
return QObject::tr( "Extend lines" );
}
QStringList QgsExtendLinesAlgorithm::tags() const
{
return QObject::tr( "linestring,continue,grow,extrapolate" ).split( ',' );
}
QString QgsExtendLinesAlgorithm::group() const
{
return QObject::tr( "Vector geometry" );
}
QString QgsExtendLinesAlgorithm::groupId() const
{
return QStringLiteral( "vectorgeometry" );
}
QString QgsExtendLinesAlgorithm::outputName() const
{
return QObject::tr( "Extended" );
}
QString QgsExtendLinesAlgorithm::shortHelpString() const
{
return QObject::tr( "This algorithm extends line geometries by a specified amount at the start and end "
"of the line. Lines are extended using the bearing of the first and last segment "
"in the line." );
}
QString QgsExtendLinesAlgorithm::shortDescription() const
{
return QObject::tr( "Extends LineString geometries by extrapolating the start and end segments." );
}
QList<int> QgsExtendLinesAlgorithm::inputLayerTypes() const
{
return QList<int>() << QgsProcessing::TypeVectorLine;
}
QgsProcessing::SourceType QgsExtendLinesAlgorithm::outputLayerType() const
{
return QgsProcessing::TypeVectorLine;
}
QgsExtendLinesAlgorithm *QgsExtendLinesAlgorithm::createInstance() const
{
return new QgsExtendLinesAlgorithm();
}
void QgsExtendLinesAlgorithm::initParameters( const QVariantMap & )
{
std::unique_ptr< QgsProcessingParameterDistance> startDistance = qgis::make_unique< QgsProcessingParameterDistance >( QStringLiteral( "START_DISTANCE" ),
QObject::tr( "Start distance" ), 0.0, QStringLiteral( "INPUT" ), false, 0 );
startDistance->setIsDynamic( true );
startDistance->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "Start Distance" ), QObject::tr( "Start distance" ), QgsPropertyDefinition::DoublePositive ) );
startDistance->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
addParameter( startDistance.release() );
std::unique_ptr< QgsProcessingParameterDistance> endDistance = qgis::make_unique< QgsProcessingParameterDistance >( QStringLiteral( "END_DISTANCE" ),
QObject::tr( "End distance" ), 0.0, QStringLiteral( "INPUT" ), false, 0 );
endDistance->setIsDynamic( true );
endDistance->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "End Distance" ), QObject::tr( "End distance" ), QgsPropertyDefinition::DoublePositive ) );
endDistance->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
addParameter( endDistance.release() );
}
QgsProcessingFeatureSource::Flag QgsExtendLinesAlgorithm::sourceFlags() const
{
// skip geometry checks - this algorithm doesn't care about invalid geometries
return QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks;
}
bool QgsExtendLinesAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
{
mStartDistance = parameterAsDouble( parameters, QStringLiteral( "START_DISTANCE" ), context );
mDynamicStartDistance = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "START_DISTANCE" ) );
if ( mDynamicStartDistance )
mStartDistanceProperty = parameters.value( QStringLiteral( "START_DISTANCE" ) ).value< QgsProperty >();
mEndDistance = parameterAsDouble( parameters, QStringLiteral( "END_DISTANCE" ), context );
mDynamicEndDistance = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "END_DISTANCE" ) );
if ( mDynamicEndDistance )
mEndDistanceProperty = parameters.value( QStringLiteral( "END_DISTANCE" ) ).value< QgsProperty >();
return true;
}
QgsFeatureList QgsExtendLinesAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
{
QgsFeature f = feature;
if ( f.hasGeometry() )
{
const QgsGeometry geometry = f.geometry();
double startDistance = mStartDistance;
if ( mDynamicStartDistance )
startDistance = mStartDistanceProperty.valueAsDouble( context.expressionContext(), startDistance );
double endDistance = mEndDistance;
if ( mDynamicEndDistance )
endDistance = mEndDistanceProperty.valueAsDouble( context.expressionContext(), endDistance );
const QgsGeometry outGeometry = geometry.extendLine( startDistance, endDistance );
if ( !outGeometry )
throw QgsProcessingException( QObject::tr( "Error calculating extended line" ) ); // don't think this can actually happen!
f.setGeometry( outGeometry );
}
return QgsFeatureList() << f;
}
///@endcond

View File

@ -0,0 +1,71 @@
/***************************************************************************
qgsalgorithmextendlines.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 QGSEXTENDLINESALGORITHM_H
#define QGSEXTENDLINESALGORITHM_H
#define SIP_NO_FILE
#include "qgis.h"
#include "qgsprocessingalgorithm.h"
///@cond PRIVATE
/**
* Native extend lines algorithm.
*/
class QgsExtendLinesAlgorithm : public QgsProcessingFeatureBasedAlgorithm
{
public:
QgsExtendLinesAlgorithm() = 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;
QList<int> inputLayerTypes() const override;
QgsProcessing::SourceType outputLayerType() const override;
QgsExtendLinesAlgorithm *createInstance() const override SIP_FACTORY;
void initParameters( const QVariantMap &configuration = QVariantMap() ) override;
protected:
QString outputName() const override;
QgsProcessingFeatureSource::Flag sourceFlags() const override;
bool prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
QgsFeatureList processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback *feedback ) override;
private:
double mStartDistance = 0.0;
bool mDynamicStartDistance = false;
QgsProperty mStartDistanceProperty;
double mEndDistance = 0.0;
bool mDynamicEndDistance = false;
QgsProperty mEndDistanceProperty;
};
///@endcond PRIVATE
#endif // QGSEXTENDLINESALGORITHM_H

View File

@ -30,6 +30,7 @@
#include "qgsalgorithmdropgeometry.h"
#include "qgsalgorithmdropmzvalues.h"
#include "qgsalgorithmexplode.h"
#include "qgsalgorithmextendlines.h"
#include "qgsalgorithmextenttolayer.h"
#include "qgsalgorithmextractbyattribute.h"
#include "qgsalgorithmextractbyexpression.h"
@ -140,6 +141,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
addAlgorithm( new QgsDropGeometryAlgorithm() );
addAlgorithm( new QgsDropMZValuesAlgorithm() );
addAlgorithm( new QgsExplodeAlgorithm() );
addAlgorithm( new QgsExtendLinesAlgorithm() );
addAlgorithm( new QgsExtentToLayerAlgorithm() );
addAlgorithm( new QgsExtractByAttributeAlgorithm() );
addAlgorithm( new QgsExtractByExpressionAlgorithm() );