[QGIS-Developer] Exporting point symbols from processing algorithm

Nyall Dawson nyall.dawson at gmail.com
Sun Aug 16 15:26:25 PDT 2020


On Fri, 14 Aug 2020 at 04:40, C Hamilton <adenaculture at gmail.com> wrote:
>
> I am trying to figure out how to get the proper size to export a point icon. From a processing algorithm that accepts a vector point layer I have been able to get the renderer and each symbol.
>
> rnd = layer.renderer()
> symbol = rnd.symbol()
>
> The symbol size as listed in the symbology size is
>
> size = symbol.size()
>
> Then I can export the symbol image with.
>
> symbol.exportImage(path,"png", QSize(icon_size, icon_size))
>
> How do I figure out what size QSize needs to be to properly draw the icon? I am guessing that since the size is in mm or some other unit, there needs to be a conversion from the symbol size to the icon size. How do I make that conversion?

Correct -- you also need to check sizeUnit and sizeMapUnitScale in
order to get all the information about the marker's size. This gets
evaluated for a particular rendering operation through the
QgsRenderContext class. Specifically, the
QgsRenderContext::convertToPainterUnits() method.

Typical code looks like this:

    scaled_size_in_pixels = context.convertToPainterUnits(
marker_symbol_layer.size(), marker_symbol_layer.sizeUnit(),
marker_symbol_layer.sizeMapUnitScale() )


As far as creating a render context goes -- the easiest way is via
QgsRenderContext.fromMapSettings(). You create a QgsMapSettings
object, set it all up with the desired map scale and extent, etc, and
then convert it to a render context. This ensures that regardless of
the size units used by the symbol (e.g. if it's in map units) it will
be correctly sized...

Nyall


>
> Thanks,
>
> Calvin
> _______________________________________________
> QGIS-Developer mailing list
> QGIS-Developer at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer


More information about the QGIS-Developer mailing list