2

This is this is a follow up to this question

I have in QGIS ver 3.22 two layers named:

  • source_point_layer
  • target_polygon_layer

I want to automatically point with a line from the source_point_layer to the center of two polygons that are no closer than 3000m and no further than 10000m with a value less than 7 on the target_polygon_layer How to define min and max distance range?

Expression on "source_point_layer"

    collect_geometries (
    array_foreach (
        overlay_nearest (
            'target_polygon_layer',
            centroid($geometry),
            filter:="value"<'7',
            limit:=2,
            max_distance:=10000 -- how to define min_distance?
        ),
        make_line (
            $geometry,
            @element
        )
    )
)
0

1 Answer 1

6

Try some thing like this.

collect_geometries(
    array_foreach(
        array_slice(
            aggregate(
                layer:='target_polygon_layer',
                aggregate:='array_agg',
                expression:=centroid($geometry),
                filter:=distance(
                    geometry(@parent),
                    centroid($geometry)
                ) BETWEEN 3000 AND 10000
                AND to_real("value") < 7,
                order_by:=distance(
                    geometry(@parent),
                    centroid($geometry)
                )
            ),
            0,
            1
        ),
        make_line(
            @element,
            $geometry
        )
    )
)

The expression is very long so maybe you will need to adjust some parameters for your data. I suppose that you will use this expression in the Geometry by expression tool, otherwise you'll need to convert this to wkb or do something else to encode the geometry in a text field.

This is the output of the expression. The polygons are labeled with their value attribute. I have added (in line 12) the to_real() function because I suspect that your field is a text type field, so you need to convert it into number and then the < 7 will work. The lines are labeled with its length in meters (between 3000 and 10000)

Modified geometries

0

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.