Skip to content
Permalink
Browse files

Port delete columns alg to new API

  • Loading branch information
nyalldawson committed Jun 6, 2017
1 parent f1511a2 commit 1d2e54d1ba78af4c5514dfc58e2b71f103fd0b21
@@ -26,11 +26,12 @@
__revision__ = '$Format:%H$'

from qgis.core import (QgsApplication,
QgsProcessingUtils)
QgsProcessingUtils,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterFeatureSink,
QgsProcessingParameterTableField,
QgsProcessingOutputVectorLayer)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterTableField
from processing.core.outputs import OutputVector


class DeleteColumn(QgisAlgorithm):
@@ -53,11 +54,14 @@ def group(self):

def __init__(self):
super().__init__()
self.addParameter(ParameterVector(self.INPUT,
self.tr('Input layer')))
self.addParameter(ParameterTableField(self.COLUMNS,
self.tr('Fields to delete'), self.INPUT, multiple=True))
self.addOutput(OutputVector(self.OUTPUT, self.tr('Output layer')))

self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT, self.tr('Input layer')))
self.addParameter(QgsProcessingParameterTableField(self.COLUMNS,
self.tr('Fields to delete'),
None, self.INPUT, QgsProcessingParameterTableField.Any, True))

self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Output layer')))
self.addOutput(QgsProcessingOutputVectorLayer(self.OUTPUT, self.tr("Output layer")))

def name(self):
return 'deletecolumn'
@@ -66,10 +70,10 @@ def displayName(self):
return self.tr('Delete column')

def processAlgorithm(self, parameters, context, feedback):
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)
source = self.parameterAsSource(parameters, self.INPUT, context)
fields_to_delete = self.parameterAsFields(parameters, self.COLUMNS, context)

fields_to_delete = self.getParameterValue(self.COLUMNS).split(';')
fields = layer.fields()
fields = source.fields()
field_indices = []
# loop through twice - first we need to build up a list of original attribute indices
for f in fields_to_delete:
@@ -83,18 +87,22 @@ def processAlgorithm(self, parameters, context, feedback):
for index in field_indices:
fields.remove(index)

writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields, layer.wkbType(), layer.crs(), context)
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
fields, source.wkbType(), source.sourceCrs())

features = QgsProcessingUtils.getFeatures(layer, context)
total = 100.0 / QgsProcessingUtils.featureCount(layer, context)
features = source.getFeatures()
total = 100.0 / source.featureCount()

for current, f in enumerate(features):
if feedback.isCanceled():
break

attributes = f.attributes()
for index in field_indices:
del attributes[index]
f.setAttributes(attributes)
writer.addFeature(f)
sink.addFeature(f)

feedback.setProgress(int(current * total))

del writer
return {self.OUTPUT: dest_id}
@@ -86,7 +86,7 @@
# from .Union import Union
# from .DensifyGeometriesInterval import DensifyGeometriesInterval
# from .SpatialJoin import SpatialJoin
# from .DeleteColumn import DeleteColumn
from .DeleteColumn import DeleteColumn
# from .DeleteHoles import DeleteHoles
# from .DeleteDuplicateGeometries import DeleteDuplicateGeometries
# from .TextToFloat import TextToFloat
@@ -216,7 +216,7 @@ def getAlgs(self):
# RandomExtractWithinSubsets(), ExtractByLocation(),
# SpatialJoin(), RegularPoints(), SymmetricalDifference(),
# VectorSplit(), VectorGridLines(), VectorGridPolygons(),
# DeleteColumn(), DeleteDuplicateGeometries(), TextToFloat(),
# DeleteDuplicateGeometries(), TextToFloat(),
# ExtractByAttribute(), SelectByAttribute(), GridPolygon(),
# GridLine(), Gridify(), HubDistancePoints(),
# HubDistanceLines(), HubLines(), Merge(),
@@ -264,7 +264,9 @@ def getAlgs(self):
AutoincrementalField(),
Boundary(),
BoundingBox(),
Clip()]
Clip(),
DeleteColumn()
]

if hasPlotly:
# from .VectorLayerHistogram import VectorLayerHistogram
@@ -1577,30 +1577,30 @@ tests:
# type: vector
# results: {}
#
# - algorithm: qgis:deletecolumn
# name: Delete columns (multiple)
# params:
# COLUMN: floatval;name
# INPUT:
# name: polys.gml
# type: vector
# results:
# OUTPUT:
# name: expected/delete_columns.gml
# type: vector
#
# - algorithm: qgis:deletecolumn
# name: Delete columns (single)
# params:
# COLUMN: intval
# INPUT:
# name: polys.gml
# type: vector
# results:
# OUTPUT:
# name: expected/delete_column.gml
# type: vector
#
- algorithm: qgis:deletecolumn
name: Delete columns (multiple)
params:
COLUMN: floatval;name
INPUT:
name: polys.gml
type: vector
results:
OUTPUT:
name: expected/delete_columns.gml
type: vector

- algorithm: qgis:deletecolumn
name: Delete columns (single)
params:
COLUMN: intval
INPUT:
name: polys.gml
type: vector
results:
OUTPUT:
name: expected/delete_column.gml
type: vector

# - algorithm: qgis:createpointslayerfromtable
# name: Create points from table
# params:

0 comments on commit 1d2e54d

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