Thursday 20 July 2017

Loading WMS layer using PyQGIS


Although there are some posts on this matter, there is no answer in any one of them. This is why I am asking it again.


One post I found was https://gis.stackexchange.com/questions/68032/raster-layer-invalid


I read information from the following link: https://hub.qgis.org/wiki/17/Arcgis_rest


I used the command:


gdal_translate "http://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer?f=json&pretty=true" s.xml -of WMS


And it generated the file successfully. However, when I try to open the file and assuming the provider is WMS, the code report layer is invalid.


The code I used is:


    file = QFileDialog.getOpenFileName(self, 
"Open WMS", ".", "WMS (*.xml)")
fileInfo = QFileInfo(file)
# Add the layer
layer = QgsRasterLayer(file, fileInfo.fileName(),"wms")

if not layer.isValid():

print "Failed to load."
return

I just choose the file from the dialog box.


I also tried the other command:


qgis.utils.iface.addRasterLayer("http://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer?f=json&pretty=true","raster")

by using the following code:


layer = QgsRasterLayer("http://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer?f=json&pretty=true", "layer")


if not layer.isValid():
print "Failed to load."
return

It also report "Failed to load". The original command can be run successfully in QGIS python command line. Also, if I try to enter the code in python console, the layer.isValid() would return true. It is just not working in standalone script.



Answer



Apparently, the newest version 2.6 solve this problem. Also, make sure you have the correct environment variables setup. The one which is throwing me off is the QGISHOME one, I setup it as C:\Program Files\QGIS Brighton\, but should be C:\Program Files\QGIS Brighton\apps\qgis.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...