26
26
__revision__ = '$Format:%H$'
27
27
28
28
import os
29
+ from collections import defaultdict
29
30
30
31
from qgis .PyQt .QtGui import QIcon
31
32
36
37
from processing .core .GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
37
38
from processing .core .parameters import ParameterVector
38
39
from processing .core .parameters import ParameterBoolean
39
- from processing .core .parameters import ParameterTableField
40
+ from processing .core .parameters import ParameterTableMultipleField
40
41
from processing .core .outputs import OutputVector
41
42
from processing .tools import vector , dataobjects
42
43
@@ -60,14 +61,14 @@ def defineCharacteristics(self):
60
61
self .tr ('Input layer' ),
61
62
[ParameterVector .VECTOR_TYPE_POLYGON , ParameterVector .VECTOR_TYPE_LINE ]))
62
63
self .addParameter (ParameterBoolean (Dissolve .DISSOLVE_ALL ,
63
- self .tr ('Dissolve all (do not use field )' ), True ))
64
- self .addParameter (ParameterTableField (Dissolve .FIELD ,
65
- self .tr ('Unique ID field ' ), Dissolve .INPUT , optional = True ))
64
+ self .tr ('Dissolve all (do not use fields )' ), True ))
65
+ self .addParameter (ParameterTableMultipleField (Dissolve .FIELD ,
66
+ self .tr ('Unique ID fields ' ), Dissolve .INPUT , optional = True ))
66
67
self .addOutput (OutputVector (Dissolve .OUTPUT , self .tr ('Dissolved' )))
67
68
68
69
def processAlgorithm (self , progress ):
69
70
useField = not self .getParameterValue (Dissolve .DISSOLVE_ALL )
70
- fieldname = self .getParameterValue (Dissolve .FIELD )
71
+ field_names = self .getParameterValue (Dissolve .FIELD )
71
72
vlayerA = dataobjects .getObjectFromUri (
72
73
self .getParameterValue (Dissolve .INPUT ))
73
74
vproviderA = vlayerA .dataProvider ()
@@ -127,20 +128,16 @@ def processAlgorithm(self, progress):
127
128
outFeat .setAttributes (attrs )
128
129
writer .addFeature (outFeat )
129
130
else :
130
- fieldIdx = vlayerA .fieldNameIndex (fieldname )
131
- unique = vector .getUniqueValues (vlayerA , int (fieldIdx ))
132
- nFeat = len (unique )
133
- myDict = {}
134
- attrDict = {}
135
- for item in unique :
136
- myDict [unicode (item ).strip ()] = []
137
- attrDict [unicode (item ).strip ()] = None
131
+ field_indexes = [vlayerA .fieldNameIndex (f ) for f in field_names .split (';' )]
138
132
139
- unique = None
133
+ attribute_dict = {}
134
+ geometry_dict = defaultdict (lambda : [])
140
135
141
136
for inFeat in features :
142
137
attrs = inFeat .attributes ()
143
- tempItem = attrs [fieldIdx ]
138
+
139
+ index_attrs = tuple ([attrs [i ] for i in field_indexes ])
140
+
144
141
tmpInGeom = QgsGeometry (inFeat .geometry ())
145
142
if tmpInGeom .isGeosEmpty ():
146
143
continue
@@ -154,16 +151,17 @@ def processAlgorithm(self, progress):
154
151
'geometry: ' )
155
152
+ error .what ())
156
153
157
- if attrDict [ unicode ( tempItem ). strip ()] is None :
154
+ if not index_attrs in attribute_dict :
158
155
# keep attributes of first feature
159
- attrDict [ unicode ( tempItem ). strip () ] = attrs
156
+ attribute_dict [ index_attrs ] = attrs
160
157
161
- myDict [ unicode ( tempItem ). strip () ].append (tmpInGeom )
158
+ geometry_dict [ index_attrs ].append (tmpInGeom )
162
159
163
- features = None
160
+ nFeat = len ( attribute_dict )
164
161
165
162
nElement = 0
166
- for key , value in myDict .items ():
163
+ for key , value in geometry_dict .items ():
164
+ outFeat = QgsFeature ()
167
165
nElement += 1
168
166
progress .setPercentage (int (nElement * 100 / nFeat ))
169
167
try :
@@ -172,7 +170,7 @@ def processAlgorithm(self, progress):
172
170
raise GeoAlgorithmExecutionException (
173
171
self .tr ('Geometry exception while dissolving' ))
174
172
outFeat .setGeometry (tmpOutGeom )
175
- outFeat .setAttributes (attrDict [key ])
173
+ outFeat .setAttributes (attribute_dict [key ])
176
174
writer .addFeature (outFeat )
177
175
178
176
del writer
0 commit comments