4

I'm working with QGIS 3.16 Hannover under MacOS 10.13 environment. I've got two polygon layers: natural_parks (multiple features) and region (single feature). Some natural_parks features lay entrely within the region feature, some lay completely outside and some are only partly within. I want to calculate the areas of the natural_parks features that do not overlap with the region feature, assigning a 0 value for those enterely within. Actually, what I need is calculate the marine area (in hectares) for each natural park. I built the following code in the natural_parks layer with the field calculator, but it doesn't work

case when overlaps($geometry, geometry(get_feature('region', 'id', 1))) = 1 then ($area - area(intersection($geometry, geometry(get_feature('region', 'id', 1)))))/10000 when overlay_disjoint('region') then $area/10000 else 0 end 

What's wrong with my code?

0

1 Answer 1

5

Try this:

(area($geometry) - (area(intersection($geometry, geometry(get_feature_by_id('region', 1))))))/10000 

If you use area($geometry) instead of $area it will be automatically 0 if its entirely within. The reason is the different calculation method between these functions.

Example:

enter image description here

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.