Skip to content

Commit

Permalink
Fix inefficient calls to QgsFeature.attributes()[idx]
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nyalldawson committed Oct 30, 2018
1 parent 6c99cbc commit 85efc77
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 14 deletions.
12 changes: 6 additions & 6 deletions python/plugins/processing/algs/qgis/MinimumBoundingGeometry.py
Expand Up @@ -162,15 +162,15 @@ def processAlgorithm(self, parameters, context, feedback):

if type == 0:
# bounding boxes - calculate on the fly for efficiency
if not f.attributes()[field_index] in bounds_dict:
bounds_dict[f.attributes()[field_index]] = f.geometry().boundingBox()
if not f[field_index] in bounds_dict:
bounds_dict[f[field_index]] = f.geometry().boundingBox()
else:
bounds_dict[f.attributes()[field_index]].combineExtentWith(f.geometry().boundingBox())
bounds_dict[f[field_index]].combineExtentWith(f.geometry().boundingBox())
else:
if not f.attributes()[field_index] in geometry_dict:
geometry_dict[f.attributes()[field_index]] = [f.geometry()]
if not f[field_index] in geometry_dict:
geometry_dict[f[field_index]] = [f.geometry()]
else:
geometry_dict[f.attributes()[field_index]].append(f.geometry())
geometry_dict[f[field_index]].append(f.geometry())

feedback.setProgress(int(current * total))

Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/qgis/PointDistance.py
Expand Up @@ -187,7 +187,7 @@ def linearMatrix(self, parameters, context, source, inField, target_source, targ
break

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

outID = outFeat.attributes()[outIdx]
outID = outFeat[outIdx]
outGeom = outFeat.geometry()
dist = distArea.measureLine(inGeom.asPoint(),
outGeom.asPoint())
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/qgis/PointsInPolygon.py
Expand Up @@ -155,14 +155,14 @@ def processAlgorithm(self, parameters, context, feedback):

if engine.contains(point_feature.geometry().constGet()):
if weight_field_index >= 0:
weight = point_feature.attributes()[weight_field_index]
weight = point_feature[weight_field_index]
try:
count += float(weight)
except:
# Ignore fields with non-numeric values
pass
elif class_field_index >= 0:
point_class = point_feature.attributes()[class_field_index]
point_class = point_feature[class_field_index]
if point_class not in classes:
classes.add(point_class)
else:
Expand Down
Expand Up @@ -129,7 +129,7 @@ def processAlgorithm(self, parameters, context, feedback):
if feedback.isCanceled():
break

classes[feature.attributes()[index]].append(feature.id())
classes[feature[index]].append(feature.id())
feedback.setProgress(int(i * total))

selran = []
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/SpatialJoin.py
Expand Up @@ -234,7 +234,7 @@ def processAlgorithm(self, parameters, context, feedback):

join_attributes = []
for a in join_field_indexes:
join_attributes.append(f.attributes()[a])
join_attributes.append(f[a])

if engine is None:
engine = QgsGeometry.createGeometryEngine(f.geometry().constGet())
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/SpatialJoinSummary.py
Expand Up @@ -306,7 +306,7 @@ def addField(original, stat, type):

join_attributes = []
for a in join_field_indexes:
join_attributes.append(test_feat.attributes()[a])
join_attributes.append(test_feat[a])

if engine is None:
engine = QgsGeometry.createGeometryEngine(f.geometry().constGet())
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/tools/vector.py
Expand Up @@ -76,7 +76,7 @@ def values(source, *attributes):

# convert attribute value to number
try:
v = float(feature.attributes()[i])
v = float(feature[i])
except:
v = None

Expand Down

0 comments on commit 85efc77

Please sign in to comment.