I have a dataset of around 20,000 points in a point layer and a polygon layer with a few hundred polygons. Some of the points lie inside the polygons, others do not. I'd like to label every point as either in or out of the polygon, separately in the attribute table of the points layer. Can anyone please advise?
-
1Select your polygons (select by location) and then run this script gis.stackexchange.com/questions/157718/…– Mapperz ♦Commented Dec 17, 2019 at 5:13
-
1Please clarify, whether you want to label the points, or you want to add an attribute to the attribute table of the point layer.– ErikCommented Dec 17, 2019 at 7:26
2 Answers
Without any plugins you can use
within($geometry,aggregate('Polygons','collect',$geometry)) = 1
for labels inside the Polygon and
within($geometry,aggregate('Polygons','collect',$geometry)) = 0
for labels outside the Polygon as your Rule. Replace 'Polygons'
with the layername of your polygons.
You can do this easily with refFunctions plugin and a rule based label:
Set up two rules. First one for points inside the polygons:
geomwithin( 'polygons','$geometry') is not null
Second one for points outside the polygons:
geomwithin( 'polygons','$geometry') is null
Note that 'polygons'
is the name of the polygonlayer. If you want to label them with different attributes, just choose different label with
fields.
-
Is it possible to label only a subset of the points within the polygon? I would like to make rule based labels and styles for different point feature groups. For example labels for mountains within the polygon are black, and labels for bays are blue. Commented Sep 11, 2021 at 17:35
-
I guess yes by using overlay_intersects(). But I think that is a separate question and some more details are needed. Commented Sep 11, 2021 at 17:42
In the layer properties of the point layer, rendering tab, show label parameter :
not contains(geometry(get_feature('polygon layer name', 'field name', 'value')), point_on_surface( $geometry))
In my case, this allows me to show label of the red dots if the value of a certain field in the polygon layer is equal to 50 and not 59 .
This works in QGIS 3.14 with no particular plugins.
There may be a way to select all the polygons (get rid of the field name/value parameters) but I don't know how.