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()
QgsProject.instance().writeEntry( "WMSServiceCapabilities" , "/", True);to tick the "Service Capabilities" as it seems the QGIS Web Client is reading info from the capabilities – ThomasG77 May 27 '20 at 11:16QgsProject.instance().write()that could have side effects to preserve QGIS file overall integrity, I do no see how a section unrelated to my modifications e.gmapcanvascould be removed. – ThomasG77 May 28 '20 at 12:13mapcanvasis never dropped. Seems unrelated to my code but could be wrong as I do not have your QGIS project context and maybe biased when testing (I know when it could fails) – ThomasG77 May 28 '20 at 12:37QgsApplication.setPrefixPath("C:\OSGEO4~1\apps\qgis-ltr", True) qgs = QgsApplication([], False) qgs.initQgis() project = QgsProject.instance() project.read('P:/30093_434_5e621c1a311b8.qgs') layer_name = "Test"Then I do the code above and at the end write to file. Do you have any clue whats going on? Or maybe I need to extract missing sections and insert again. – Stefhan Jonas May 29 '20 at 06:22QgsProject.instance().readEntry( "WFSLayers" , "/"), but I am only getting output('', False)when I have multiple existing layers published. But when I have only one layer published, it returns this layer. I am extremely new to this, so probably a logical explanation. – Stefhan Jonas Jun 08 '20 at 12:05QgsProject.instance().readListEntry( "WFSLayers", "/")instead of the line you mention to read all the entries. A particular case is for precision: use[QgsProject.instance().readEntry("WFSLayersPrecision", "/" + k) for k in vectorLayers.keys()]– ThomasG77 Jun 08 '20 at 14:08QgsProject.instance().readListEntry( "WFSTLayers" , "Insert"),QgsProject.instance().readListEntry( "WFSTLayers" , "Delete")andQgsProject.instance().readListEntry( "WFSTLayers" , "Update")– ThomasG77 Jun 12 '20 at 13:05mapcanvassection. Do you have any last suggestions? Maybe how to add this section back as workaround? I am beginning Linux script with:from qgis.core import *QgsApplication.setPrefixPath("/usr", True)qgs = QgsApplication([], False)qgs.initQgis()– Stefhan Jonas Jun 16 '20 at 07:38qgs = QgsApplication([], False)withqgs = QgsApplication([], True)to see if calling with GUI enabled changes the result (the map canvas going away...) – ThomasG77 Jun 16 '20 at 09:08file = open('P:/30093_434_5e621c1a311b8.qgs')xml = file.read()document = QDomDocument(xml)cs = QgsMapCanvas().readProject(document)QgsProject.instance().write()Then somehow use QgsMapCanvas().writeProject() to add again. – Stefhan Jonas Jun 16 '20 at 11:56<QgsRectangle: 0 0, 0 0>when runningcanvas.extent(), also directly from QGIS client software. But extent is set in qgs file.canvas.readProject(document)in last comment yieldsNone, but I am probably doing something wrong. Really don't want to use last resort - manually insert missing section in xml file. :) – Stefhan Jonas Jun 16 '20 at 12:07QgsMapCanvas().setDestinationCrs(QgsCoordinateReferenceSystem("EPSG:3857"))This is the thing missing which causes the problem. I am working on finding out how to also get the map extent and set this. Cannot use iface class in standalone script so don't know yet. I have separate question for this on forum. Thank you for you incredible help. – Stefhan Jonas Jun 17 '20 at 10:26
QgsMapCanvasinstance. At the beginning, you set canvas =QgsMapCanvas()and then reuse objectcanvaseverywhere e.gcanvas.setDestinationCrs(QgsCoordinateReferenceSystem("EPSG:3857")). If you do everywhereQgsMapCanvas().someMethodyou create as many canvas as you create calls. It will clearly lead to many issues. – ThomasG77 Jun 17 '20 at 10:48