[QGIS-Developer] Function to generate uri from settings in QGIS

Bo Victor Thomsen bo.victor.thomsen at gmail.com
Mon Jan 11 05:16:23 PST 2021


Multiple thanks to both Alessandro and Etienne for helping me with this 
question. I owe you both a large cold beer (each !) next time we meet.

Med venlig hilsen / Kind regards

Bo Victor Thomsen

Den 10-01-2021 kl. 04:10 skrev Etienne Trimaille:
> The connection API is indeed convenient for that :
>
> metadata = QgsProviderRegistry.instance().providerMetadata('postgres')
> connection = metadata.findConnection("the postgis connection name")
> uri = QgsDataSourceUri(connection.uri())
>
> Le sam. 9 janv. 2021 à 22:36, Alessandro Pasotti <apasotti at gmail.com 
> <mailto:apasotti at gmail.com>> a écrit :
>
>     Hi Bo,
>
>     you may have a look to
>     https://qgis.org/pyqgis/master/core/QgsProviderRegistry.html?highlight=decodeuri#qgis.core.QgsProviderRegistry.decodeUri
>     <https://qgis.org/pyqgis/master/core/QgsProviderRegistry.html?highlight=decodeuri#qgis.core.QgsProviderRegistry.decodeUri>
>     and encodeUri.
>
>     Or better: the connections API
>     https://qgis.org/pyqgis/master/core/QgsAbstractProviderConnection.html
>     <https://qgis.org/pyqgis/master/core/QgsAbstractProviderConnection.html>
>
>
>
>     On Sat, Jan 9, 2021 at 2:03 PM Bo Victor Thomsen
>     <bo.victor.thomsen at gmail.com <mailto:bo.victor.thomsen at gmail.com>>
>     wrote:
>     >
>     > Ok, this has me stumped.
>     >
>     > I know, that I can generate a database URI like this (example
>     from cookbook):
>     >
>     > uri = QgsDataSourceUri()
>     > # set host name, port, database name, username and password
>     > uri.setConnection("localhost", "5432", "dbname", "johny", "xxx")
>     > # set database schema, table name, geometry column and optionally
>     > # subset (WHERE clause)
>     > uri.setDataSource("public", "roads", "the_geom", "cityid = 2643")
>     >
>     > vlayer = QgsVectorLayer(uri.uri(False), "layer name you like",
>     "postgres")
>     >
>     >
>     > But I would like to use information from a already defined
>     Datasource connection from the Datamanager dialog.
>     >
>     > I suspect, that there must be a
>     "QgsSettingsConvertToUri_or_whatever_it_is_called()" function that
>     uses a  QGS settings address  (ex. like
>     "/PostgreSQL/connections/MyDatabase" for a Postgresql database
>     connection)  as input and automatically convert the settings
>     values to a URI string.
>     >
>     > Is there anyone, that can point me in the right direction ?
>     >
>     > --
>     >
>     > Med venlig hilsen / Kind regards
>     >
>     > Bo Victor Thomsen
>     >
>     > _______________________________________________
>     > QGIS-Developer mailing list
>     > QGIS-Developer at lists.osgeo.org
>     <mailto:QGIS-Developer at lists.osgeo.org>
>     > List info:
>     https://lists.osgeo.org/mailman/listinfo/qgis-developer
>     <https://lists.osgeo.org/mailman/listinfo/qgis-developer>
>     > Unsubscribe:
>     https://lists.osgeo.org/mailman/listinfo/qgis-developer
>     <https://lists.osgeo.org/mailman/listinfo/qgis-developer>
>
>
>
>     -- 
>     Alessandro Pasotti
>     QCooperative: www.qcooperative.net <http://www.qcooperative.net>
>     ItOpen: www.itopen.it <http://www.itopen.it>
>     _______________________________________________
>     QGIS-Developer mailing list
>     QGIS-Developer at lists.osgeo.org <mailto:QGIS-Developer at lists.osgeo.org>
>     List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
>     <https://lists.osgeo.org/mailman/listinfo/qgis-developer>
>     Unsubscribe:
>     https://lists.osgeo.org/mailman/listinfo/qgis-developer
>     <https://lists.osgeo.org/mailman/listinfo/qgis-developer>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20210111/c4cbe3e1/attachment.html>


More information about the QGIS-Developer mailing list