3

I'm trying the user-defined expression functions for QGIS, as listed on Nathans blog: http://nathanw.net/2012/11/10/user-defined-expression-functions-for-qgis/

I always get the feature.geometry() as None, when running the user expression in the expression builder updating a text column? This happens for all geometries like point, lines and polygons. The data format is ESRI Shape. The code is in an userfunctions.py referenced from startup.py.

Simplified example:

from qgis.utils import qgsfunction
from qgis.core import QGis

@qgsfunction(0, "Python")
def test(values, feature, parent):
    geom = feature.geometry()  
    if geom is None: 
        return 'geom is None'

    return 'geom is NOT None'

My text column is updated with the text: 'geom is None'. Why can I not reference the geometry of a feature?

1 Answer 1

5

You need to specify that this function requires the geometry with the kwarg usesgeometry=True. If you don't, you may get a geometry but there is no guarantee (as you realized).

This works starting from QGIS 2.4

from qgis.utils import qgsfunction
from qgis.core import QGis

@qgsfunction(0, "Python", usesgeometry=True)
def test(values, feature, parent):
    geom = feature.geometry()  
    if geom is None: 
        return 'geom is None'

    return 'geom is NOT None'

This requirement was introduced for performance reasons, so a (possibly large) geometry does not need to be fetched from the database in many cases and therefore relieves the database server and network server where possible.

1
  • It works, thanks a lot for the hint Matthias. Will use some of this at my FOSS4G2014-Portland presentation next month.
    – Jakob
    Commented Aug 22, 2014 at 9:53

Your Answer

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

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