1

I have several one-part and multipart polygons that I want to label.

I Set X and Y values to move the label to a certain position and want to connect the label to the polygon using the following expression in a geometry generator:

make_line( point_on_surface( $geometry ), make_point( "Label_X" , "Label_Y" ))

I want the line to connect from the label to every part of a multipart Polygon.

Is this possible?

2
  • Can you please indicate what software or API you're intending to do this with. What have you tried so far? Jun 15, 2018 at 5:44
  • I'm using Qgis 3.0.0. I tried to do an if-then-else-chain to get all parts of multipart polygons (geometry_n) - but coding skills are a bit rusty so it didn't work out
    – Chaaap
    Jun 15, 2018 at 5:46

2 Answers 2

1

Fortunately, the centroid() function places a point at the centroid of each part of a multipart geometry. So you can use a simple expression to draw lines from the centroids to the label.

Add a geometry generator symbol layer to the polygon layer: enter image description here

Use this expression:

make_line( centroid( $geometry ), make_point("Label_X" , "Label_Y"))

It looks like this (this is a multipart polygon with four parts): enter image description here

Tweak the label offset settings to get the label directly over the point where the lines converge.

2
  • I tested it, but it doesn't work. The centroid functions displays only one point in the middle of the four polygons (outside the polygon area).
    – Chaaap
    Jun 20, 2018 at 5:11
  • It worked for me as described on my work computer, running QGIS 3.0.3. But I tested it again just now on my home computer, running QGIS 3.0.2, and it displays only one point in the centroid of the multipolygon instead of the centroid of each part. Both computers run Windows. The difference is 3.0.2 versus 3.0.3, but the centroid function shouldn't have changed between minor versions. Really not sure what to make of this.
    – csk
    Jun 27, 2018 at 17:16
1

You can use the following code to create your center points for each part of your mutlipolygon:

centroid( geometry_n($geometry, @geometry_part_num ))

So your code for the lines will be:

make_line(centroid( geometry_n($geometry, @geometry_part_num )),make_point("Label_X" , "Label_Y"))

Replace centroid with point_on_surface if you want.

The result:

enter image description here

Tested on QGIS 3.4.2 and QGIS 2.18.7

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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