Skip to content

Commit 361202e

Browse files
committed
[processing] JoinAttributes improvement
- allow join not only with vector attribute table but also with geometryless table - performance and key comparison improvement - str to unicode fix
1 parent 51408e1 commit 361202e

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

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

+18-14
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from qgis.core import *
3131
from processing.core.GeoAlgorithm import GeoAlgorithm
3232
from processing.parameters.ParameterVector import ParameterVector
33+
from processing.parameters.ParameterTable import ParameterTable
3334
from processing.parameters.ParameterTableField import ParameterTableField
3435
from processing.outputs.OutputVector import OutputVector
3536
from processing.tools import dataobjects, vector
@@ -48,8 +49,8 @@ def defineCharacteristics(self):
4849
self.group = 'Vector general tools'
4950
self.addParameter(ParameterVector(self.INPUT_LAYER, 'Input layer',
5051
[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))
5354
self.addParameter(ParameterTableField(self.TABLE_FIELD, 'Table field',
5455
self.INPUT_LAYER))
5556
self.addParameter(ParameterTableField(self.TABLE_FIELD_2,
@@ -84,22 +85,25 @@ def processAlgorithm(self, progress):
8485
inFeat2 = QgsFeature()
8586
outFeat = QgsFeature()
8687

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+
87100
# Create output vector layer with additional attribute
88101
features = vector.features(layer)
89102
for inFeat in features:
90-
inGeom = inFeat.geometry()
103+
outFeat.setGeometry(inFeat.geometry())
91104
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, []))
103107
outFeat.setAttributes(attrs)
104108
writer.addFeature(outFeat)
105109
del writer

python/plugins/processing/parameters/ParameterTableField.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def __init__(self, name='', description='', parent=None, datatype=-1,
4343
self.optional = optional
4444

4545
def getValueAsCommandLineParameter(self):
46-
return '"' + str(self.value) + '"'
46+
return '"' + unicode(self.value) + '"'
4747

4848
def getAsScriptCode(self):
4949
return '##' + self.name + '=field ' + str(self.parent)
@@ -52,7 +52,7 @@ def setValue(self, value):
5252
if value is None:
5353
return self.optional
5454
elif len(value) > 0:
55-
self.value = str(value)
55+
self.value = unicode(value)
5656
else:
5757
return self.optional
5858
return True

0 commit comments

Comments
 (0)