<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
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 class="moz-signature" 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 class="moz-txt-link-abbreviated" href="http://www.geograph.co.za">www.geograph.co.za</a>
=============================================
</pre>
</body>
</html>