Skip to content

Commit 504cc1f

Browse files
committed
Port Truncate alg to new API
1 parent 856125d commit 504cc1f

File tree

4 files changed

+26
-35
lines changed

4 files changed

+26
-35
lines changed

python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
from .SymmetricalDifference import SymmetricalDifference
115115
from .TextToFloat import TextToFloat
116116
from .Translate import Translate
117+
from .TruncateTable import TruncateTable
117118
from .Union import Union
118119
from .UniqueValues import UniqueValues
119120
from .VectorSplit import VectorSplit
@@ -165,7 +166,6 @@
165166
# from .TinInterpolation import TinInterpolation
166167
# from .ExtractSpecificNodes import ExtractSpecificNodes
167168
# from .RasterCalculator import RasterCalculator
168-
# from .TruncateTable import TruncateTable
169169
# from .Polygonize import Polygonize
170170
# from .ExecuteSQL import ExecuteSQL
171171
# from .FindProjection import FindProjection
@@ -202,10 +202,7 @@ def getAlgs(self):
202202
# StatisticsByCategories(),
203203
# RasterLayerStatistics(), PointsDisplacement(),
204204
# PointsFromPolygons(),
205-
# PointsFromLines(), RandomPointsExtent(),
206-
# RandomPointsLayer(), RandomPointsPolygonsFixed(),
207-
# RandomPointsPolygonsVariable(),
208-
# RandomPointsAlongLines(), PointsToPaths(),
205+
# PointsFromLines(), PointsToPaths(),
209206
# SetVectorStyle(), SetRasterStyle(),
210207
# HypsometricCurves(),
211208
# SplitWithLines(), CreateConstantRaster(),
@@ -219,9 +216,7 @@ def getAlgs(self):
219216
# IdwInterpolation(), TinInterpolation(),
220217
# ExtractSpecificNodes(),
221218
# RasterCalculator(),
222-
# ShortestPathPointToPoint(), ShortestPathPointToLayer(),
223-
# ShortestPathLayerToPoint(), ServiceAreaFromPoint(),
224-
# ServiceAreaFromLayer(), TruncateTable(), Polygonize(),
219+
# Polygonize(),
225220
# ExecuteSQL(), FindProjection(),
226221
# TopoColor(), EliminateSelection()
227222
# ]
@@ -299,6 +294,7 @@ def getAlgs(self):
299294
SymmetricalDifference(),
300295
TextToFloat(),
301296
Translate(),
297+
TruncateTable(),
302298
Union(),
303299
UniqueValues(),
304300
VectorSplit(),

python/plugins/processing/algs/qgis/TruncateTable.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,10 @@
2525

2626
__revision__ = '$Format:%H$'
2727

28-
from qgis.core import (QgsApplication,
29-
QgsFeatureSink,
30-
QgsProcessingUtils)
28+
from qgis.core import (QgsProcessingParameterVectorLayer,
29+
QgsProcessingOutputVectorLayer,
30+
QgsProcessingException)
3131
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
32-
from processing.core.parameters import ParameterTable
33-
from processing.core.outputs import OutputVector
34-
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
3532

3633

3734
class TruncateTable(QgisAlgorithm):
@@ -49,10 +46,9 @@ def __init__(self):
4946
super().__init__()
5047

5148
def initAlgorithm(self, config=None):
52-
self.addParameter(ParameterTable(self.INPUT,
53-
self.tr('Input Layer')))
54-
self.addOutput(OutputVector(self.OUTPUT,
55-
self.tr('Truncated layer'), True))
49+
self.addParameter(QgsProcessingParameterVectorLayer(self.INPUT,
50+
self.tr('Input Layer')))
51+
self.addOutput(QgsProcessingOutputVectorLayer(self.OUTPUT, self.tr('Truncated layer')))
5652

5753
def name(self):
5854
return 'truncatetable'
@@ -61,12 +57,11 @@ def displayName(self):
6157
return self.tr('Truncate table')
6258

6359
def processAlgorithm(self, parameters, context, feedback):
64-
file_name = self.getParameterValue(self.INPUT)
65-
layer = QgsProcessingUtils.mapLayerFromString(file_name, context)
60+
layer = self.parameterAsVectorLayer(parameters, self.INPUT, context)
6661
provider = layer.dataProvider()
6762

6863
if not provider.truncate():
69-
raise GeoAlgorithmExecutionException(
64+
raise QgsProcessingException(
7065
self.tr('Could not truncate table.'))
7166

72-
self.setOutputValue(self.OUTPUT, file_name)
67+
return {self.OUTPUT: layer.id()}

python/plugins/processing/tests/AlgorithmsTestBase.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ def check_results(self, results, context, params, expected):
256256
expected_lyr = self.load_layer(id, expected_result)
257257
if 'in_place_result' in expected_result:
258258
result_lyr = QgsProcessingUtils.mapLayerFromString(self.in_place_layers[id], context)
259-
self.assertTrue(result_lyr, self.in_place_layers[id])
259+
self.assertTrue(result_lyr.isValid(), self.in_place_layers[id])
260260
else:
261261
try:
262262
results[id]

python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2261,18 +2261,18 @@ tests:
22612261
type: vector
22622262
in_place_result: true
22632263

2264-
# - algorithm: qgis:truncatetable
2265-
# name: Truncate table
2266-
# params:
2267-
# INPUT:
2268-
# name: custom/points.shp
2269-
# type: vector
2270-
# in_place: true
2271-
# results:
2272-
# INPUT:
2273-
# name: expected/truncated.shp
2274-
# type: vector
2275-
# in_place_result: true
2264+
- algorithm: qgis:truncatetable
2265+
name: Truncate table
2266+
params:
2267+
INPUT:
2268+
name: custom/points.shp
2269+
type: vector
2270+
in_place: true
2271+
results:
2272+
INPUT:
2273+
name: expected/truncated.shp
2274+
type: vector
2275+
in_place_result: true
22762276

22772277
- algorithm: qgis:distancematrix
22782278
name: Distance matrix (only tests for run, does not check result as rows are in random order)

0 commit comments

Comments
 (0)