Skip to content
Permalink
Browse files

[processing][FEATURE] Port extend lines algorithm to c++, allow dynam…

…ic start/end distance

Allows the start and end distance to be data defined
  • Loading branch information
nyalldawson committed Jul 23, 2018
1 parent 8a1964f commit 59ec2e0bceb83fa5af28bf19df946a524bf35af4
@@ -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.

This file was deleted.

@@ -65,7 +65,6 @@
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 @@ def getAlgs(self):
EliminateSelection(),
ExecuteSQL(),
ExportGeometryInfo(),
ExtendLines(),
ExtentFromLayer(),
ExtractSpecificVertices(),
FieldsCalculator(),
@@ -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
@@ -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
@@ -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


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


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

0 comments on commit 59ec2e0

Please sign in to comment.
You can’t perform that action at this time.