[Qgis-user] Programmatically export layout with georeferenced file
Nyall Dawson
nyall.dawson at gmail.com
Sun May 27 17:04:16 PDT 2018
On 27 May 2018 at 17:49, DelazJ <delazj at gmail.com> wrote:
> Unfortunately, in my real world use case, I don't know whether the world
> file is desired. I'm updating my MapsPrinter plugin [0] which helps
> exporting multiple layouts at once.. So I should catch the selection the
> user made in the GUI. This leads me to use the customProperty:
>
> exporter = QgsLayoutExporter(mylayout)
> settings = QgsLayoutExporter.ImageExportSettings()
> settings.generateWorldFile = mylayout.customProperty('exportWorldFile')
>
> leading to this function [1] . It does seem to work but I realize that for
> svg particularly, I cannot have access to properties like RDF metadata nor
> layered SVG as they are proposed to the user only in the export process.
These are also stored as custom properties:
- svgIncludeMetadata
- svgGroupLayers
There's also:
- rasterize: corresponds to PdfExportSettings.rasterizeWholeImage
- forceVector: corresponds to PdfExportSettings.forceVectorOutput
- atlasRasterFormat: the atlas raster export image format
- singleFile: whether atlas PDF exports should be "single file".
- imageAntialias: relates to ImageExportSettings.flags
I'd say your use case for accessing these properties via
customProperty is valid (I still wouldn't want to see these added to
stable API).
> I
> understand that there's some unstability in layout(?) customProperty API but
> would that be possible to have these options in the layout export settings
> frame or what are the plans regarding them (if any)?
I'm -0 on this - to be me it would be a LOT of extra ui clutter, for
not a lot of benefit. I'd rather see someone make a Layouts++ plugin
which adds a new dock exposing "advanced" settings like these!
> Side note on the plugin I'm working on:
> 1/ I'm trying to count the number of pages that are actually exportable and
> use the following code:
>
> page_number, pagesToExport = 0, 0
> while page_number < layout.pageCollection().pageCount():
> if layout.pageCollection().shouldExportPage(page_number):
> pagesToExport += 1
> print(pagesToExport)
>
> It seems that layout.pageCollection().shouldExportPage(page_number) always
> return true. In a two pages layout, I checked the "Exclude page from export"
> option on one page and calling that returns true for both pages. Exporting
> from GUI or code does however output only one page. Isn't that the right
> property to check?
Yes - but it's only usable while actually exporting a layout - so not
much use to you here (I'll add a note stating this to the method
documentation).
For your plugin I'd just consider that all pages will be exported, and
let QgsLayoutExporter do the magic checks. Worst case scenario a user
suddenly sees a progress bar "jump forward" if a page isn't required.
>
> 2/ The count of pages was intended to help me display a progress bar for
> each processed page but while this is possible for simple export, it does
> not look like there's a way to catch when a page/feature is output in a
> atlas configuration. No signal emitted until the end of all the iterations,
> right?
If you're exporting atlases, then QgsLayoutExporter can do most of the
work for you. All the methods which export atlases (or reports, or any
QgsAbstractLayoutIterator subclass) to pdf/image/svg/etc have an
optional QgsFeedback argument. So you should:
1. Create a QgsFeedback object
2. Create a QProgressDialog
3. Connect the feedback progressChanged signal to the QProgressDialog
setValue slot (actually, you'll probably need to make your own slot
which sets the dialog's value, and then calls
QCoreApplication.processEvents() 100 times in order to get the dialog
to redraw... it's a bit of a gross hack but semi-supported by Qt)
4. Connect the progress dialog canceled signal to the feedback cancel slot
5. Run the export - you'll get automatic progress reports and
cancelation support!
Nyall
>
> Again, thanks Nyall for the help and for having simplified (afaict) the
> Python side of layout (despite all my questions - /me sunday Python learner)
>
> Regards,
> Harrissou
>
> [0] https://github.com/DelazJ/MapsPrinter/tree/migration_to_v3
> [1]
> https://github.com/DelazJ/MapsPrinter/commit/1a1adbe6a8500abf5b738c4ec907ff7e02f364fb#diff-a5fa5f963eba950b6c361daae67e552fR670
>
>>
>> > 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')
>>
>>
>> >
>> > [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