Skip to content

Commit

Permalink
[processing] port truncate table to C++
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Dec 9, 2019
1 parent c974125 commit 4622661
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 81 deletions.
8 changes: 0 additions & 8 deletions python/plugins/processing/algs/help/qgis.yaml
Expand Up @@ -432,14 +432,6 @@ qgis:topologicalcoloring: >

A minimum number of colors can be specified if desired. The color index is saved to a new attribute named color_id.

qgis:translate: >
This algorithm moves the geometries within a layer, by offsetting them with a specified x and y displacement.

qgis:truncatetable: >
This algorithm truncates a layer, by deleting all features from within the layer.

Warning - this algorithm modifies the layer in place, and deleted features cannot be restored!

qgis:variabledistancebuffer: >
This algorithm computes a buffer area for all the features in an input layer. The size of the buffer for a given feature is defined by an attribute, so it allows different features to have different buffer sizes.

Expand Down
2 changes: 0 additions & 2 deletions python/plugins/processing/algs/qgis/QgisAlgorithmProvider.py
Expand Up @@ -95,7 +95,6 @@
from .TilesXYZ import TilesXYZAlgorithmDirectory, TilesXYZAlgorithmMBTiles
from .TinInterpolation import TinInterpolation
from .TopoColors import TopoColor
from .TruncateTable import TruncateTable
from .UniqueValues import UniqueValues
from .VariableDistanceBuffer import VariableDistanceBuffer
from .VectorLayerHistogram import VectorLayerHistogram
Expand Down Expand Up @@ -183,7 +182,6 @@ def getAlgs(self):
TilesXYZAlgorithmMBTiles(),
TinInterpolation(),
TopoColor(),
TruncateTable(),
UniqueValues(),
VariableDistanceBuffer(),
VectorLayerHistogram(),
Expand Down
70 changes: 0 additions & 70 deletions python/plugins/processing/algs/qgis/TruncateTable.py

This file was deleted.

Expand Up @@ -1932,7 +1932,7 @@ tests:
type: vector
in_place_result: true

- algorithm: qgis:truncatetable
- algorithm: native:truncatetable
name: Truncate table
params:
INPUT:
Expand Down
1 change: 1 addition & 0 deletions src/analysis/CMakeLists.txt
Expand Up @@ -147,6 +147,7 @@ SET(QGIS_ANALYSIS_SRCS
processing/qgsalgorithmtransect.cpp
processing/qgsalgorithmtransform.cpp
processing/qgsalgorithmtranslate.cpp
processing/qgsalgorithmtruncatetable.cpp
processing/qgsalgorithmunion.cpp
processing/qgsalgorithmuniquevalueindex.cpp
processing/qgsalgorithmvectorize.cpp
Expand Down
88 changes: 88 additions & 0 deletions src/analysis/processing/qgsalgorithmtruncatetable.cpp
@@ -0,0 +1,88 @@
/***************************************************************************
qgsalgorithmtruncatetable.cpp
---------------------
begin : December 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 "qgsalgorithmtruncatetable.h"
#include "qgsvectorlayer.h"

///@cond PRIVATE

QString QgsTruncateTableAlgorithm::name() const
{
return QStringLiteral( "truncatetable" );
}

QString QgsTruncateTableAlgorithm::displayName() const
{
return QObject::tr( "Truncate table" );
}

QStringList QgsTruncateTableAlgorithm::tags() const
{
return QObject::tr( "empty,delete,layer,clear,features" ).split( ',' );
}

QString QgsTruncateTableAlgorithm::group() const
{
return QObject::tr( "Vector general" );
}

QString QgsTruncateTableAlgorithm::groupId() const
{
return QStringLiteral( "vectorgeneral" );
}

QString QgsTruncateTableAlgorithm::shortHelpString() const
{
return QObject::tr( "This algorithm truncates a layer, by deleting all features from within the layer." )
+ QStringLiteral( "\n\n" )
+ QObject::tr( "Warning — this algorithm modifies the layer in place, and deleted features cannot be restored!" );
}

QgsProcessingAlgorithm::Flags QgsTruncateTableAlgorithm::flags() const
{
return QgsProcessingAlgorithm::flags() | QgsProcessingAlgorithm::FlagNoThreading;
}

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

void QgsTruncateTableAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterVectorLayer( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
addOutput( new QgsProcessingOutputVectorLayer( QStringLiteral( "OUTPUT" ), QObject::tr( "Truncated layer" ) ) );
}

QVariantMap QgsTruncateTableAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
{
QgsVectorLayer *layer = parameterAsVectorLayer( parameters, QStringLiteral( "INPUT" ), context );

if ( !layer )
throw QgsProcessingException( QObject::tr( "Invalid input layer" ) );

if ( !layer->dataProvider()->truncate() )
{
throw QgsProcessingException( QObject::tr( "Could not truncate table." ) );
}

QVariantMap outputs;
outputs.insert( QStringLiteral( "OUTPUT" ), layer->id() );
return outputs;
}

///@endcond
55 changes: 55 additions & 0 deletions src/analysis/processing/qgsalgorithmtruncatetable.h
@@ -0,0 +1,55 @@
/***************************************************************************
qgsalgorithmtruncatetable.h
------------------------------
begin : December 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 QGSALGORITHMTRUNCATETABLE_H
#define QGSALGORITHMTRUNCATETABLE_H

#define SIP_NO_FILE

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

///@cond PRIVATE

/**
* Native truncate table algorithm.
*/
class QgsTruncateTableAlgorithm : public QgsProcessingAlgorithm
{

public:

QgsTruncateTableAlgorithm() = default;
Flags flags() const override;
QString name() const override;
QString displayName() const override;
QStringList tags() const override;
QString group() const override;
QString groupId() const override;
QString shortHelpString() const override;
void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
QgsTruncateTableAlgorithm *createInstance() const override SIP_FACTORY;

protected:

QVariantMap processAlgorithm( const QVariantMap &parameters,
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
};

///@endcond PRIVATE

#endif // QGSALGORITHMTRUNCATETABLE_H
2 changes: 2 additions & 0 deletions src/analysis/processing/qgsnativealgorithms.cpp
Expand Up @@ -141,6 +141,7 @@
#include "qgsalgorithmtransect.h"
#include "qgsalgorithmtransform.h"
#include "qgsalgorithmtranslate.h"
#include "qgsalgorithmtruncatetable.h"
#include "qgsalgorithmunion.h"
#include "qgsalgorithmuniquevalueindex.h"
#include "qgsalgorithmvectorize.h"
Expand Down Expand Up @@ -332,6 +333,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
addAlgorithm( new QgsTransectAlgorithm() );
addAlgorithm( new QgsTransformAlgorithm() );
addAlgorithm( new QgsTranslateAlgorithm() );
addAlgorithm( new QgsTruncateTableAlgorithm() );
addAlgorithm( new QgsUnionAlgorithm() );
addAlgorithm( new QgsVariableWidthBufferByMAlgorithm() );
addAlgorithm( new QgsWedgeBuffersAlgorithm() );
Expand Down

0 comments on commit 4622661

Please sign in to comment.