5

Using standalone script I loaded a layer, and now I need to export it as an image. See code below:

 from qgis.core import *
 from qgis.utils import *
 from qgis.gui import *
 from PyQt4.QtGui import *
 from PyQt4.QtCore import *
 QgsApplication.setPrefixPath("C:\OSGeo4W\apps\qgis", True)
 qgs = QgsApplication([], True)
 qgs.initQgis()
 rasterpath = "E:/MODIS DATA/2016/15-10-2016/TIRUNELVELI.tif"
 lyr = QgsRasterLayer(rasterpath, "TIRUNELVELI")
 QgsMapLayerRegistry.instance().addMapLayer(lyr)

I checked the post QGis Save Raster as Rendered Image, but the code is executed using Python console. If I use the same code it shows an error:

NoneType object has no attribute 'clone'.

Updated Code after Comment

from qgis.core import *
from qgis.utils import *
from qgis.gui import *
from PyQt4.QtGui import *
from PyQt4.QtCore import *
rasterpath = "E:/MODIS/TIRUNELVELI.tif"
layer = QgsRasterLayer(rasterpath, "TIRUNELVELI")
QgsMapLayerRegistry.instance().addMapLayer(layer)
uri = "E:/MODIS/newStyleqgis.qml"
layer.loadNamedStyle(uri)
extent = layer.extent()
width, height = layer.width(), layer.height()
renderer = layer.renderer()
provider=layer.dataProvider()
crs = layer.crs().toWkt()
pipe = QgsRasterPipe()
pipe.set(provider.clone())
pipe.set(renderer.clone())
file_writer = QgsRasterFileWriter('E:/MODIS/abcd.tif')
file_writer.writeRaster(pipe,
                       width,
                       height,
                       extent,
                       layer.crs())

updated but showing error as shown : enter image description here

2

From Python: Attribute Error - 'NoneType' object has no attribute 'something':

NoneType means that instead of an instance of whatever Class or Object you think you're working with, you've actually got None. That usually means that an assignment or function call up above failed or returned an unexpected result.

The error is probably given by the path of your layer because there is a blank space.

For example, try to add an underscore to the pathname (you need to make the change in the system folder too):

rasterpath = "E:/MODIS_DATA/2016/15-10-2016/TIRUNELVELI.tif"

or change the input folder.

  • check the updated code . i do so as u told. but raster is not saved as an image . – User18 Dec 6 '16 at 4:48
  • I just used your code for a test (using another path and other files), and it worked... I'm sure that the issue is due to your filepath or filename: have you moved your file to the path E:/MODIS/? Otherwise, there is some problem with the reading of your E drive. – mgri Dec 6 '16 at 9:09
  • Did u also run stand alone script ?? yeah i moved my file to the MODIS Folder. i will check it for giving another drive . – User18 Dec 6 '16 at 9:44
2

Along with what HowToInQGIS mentioned, I think you also need to include the QgsApplication class in your updated code. Putting this together with your code works (note that you may need to escape your backslashes "C:\OSGeo4W\apps\qgis" to something like "C:/OSGeo4W64/apps/qgis"):.

Here is the complete code which worked for me:

from qgis.core import *
from qgis.utils import *
from qgis.gui import *
from PyQt4.QtGui import *
from PyQt4.QtCore import *

QgsApplication.setPrefixPath("C:/OSGeo4W64/apps/qgis", True)
app = QApplication([], True)
QgsApplication.initQgis()

rasterpath = "C:/Users/You/Desktop/New folder/vir.tif"
layer = QgsRasterLayer(rasterpath, "TIRUNELVELI")
QgsMapLayerRegistry.instance().addMapLayer(layer)
uri = "C:/Users/You/Desktop/style.qml"
layer.loadNamedStyle(uri)
extent = layer.extent()
width, height = layer.width(), layer.height()
renderer = layer.renderer()
provider=layer.dataProvider()
crs = layer.crs().toWkt()
pipe = QgsRasterPipe()
pipe.set(provider.clone())
pipe.set(renderer.clone())
file_writer = QgsRasterFileWriter('C:/Users/You/Desktop/result.tif')
file_writer.writeRaster(pipe,
                       width,
                       height,
                       extent,
                       layer.crs())

Result

Note the 0 at the end indicating it was successfully written.

Desktop image

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.