Skip to content

Commit

Permalink
[processing] port set Z value algorithm to C++
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy authored and nyalldawson committed Nov 25, 2019
1 parent 9411d96 commit f1d6080
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 114 deletions.
5 changes: 0 additions & 5 deletions python/plugins/processing/algs/help/qgis.yaml
Expand Up @@ -453,11 +453,6 @@ qgis:setstyleforrasterlayer: >
qgis:setstyleforvectorlayer: >
This algorithm sets the style of a vector layer. The style must be defined in a QML file.

qgis:setzvalue: >
This algorithm sets the Z value for geometries in a layer.

If Z values already exist in the layer, they will be overwritten with the new value. If no Z values exist, the geometry will be upgraded to include Z values and the specified value used as the initial Z value for all geometries.

qgis:singlesidedbuffer: >
This algorithm buffers lines by a specified distance on one side of the line only.

Expand Down
2 changes: 0 additions & 2 deletions python/plugins/processing/algs/qgis/QgisAlgorithmProvider.py
Expand Up @@ -96,7 +96,6 @@
from .SelectByExpression import SelectByExpression
from .SetRasterStyle import SetRasterStyle
from .SetVectorStyle import SetVectorStyle
from .SetZValue import SetZValue
from .SingleSidedBuffer import SingleSidedBuffer
from .SnapGeometries import SnapGeometriesToLayer
from .SpatialiteExecuteSQL import SpatialiteExecuteSQL
Expand Down Expand Up @@ -196,7 +195,6 @@ def getAlgs(self):
SelectByExpression(),
SetRasterStyle(),
SetVectorStyle(),
SetZValue(),
SingleSidedBuffer(),
SnapGeometriesToLayer(),
SpatialiteExecuteSQL(),
Expand Down
106 changes: 0 additions & 106 deletions python/plugins/processing/algs/qgis/SetZValue.py

This file was deleted.

Expand Up @@ -877,7 +877,7 @@ tests:
name: expected/set_m_value.shp
type: vector

- algorithm: qgis:setzvalue
- algorithm: native:setzvalue
name: Set Z Value
params:
INPUT:
Expand Down
1 change: 1 addition & 0 deletions src/analysis/CMakeLists.txt
Expand Up @@ -112,6 +112,7 @@ SET(QGIS_ANALYSIS_SRCS
processing/qgsalgorithmserviceareafromlayer.cpp
processing/qgsalgorithmserviceareafrompoint.cpp
processing/qgsalgorithmsetmvalue.cpp
processing/qgsalgorithmsetzvalue.cpp
processing/qgsalgorithmshortestpathlayertopoint.cpp
processing/qgsalgorithmshortestpathpointtolayer.cpp
processing/qgsalgorithmshortestpathpointtopoint.cpp
Expand Down
128 changes: 128 additions & 0 deletions src/analysis/processing/qgsalgorithmsetzvalue.cpp
@@ -0,0 +1,128 @@
/***************************************************************************
qgsalgorithmsetzvalue.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 "qgsalgorithmsetzvalue.h"
#include "qgsvectorlayer.h"

///@cond PRIVATE

QString QgsSetZValueAlgorithm::name() const
{
return QStringLiteral( "setzvalue" );
}

QString QgsSetZValueAlgorithm::displayName() const
{
return QObject::tr( "Set Z value" );
}

QStringList QgsSetZValueAlgorithm::tags() const
{
return QObject::tr( "set,add,z,25d,3d,values" ).split( ',' );
}

QString QgsSetZValueAlgorithm::group() const
{
return QObject::tr( "Vector geometry" );
}

QString QgsSetZValueAlgorithm::groupId() const
{
return QStringLiteral( "vectorgeometry" );
}

QString QgsSetZValueAlgorithm::shortHelpString() const
{
return QObject::tr( "This algorithm sets the Z value for geometries in a layer.\n\n"
"If Z values already exist in the layer, they will be overwritten "
"with the new value. If no Z values exist, the geometry will be "
"upgraded to include Z values and the specified value used as "
"the initial Z value for all geometries." );
}

QString QgsSetZValueAlgorithm::outputName() const
{
return QObject::tr( "Z Added" );
}

QgsSetZValueAlgorithm *QgsSetZValueAlgorithm::createInstance() const
{
return new QgsSetZValueAlgorithm();
}

bool QgsSetZValueAlgorithm::supportInPlaceEdit( const QgsMapLayer *l ) const
{
const QgsVectorLayer *layer = qobject_cast< const QgsVectorLayer * >( l );
if ( !layer )
return false;

return QgsProcessingFeatureBasedAlgorithm::supportInPlaceEdit( l ) && QgsWkbTypes::hasZ( layer->wkbType() );
}

QgsProcessingFeatureSource::Flag QgsSetZValueAlgorithm::sourceFlags() const
{
return QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks;
}

QgsWkbTypes::Type QgsSetZValueAlgorithm::outputWkbType( QgsWkbTypes::Type type ) const
{
return QgsWkbTypes::addZ( type );
}

void QgsSetZValueAlgorithm::initParameters( const QVariantMap & )
{
auto zValueParam = qgis::make_unique < QgsProcessingParameterNumber >( QStringLiteral( "Z_VALUE" ), QObject::tr( "Z Value" ), QgsProcessingParameterNumber::Double, 0.0 );
zValueParam->setIsDynamic( true );
zValueParam->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "Z_VALUE" ), QObject::tr( "Z Value" ), QgsPropertyDefinition::Double ) );
zValueParam->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
addParameter( zValueParam.release() );
}

bool QgsSetZValueAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
{
mZValue = parameterAsDouble( parameters, QStringLiteral( "Z_VALUE" ), context );
mDynamicZValue = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "Z_VALUE" ) );
if ( mDynamicZValue )
mZValueProperty = parameters.value( QStringLiteral( "Z_VALUE" ) ).value< QgsProperty >();

return true;
}

QgsFeatureList QgsSetZValueAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
{
QgsFeature f = feature;

if ( f.hasGeometry() )
{
std::unique_ptr< QgsAbstractGeometry > newGeometry( f.geometry().constGet()->clone() );
// addZValue won't alter existing Z values, so drop them first
if ( QgsWkbTypes::hasZ( newGeometry->wkbType() ) )
newGeometry->dropZValue();

double z = mZValue;
if ( mDynamicZValue )
z = mZValueProperty.valueAsDouble( context.expressionContext(), z );

newGeometry->addZValue( z );

f.setGeometry( QgsGeometry( std::move( newGeometry ) ) );
}

return QgsFeatureList() << f;
}

///@endcond
65 changes: 65 additions & 0 deletions src/analysis/processing/qgsalgorithmsetzvalue.h
@@ -0,0 +1,65 @@
/***************************************************************************
qgsalgorithmsetzvalue.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 QGSALGORITHMSETZVALUE_H
#define QGSALGORITHMSETZVALUE_H

#define SIP_NO_FILE

#include "qgis_sip.h"
#include "qgsprocessingalgorithm.h"

///@cond PRIVATE

/**
* Native set Z value algorithm.
*/
class QgsSetZValueAlgorithm : public QgsProcessingFeatureBasedAlgorithm
{

public:

QgsSetZValueAlgorithm() = 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;
QgsSetZValueAlgorithm *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 &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
QgsFeatureList processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;

private:

double mZValue = 0.0;
bool mDynamicZValue = false;
QgsProperty mZValueProperty;
};

///@endcond PRIVATE

#endif // QGSALGORITHMSETZVALUE_H
2 changes: 2 additions & 0 deletions src/analysis/processing/qgsnativealgorithms.cpp
Expand Up @@ -106,6 +106,7 @@
#include "qgsalgorithmserviceareafromlayer.h"
#include "qgsalgorithmserviceareafrompoint.h"
#include "qgsalgorithmsetmvalue.h"
#include "qgsalgorithmsetzvalue.h"
#include "qgsalgorithmshortestpathlayertopoint.h"
#include "qgsalgorithmshortestpathpointtolayer.h"
#include "qgsalgorithmshortestpathpointtopoint.h"
Expand Down Expand Up @@ -281,6 +282,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
addAlgorithm( new QgsServiceAreaFromLayerAlgorithm() );
addAlgorithm( new QgsServiceAreaFromPointAlgorithm() );
addAlgorithm( new QgsSetMValueAlgorithm() );
addAlgorithm( new QgsSetZValueAlgorithm() );
addAlgorithm( new QgsShortestPathLayerToPointAlgorithm() );
addAlgorithm( new QgsShortestPathPointToLayerAlgorithm() );
addAlgorithm( new QgsShortestPathPointToPointAlgorithm() );
Expand Down

0 comments on commit f1d6080

Please sign in to comment.