[Qgis-user] Programmatically export layout with georeferenced file

Nyall Dawson nyall.dawson at gmail.com
Thu May 24 16:58:35 PDT 2018


On 25 May 2018 at 03:02, DelazJ <delazj at gmail.com> wrote:
> Hi all,
>
> I have a layout on which I checked the "save world file" option in Layout
> --> Exports settings
> When I export this layout to image, I'd expect it to generate a world file
> next to it considering that the generateWorldFile is an attribute of the
> ImageExportSettings class [0]. But no world file.

The GUI setting isn't stored in the layout*  - you need to manually
set generateWorldFile to true
in your ImageExportSettings if you want the world file created.


> mylayout=QgsProject.instance().layoutManager().layoutByName('Composeur 1')
> exporter = QgsLayoutExporter(mylayout)
> exporter.exportToImage(filePath, QgsLayoutExporter.ImageExportSettings)

If you want the world file, this should be

    exporter = QgsLayoutExporter(mylayout)
    settings = QgsLayoutExporter.ImageExportSettings()
    settings.generateWorldFile = True
    exporter.exportToImage(filePath, settings)


> ls=QgsProject.instance().layoutManager().layouts()
> for elt in ls:
>   exporter = QgsLayoutExporter(elt)
>   imgSettings = exporter.ImageExportSettings()
>   print(elt.name(), imgSettings.generateWorldFile)
>
> which returns False for any layout, including the one I checked the option
> for. Btw, dpi returns -1.0, pages return an empty list... I guess I might
> have missed something but...No idea.

That's because imgSettings here is a default constructed
ImageExportSettings - which defaults to no world file, and dpi of -1
(as noted in the docs, the dpi in ImageExportSettings is an override
... if it's -1, then the layout's DPI will be used). An empty pages
list means export all pages. You need to manually populate this
ImageExportSettings as required for your export.

Nyall


* actually it is stored in a customProperty for the layout, but that's
not part of stable API

    layout.customProperty('exportWorldFile')


> I use this kind of code
>
> mylayout=QgsProject.instance().layoutManager().layoutByName('Composeur 1')
> exporter = QgsLayoutExporter(mylayout)
> exporter.exportToImage(filePath, QgsLayoutExporter.ImageExportSettings)
>
> and then I check the image export settings with the next code
>
> ls=QgsProject.instance().layoutManager().layouts()
> for elt in ls:
>   exporter = QgsLayoutExporter(elt)
>   imgSettings = exporter.ImageExportSettings()
>   print(elt.name(), imgSettings.generateWorldFile)
>
> which returns False for any layout, including the one I checked the option
> for. Btw, dpi returns -1.0, pages return an empty list... I guess I might
> have missed something but...No idea.
> When is the generateWorldFile set to True (if ever it's what will trigger my
> world file export)?
>
> [0]
> https://qgis.org/pyqgis/master/core/Layout/QgsLayoutExporter.html#qgis.core.QgsLayoutExporter.ImageExportSettings
>
> Thanks for your help,
> Harrissou
>
> _______________________________________________
> Qgis-user mailing list
> Qgis-user at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user



More information about the Qgis-user mailing list