Port Truncate alg to new API

This commit is contained in:
Nyall Dawson 2017-07-27 14:43:42 +10:00
parent 856125d366
commit 504cc1f390
4 changed files with 26 additions and 35 deletions

View File

@ -114,6 +114,7 @@ from .SumLines import SumLines
from .SymmetricalDifference import SymmetricalDifference from .SymmetricalDifference import SymmetricalDifference
from .TextToFloat import TextToFloat from .TextToFloat import TextToFloat
from .Translate import Translate from .Translate import Translate
from .TruncateTable import TruncateTable
from .Union import Union from .Union import Union
from .UniqueValues import UniqueValues from .UniqueValues import UniqueValues
from .VectorSplit import VectorSplit from .VectorSplit import VectorSplit
@ -165,7 +166,6 @@ from .ZonalStatistics import ZonalStatistics
# from .TinInterpolation import TinInterpolation # from .TinInterpolation import TinInterpolation
# from .ExtractSpecificNodes import ExtractSpecificNodes # from .ExtractSpecificNodes import ExtractSpecificNodes
# from .RasterCalculator import RasterCalculator # from .RasterCalculator import RasterCalculator
# from .TruncateTable import TruncateTable
# from .Polygonize import Polygonize # from .Polygonize import Polygonize
# from .ExecuteSQL import ExecuteSQL # from .ExecuteSQL import ExecuteSQL
# from .FindProjection import FindProjection # from .FindProjection import FindProjection
@ -202,10 +202,7 @@ class QGISAlgorithmProvider(QgsProcessingProvider):
# StatisticsByCategories(), # StatisticsByCategories(),
# RasterLayerStatistics(), PointsDisplacement(), # RasterLayerStatistics(), PointsDisplacement(),
# PointsFromPolygons(), # PointsFromPolygons(),
# PointsFromLines(), RandomPointsExtent(), # PointsFromLines(), PointsToPaths(),
# RandomPointsLayer(), RandomPointsPolygonsFixed(),
# RandomPointsPolygonsVariable(),
# RandomPointsAlongLines(), PointsToPaths(),
# SetVectorStyle(), SetRasterStyle(), # SetVectorStyle(), SetRasterStyle(),
# HypsometricCurves(), # HypsometricCurves(),
# SplitWithLines(), CreateConstantRaster(), # SplitWithLines(), CreateConstantRaster(),
@ -219,9 +216,7 @@ class QGISAlgorithmProvider(QgsProcessingProvider):
# IdwInterpolation(), TinInterpolation(), # IdwInterpolation(), TinInterpolation(),
# ExtractSpecificNodes(), # ExtractSpecificNodes(),
# RasterCalculator(), # RasterCalculator(),
# ShortestPathPointToPoint(), ShortestPathPointToLayer(), # Polygonize(),
# ShortestPathLayerToPoint(), ServiceAreaFromPoint(),
# ServiceAreaFromLayer(), TruncateTable(), Polygonize(),
# ExecuteSQL(), FindProjection(), # ExecuteSQL(), FindProjection(),
# TopoColor(), EliminateSelection() # TopoColor(), EliminateSelection()
# ] # ]
@ -299,6 +294,7 @@ class QGISAlgorithmProvider(QgsProcessingProvider):
SymmetricalDifference(), SymmetricalDifference(),
TextToFloat(), TextToFloat(),
Translate(), Translate(),
TruncateTable(),
Union(), Union(),
UniqueValues(), UniqueValues(),
VectorSplit(), VectorSplit(),

View File

@ -25,13 +25,10 @@ __copyright__ = '(C) 2017, Nyall Dawson'
__revision__ = '$Format:%H$' __revision__ = '$Format:%H$'
from qgis.core import (QgsApplication, from qgis.core import (QgsProcessingParameterVectorLayer,
QgsFeatureSink, QgsProcessingOutputVectorLayer,
QgsProcessingUtils) QgsProcessingException)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm 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): class TruncateTable(QgisAlgorithm):
@ -49,10 +46,9 @@ class TruncateTable(QgisAlgorithm):
super().__init__() super().__init__()
def initAlgorithm(self, config=None): def initAlgorithm(self, config=None):
self.addParameter(ParameterTable(self.INPUT, self.addParameter(QgsProcessingParameterVectorLayer(self.INPUT,
self.tr('Input Layer'))) self.tr('Input Layer')))
self.addOutput(OutputVector(self.OUTPUT, self.addOutput(QgsProcessingOutputVectorLayer(self.OUTPUT, self.tr('Truncated layer')))
self.tr('Truncated layer'), True))
def name(self): def name(self):
return 'truncatetable' return 'truncatetable'
@ -61,12 +57,11 @@ class TruncateTable(QgisAlgorithm):
return self.tr('Truncate table') return self.tr('Truncate table')
def processAlgorithm(self, parameters, context, feedback): def processAlgorithm(self, parameters, context, feedback):
file_name = self.getParameterValue(self.INPUT) layer = self.parameterAsVectorLayer(parameters, self.INPUT, context)
layer = QgsProcessingUtils.mapLayerFromString(file_name, context)
provider = layer.dataProvider() provider = layer.dataProvider()
if not provider.truncate(): if not provider.truncate():
raise GeoAlgorithmExecutionException( raise QgsProcessingException(
self.tr('Could not truncate table.')) self.tr('Could not truncate table.'))
self.setOutputValue(self.OUTPUT, file_name) return {self.OUTPUT: layer.id()}

View File

@ -256,7 +256,7 @@ class AlgorithmsTest(object):
expected_lyr = self.load_layer(id, expected_result) expected_lyr = self.load_layer(id, expected_result)
if 'in_place_result' in expected_result: if 'in_place_result' in expected_result:
result_lyr = QgsProcessingUtils.mapLayerFromString(self.in_place_layers[id], context) 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: else:
try: try:
results[id] results[id]

View File

@ -2261,18 +2261,18 @@ tests:
type: vector type: vector
in_place_result: true in_place_result: true
# - algorithm: qgis:truncatetable - algorithm: qgis:truncatetable
# name: Truncate table name: Truncate table
# params: params:
# INPUT: INPUT:
# name: custom/points.shp name: custom/points.shp
# type: vector type: vector
# in_place: true in_place: true
# results: results:
# INPUT: INPUT:
# name: expected/truncated.shp name: expected/truncated.shp
# type: vector type: vector
# in_place_result: true in_place_result: true
- algorithm: qgis:distancematrix - algorithm: qgis:distancematrix
name: Distance matrix (only tests for run, does not check result as rows are in random order) name: Distance matrix (only tests for run, does not check result as rows are in random order)