[QGIS-Developer] Cannot load WMS capabilities from WMS provider in QGIS3 from python

Alessandro Pasotti apasotti at gmail.com
Mon Jan 15 05:50:17 PST 2018


Forgot to mention that some time ago I wrote a python class to handle all
this boring stuff for QGIS 2:

https://github.com/boundlessgeo/lib-qgis-commons/blob/master/qgiscommons2/network/networkaccessmanager.py

On Mon, Jan 15, 2018 at 2:48 PM, Alessandro Pasotti <apasotti at gmail.com>
wrote:

> On Mon, Jan 15, 2018 at 12:48 PM, David Marteau <dmarteau at 3liz.com> wrote:
>
>> Hi,
>>
>> We hit a very nasty problem when trying to load projects with wms layer
>> defined from python: loading capabilities fail with timeout error, thus
>> preventing creating any project or layer from python.
>>
>> The problem has been verified in Debian, Ubuntu and fresh OSX build from
>> master branch.
>>
>> We have somehow been able to reduce the problem to the loading of the
>> wmsprovider. The following python code provide a minimal example for
>> reproducing the problem:
>>
>> ===================8<==============================
>> import os
>> import logging
>>
>> from qgis.core import QgsApplication, QgsMessageLog
>> from qgis.core import QgsProject, QgsProviderRegistry
>>
>> logger = logging.getLogger()
>> logger.setLevel(logging.DEBUG)
>>
>> # No Display
>> os.environ['QT_QPA_PLATFORM'] = 'offscreen'
>>
>> qgis_application = QgsApplication([], False )
>> qgis_application.setPrefixPath('/usr/local', True)
>> qgis_application.initQgis()
>>
>> # Log any messages
>> def writelogmessage(message, tag, level):
>>     arg = '{}: {}'.format( tag, message )
>>     if level == QgsMessageLog.WARNING:
>>         logger.warning(arg)
>>     elif level == QgsMessageLog.CRITICAL:
>>         logger.error(arg)
>>     else:
>>         logger.info(arg)
>>
>> messageLog = QgsApplication.messageLog()
>> messageLog.messageReceived.connect( writelogmessage )
>>
>>
>> wmsuri = ("contextualWMSLegend=0&crs=EPSG:4326&dpiMode=7&featureCount
>> =10&format=image/jpeg"
>>           "&layers=s2cloudless&styles&tileMatrixSet=s2cloudless-wm
>> sc-14"
>>           "&url=http://tiles.maps.eox.at/wms?" )
>>
>> # Request Timeout
>> provider = QgsProviderRegistry.instance().createProvider( "wms", wmsuri )
>>
>> qgis_application.exitQgis()
>> del qgis_application
>> ====================8<=============================
>>
>> This fail with the following qgis errors
>>
>> WMS: Download of capabilities failed: Operation canceled
>> Network: Network request https://tiles.maps.eox
>> .at/wms?SERVICE=WMS&REQUEST=GetCapabilities timed out
>>
>> Note that the following piece of code work perfectly and proceed the
>> request without problems:
>>
>> ==============================================================
>> from PyQt5.QtCore import QUrl, QEventLoop
>> from PyQt5.QtNetwork import QNetworkRequest
>>
>> url = QUrl("https://tiles.maps.eox.at/wms?SERVICE=WMS&REQUEST=GetC
>> apabilities")
>>
>> request = QNetworkRequest()
>> request.setUrl(url)
>> manager = QgsNetworkAccessManager.instance()
>> replyObject = manager.get(request)
>>
>> loop = QEventLoop()
>> def onfinish(  ):
>>     print("Qt Return Code:",replyObject.attribute(Q
>> NetworkRequest.HttpStatusCodeAttribute))
>>     answer = replyObject.readAll();
>>     print(answer[:200],"\n")
>>     loop.quit()
>>
>> replyObject.finished.connect( onfinish )
>> loop.exec()
>> ==============================================================
>>
>> So this is not fundamentally a problem with the network manager.
>>
>> At this point we are stuck because the provider do not do something
>> fundamentally different from the last piece of code,
>>
>> In qgis code the request execution stall at: https://github.com/qgis/QG
>> IS/blob/master/src/providers/wms/qgswmscapabilities.cpp#L1964
>>
>> The problem does not occur if we run the project from the Desktop
>> application: we think that there is a problem with the QEventLoop execution
>> but we have no clue atm to check this assumption.
>>
>> So if anybody have any idea on the subject…...
>>
>
>
> Hi David,
>
> You  need an event loop to use a Q(qg)NetworkAccessManager, it's
> asynchronous.
>
>
>
> --
> Alessandro Pasotti
> w3:   www.itopen.it
>



-- 
Alessandro Pasotti
w3:   www.itopen.it
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20180115/565cb220/attachment.html>


More information about the QGIS-Developer mailing list