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
4
  • I guess it depend on what and where you made the change, could you provide more information on the variables you are changing ?
    – J.R
    Commented Jan 27, 2023 at 17:11
  • Yes! Added that to my Question...
    – MartinMap
    Commented Jan 28, 2023 at 9:20
  • 1
    Have you considered macros? Commented Jan 28, 2023 at 9:28
  • no, I don't! And it sounds interesting
    – MartinMap
    Commented Jan 28, 2023 at 9:41

2 Answers 2

5
+100

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.

3
  • That sounds like the solution I'm looking for, but I don't understand how to implement it. Can you provide more details?
    – MartinMap
    Commented Feb 3, 2023 at 21:45
  • How can I use that as macro on project start up?
    – MartinMap
    Commented Feb 6, 2023 at 20:12
  • 1
    gis.stackexchange.com/questions/89903/… Watch here!
    – fsg
    Commented Feb 7, 2023 at 7:45
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()
1
  • 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
    Commented Feb 2, 2023 at 9:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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