2

I'm trying to create a new field defined as the area of the convex hull of the current feature, in the Field Calculator in QGIS 2.8.

Calculating the area of the current feature is easy ($area), and calculating the convex hull of the current feature is easy (convexHull($geometry)), but I can't find any way to calculate the area of that resulting geometry (as opposed to the geometry of the current feature).

I think I need to be able to either:

  • Apply an area calculation to that convex hull geometry in the Field Calculator -- this is what I'd prefer; or
  • Write a custom function -- there appears to be a free-standing area function in the Function Editor, but I can't figure out how to get a simple custom function defined and called properly; shouldn't need any arguments, as it's just the area of the convex hull of the current geometry; or
  • Create a new layer from an interim attribute that just contains the geometry of the convex hull -- then I could calculate the areas of those features in the new layer

Any suggestions on any of these approaches?

  • 3
    Haven't used this before but how about area(convex_hull($geometry)) where area returns the area of geometry polygon object, in this case, the convex polygon? – Joseph Mar 1 '16 at 10:17
  • Joseph is spot on – MappaGnosis Mar 1 '16 at 12:32
  • Exactly! That was my first thought. Seems like that should obviously work, but the Output preview says that the "Expression is invalid" with a "Parser Error: syntax error, unexpected '(', expecting $end". – Alec Ramsay Mar 1 '16 at 14:56
  • convexHull($geometry) works and returns a geometry (the function is convexHull vs. convex_hull), but area(convexHull($geometry)) gives the error. Seems no obvious way to operate on the resulting geometry vs. the geometry of the current feature. – Alec Ramsay Mar 1 '16 at 15:13
  • I've almost got the second approach, a custom function, working. I created this little script in the Function Editor: from qgis.core import * @qgsfunction(args=0, group='Custom') def AreaOfConvexHull(value1, feature, parent): return QgsGeometry.area(QgsGeometry.convexHull(feature.geometry())) In the Output preview, it displays good looking result (vs. an error). However, when I use the function to create a new attribute, I get a popup with the msg: "QgqGeometry.convexHull(): first argument of unbound method must have type 'QgsGeometry'. What am I doing wrong? So close ... – Alec Ramsay Mar 2 '16 at 19:02
1

The UPDATE in this post explains BOTH how to write the kind of custom function I was trying to write AND the specific function I needed. With this custom area function, the expression I need becomes simply area(convexHull($geometry)).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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