6

Using QGIS 2.12.3, within the Field Calculator "Geometry" section, there are certain functions, such as "area", that also have a corresponding term beginning with a dollar sign, i.e. "$area". I don't understand the difference between the two.

For example, in the case of "area", the Field Calculator's description pane states: "Returns the area of a geometry polygon object", while the "$area" description is "Returns the area of the current feature." I confess that I don't understand the difference, nor how to use them in a formula.

2 Answers 2

8

$area is the area of the current feature. It's a numeric variable.

area is a function which takes a geometry, and returns its area. This geometry could be any geometry, not just the current feature.

You could say that $area is the same thing as area($geometry)

Some examples - you could use area (the function) to get the area of the bounding box of the feature, for example...

area(bounds($geometry))

.. or the area of the feature after applying buffering

area(buffer($geometry,0.01))

You could replace $geometry in those examples with a different geometry completely, for example you could lift gml or wkt from a field and use that as the geometry.

5
  • Sorry, I ran out of edit time... Let's see if I understand. Say we have a polygon shapefile, named test123, containing 20 polygons. If I wanted to populate a numeric attribute field with the bounding box of each of the 20 polygons I would use: area(bounds($geometry)). If I wanted to populate the attribute field with the shapefile's overall bounding box area (I realize that the output value would be the same for all 20 records, but humor me!) I would use area(bounds("test123")). Is this correct?
    – Stu Smith
    Commented Jan 30, 2016 at 22:57
  • no - i don't think you can do that, just passing in a layer name. The expression builder function is only applied once per feature. You could add a 'geometry' text field to each feature with its bounding box in WKT format, using geomtowkt(bounds($geometry)) but you'd still need to merge the bounding boxes, or sum up their areas. If that's the problem you're trying to solve, can you edit your question? (I saw a good answer for this problem recently, I'll see if I can find it.. it used pyqgis, though)
    – Steven Kay
    Commented Jan 30, 2016 at 23:27
  • Thanks again. My quest started when I wanted to find the centroid (if that's the correct word...) for a polygon shapefile. Not the centroid for any one polygon, but the entire shapefile. I think that this x,y location would be the center of the shapefile's bounding box. That question led me to the field calculator's functions, where I became puzzled by the difference between x, $x, y, $y, etc. That's where the original question came from.
    – Stu Smith
    Commented Jan 31, 2016 at 0:25
  • 1
    Please also note that the dollar sign $ in from of a function means that the function does not take any argument.
    – Jakob
    Commented Jan 31, 2016 at 8:19
  • @Jakob this isn't true - take the $x_at function for example Commented Nov 9, 2017 at 14:16
0

The dollar sign prefix to a function means that it applies to the current record, and often means that no arguments are required for the function. The current record might be the the feature being labelled (in a label expression), or the row for which a field is being calculated (for a field calculator expression). The corresponding functions without a dollar sign will generally require you to specify the feature or record to apply the function to in the form of an argument

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.