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?
area(convex_hull($geometry))
wherearea
returns the area of geometry polygon object, in this case, the convex polygon?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 ...