user3547305
user3547305

Reputation: 29

How to load a raster layer using PyQGIS?

Although there are some posts on this matter, there is no answer in anyone 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.

Upvotes: 1

Views: 3912

Answers (2)

Chetan chadha
Chetan chadha

Reputation: 568

This is working for me for single band image.I am using python 2.7 and QGIS 2.0.1 .You can load any raster layer like wms,tiff (single band or multiband) etc. using this.:

def ifile(self):
        global fileName
        fileName = str(QtGui.QFileDialog.getOpenFileName(self.iface.mainWindow(),"Open Raster File",'C:\\',"raster files(*.tif *.tiff *.TIF *.TIFF *.IMG *.img )"))
        if len(fileName) is 0:
            return
        else:
            self.inFileName = fileName;
        filelayer = QgsRasterLayer(fileName,os.path.basename(fileName))
        if filelayer == None or filelayer.bandCount() != 1:
            self.errorMessage = "Not a DEM Image"
            QMessageBox.information(self.iface.mainWindow(), "Error", self.errorMessage)
        else:
            #f=open(str(self.inFileName))
            self.dlg.lineEdit.setText(self.inFileName)
            if filelayer.isValid():
                QgsMapLayerRegistry.instance().addMapLayer(filelayer)
        pass

Upvotes: 0

user3547305
user3547305

Reputation: 29

Answer can be found here: https://gis.stackexchange.com/questions/120823/how-to-load-a-wms-layer-using-pyqgis.

Basically, it is just a version problem. If you have qgis previous than v2.6, it would not work. But it is fixed for 2.6

If it is still not working for you, you most likely have problem for environment variable settings.

Upvotes: 0

Related Questions