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.
-
possible duplicate of WMS layer in a QGIS 2.4 stand alone Python scriptGabor Farkas– Gabor Farkas2014年11月04日 10:34:49 +00:00Commented Nov 4, 2014 at 10:34
-
@GaborFarkas I think I am doing the exact thing as the answer in the post suggests. The only difference is that he is using an url and I am using a local file. And my second tried solution is also url. However, since I do not know the data provider, I just left it null.user3547305– user35473052014年11月04日 16:37:48 +00:00Commented Nov 4, 2014 at 16:37
-
as the comment states in the answer, the method of adding a WMS layer doesn't work in a standalone script. It might be a QGIS issue. What version do you use? QGIS 2.6 has just been released, you might try with that.Gabor Farkas– Gabor Farkas2014年11月05日 10:41:15 +00:00Commented Nov 5, 2014 at 10:41
-
@GaborFarkas I did not just try the WMS layer. The second solution I tried, if I am not mistaken, it is a gdal layer. So maybe it just does not support adding any layers in standalone script? I will try 2.6 now.user3547305– user35473052014年11月05日 16:39:26 +00:00Commented Nov 5, 2014 at 16:39
-
Also, just out of curiosity, why is it not possible to run it in a standalone script? I have setup the environment such that the python paths are all pointing to the python in QGIS installation fold. So basically, it should be using the same things as the Python Console in QGIS.user3547305– user35473052014年11月05日 17:20:00 +00:00Commented Nov 5, 2014 at 17:20
1 Answer 1
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.