Tuesday, 7 February 2017

python - Load the raster image which is in TIF format using QGIS API


I am quite new inthe field of GIS. I am interested in is to load the raster image which is in TIF format using QGIS API


I have followed the steps given the in the follwing link to load shape file enter link description here http://geospatialdesktop.com/2009/02/creating_a_standalone_gis_application_1/ and it works fine


To load the rester image i have followed the steps given int he link http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/loadlayer.html


I have python 2.7.6 ,GDAL 1.10.1 installed with qgis2.0 installed in my system



code snippet


import osgeo.gdal
from gdalconst import *
from PyQt4.QtCore import QFileInfo,QSettings
from qgis.core import QgsRasterLayer, QgsCoordinateReferenceSystem

file = QFileDialog.getOpenFileName(self,
"Open TIF Image", ".", "Tagged image (*.tif)")
fileInfo = QFileInfo(file)
fileName=fileInfo.fileName()

baseName = fileInfo.baseName()

layer = QgsRasterLayer(fileName, baseName)

but the image is not loaded and i got the following error in the terminal


ERROR 4: `mypic.tif' does not exist in the file system,
and is not recognised as a supported dataset name.

Layer failed to load!
QObject::connect: Cannot connect (null)::repaintRequested() to QgsMapCanvas::refresh()

QObject::connect: Cannot connect (null)::screenUpdateRequested() to QgsMapCanvas::updateMap()

Answer



Change your last line to:


layer = QgsRasterLayer(file, baseName)


file contains the full path to your raster file (e.g., /home/user/geodata/mypic.tif), while fileName, which you are passing as argument right now, only contains the name of the file (e.g., mypic.tif).


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...