Skip to content

Commit

Permalink
Port Truncate alg to new API
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 27, 2017
1 parent 856125d commit 504cc1f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 35 deletions.
12 changes: 4 additions & 8 deletions python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py
Expand Up @@ -114,6 +114,7 @@
from .SymmetricalDifference import SymmetricalDifference
from .TextToFloat import TextToFloat
from .Translate import Translate
from .TruncateTable import TruncateTable
from .Union import Union
from .UniqueValues import UniqueValues
from .VectorSplit import VectorSplit
Expand Down Expand Up @@ -165,7 +166,6 @@
# from .TinInterpolation import TinInterpolation
# from .ExtractSpecificNodes import ExtractSpecificNodes
# from .RasterCalculator import RasterCalculator
# from .TruncateTable import TruncateTable
# from .Polygonize import Polygonize
# from .ExecuteSQL import ExecuteSQL
# from .FindProjection import FindProjection
Expand Down Expand Up @@ -202,10 +202,7 @@ def getAlgs(self):
# StatisticsByCategories(),
# RasterLayerStatistics(), PointsDisplacement(),
# PointsFromPolygons(),
# PointsFromLines(), RandomPointsExtent(),
# RandomPointsLayer(), RandomPointsPolygonsFixed(),
# RandomPointsPolygonsVariable(),
# RandomPointsAlongLines(), PointsToPaths(),
# PointsFromLines(), PointsToPaths(),
# SetVectorStyle(), SetRasterStyle(),
# HypsometricCurves(),
# SplitWithLines(), CreateConstantRaster(),
Expand All @@ -219,9 +216,7 @@ def getAlgs(self):
# IdwInterpolation(), TinInterpolation(),
# ExtractSpecificNodes(),
# RasterCalculator(),
# ShortestPathPointToPoint(), ShortestPathPointToLayer(),
# ShortestPathLayerToPoint(), ServiceAreaFromPoint(),
# ServiceAreaFromLayer(), TruncateTable(), Polygonize(),
# Polygonize(),
# ExecuteSQL(), FindProjection(),
# TopoColor(), EliminateSelection()
# ]
Expand Down Expand Up @@ -299,6 +294,7 @@ def getAlgs(self):
SymmetricalDifference(),
TextToFloat(),
Translate(),
TruncateTable(),
Union(),
UniqueValues(),
VectorSplit(),
Expand Down
23 changes: 9 additions & 14 deletions python/plugins/processing/algs/qgis/TruncateTable.py
Expand Up @@ -25,13 +25,10 @@

__revision__ = '$Format:%H$'

from qgis.core import (QgsApplication,
QgsFeatureSink,
QgsProcessingUtils)
from qgis.core import (QgsProcessingParameterVectorLayer,
QgsProcessingOutputVectorLayer,
QgsProcessingException)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.parameters import ParameterTable
from processing.core.outputs import OutputVector
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException


class TruncateTable(QgisAlgorithm):
Expand All @@ -49,10 +46,9 @@ def __init__(self):
super().__init__()

def initAlgorithm(self, config=None):
self.addParameter(ParameterTable(self.INPUT,
self.tr('Input Layer')))
self.addOutput(OutputVector(self.OUTPUT,
self.tr('Truncated layer'), True))
self.addParameter(QgsProcessingParameterVectorLayer(self.INPUT,
self.tr('Input Layer')))
self.addOutput(QgsProcessingOutputVectorLayer(self.OUTPUT, self.tr('Truncated layer')))

def name(self):
return 'truncatetable'
Expand All @@ -61,12 +57,11 @@ def displayName(self):
return self.tr('Truncate table')

def processAlgorithm(self, parameters, context, feedback):
file_name = self.getParameterValue(self.INPUT)
layer = QgsProcessingUtils.mapLayerFromString(file_name, context)
layer = self.parameterAsVectorLayer(parameters, self.INPUT, context)
provider = layer.dataProvider()

if not provider.truncate():
raise GeoAlgorithmExecutionException(
raise QgsProcessingException(
self.tr('Could not truncate table.'))

self.setOutputValue(self.OUTPUT, file_name)
return {self.OUTPUT: layer.id()}
2 changes: 1 addition & 1 deletion python/plugins/processing/tests/AlgorithmsTestBase.py
Expand Up @@ -256,7 +256,7 @@ def check_results(self, results, context, params, expected):
expected_lyr = self.load_layer(id, expected_result)
if 'in_place_result' in expected_result:
result_lyr = QgsProcessingUtils.mapLayerFromString(self.in_place_layers[id], context)
self.assertTrue(result_lyr, self.in_place_layers[id])
self.assertTrue(result_lyr.isValid(), self.in_place_layers[id])
else:
try:
results[id]
Expand Down
24 changes: 12 additions & 12 deletions python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml
Expand Up @@ -2261,18 +2261,18 @@ tests:
type: vector
in_place_result: true

# - algorithm: qgis:truncatetable
# name: Truncate table
# params:
# INPUT:
# name: custom/points.shp
# type: vector
# in_place: true
# results:
# INPUT:
# name: expected/truncated.shp
# type: vector
# in_place_result: true
- algorithm: qgis:truncatetable
name: Truncate table
params:
INPUT:
name: custom/points.shp
type: vector
in_place: true
results:
INPUT:
name: expected/truncated.shp
type: vector
in_place_result: true

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

0 comments on commit 504cc1f

Please sign in to comment.