1

Using QGIS 3.4 I am currently creating new and also cloning vector layers with postgres datasource in an existing project using PyQGIS. This works OK but I need to publish these layers and enable update, insert and delete. In other words, do the equivalent of going to Project Properties - QGIS Server, and then check checkboxes for "Published", "Update", "Insert" and "Delete" (in QGIS client software).

Is this possible using PyQGIS?

1 Answer 1

2

It's possible to set the configuration from PyQGIS although it has been a bit annoying to look at the C code to find the keys to write in the QGIS project file (https://github.com/qgis/QGIS/blob/9a0a1297c2e585cdbc3dbeeb64f5792024a451f9/src/app/qgsprojectproperties.cpp#L1479)

You can find below a recipe. After running it, you will be able to see if it works fine by opening the "Project properties | QGIS Server" panel

Limit of the recipe: we didn't try to catch error when the layer name in the config does not match the layers in the QGIS project

vectorLayers = {layer.id(): layer.name() for layer in QgsProject.instance().mapLayers().values() if isinstance(layer, QgsVectorLayer)}

wfsLayersConfig = [
  {
    "name": "your layer name 1",
    "published": True,
    "precision": 8,
    "Update": True,
    "Insert": False,
    "Delete": True
   },
   {
    "name": "your layer name 2",
    "published": False,
    "precision": 8,
    "Update": False,
    "Insert": True,
    "Delete": False
   }
]

# To join by name as a key instead of identifier
# Weak but to be generic, more simple to use layer name
# whereas layers identifiers hidden
vectorLayersKeyValReversed = {v: k for k, v in vectorLayers.items()}
# To set if published
QgsProject.instance().writeEntry( "WFSLayers" , "/", [vectorLayersKeyValReversed[l['name']] for l in wfsLayersConfig if l["published"]]);
# Set precision (need to loop as the xml tag is the layer identifier)
[QgsProject.instance().writeEntry("WFSLayersPrecision", "/" + vectorLayersKeyValReversed[l['name']], l["precision"]) for l in wfsLayersConfig]
# Set Update
QgsProject.instance().writeEntry( "WFSTLayers" , "Update", [vectorLayersKeyValReversed[l['name']] for l in wfsLayersConfig if l["Update"]]);
# Set Insert
QgsProject.instance().writeEntry( "WFSTLayers" , "Insert", [vectorLayersKeyValReversed[l['name']] for l in wfsLayersConfig if l["Insert"]]);
# Set Delete
QgsProject.instance().writeEntry( "WFSTLayers" , "Delete", [vectorLayersKeyValReversed[l['name']] for l in wfsLayersConfig if l["Delete"]]);

QgsProject.instance().write()
19
  • How fantastic to get such a good response! Your example sets the layer as published. Unfortunately not all is OK. I am loading projects using QGIS Web Client, and I receive this error on load: "Error code: 200. Map CRS not published in QGIS Project properties OWS Server!" When I then open the project in QGIS client software again, and just saves it (without any changes), things are working. So it looks like there is something more that must be done via PyQGIS to get this working. Do you have any suggestions? Commented May 27, 2020 at 7:57
  • Process = keep original, run the PyQGIS script on the copy of the original, do a backup of this new version. Go through "When I then open the project in QGIS client software again, and just saves it (without any changes), things are working.", make a backup. Then, compare the two backup files XML if qgs (if extension = qgz, uncompress the .qgz files to qgs gis.stackexchange.com/questions/331691/…) using a software for diff (I use Meld personally)
    – ThomasG77
    Commented May 27, 2020 at 10:05
  • The issue can be in my script or in the publication process required by QGIS Web Client (what version is it, 1 or 2 ?) As I'm not sure, the best is to look at the files difference as it translates the difference in QGIS configuration to see what is missing here.
    – ThomasG77
    Commented May 27, 2020 at 10:10
  • You may add QgsProject.instance().writeEntry( "WMSServiceCapabilities" , "/", True); to tick the "Service Capabilities" as it seems the QGIS Web Client is reading info from the capabilities
    – ThomasG77
    Commented May 27, 2020 at 11:16
  • The script is overwriting some sections in .qgs file. A missing 'mapcanvas' section is responsible for the error, and adding it back make things work. But also a section for 'legend' should not be removed. Any idea how to keep script from removing these sections? Missing sections here: newtextdocument.com/85c35f9857. . Commented May 28, 2020 at 10:21

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.