Skip to content

Commit

Permalink
Port delete columns alg to new API
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 6, 2017
1 parent f1511a2 commit 1d2e54d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 44 deletions.
42 changes: 25 additions & 17 deletions python/plugins/processing/algs/qgis/DeleteColumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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'
Expand All @@ -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:
Expand All @@ -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}
8 changes: 5 additions & 3 deletions python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -264,7 +264,9 @@ def getAlgs(self):
AutoincrementalField(),
Boundary(),
BoundingBox(),
Clip()]
Clip(),
DeleteColumn()
]

if hasPlotly:
# from .VectorLayerHistogram import VectorLayerHistogram
Expand Down
48 changes: 24 additions & 24 deletions python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 1d2e54d

Please sign in to comment.