How can I use this field calculator expression in PyQGIS? Right now i have the following code. It generates a new field but it does not fill the field. (see bottom for layer example)
layer = iface.activeLayer()
pv = layer.dataProvider()
pv.addAttributes([QgsField('Count', QVariant.Double)])
layer.updateFields()
expression1 = QgsExpression('array_length(overlay_nearest(PND_Aangrenzend,"",limit:=10,max_distance:=5))')
context = QgsExpressionContext()
context.appendScopes(QgsExpressionContextUtils.globalProjectLayerScopes(layer))
with edit(layer):
for f in layer.getFeatures():
context.setFeature(f)
f['Count'] = expression1.evaluate(context)
layer.updateFeature(f)