4

By using Rule based layer styling for a polygon shapefile I want to display only those features whose centroid is separated by 1.000m from the feature centroids of another polygon layer ('layerB'). The following filter expression is invalid and I cannot understand why:

distance(centroid($geometry), centroid(geometry('layerB_011018_5154ef48_b1da_43ae_91b9_daeb20f6cab9'))) > 1000

Getting the feature geometries from 'layerB' is probably the problem here. The geometry operator doesn't seem to fetch all feature geometries from a 'layerB'. How can I solve this?

3
  • 3
    The functions geometry() has one argument: it expects a feature as input. However, you set a layer as input. that cannot work. Honestly I don't quite understand how your solution should look like: can you post a screenshot? Rule based style on layer A if distance to which centroid(s) of layer B? All of them? The closest one?
    – Babel
    Commented Aug 30, 2021 at 16:00
  • I want to style the features in layer A depending on whether the feature-centroid is within or beyond a 1.000m from any feature's centroid in layer B. Hope that's more concise :-) In any case, thanks for elaborating on geometry(), that wasn't clear to me yet. Commented Aug 31, 2021 at 7:09
  • See the help in the expression string builder - when you click on a function (like geometry()) in the middle, on the right side you see the help where you see exactly what kind of input (arguments) the functions expects. By the way: Make it clear if you still look for an answer or if the answer by @MrXsquared solved your problem (by accepting it). If not, give some information what you're missing - otherwise, it's difficult to guess. And: any substantial clarification about your problem should be included in the initial question by editing not - not in comments here.
    – Babel
    Commented Aug 31, 2021 at 7:30

1 Answer 1

5

How about:

distance(centroid($geometry), centroid(aggregate('layerB', 'collect', $geometry))) > 1000

The centroid(aggregate()) part does collect all geometries of 'layerB' as a Multi-Geometry and return the centroid this collection (so the whole layer).

The centroid() function expects a geometry as input, not a layer nor a feature. And the geometry() function would expect a feature, as already mentioned by @Babel.


To only take the nearest centroid into consideration, you can use overlay_nearest() since QGIS 3.16+ the following:

distance(centroid($geometry), centroid(overlay_nearest('layerB',$geometry)[0])) > 1000

You can also add a limit:=, filter:= or max_distance:= as optional arguments.

2
  • Thanks, this works in principle, but in my particular context it's not useful to consider layerB as a single multi-geometry. I don't think my idea is going anywhere and I'll abandon this approach. FYI I've used with get_feature() successfully in another project, to filter features from layer A laying within a single feature of layer B: within( $geometry , geometry(get_feature( 'layerB','fid', 213))). Commented Aug 31, 2021 at 7:26
  • 3
    @HansRoelofsen if you have QGIS 3.16+ available that would be possible by using overlay_nearest(), try my updated answer.
    – MrXsquared
    Commented Aug 31, 2021 at 8:01

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.