7

I have a large QGIS project file (let's call it the master project) and would like to use it for several customers.

Currently, I am creating copies of the master project for each customer and modifying the variables within them. However, if I make changes to the master project, these changes will not be reflected in the copies.

Therefore, I would like to have a project file for all customers that are configured by something like an external config file. So if I update the master project, I just have to send it to my customers, who overwrite the old one. The customers' configuration is stored externally (or is inside the .qgz?) and doesn't have to be updated.

So is it possible to configure a QGIS project using an external (or semi-internal) config file?


The variables I will store in the config are:

  • Customers' Name, Address, etc.
  • RGB Colors
  • Fonts and Sizes
  • Paths
  • Preferred Scales
  • Several parameters used for Data Defined Overrides
Taras
  • 32,823
  • 4
  • 66
  • 137
MartinMap
  • 8,262
  • 7
  • 50
  • 114

2 Answers2

5

You could make a QGIS-Plugin with just one Button. By clicking the Button you can trigger a function which change the QGIS-Variables.

QgsExpressionContextUtils.setProjectVariable( QgsProject.instance(), var, value)

You can create new Variables with your desired names and read them later with other functions.

value = QgsExpressionContextUtils.projectScope(QgsProject.instance()).variable(var)

You could store the values in a config-File and read this File within the function. Then you just send your new config-File to your customers and they need to place it in a folder you specified as the File-Location in your function.

If your customers have access to a network drive, you could place the config file there. Then you customers do not have to do something when you update your configs.

Matt
  • 16,843
  • 3
  • 21
  • 52
fsg
  • 682
  • 1
  • 13
4

Along the same idea as @fsg, you can programmatically generate a QgsProject with custom variables and save it to file fully configured.

Setting the values inside the project can be done like this:

project = QgsProject()
project.setCustomVariables({
   "Name": x, 
   "Address": y, 
   ...
})
# and to get the variables when loading the project
project = QgsProject()
project.read("path/to/save/file.qgz")
dict_variables = project.customVariables()
# dict_variables == {"Name": x, "Adress": y, ...}

You can also add layers like this: project.addMapLayer(QgsVectorLayer/QgsMapLayer) or project.addMapLayers(List[QgsVectorLayer/QgsMapLayer])

And finally you can save it with:

project.setFileName("path/to/save/file.qgz")
project.write()
Kalak
  • 3,848
  • 8
  • 26
  • It kind of bypasses the question but I believe it's still a valid possible answer since using this method you can automate the production of the different project.qgz files the same way you could an external one. And in any case, you would have to send an external file in addition to the fixed project file. This way, the project file is the only file to handle. – Kalak Feb 02 '23 at 09:41