I would like to locate a QGIS project on a shared ressource.

This project makes use of WMS / WFS data.

Is there a way at project opening to programmatically define theses sources (if not existing on the workstation) so that the project gets usable by anybody?

Any hint?


If you don't want to put it in the startup.py you can define it in the macro section from the project properties dialog, below you find an example with a WFS. You have to enable macros without asking (Settings->Options->general) - which might be also a per computer scenario:

from qgis.core import QgsDataSourceUri, QgsVectorLayer, QgsProject
def openProject():
    dsu = QgsDataSourceUri()
    dsu.setParam( 'url', 'https://fbinter.stadt-berlin.de/fb/wfs/data/senstadt/s_wfs_adressenberlin' )
    dsu.setParam( 'version', '1.0.0' )
    dsu.setParam( 'typename', 'fis:s_wfs_adressenberlin')
    dsu.setParam( 'maxNumFeatures', '100')
    dsu.setParam( 'srsname', 'EPSG:25833')
    dsu.setParam( 'filter', 'HNR=5')
    layer = QgsVectorLayer( dsu.uri(), "Adressen Berlin", "WFS" )

Adding resources to QGIS for a specific project, I'm not sure about the feasibility but you can add resources when starting QGIS. However, this will impact all QGIS projects.

To do this, you must create a startup.py file and place it in a particular place:

  • Linux: .local/share/QGIS/QGIS3

  • Windows: AppData Roaming QGIS QGIS QGIS3

  • macOS: Library/Application Support/QGIS/QGIS3

For example, the content of my startup.py file:

from qgis.utils import iface
from qgis.core import QgsExpressionContextTools

## Function
def hello():

## When the initialization of QGIS is complete, we launch the hello function

In order to avoid that resources are added for all projects, I suggest you make a function that reads the name of the project and if it matches the shared one, then you add the wfs wms links. However, this implies that users open the project directly in the folder and not open QGIS and then open a project.

In the case where users open QGIS and then the project, you can set up a function with a signal that does the following action: when the current project change, read the name of the project and if the name matches the shared one then you add the wms wfs links.

Good luck!

  • 1
    i would like to avoid any per computer scenario so ... modifying each computer's startup.py is not really an option.
    – Snaileater
    Dec 5 '19 at 12:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.