[Qgis-user] When running a Custom Function, the return value is different from when stepping through it and watching the 'Preview' value in Field Calculator

Zoltan zoltans at geograph.co.za
Tue Oct 19 09:32:50 PDT 2021


Hi,
Me again :-(

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.

When I step through the features I get the correct and expected values 
shown next to 'Preview'
But
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.
*Note:* variable rd_at reflects correct contents when stepping through 
and observing 'Preview' but seems to revert to Nonetype when running the 
function.

What am I missing regarding using custom functions?
Is it an intended thing that the Field Calculator auto-closes after 
running a custom function?

Thanks again,
Kind regards,
Zoltan

*Note:*  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.

    from qgis.core import *
    from qgis.gui import *
    import processing

    @qgsfunction(args='auto', group='Custom', referenced_columns=[])
    def del_cutboxes(feature, parent):
         geom = feature.geometry()
         attr = feature.attributes()
         rd_at = str(attr[5])
         lyr_rcl =
    QgsVectorLayer('Z:/Projects/RCL_boxes.shp','lyrrcl','ogr')
         rcl_feats = lyr_rcl.getFeatures()
         msg = ''
         for rcl_feat in rcl_feats:
             if len(msg) == 0:
                 rclbox = rcl_feat.geometry()
                 if rclbox.contains(geom):
                     if rcl_feat.attributes()[0] is not rd_at:
                         #feature.delete()
                         msg = 'ToDelete ' + rd_at
                     else:
                         msg = 'ToKeep   ' + rd_at
         if len(msg) == 0:
             msg = 'Nothing for ' + rd_at
         return msg






-- 

=============================================
Zoltan Szecsei GPrGISc 0031
Geograph (Pty) Ltd.
GIS and Photogrammetric Services

Cape Town, South Africa.

Mobile: +27-83-6004028 (Signal, not WhatsApp)
www.geograph.co.za
=============================================
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20211019/2230fc43/attachment.html>


More information about the Qgis-user mailing list