5

In QGIS I have a points layer and a polygon layer. Two of the points in the points layer lie within a polygon in the polygon layer. I'd like to generate a new polygon that includes all the parts of the first polygon that are closer to point A than point B. Any suggestions?

2 points in yellow lie within pink polygon

0

2 Answers 2

5

You can use QGIS expressions with either "Geometry generator" (for visualization only) or "Geometry by expression" (to create actual geometries): see here for details about both options.

Use this expression based on the point layer and change the names of your point attribute (for naming points as 'A' and 'B') and the name of the polygon layer. Also adapt the distance in line 3 (here: 30000) if the created area does not extent far enough to cover the whole polygon. See at the bottom how to replace line 3 by an expression to automatically get a distance long enough, based on the extent of the polygon layer.

with_variable(
    'dist',
    30000,  -- change/increase this value if not the whole polygon is covered
    intersection (
        single_sided_buffer( 
             with_variable (
                'line',
                make_line (
                    geometry(get_feature(@layer, 'name_of_your_attribute','A')),  -- change attribute name if necessary
                    geometry(get_feature(@layer, 'name_of_your_attribute','B'))  -- change attribute name if necessary
                ),
                extend (
                    make_line (
                        centroid (@line),
                        project( 
                            centroid (@line),
                            @dist,
                            radians (line_interpolate_angle( @line, length (@line)/2)-90)
                        )
                    ),
                    @dist,
                    0
                )
            ), 
            @dist -- change here to -@dist to create the area on the other side (closer to point B)
        ),
        overlay_within(
            'name_of_your_polygon_layer',
            $geometry
        )[0]
    )
)

Area hached in blue is created by the expression above, using Geometry generator:

enter image description here

Calculate maximum length automatically

If you are unsure which length to use in line 3 (in my case: 30000), you can simply try a higher value - or use the following expression instead, that automatically creates a length large enough to cover the whole polygon. Simply replace 30000 in line 3 of the expression above with the following expression. Explanation: it creates the diagonal of the bounding box (layer extent) of the polygon layer and creates its length:

length (
        with_variable (
            'poly',
            layer_property ('name_of_your_polygon_layer', 'extent'),  
            make_line (
                make_point (x_min (@poly), y_min (@poly)),
                make_point (x_max (@poly), y_max (@poly))
            )
        )
    )
1
  • 1
    I'm always impressed by expression solutions!
    – Spacedman
    Jun 23 at 10:48
4

Here's an approach that might need some adjustment for a particular case.

  1. Use "Points to path" to create a line joining the two points.

  2. Use "Extend lines" to add a large distance (relative to the size of the polygon) to both sides of the line. You should now have a long line layer that goes through the points and beyond on both sides.

  3. Use "Rotate" to rotate this long line by 90° degrees about the default rotation point which is the midpoint of the line.

  4. Use "Split with lines" to divide the polygon by the Rotated line.

The only awkward parameter here is the distance to extend the lines in step 2. You could probably use something like twice the perimeter of the polygon which should be enough to guarantee the line, once rotated, spans the polygon.

For bonus points, this would be a fun thing to make into a processing pipeline.

Here's the resulting split polygon and the other intermediate layers created.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.