Try running this code. Make sure you have a point layer selected that is using single symbol symbology. The value that is returned is 0. In my case the units for the symbol are millimeters and a value of 5. I am using QGIS 3.14.15 on windows.
canvas = iface.mapCanvas() context = QgsRenderContext.fromMapSettings(canvas.mapSettings()) lyr = iface.activeLayer() rnd = lyr.renderer() sym = rnd.symbol() sym_size = QgsSymbolLayerUtils.estimateMaxSymbolBleed(sym, context) print(sym_size) I am not sure what estimateMaxSymbolBleed really does, but it doesn't seem to represent symbol size. Thanks, Calvin On Thu, Sep 3, 2020 at 7:13 PM Nyall Dawson <[email protected]> wrote: > On Thu, 3 Sep 2020 at 00:06, C Hamilton <[email protected]> wrote: > > > > Nyall, > > > > Thanks. This almost gets the right answer. > QgsSymbolLayerUtils::estimateMaxSymbolBleed is returning 0 as its value. > I'm not sure what the problem is with it. bounds() seems to work for small > symbols which is probably all I'll need, but if the symbol is very large > with a large border and join style set to miter, the bounding box it > returns is a little too small. My example used an excessively large icon > and large border which probably would not be used in any real project. > > Hm - not sure what your issue with estimateMaxSymbolBleed is. Are you > using data defined properties at all? > > Regarding the miter question -- that's something which none of the > symbol bounds checking functions currently available account for. > > Nyall > > > > > Calvin > > > > On Tue, Sep 1, 2020 at 6:39 PM Nyall Dawson <[email protected]> > wrote: > >> > >> On Wed, 2 Sep 2020 at 03:39, C Hamilton <[email protected]> wrote: > >> > > >> > I am trying to calculate the size of a single symbol in pixels using: > >> > > >> > canvas = iface.mapCanvas() > >> > context = QgsRenderContext.fromMapSettings(canvas.mapSettings()) > >> > layer = iface.activeLayer() > >> > rnd = layer.renderer() > >> > sym = rnd.symbol() > >> > scaled_size_in_pixels = context.convertToPainterUnits( > >> > sym.size(), > >> > sym.sizeUnit(), > >> > sym.sizeMapUnitScale() ) > >> > > >> > The last line can also be calculated by: > >> > > >> > scaled_size_in_pixels = sym.size(context) > >> > > >> > I am finding that this is returning the size of the filled area of > the symbol, but it does not include the border. Is this a bug or do I need > to add something additional to include the border? > >> > >> You can use QgsSymbolLayerUtils::estimateMaxSymbolBleed if you're > >> wanting to calculate the "maximum" bounding box of a rendered symbol. > >> That includes any stroke/offset/other settings which also affect the > >> rendered bounds. > >> > >> There's also QgsMarkerSymbol::bounds which returns an approximate > >> bounding box of a marker symbol, which also accounts for things like > >> the stroke and offset. > >> > >> Nyall > >> > >> > >> > > >> > Thanks, > >> > > >> > Calvin > >> > > >> > _______________________________________________ > >> > QGIS-Developer mailing list > >> > [email protected] > >> > List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer > >> > Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer >
_______________________________________________ QGIS-Developer mailing list [email protected] List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
