3

In able to show labels in a legend (see my other question regarding the legend here: QGIS styling a legend) I need to show points as rectangles with the same height but a length based on the length of text in a font marker.

This is a screen view of the points with the font markers with text of differing length. enter image description here

I tried the buffer function but have so far only busted my brain because I could not work out how to do it.

enter image description here

8
  • Do you simply want to label the points and shall the labels have a background? Then have a look at the option background (Hintergrund) in the labeling tab of the layerproperties. If not, please explain more detailled, what you're trying to achieve.
    – Erik
    Commented Nov 6, 2019 at 14:02
  • I cannot show labels in a legend as I was taught in my other query here: gis.stackexchange.com/questions/332734/… So I need to create a points layer style similar to what the labels look like- Commented Nov 6, 2019 at 14:06
  • Ah, German bureaucratic maps. Those are bad cartographic practice, but I know, "we always did it this way"... bla. Two things: 1. Having many labels on a small scale map will render it next to useless when it comes to easily and efficiently transport your information. 2. In my opinion it would be easier to use coloured labels and add the symbols of the labels to the legend manually.
    – Erik
    Commented Nov 6, 2019 at 14:17
  • @Erik, you speak my mind re. German buerocracy :D But who am I to argue with my boss as the mere QGIS monkey that I am... ;) Commented Nov 6, 2019 at 14:20
  • Would creating the symbols for the legend manually be an issue? Either workloadwise, or are you creating an online-map rather than PDF/paper?
    – Erik
    Commented Nov 6, 2019 at 14:22

2 Answers 2

3

Good news, bad news.

This code creates a polygon based on your point geometry, currently 40 m wide and 20 m high (if using a m-based CRS)

make_polygon( make_line( make_point($x-20,$y-10), make_point($x+20,$y-10), make_point($x+20,$y+10), make_point($x-20,$y+10), make_point($x-20,$y-10)))

But since it is based on the CRS of the layer, it is slightly rotated.

Also, the rectangle is not filled. So I think you'd be better of with the "manual legend" approach.

8
  • If you put this in the geometry generator you should be able to choose any polygon fill style you want.
    – csk
    Commented Nov 6, 2019 at 19:41
  • I filled it blue (tripple checked) but didn't get a fill (QGIS 3.4.9).
    – Erik
    Commented Nov 7, 2019 at 8:08
  • 2
    The rectangle is not rotated (probably because I'm working with a rather obscure CRS: EPSG:3068 - DHDN / Soldner Berlin) It only applies for Berlin so no distortion due to earth curvature ;) Commented Nov 7, 2019 at 8:44
  • 1
    I have the same result, a polygon with no fill. I used the geometry by expression tool to generate a new layer with that expression, and it's still a polygon with no fill. Using the geom_to_wkt function reveals that it's actually four separate invalid geometries, which means there's something wrong with the way it's constructed. I suspect the points are joined in the wrong order.
    – csk
    Commented Nov 8, 2019 at 17:10
  • 1
    make_polygon needs a LineString for each ring. make_line creates a linestring with n vertices, so you can create just a closed linestring with 4 vertices (+ 1 to close it), with: make_polygon( make_line( make_point($x-20,$y-10), make_point($x+20,$y-10), make_point($x+20,$y+10), make_point($x-20,$y+10), make_point($x-20,$y-10))). The output is valid polygon with just one ring and can be filled. Commented Nov 12, 2019 at 0:37
1

I solved my similar problem using an SVG Marker and setting the width depending on the number of characters of the labeling text:

count( "Fieldname" )*0.6

You might need to play around with the factor. 0.6 worked for me.

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.