Skip to content

Commit a915c41

Browse files
committedMar 13, 2013
fix Join attributes by location (#7103)
1 parent a57aa0c commit a915c41

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed
 

‎python/plugins/fTools/tools/doSpatialJoin.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,23 +123,23 @@ def outFile(self):
123123
def compute(self, inName, joinName, outName, summary, sumList, keep, progressBar):
124124
layer1 = ftools_utils.getVectorLayerByName(inName)
125125
provider1 = layer1.dataProvider()
126-
fieldList1 = ftools_utils.getFieldList(layer1).values()
126+
fieldList1 = ftools_utils.getFieldList(layer1).toList()
127127

128128
layer2 = ftools_utils.getVectorLayerByName(joinName)
129129
provider2 = layer2.dataProvider()
130130

131-
fieldList2 = ftools_utils.getFieldList(layer2)
131+
fieldList2 = ftools_utils.getFieldList(layer2).toList()
132132
fieldList = []
133133
if provider1.crs() != provider2.crs():
134134
QMessageBox.warning(self, self.tr("CRS warning!"), self.tr("Warning: Input layers have non-matching CRS.\nThis may cause unexpected results."))
135135
if not summary:
136-
fieldList2 = ftools_utils.testForUniqueness(fieldList1, fieldList2.values())
136+
fieldList2 = ftools_utils.testForUniqueness(fieldList1, fieldList2)
137137
seq = range(0, len(fieldList1) + len(fieldList2))
138138
fieldList1.extend(fieldList2)
139139
fieldList1 = dict(zip(seq, fieldList1))
140140
else:
141141
numFields = {}
142-
for j in fieldList2.keys():
142+
for j in xrange(len(fieldList2)):
143143
if fieldList2[j].type() == QVariant.Int or fieldList2[j].type() == QVariant.Double:
144144
numFields[j] = []
145145
for i in sumList:
@@ -153,7 +153,8 @@ def compute(self, inName, joinName, outName, summary, sumList, keep, progressBar
153153
fieldList1 = dict(zip(seq, fieldList1))
154154

155155
# check for correct field names
156-
longNames = ftools_utils.checkFieldNameLength( fieldList1 )
156+
print fieldList1
157+
longNames = ftools_utils.checkFieldNameLength( fieldList1.values() )
157158
if not longNames.isEmpty():
158159
QMessageBox.warning( self, self.tr( 'Incorrect field names' ),
159160
self.tr( 'No output will be created.\nFollowing field names are longer than 10 characters:\n%1' )
@@ -168,7 +169,10 @@ def compute(self, inName, joinName, outName, summary, sumList, keep, progressBar
168169
QMessageBox.warning( self, self.tr( 'Error deleting shapefile' ),
169170
self.tr( "Can't delete existing shapefile\n%1" ).arg( self.shapefileName ) )
170171
return False
171-
writer = QgsVectorFileWriter(self.shapefileName, self.encoding, fieldList1, provider1.geometryType(), sRs)
172+
fields = QgsFields()
173+
for f in fieldList1.values():
174+
fields.append(f)
175+
writer = QgsVectorFileWriter(self.shapefileName, self.encoding, fields, provider1.geometryType(), sRs)
172176
#writer = QgsVectorFileWriter(outName, "UTF-8", fieldList1, provider1.geometryType(), sRs)
173177
inFeat = QgsFeature()
174178
outFeat = QgsFeature()
@@ -179,7 +183,7 @@ def compute(self, inName, joinName, outName, summary, sumList, keep, progressBar
179183
add = 85.00 / provider1.featureCount()
180184

181185
index = ftools_utils.createIndex(provider2)
182-
fit1 = provider1.getFeatures()
186+
fit1 = provider1.getFeatures()
183187
while fit1.nextFeature(inFeat):
184188
inGeom = inFeat.geometry()
185189
atMap1 = inFeat.attributes()
@@ -211,16 +215,16 @@ def compute(self, inName, joinName, outName, summary, sumList, keep, progressBar
211215
none = False
212216
atMap2 = inFeatB.attributes()
213217
if not summary:
214-
atMap = atMap1.values()
215-
atMap2 = atMap2.values()
218+
atMap = atMap1
219+
atMap2 = atMap2
216220
atMap.extend(atMap2)
217221
atMap = dict(zip(seq, atMap))
218222
break
219223
else:
220224
for j in numFields.keys():
221225
numFields[j].append(atMap2[j].toDouble()[0])
222226
if summary and not none:
223-
atMap = atMap1.values()
227+
atMap = atMap1
224228
for j in numFields.keys():
225229
for k in sumList:
226230
if k == "SUM": atMap.append(QVariant(sum(numFields[j])))
@@ -234,7 +238,7 @@ def compute(self, inName, joinName, outName, summary, sumList, keep, progressBar
234238
if none:
235239
outFeat.setAttributes(atMap1)
236240
else:
237-
outFeat.setAttributes(atMap)
241+
outFeat.setAttributes(atMap.values())
238242
if keep: # keep all records
239243
writer.addFeature(outFeat)
240244
else: # keep only matching records

0 commit comments

Comments
 (0)
Please sign in to comment.