Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

6
  • 1
    I was thinking of something similar too. Especially if using map units and a projection in meters. The new(ish) geometry operators could also help here, assuming the labels are centered on the centroid. This will work better with a fixed-width (monospaced) font than a proportional font (Where a 'W' is much wider than an 'i', for example). You're on to something here, but I suspect proportional fonts and kerning will add complexity.
    – Steven Kay
    Commented Jan 15, 2017 at 22:54
  • You could use pil's imagefont.getsize(). That would give you an exact width of a string rendered in your chosen font, but I think that would have a major performance hit. The rough math above is far simpler, especially if you have hundreds of labels on screen
    – user52245
    Commented Jan 16, 2017 at 0:43
  • The only minor issue is that the function doesn't seem to work as a filter in attribute tables in print composer.
    – Ian Turton
    Commented Jan 26, 2017 at 9:36
  • Does your map scale vary from page to page? Try manually putting in the scale instead of the @map_scale variable. my guess would be the attribute table has no way of knowing your map scale. (Because for instance you could have multiple map items in composer at different scales)
    – user52245
    Commented Jan 26, 2017 at 9:40
  • 1
    @iant I posted a link to another custom expression that will get the scale of your composer map programmatically.
    – user52245
    Commented Jan 31, 2017 at 3:27