3

I'm writing a script to clip many raster by a shape. When a try the tool in pyqgis just for one raster, value around image are nadata, it's good !

Here is the code :

##test=group
##clip=name
##raster=raster
##shape=vector
##sortie=output raster

clip=processing.runalg('gdalogr:cliprasterbymasklayer', raster,shape,None,False,False,False,5,4,75.0,6.0,1.0,False,0,False,None,sortie)

After that I create a script with a loop on a folder. It's works but I have a little issue. Nodata value become 0 and I really don't know why.

Here is the new code :

##Fruition=group
##Clip_raster=name
##dossier_entree=folder
##dossier_sortie=folder
##parcellaire=vector

from qgis.utils import iface
import sys, processing, os, glob, numpy
from qgis.core import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *


fichiers = os.listdir(dossier_entree)
ndvibruts = glob.glob(dossier_entree+'/*.tif')
for ndvibrut in ndvibruts:

    Clip = processing.runalg('gdalogr:cliprasterbymasklayer', ndvibrut,parcellaire,None,False,False,False,5,4,75.0,6.0,1.0,False,0,False,None,None)

    sortie = QgsRasterLayer(Clip['OUTPUT'])

    extent = sortie.extent()

    width, height = sortie.width(), sortie.height()

    renderer = sortie.renderer()

    provider = sortie.dataProvider()

    crs = sortie.crs().toWkt()

    pipe = QgsRasterPipe()

    pipe.set(provider.clone())

    pipe.set(renderer.clone())

    nomndvibrut = ndvibrut[(ndvibrut.find('ndvi_')):(len(ndvibrut))]

    file_writer = QgsRasterFileWriter("{0}/{1}".format(dossier_sortie,nomndvibrut))

    file_writer.writeRaster(pipe,
                   width,
                   height,
                   extent,
                   sortie.crs())

Somebody can help me ?

1 Answer 1

1

I finally found myself. I had to delete those 2 lines :

renderer = sortie.renderer()

pipe.set(renderer.clone())

Renderer allows to export raster current extent : QgsMapRenderer to export current extent using PyQGIS

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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