[Qgis-developer] loading multiple wfs layers causes unexpected behaviour

Raymond Nijssen r.nijssen at terglobo.nl
Sat Dec 10 04:53:08 PST 2016


In a loop, my python script adds 4 WFS layers to a new layer group in my 
map. Before adding them I test for vlayer.isValid() and for some reason 
they randomly seem invalid. Adding 0, 1, 2, 3 or 4 layers to my new 
group. Check attached screenshot.

I guess this might have to do with the remote WFS-service behind the 
layer's data. Could I use some kind of onReady() or isReady() functionality?
I hope someone can explain this behaviour?

Another, possibly related, thing I ran into; When I load a qgs-file 
containing some WFS layers, QGIS reports most of the WFS layers in the 
'Handle bad layers' dialog. When I press OK (without changing anything) 
some layers are "fixed" while others are still bad.


This is my script, sorry for the terrible dutch layer names etc:




import urllib
#from qgis.core import import
import time

plangebied = 'NL.IMRO.0796.0002120-1301'

'''
params = {
     'service': 'WFS',
     'version': '1.0.0',
     'request': 'GetFeature',
     'typename': 'union',
     'srsname': "EPSG:23030"
}
'''


def getWfsLayer(service, typename, filter):
     params = {
         'typename': typename,
         'filter': filter,
         'srsname': 'EPSG:28992',
         #'restrictToRequestBBOX': '1',
         #'version': '2.0.0',
         #'table': '',
         #'sql': ''
     }
     if not service[-1] == '?':
         service += '?'
     uri = service + urllib.unquote(urllib.urlencode(params))
     #print uri
     layername = typename
     vlayer = QgsVectorLayer(uri, layername, "WFS")
     return vlayer


def addBestemmingsplan(plangebied):
     service = 'http://afnemers.ruimtelijkeplannen.nl/afnemers2012/services'
     filter = '"plangebied"=\'' + plangebied + '\''
     #print(filter)
     layers = [ \
         'app:Bouwaanduiding', \
         'app:Bouwvlak', \
         'app:Enkelbestemming', \
         'app:Bestemmingsplangebied', \
     ]

     root = QgsProject.instance().layerTreeRoot()
     bpName = plangebied
     bpGroup = root.insertGroup(0, bpName)
     #print bpGroup

     for layer in layers:
         #time.sleep(1)
         print layer
         vlayer = getWfsLayer(service, layer, filter)
         print vlayer
         #vlayer.updateExtents()
         if vlayer.isValid():
             QgsMapLayerRegistry.instance().addMapLayer(vlayer, False)
             #time.sleep(1)
             node_vlayer = bpGroup.addLayer(vlayer)
             #time.sleep(1)
         else:
             print 'invalid layer'

     canvas = iface.mapCanvas()
     canvas.refresh()


addBestemmingsplan(plangebied)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: Screenshot from 2016-12-10 13-28-56.png
Type: image/png
Size: 50554 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20161210/54462ab1/attachment-0001.png>


More information about the Qgis-developer mailing list