13

What is the difference between geometry() and $geometry in QGIS?

I didn't find any examples of using this function.

geometry and $geometry

Particularly I'm interested in geometry(), in what cases it is helpful?

2 Answers 2

23

Right next to the list of functions your screenshot is of, should be the currently selected function's documentation. For me, with QGIS 2.18.12 those read:

$geometry Returns the geometry of the current feature. Can be used for processing with other functions.

geometry() Returns a feature's geometry.

This is like the different of a method versus a function in object-oriented programming. $geometry is this/self's geometry. You can think of it more like an attribute or property maybe.

geometry() on the other hand requires/allows you to pass a feature to it and it will return the corresponding geometry. As the documentation shows, you can use this to get the geometry from an attribute-based feature selection:

geometry(
  get_feature(layer, attributeField, value)
)

What else you can do with it is just limited by your creativity and the sources for features to pass to it. :)

5
  • So, how I can use it when trying to transform separate points that forms line ( (make_line( make_point( "xlbl" -1000, "ylbl" ), make_point(( "xlbl"+ length( ( "time" ))*3000+7000 ) , "ylbl" ),) ... etc I mean that I want to transform every point of line, based on coordinates in attribute table values
    – Jane
    Commented Aug 24, 2017 at 9:43
  • I suppose I have to represent every point of my line (from make_point func) as geometry, then transform it, so far as the whole constructed geometry make_line transforms wrong gis.stackexchange.com/questions/253002/…
    – Jane
    Commented Aug 24, 2017 at 9:46
  • Sure thing. This sounds like using the wrong tool though, have you tried the points2one plugin or some other processing? Commented Aug 24, 2017 at 9:53
  • 1
    No, this is a style of the layer, I can't use any plugins in layer properties. I try to solve my problem by dint of qgis built-in functions
    – Jane
    Commented Aug 24, 2017 at 10:05
  • 2
    The property comparison sounds good but the OO comparison is a bit weak, $geometry is rather a (const) variable, regardless if OO or not. Commented Aug 24, 2017 at 12:59
17

$geometry returns the geometry of the current feature as in:

geom_to_wkt($geometry)

geometry() returns the geometry of a specific feature as in

geom_to_wkt(geometry(get_feature('my_layer', 'my_feature', feature_id)))

You would use the second case if you wanted, for example, to process the current feature against a specific feature:

intersects($geometry, geometry(get_feature(layer, attributeField, value)))

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.