[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

DelazJ delazj at gmail.com
Tue Oct 19 10:07:38 PDT 2021


Hi Zoltan,

You are playing with geometry so you need usesgeometry=True in your
function decorator.
See
https://docs.qgis.org/3.16/en/docs/user_manual/working_with_vector/expression.html#function-editor
or the help section in the Function Editor tab.

Regards,
Harrissou

Le mar. 19 oct. 2021 à 18:33, Zoltan <zoltans at geograph.co.za> a écrit :

> 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
> =============================================
>
> _______________________________________________
> Qgis-user mailing list
> Qgis-user at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20211019/94206f8c/attachment.html>


More information about the Qgis-user mailing list