Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 4 down vote accepted

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.

share|improve this answer
    
It works, thanks a lot for the hint Matthias. Will use some of this at my FOSS4G2014-Portland presentation next month. –  Jakob Aug 22 at 9:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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