Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[processing] port set M value algorithm to C++
- Loading branch information
1 parent
5324d7f
commit 9411d96
Showing
8 changed files
with
197 additions
and
114 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/*************************************************************************** | ||
qgsalgorithmsetmvalue.cpp | ||
--------------------- | ||
begin : November 2019 | ||
copyright : (C) 2019 by Alexander Bruy | ||
email : alexander dot bruy 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 "qgsalgorithmsetmvalue.h" | ||
#include "qgsvectorlayer.h" | ||
|
||
///@cond PRIVATE | ||
|
||
QString QgsSetMValueAlgorithm::name() const | ||
{ | ||
return QStringLiteral( "setmvalue" ); | ||
} | ||
|
||
QString QgsSetMValueAlgorithm::displayName() const | ||
{ | ||
return QObject::tr( "Set M value" ); | ||
} | ||
|
||
QStringList QgsSetMValueAlgorithm::tags() const | ||
{ | ||
return QObject::tr( "set,add,m,measure,values" ).split( ',' ); | ||
} | ||
|
||
QString QgsSetMValueAlgorithm::group() const | ||
{ | ||
return QObject::tr( "Vector geometry" ); | ||
} | ||
|
||
QString QgsSetMValueAlgorithm::groupId() const | ||
{ | ||
return QStringLiteral( "vectorgeometry" ); | ||
} | ||
|
||
QString QgsSetMValueAlgorithm::shortHelpString() const | ||
{ | ||
return QObject::tr( "This algorithm sets the M value for geometries in a layer.\n\n" | ||
"If M values already exist in the layer, they will be overwritten " | ||
"with the new value. If no M values exist, the geometry will be " | ||
"upgraded to include M values and the specified value used as " | ||
"the initial M value for all geometries." ); | ||
} | ||
|
||
QString QgsSetMValueAlgorithm::outputName() const | ||
{ | ||
return QObject::tr( "M Added" ); | ||
} | ||
|
||
QgsSetMValueAlgorithm *QgsSetMValueAlgorithm::createInstance() const | ||
{ | ||
return new QgsSetMValueAlgorithm(); | ||
} | ||
|
||
bool QgsSetMValueAlgorithm::supportInPlaceEdit( const QgsMapLayer *l ) const | ||
{ | ||
const QgsVectorLayer *layer = qobject_cast< const QgsVectorLayer * >( l ); | ||
if ( !layer ) | ||
return false; | ||
|
||
return QgsProcessingFeatureBasedAlgorithm::supportInPlaceEdit( l ) && QgsWkbTypes::hasM( layer->wkbType() ); | ||
} | ||
|
||
QgsProcessingFeatureSource::Flag QgsSetMValueAlgorithm::sourceFlags() const | ||
{ | ||
return QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks; | ||
} | ||
|
||
QgsWkbTypes::Type QgsSetMValueAlgorithm::outputWkbType( QgsWkbTypes::Type type ) const | ||
{ | ||
return QgsWkbTypes::addM( type ); | ||
} | ||
|
||
void QgsSetMValueAlgorithm::initParameters( const QVariantMap & ) | ||
{ | ||
auto mValueParam = qgis::make_unique < QgsProcessingParameterNumber >( QStringLiteral( "M_VALUE" ), QObject::tr( "M Value" ), QgsProcessingParameterNumber::Double, 0.0 ); | ||
mValueParam->setIsDynamic( true ); | ||
mValueParam->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "M_VALUE" ), QObject::tr( "M Value" ), QgsPropertyDefinition::Double ) ); | ||
mValueParam->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) ); | ||
addParameter( mValueParam.release() ); | ||
} | ||
|
||
bool QgsSetMValueAlgorithm::prepareAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback * ) | ||
{ | ||
mMValue = parameterAsDouble( parameters, QStringLiteral( "M_VALUE" ), context ); | ||
mDynamicMValue = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "M_VALUE" ) ); | ||
if ( mDynamicMValue ) | ||
mMValueProperty = parameters.value( QStringLiteral( "M_VALUE" ) ).value< QgsProperty >(); | ||
|
||
return true; | ||
} | ||
|
||
QgsFeatureList QgsSetMValueAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * ) | ||
{ | ||
QgsFeature f = feature; | ||
|
||
if ( f.hasGeometry() ) | ||
{ | ||
std::unique_ptr< QgsAbstractGeometry > newGeometry( f.geometry().constGet()->clone() ); | ||
// addMValue won't alter existing M values, so drop them first | ||
if ( QgsWkbTypes::hasM( newGeometry->wkbType() ) ) | ||
newGeometry->dropMValue(); | ||
|
||
double m = mMValue; | ||
if ( mDynamicMValue ) | ||
m = mMValueProperty.valueAsDouble( context.expressionContext(), m ); | ||
|
||
newGeometry->addMValue( m ); | ||
|
||
f.setGeometry( QgsGeometry( std::move( newGeometry ) ) ); | ||
} | ||
|
||
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,65 @@ | ||
/*************************************************************************** | ||
qgsalgorithmsetmvalue.h | ||
--------------------- | ||
begin : November 2019 | ||
copyright : (C) 2019 by Alexander Bruy | ||
email : alexander dot bruy 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 QGSALGORITHMSETMVALUE_H | ||
#define QGSALGORITHMSETMVALUE_H | ||
|
||
#define SIP_NO_FILE | ||
|
||
#include "qgis_sip.h" | ||
#include "qgsprocessingalgorithm.h" | ||
|
||
///@cond PRIVATE | ||
|
||
/** | ||
* Native set M value algorithm. | ||
*/ | ||
class QgsSetMValueAlgorithm : public QgsProcessingFeatureBasedAlgorithm | ||
{ | ||
|
||
public: | ||
|
||
QgsSetMValueAlgorithm() = 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; | ||
QgsSetMValueAlgorithm *createInstance() const override SIP_FACTORY; | ||
|
||
protected: | ||
|
||
void initParameters( const QVariantMap &configuration = QVariantMap() ) override; | ||
QString outputName() const override; | ||
QgsWkbTypes::Type outputWkbType( QgsWkbTypes::Type inputWkbType ) const override; | ||
QgsProcessingFeatureSource::Flag sourceFlags() const override; | ||
bool supportInPlaceEdit( const QgsMapLayer *l ) const override; | ||
|
||
bool prepareAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override; | ||
QgsFeatureList processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override; | ||
|
||
private: | ||
|
||
double mMValue = 0.0; | ||
bool mDynamicMValue = false; | ||
QgsProperty mMValueProperty; | ||
}; | ||
|
||
///@endcond PRIVATE | ||
|
||
#endif // QGSALGORITHMSETMVALUE_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