[QGIS-Developer] Add a WFS/WMS (or possibly any data source) from API

Richard Duivenvoorde rdmailings at duif.net
Tue Feb 16 03:13:08 PST 2021


On 2/16/21 9:13 AM, Janvier Thomas wrote:
> Hi all,
> 
> I’m looking for an automated/scriptable method to publish (and update) an organization-wide set of webservices.
> 
> The idea is to deploy  a “collection of data resources” as a collection of WMS/WFS, among an organization…
> 
> It’s intended to be maintainable (update/add/remove links on-the-fly) so all coworkers will have access to the same datafeeds without having to manually add each URL (especially for non-expert users).
> 
> My first thought was to provide a plugin that only job would be to register “connections” at launch and update the version of such plugin along with the connections list then deploy the update through an internal QGIS repo…
> 
> But I did not find how to “register” the webservices through the API (I found tons of doc about how to request them to serve layers, nothing on the “add connection to sources”)…

I do not think there is an api for such. There is (per provider) a way to save connections to a (provider specific) xml format.
Maybe use that?

Loading this (via the load button in the connections dialog) is not part of the api, but they are all private methods in 
QgsManageConnectionsDialog

See for example the one for WFS:
https://github.com/qgis/QGIS/blob/master/src/gui/qgsmanageconnectionsdialog.cpp#L947
of for postgres connections
https://github.com/qgis/QGIS/blob/master/src/gui/qgsmanageconnectionsdialog.cpp#L1041

In essence it is nothing more then:
- reading all connection props from the xml
- write the (exact) right key/value pairs to users QSettings ( aka QGIS.ini of CURRENT profile )
- emit QgsWFSSourceSelect.connectionsChanged signal (so dropdowns are updated)

Difficult part here is that the code is part of the (non public) api/ui, so I do not think you can do this 'silently': the user will always be asked to 'overwrite' a (named) connection or not.

So I think you should either 'mimic' (copy) all the 'loadXXXconnections' logic in python...

OR QGIS could maybe make it possible to call these methods from python (make public and part of pyqgis), preferably adding an extra boolean 'askPermission' to NOT ask the user/skip gui part (but that will be future versions probably).

I tested something, my make all the loadxxxconnections public in qgsmanageconnectionsdialog.h 
Then this works (in python, given aa.xml was a valid 'export' of my WFS connections):

fh = QFile('/tmp/aa.xml')
doc = QDomDocument()
doc.setContent(fh)
QgsManageConnectionsDialog().loadWfsConnections(doc, ['onelayerfrom connections.xml'])

But I fail to fire up the QgsWFSSourceSelect.connectionsChanged ...

I agree that having some kind of api would make it for larger organisations easier to deploy needed connections to all users.

Another way I've seen people doing is really creating/read/write the QGIS.ini files, and merge them with the users ones...

Regards,

Richard Duivenvoorde



More information about the QGIS-Developer mailing list