3

I'm trying to update the Using Custom Functions tutorial to QGis 3 and Python 3 and I've run into an interesting issue. With the following code I can create an expression GetUtmZone()='33N' and it works in the Output preview, it works to zoom to the features but if I try to select features it says no features are found!


Update

There seems to be no issue with the function code as it works fine as a label and a map tip.


import math
from qgis.core import *
from qgis.gui import *

@qgsfunction(args='auto', group='Custom', usesGeometry=True)
def GetUtmZone(feature, parent):
    centroid = feature.geometry()
    longitude = centroid.asPoint().x()
    latitude = centroid.asPoint().y()
    zone_number = math.floor(((longitude + 180) / 6) % 60) + 1

    if latitude >= 0:
        zone_letter = 'N'
    else:
        zone_letter = 'S'

    return '%d%s' % (int(zone_number), zone_letter)

enter image description here

The zoom to's are clearly working but each select returns the warning: No matching features found.

versions etc:

QGIS version 3.4.4-Madeira

QGIS code revision f6ddc62

Compiled against Qt 5.9.5

Running against Qt 5.9.5

Compiled against GDAL/OGR 2.2.3

Running against GDAL/OGR 2.2.3

Compiled against GEOS 3.6.2-CAPI-1.10.2

Running against GEOS 3.6.2-CAPI-1.10.2 4d2925d6

PostgreSQL Client Version 10.6 (Ubuntu 10.6-0ubuntu0.18.04.1)

SpatiaLite Version 4.3.0a

QWT Version 6.1.3

QScintilla2 Version 2.10.2 PROJ.4 Version 493

Linux astun-desktop 4.15.0-45-generic #48-Ubuntu SMP Tue Jan 29 16:28:13 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

4
  • Works fine for me, 3.4.0-Madeira. I'm testing on some Cumbria regions and GetUtmZone() = '31N' and click "select features" in "Select by expression" turns them all yellow. Zoom to Features also works...
    – Spacedman
    Feb 19, 2019 at 16:48
  • And it doesn't work (same behaviour as yours) for a larger set of points across several zones (previous data was polygons all in 31N) - except I don't even get a warning... Just nothing selected.
    – Spacedman
    Feb 19, 2019 at 16:52
  • Selection does seem to work if all the features in the layer are selected by the expression - otherwise none are. Have just tested that with a subset of the large set of points that are all in one zone.
    – Spacedman
    Feb 19, 2019 at 16:57
  • Except I've just made a counter-example to that, with a dataset that has one point in another zone and 12 in another. Function will zoom to and select the data. So in conclusion... agggh.
    – Spacedman
    Feb 19, 2019 at 16:59

1 Answer 1

3

It's a combination of two things:

  1. You have a typo, usesGeometry should be usesgeometry:

    @qgsfunction(args='auto', group='Custom', usesgeometry=True)

  2. A bug in QGIS, fixed here: https://github.com/qgis/QGIS/pull/9218

2
  • Thanks, shouldn't usesgeometry default to true anyway, so the typo shouldn't matter? I just going by the help in the editor window
    – Ian Turton
    Feb 20, 2019 at 12:11
  • 1
    Right -- those docs are wrong, it defaults to false. I'll fix.
    – ndawson
    Feb 21, 2019 at 0:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.