Skip to content
Permalink
Browse files

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.
  • Loading branch information
nyalldawson committed Oct 30, 2018
1 parent 6c99cbc commit 85efc77bb4441c645868931eeb2fe8476031a645
@@ -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))

@@ -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
@@ -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())
@@ -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:
@@ -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 = []
@@ -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())
@@ -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())
@@ -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

0 comments on commit 85efc77

Please sign in to comment.
You can’t perform that action at this time.