-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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
1 parent
8a1964f
commit 59ec2e0
Showing
8 changed files
with
218 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ¶meters, 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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ¶meters, 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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters