[Qgis-user] PyQgis - PostGIS layers won't show in export to PDF (layout)
Charles Dixon-Paver
charles at kartoza.com
Wed Aug 18 02:50:11 PDT 2021
If your local files are exporting but nothing is being pulled from the
database I would suspect an authentication/ permissions issue.
There are a number of ways that QGIS/PG authentication can work in a
project, mainly:
- Basic (plaintext) stored in the QGIS Project - Insecure and not
recommended, but convenient if you want to make a copy of your project
- Auth manager - Each user profile has it's own qgis-auth.db which is
sqlite. You would need to share different auths between users and projects
by using the same authid. Note that I don't think that loading the QGIS
project from PyQGIS is automatically going to load your auth config though,
and it may need to be instantiated in your script [1]. My suspicion is that
you are using this method and the QGIS GUI unlocks it for you, resulting in
the varied result. You could always try to run most of your script from the
python console in the project to check if that works too.
- PGSERVICE - You can also use pg_service.conf (it even supports
password=), or similar connection environment variables, which is both
convenient and significantly more secure than plaintext credentials in the
project. If managed well it can offer a good level of security, but a word
of caution is that misuse of it can be a lot more dangerous from a security
persepctive. Good for testing purposes though... But note that the way
layers are defined and loaded in a project file will vary between service
based connections and those managed by the auth manager.
You can also make a new (or copy an existing) user profile to define the
database connection to prevent the risk of breaking your existing project
and db configs.
I think doing some quick tests to verify whether or not it is an
authentication issue is recommended, because auth management can be a bit
of a rabbit hole.
Regards
[1]
https://docs.qgis.org/3.16/en/docs/pyqgis_developer_cookbook/authentication.html
[2] https://www.postgresql.org/docs/13/libpq-pgservice.html
On Tue, 17 Aug 2021 at 23:28, Baker, Matthew <MATTHEW_BAKER at dpsk12.org>
wrote:
> Hi all,
>
> When using PyQGIS with Python 3.8 I can't seem to get the PostGIS layers
> of a QGIS layout to export to PDF while the local / shapefile layers will.
>
> The manual export to PDF from the QGIS Layout window will export the
> PostGIS layers just fine along with the SHP layers.
>
> From the code below, can anyone spot what I might be missing?
>
> (Running QGIS 3.18 on Linux Mint 20 with Python 3.8)
>
> Thank you, and let me know if more info is needed!
>
> (BEGIN CODE)
>
> #!/usr/bin/env python3
> import os
> from qgis.core import (
> #layout and general
> QgsProject, QgsLayoutExporter, QgsApplication
> )
>
> QgsApplication.setPrefixPath("/usr", True)
> gui_flag = False
> app = QgsApplication([], gui_flag)
> app.initQgis()
>
> project_path = os.getcwd() + '/rec_centers.qgz'
> project_instance = QgsProject.instance()
> project_instance.setFileName(project_path)
> project_instance.read()
>
> #print layer names in layout (not used anywhere else):
> names = [layer.name() for layer in
> QgsProject.instance().mapLayers().values()]
> print ('Layers in project: ', names)
>
> #layout stuff
> manager = QgsProject.instance().layoutManager()
> layout = manager.layoutByName("test_layout") # name of the layout
>
> #export stuff
> exporter = QgsLayoutExporter(layout)
> exporter.exportToPdf(project_instance.absolutePath() +
> "/output/layout.pdf",
> QgsLayoutExporter.PdfExportSettings())
>
> app.exitQgis()
>
> (END CODE)
>
> -m
> --
>
> MATTHEW BAKER
> Spatial Information Manager
> Denver Public Schools
> Department of Enrollment and Campus Planning
>
> 1860 Lincoln St., Denver, CO 80203 | 11th Floor
> 720-423-3419 | matthew_baker at dpsk12.org | planning.dpsk12.org
>
> Students First . Integrity . Equity . Collaboration . Accountability . Fun
> .
> _______________________________________________
> 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20210818/719d0b5c/attachment.html>
More information about the Qgis-user
mailing list