Skip to content

Commit a3c8289

Browse files
committedJul 5, 2014
Merge pull request #1498 from minorua/joinattrs
[processing] JoinAttributes improvement
2 parents 47e8bde + 228892c commit a3c8289

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed
 

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

Lines changed: 18 additions & 14 deletions
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

Lines changed: 2 additions & 2 deletions
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

‎python/plugins/processing/tools/vector.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,10 +260,10 @@ def combineVectorFields(layerA, layerB):
260260
"""Create single field map from two input field maps.
261261
"""
262262
fields = []
263-
fieldsA = layerA.dataProvider().fields()
263+
fieldsA = layerA.pendingFields()
264264
fields.extend(fieldsA)
265265
namesA = [unicode(f.name()).lower() for f in fieldsA]
266-
fieldsB = layerB.dataProvider().fields()
266+
fieldsB = layerB.pendingFields()
267267
for field in fieldsB:
268268
name = unicode(field.name()).lower()
269269
if name in namesA:

0 commit comments

Comments
 (0)