30
30
from qgis .core import *
31
31
from processing .core .GeoAlgorithm import GeoAlgorithm
32
32
from processing .parameters .ParameterVector import ParameterVector
33
+ from processing .parameters .ParameterTable import ParameterTable
33
34
from processing .parameters .ParameterTableField import ParameterTableField
34
35
from processing .outputs .OutputVector import OutputVector
35
36
from processing .tools import dataobjects , vector
@@ -48,8 +49,8 @@ def defineCharacteristics(self):
48
49
self .group = 'Vector general tools'
49
50
self .addParameter (ParameterVector (self .INPUT_LAYER , 'Input layer' ,
50
51
[ParameterVector .VECTOR_TYPE_ANY ], False ))
51
- self .addParameter (ParameterVector (self .INPUT_LAYER_2 , 'Input layer 2' ,
52
- [ ParameterVector . VECTOR_TYPE_ANY ], False ))
52
+ self .addParameter (ParameterTable (self .INPUT_LAYER_2 , 'Input layer 2' ,
53
+ False ))
53
54
self .addParameter (ParameterTableField (self .TABLE_FIELD , 'Table field' ,
54
55
self .INPUT_LAYER ))
55
56
self .addParameter (ParameterTableField (self .TABLE_FIELD_2 ,
@@ -84,22 +85,25 @@ def processAlgorithm(self, progress):
84
85
inFeat2 = QgsFeature ()
85
86
outFeat = QgsFeature ()
86
87
88
+ # Cache attributes of Layer 2
89
+ cache = {}
90
+ features2 = vector .features (layer2 )
91
+ for inFeat2 in features2 :
92
+ attrs2 = inFeat2 .attributes ()
93
+ joinValue2 = unicode (attrs2 [joinField2Index ])
94
+ # Put the attributes into the dict if the join key is not contained in the keys of the dict.
95
+ # Note: This behavior is same as previous behavior of this function,
96
+ # but different from the attribute cache function of QGIS core.
97
+ if not joinValue2 in cache :
98
+ cache [joinValue2 ] = attrs2
99
+
87
100
# Create output vector layer with additional attribute
88
101
features = vector .features (layer )
89
102
for inFeat in features :
90
- inGeom = inFeat .geometry ()
103
+ outFeat . setGeometry ( inFeat .geometry () )
91
104
attrs = inFeat .attributes ()
92
- joinValue1 = attrs [joinField1Index ]
93
- features2 = vector .features (layer2 )
94
- for inFeat2 in features2 :
95
- # Maybe it should cache this entries...
96
- attrs2 = inFeat2 .attributes ()
97
- joinValue2 = attrs2 [joinField2Index ]
98
- if joinValue1 == joinValue2 :
99
- # Create the new feature
100
- outFeat .setGeometry (inGeom )
101
- attrs .extend (attrs2 )
102
- break
105
+ joinValue1 = unicode (attrs [joinField1Index ])
106
+ attrs .extend (cache .get (joinValue1 , []))
103
107
outFeat .setAttributes (attrs )
104
108
writer .addFeature (outFeat )
105
109
del writer
0 commit comments