Thursday, 25 October 2018

python - Exporting raster to rendered image using standalone script in QGIS?



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




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