Skip to content

Commit ef1cf09

Browse files
committedOct 30, 2018
Fix inefficient calls to QgsFeature.attributes()[idx]
Replace with just f[idx]. Calling QgsFeature.attributes() allocates a list of all attributes, which is inefficient when only a single attribute value is needed. (cherry picked from commit 85efc77)
1 parent 44c851d commit ef1cf09

File tree

7 files changed

+14
-14
lines changed

7 files changed

+14
-14
lines changed
 

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,15 @@ def processAlgorithm(self, parameters, context, feedback):
162162

163163
if type == 0:
164164
# bounding boxes - calculate on the fly for efficiency
165-
if not f.attributes()[field_index] in bounds_dict:
166-
bounds_dict[f.attributes()[field_index]] = f.geometry().boundingBox()
165+
if not f[field_index] in bounds_dict:
166+
bounds_dict[f[field_index]] = f.geometry().boundingBox()
167167
else:
168-
bounds_dict[f.attributes()[field_index]].combineExtentWith(f.geometry().boundingBox())
168+
bounds_dict[f[field_index]].combineExtentWith(f.geometry().boundingBox())
169169
else:
170-
if not f.attributes()[field_index] in geometry_dict:
171-
geometry_dict[f.attributes()[field_index]] = [f.geometry()]
170+
if not f[field_index] in geometry_dict:
171+
geometry_dict[f[field_index]] = [f.geometry()]
172172
else:
173-
geometry_dict[f.attributes()[field_index]].append(f.geometry())
173+
geometry_dict[f[field_index]].append(f.geometry())
174174

175175
feedback.setProgress(int(current * total))
176176

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def linearMatrix(self, parameters, context, source, inField, target_source, targ
187187
break
188188

189189
inGeom = inFeat.geometry()
190-
inID = str(inFeat.attributes()[inIdx])
190+
inID = str(inFeat[inIdx])
191191
featList = index.nearestNeighbor(inGeom.asPoint(), nPoints)
192192
distList = []
193193
vari = 0.0
@@ -199,7 +199,7 @@ def linearMatrix(self, parameters, context, source, inField, target_source, targ
199199
if same_source_and_target and inFeat.id() == outFeat.id():
200200
continue
201201

202-
outID = outFeat.attributes()[outIdx]
202+
outID = outFeat[outIdx]
203203
outGeom = outFeat.geometry()
204204
dist = distArea.measureLine(inGeom.asPoint(),
205205
outGeom.asPoint())

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,14 @@ def processAlgorithm(self, parameters, context, feedback):
155155

156156
if engine.contains(point_feature.geometry().constGet()):
157157
if weight_field_index >= 0:
158-
weight = point_feature.attributes()[weight_field_index]
158+
weight = point_feature[weight_field_index]
159159
try:
160160
count += float(weight)
161161
except:
162162
# Ignore fields with non-numeric values
163163
pass
164164
elif class_field_index >= 0:
165-
point_class = point_feature.attributes()[class_field_index]
165+
point_class = point_feature[class_field_index]
166166
if point_class not in classes:
167167
classes.add(point_class)
168168
else:

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def processAlgorithm(self, parameters, context, feedback):
129129
if feedback.isCanceled():
130130
break
131131

132-
classes[feature.attributes()[index]].append(feature.id())
132+
classes[feature[index]].append(feature.id())
133133
feedback.setProgress(int(i * total))
134134

135135
selran = []

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ def processAlgorithm(self, parameters, context, feedback):
234234

235235
join_attributes = []
236236
for a in join_field_indexes:
237-
join_attributes.append(f.attributes()[a])
237+
join_attributes.append(f[a])
238238

239239
if engine is None:
240240
engine = QgsGeometry.createGeometryEngine(f.geometry().constGet())

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ def addField(original, stat, type):
306306

307307
join_attributes = []
308308
for a in join_field_indexes:
309-
join_attributes.append(test_feat.attributes()[a])
309+
join_attributes.append(test_feat[a])
310310

311311
if engine is None:
312312
engine = QgsGeometry.createGeometryEngine(f.geometry().constGet())

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def values(source, *attributes):
7676

7777
# convert attribute value to number
7878
try:
79-
v = float(feature.attributes()[i])
79+
v = float(feature[i])
8080
except:
8181
v = None
8282

0 commit comments

Comments
 (0)
Please sign in to comment.