Skip to content

Commit e5e0ec5

Browse files
committedJan 6, 2017
[FEATURE][processing] New algorithm to truncate tables
Irretrievably deletes all features from a table... use with caution!
1 parent bce716c commit e5e0ec5

File tree

10 files changed

+81
-1
lines changed

10 files changed

+81
-1
lines changed
 

‎python/plugins/processing/algs/help/qgis.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,11 @@ qgis:texttofloat: >
540540
qgis:translate: >
541541
This algorithm moves the geometries within a layer, by offsetting them with a specified x and y displacement.
542542

543+
qgis:truncatetable: >
544+
This algorithm truncates a layer, by deleting all features from within the layer.
545+
546+
Warning - this algorithm modifies the layer in place, and deleted features cannot be restored!
547+
543548
qgis:union: >
544549
This algorithm creates a layer containing all the features from both input layers. In the case of polygon layers, separate features are created for overlapping and non-overlapping features. The attribute table of the union layer contains attribute values from the respective input layer for non-overlapping features, and attribute values from both input layers for overlapping features.
545550

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@
187187
from .ShortestPathLayerToPoint import ShortestPathLayerToPoint
188188
from .ServiceAreaFromPoint import ServiceAreaFromPoint
189189
from .ServiceAreaFromLayer import ServiceAreaFromLayer
190+
from .TruncateTable import TruncateTable
190191

191192
pluginPath = os.path.normpath(os.path.join(
192193
os.path.split(os.path.dirname(__file__))[0], os.pardir))
@@ -255,7 +256,7 @@ def __init__(self):
255256
RasterCalculator(), Heatmap(), Orthogonalize(),
256257
ShortestPathPointToPoint(), ShortestPathPointToLayer(),
257258
ShortestPathLayerToPoint(), ServiceAreaFromPoint(),
258-
ServiceAreaFromLayer()
259+
ServiceAreaFromLayer(), TruncateTable()
259260
]
260261

261262
if hasMatplotlib:
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
TruncateTable.py
6+
-----------------------
7+
Date : January 2017
8+
Copyright : (C) 2017 by Nyall Dawson
9+
Email : nyall dot dawson at gmail dot com
10+
***************************************************************************
11+
* *
12+
* This program is free software; you can redistribute it and/or modify *
13+
* it under the terms of the GNU General Public License as published by *
14+
* the Free Software Foundation; either version 2 of the License, or *
15+
* (at your option) any later version. *
16+
* *
17+
***************************************************************************
18+
"""
19+
20+
__author__ = 'Nyall Dawson'
21+
__date__ = 'January 2017'
22+
__copyright__ = '(C) 2017, Nyall Dawson'
23+
24+
# This will get replaced with a git SHA1 when you do a git archive
25+
26+
__revision__ = '$Format:%H$'
27+
28+
from processing.core.GeoAlgorithm import GeoAlgorithm
29+
from processing.core.parameters import ParameterTable
30+
from processing.core.outputs import OutputVector
31+
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
32+
from processing.tools import dataobjects
33+
34+
35+
class TruncateTable(GeoAlgorithm):
36+
37+
INPUT = 'INPUT'
38+
OUTPUT = 'OUTPUT'
39+
40+
def defineCharacteristics(self):
41+
self.name, self.i18n_name = self.trAlgorithm('Truncate table')
42+
self.tags = self.tr('empty,delete,layer,clear,features')
43+
self.group, self.i18n_group = self.trAlgorithm('Vector general tools')
44+
45+
self.addParameter(ParameterTable(self.INPUT,
46+
self.tr('Input Layer')))
47+
self.addOutput(OutputVector(self.OUTPUT,
48+
self.tr('Truncated layer'), True))
49+
50+
def processAlgorithm(self, progress):
51+
file_name = self.getParameterValue(self.INPUT)
52+
layer = dataobjects.getObjectFromUri(file_name)
53+
provider = layer.dataProvider()
54+
55+
if not provider.truncate():
56+
raise GeoAlgorithmExecutionException(
57+
self.tr('Could not truncate table.'))
58+
59+
self.setOutputValue(self.OUTPUT, file_name)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
UTF-8
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]
Binary file not shown.
Binary file not shown.

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2118,3 +2118,15 @@ tests:
21182118
type: vector
21192119
in_place_result: true
21202120

2121+
- algorithm: qgis:truncatetable
2122+
name: Truncate table
2123+
params:
2124+
INPUT:
2125+
name: custom/points.shp
2126+
type: vector
2127+
in_place: true
2128+
results:
2129+
INPUT:
2130+
name: expected/truncated.shp
2131+
type: vector
2132+
in_place_result: true

0 commit comments

Comments
 (0)
Please sign in to comment.