Skip to content

Commit 07d584d

Browse files
committed
start refactoring of the Processing mmqgis backend: Delete column and
Delete duplicate geometries
1 parent 25fa170 commit 07d584d

File tree

4 files changed

+178
-19
lines changed

4 files changed

+178
-19
lines changed

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@
7373
from ftools.Eliminate import Eliminate
7474
from ftools.SpatialJoin import SpatialJoin
7575

76-
from mmqgisx.MMQGISXAlgorithms import *
76+
from mmqgisx.DeleteColumn import DeleteColumn
77+
from mmqgisx.DeleteDuplicateGeometries import DeleteDuplicateGeometries
7778

7879
from ConcaveHull import ConcaveHull
7980
from Polygonize import Polygonize
@@ -138,17 +139,17 @@ def __init__(self):
138139
RandomExtractWithinSubsets(), ExtractByLocation(),
139140
SpatialJoin(),
140141
# ------ mmqgisx ------
141-
mmqgisx_delete_columns_algorithm(),
142-
mmqgisx_delete_duplicate_geometries_algorithm(),
143-
mmqgisx_geometry_convert_algorithm(),
144-
mmqgisx_grid_algorithm(),
145-
mmqgisx_gridify_algorithm(),
146-
mmqgisx_hub_distance_algorithm(),
147-
mmqgisx_hub_lines_algorithm(),
148-
mmqgisx_merge_algorithm(),
149-
mmqgisx_select_algorithm(),
150-
mmqgisx_extract_algorithm(),
151-
mmqgisx_text_to_float_algorithm(),
142+
DeleteColumn(), DeleteDuplicateGeometries(),
143+
#mmqgisx_delete_duplicate_geometries_algorithm(),
144+
#mmqgisx_geometry_convert_algorithm(),
145+
#mmqgisx_grid_algorithm(),
146+
#mmqgisx_gridify_algorithm(),
147+
#mmqgisx_hub_distance_algorithm(),
148+
#mmqgisx_hub_lines_algorithm(),
149+
#mmqgisx_merge_algorithm(),
150+
#mmqgisx_select_algorithm(),
151+
#mmqgisx_extract_algorithm(),
152+
#mmqgisx_text_to_float_algorithm(),
152153
# ------ native algs ------
153154
AddTableField(), FieldsCalculator(),
154155
SaveSelectedFeatures(), JoinAttributes(),
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
DeleteColumn.py
6+
---------------------
7+
Date : May 2010
8+
Copyright : (C) 2010 by Michael Minn
9+
Email : pyqgis at michaelminn 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__ = 'Michael Minn'
21+
__date__ = 'May 2010'
22+
__copyright__ = '(C) 2010, Michael Minn'
23+
24+
# This will get replaced with a git SHA1 when you do a git archive
25+
26+
__revision__ = '$Format:%H$'
27+
28+
from PyQt4.QtCore import *
29+
from qgis.core import *
30+
from processing.core.GeoAlgorithm import GeoAlgorithm
31+
from processing.core.parameters import ParameterVector
32+
from processing.core.parameters import ParameterTableField
33+
from processing.core.outputs import OutputVector
34+
from processing.tools import dataobjects, vector
35+
36+
37+
class DeleteColumn(GeoAlgorithm):
38+
INPUT = 'INPUT'
39+
COLUMN = 'COLUMN'
40+
OUTPUT = 'OUTPUT'
41+
42+
def defineCharacteristics(self):
43+
self.name = 'Delete column'
44+
self.group = 'Vector table tools'
45+
46+
self.addParameter(ParameterVector(
47+
self.INPUT, 'Input layer', [ParameterVector.VECTOR_TYPE_ANY]))
48+
self.addParameter(ParameterTableField(
49+
self.COLUMN, 'Field to delete', self.INPUT))
50+
self.addOutput(OutputVector(self.OUTPUT, 'Output'))
51+
52+
def processAlgorithm(self, progress):
53+
layer = dataobjects.getObjectFromUri(
54+
self.getParameterValue(self.INPUT))
55+
idx = layer.fieldNameIndex(self.getParameterValue(self.COLUMN))
56+
57+
fields = layer.pendingFields()
58+
fields.remove(idx)
59+
60+
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields,
61+
layer.wkbType(), layer.crs())
62+
63+
features = vector.features(layer)
64+
count = len(features)
65+
total = 100.0 / float(count)
66+
67+
feat = QgsFeature()
68+
for count, f in enumerate(features):
69+
feat.setGeometry(f.geometry())
70+
attributes = f.attributes()
71+
del attributes[idx]
72+
feat.setAttributes(attributes)
73+
writer.addFeature(feat)
74+
75+
progress.setPercentage(int(count * total))
76+
77+
del writer
78+
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
DeleteColumn.py
6+
---------------------
7+
Date : May 2010
8+
Copyright : (C) 2010 by Michael Minn
9+
Email : pyqgis at michaelminn 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__ = 'Michael Minn'
21+
__date__ = 'May 2010'
22+
__copyright__ = '(C) 2010, Michael Minn'
23+
24+
# This will get replaced with a git SHA1 when you do a git archive
25+
26+
__revision__ = '$Format:%H$'
27+
28+
from PyQt4.QtCore import *
29+
from qgis.core import *
30+
from processing.core.GeoAlgorithm import GeoAlgorithm
31+
from processing.core.parameters import ParameterVector
32+
from processing.core.outputs import OutputVector
33+
from processing.tools import dataobjects, vector
34+
35+
36+
class DeleteDuplicateGeometries(GeoAlgorithm):
37+
INPUT = 'INPUT'
38+
OUTPUT = 'OUTPUT'
39+
40+
def defineCharacteristics(self):
41+
self.name = 'Delete duplicate geometries'
42+
self.group = 'Vector general tools'
43+
44+
self.addParameter(ParameterVector(
45+
self.INPUT, 'Input layer', [ParameterVector.VECTOR_TYPE_ANY]))
46+
self.addOutput(OutputVector(self.OUTPUT, 'Output'))
47+
48+
def processAlgorithm(self, progress):
49+
layer = dataobjects.getObjectFromUri(
50+
self.getParameterValue(self.INPUT))
51+
52+
fields = layer.pendingFields()
53+
54+
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields,
55+
layer.wkbType(), layer.crs())
56+
57+
features = vector.features(layer)
58+
59+
count = len(features)
60+
total = 100.0 / float(count)
61+
geoms = dict()
62+
for count, f in enumerate(features):
63+
geoms[f.id()] = QgsGeometry(f.geometry())
64+
progress.setPercentage(int(count * total))
65+
66+
cleaned = dict(geoms)
67+
68+
for i, g in geoms.iteritems():
69+
for j in cleaned.keys():
70+
if i == j or i not in cleaned:
71+
continue
72+
if g.isGeosEqual(cleaned[j]):
73+
del cleaned[j]
74+
75+
count = len(cleaned)
76+
total = 100.0 / float(count)
77+
request = QgsFeatureRequest().setFilterFids(cleaned.keys())
78+
for count, f in enumerate(layer.getFeatures(request)):
79+
writer.addFeature(f)
80+
progress.setPercentage(int(count * total))
81+
82+
del writer
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
Copyright notice
2-
-------------------
3-
MMQGIS Wrapper
4-
2+
----------------
53
MMQGIS (c) 2012 by Michael Minn
64

75
http://michaelminn.com
86

97
MMQGIS is free software and is offered without guarantee
10-
or warranty. You can redistribute it and/or modify it
11-
under the terms of version 2 of the GNU General Public
12-
License (GPL v2) as published by the Free Software
8+
or warranty. You can redistribute it and/or modify it
9+
under the terms of version 2 of the GNU General Public
10+
License (GPL v2) as published by the Free Software
1311
Foundation (www.gnu.org).
1412

15-
QGIS processing framework (c) 2012 by Victor Olaya
13+
QGIS Processing framework (c) 2012 by Victor Olaya

0 commit comments

Comments
 (0)