<div dir="ltr"><div>Hi Zoltan,</div><div><br></div><div>You are playing with geometry so you need usesgeometry=True in your function decorator.</div><div>See <a href="https://docs.qgis.org/3.16/en/docs/user_manual/working_with_vector/expression.html#function-editor">https://docs.qgis.org/3.16/en/docs/user_manual/working_with_vector/expression.html#function-editor</a> or the help section in the Function Editor tab.</div><div><br></div><div>Regards,</div><div>Harrissou<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Le mar. 19 oct. 2021 à 18:33, Zoltan <<a href="mailto:zoltans@geograph.co.za">zoltans@geograph.co.za</a>> a écrit :<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div>
Hi,<br>
Me again :-(<br>
<br>
I've written a custom function in Field Calculator and want to put
its return value into a new Text field in the attribute table.<br>
<br>
When I step through the features I get the correct and expected
values shown next to 'Preview'<br>
But <br>
when I click OK to run the Field Calculator, it runs for a long
enough time, auto-closes on completion and in the attribute table
the new field is created, but value is 'Nothing for None' for all
features.<br>
<b>Note:</b> variable rd_at reflects correct contents when stepping
through and observing 'Preview' but seems to revert to Nonetype when
running the function.<br>
<br>
What am I missing regarding using custom functions?<br>
Is it an intended thing that the Field Calculator auto-closes after
running a custom function?<br>
<br>
Thanks again,<br>
Kind regards,<br>
Zoltan<br>
<br>
<b>Note:</b> The msg variable was introduced in case placing
'return' in various lines in the script was causing problems. I
have also made the script below "more pedantic" to try and debug it.<br>
<blockquote>from qgis.core import *<br>
from qgis.gui import *<br>
import processing<br>
<br>
@qgsfunction(args='auto', group='Custom', referenced_columns=[])<br>
def del_cutboxes(feature, parent):<br>
geom = feature.geometry()<br>
attr = feature.attributes()<br>
rd_at = str(attr[5])<br>
lyr_rcl =
QgsVectorLayer('Z:/Projects/RCL_boxes.shp','lyrrcl','ogr')<br>
rcl_feats = lyr_rcl.getFeatures()<br>
msg = ''<br>
for rcl_feat in rcl_feats:<br>
if len(msg) == 0:<br>
rclbox = rcl_feat.geometry()<br>
if rclbox.contains(geom):<br>
if rcl_feat.attributes()[0] is not rd_at:<br>
#feature.delete()<br>
msg = 'ToDelete ' + rd_at<br>
else:<br>
msg = 'ToKeep ' + rd_at<br>
if len(msg) == 0:<br>
msg = 'Nothing for ' + rd_at<br>
return msg<br>
</blockquote>
<br>
<br>
<br>
<br>
<br>
<pre cols="72">--
=============================================
Zoltan Szecsei GPrGISc 0031
Geograph (Pty) Ltd.
GIS and Photogrammetric Services
Cape Town, South Africa.
Mobile: +27-83-6004028 (Signal, not WhatsApp)
<a href="http://www.geograph.co.za" target="_blank">www.geograph.co.za</a>
=============================================
</pre>
</div>
_______________________________________________<br>
Qgis-user mailing list<br>
<a href="mailto:Qgis-user@lists.osgeo.org" target="_blank">Qgis-user@lists.osgeo.org</a><br>
List info: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-user" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-user</a><br>
Unsubscribe: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-user" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-user</a><br>
</blockquote></div>