[Qgis-developer] PyQGIS save raster as rendered image, then use GDAL tools on it

Tom Chadwin tom.chadwin at nnpa.org.uk
Thu Jan 7 16:32:34 PST 2016


I posted this to Stackexchange, but have had no bites yet.

I have tried to use the code from
http://gis.stackexchange.com/questions/68803/qgis-save-raster-as-rendered-image?noredirect=1#comment207705_68803.
`i` is a raster layer:

    pipelayer = i
    pipeextent = pipelayer.extent()
    pipewidth, pipeheight = (pipelayer.width(),
                             pipelayer.height())
    piperenderer = pipelayer.renderer()
    pipeprovider = pipelayer.dataProvider()
    crs = pipelayer.crs().toWkt()
    pipe = QgsRasterPipe()
    pipe.set(pipeprovider.clone())
    pipe.set(piperenderer.clone())
    pipedFile = os.path.join(tempfile.gettempdir(),
                             safeLayerName + '_pipe.tif')
    print pipedFile
    file_writer = QgsRasterFileWriter(pipedFile)
    file_writer.writeRaster(pipe,
                            pipewidth,
                            pipeheight,
                            pipeextent,
                            pipelayer.crs())
    
    in_raster = pipedFile
    prov_raster = os.path.join(tempfile.gettempdir(),
                               'json_' + safeLayerName +
                               '_prov.tif')
    out_raster = dataPath + '.png'
    crsSrc = i.crs()
    crsDest = QgsCoordinateReferenceSystem(4326)
    xform = QgsCoordinateTransform(crsSrc, crsDest)
    extentRep = xform.transform(i.extent())
    extentRepNew = ','.join([unicode(extentRep.xMinimum()),
                             unicode(extentRep.xMaximum()),
                             unicode(extentRep.yMinimum()),
                             unicode(extentRep.yMaximum())])
    processing.runalg("gdalogr:warpreproject", in_raster,
                      i.crs().authid(), "EPSG:4326", "", 0, 1,
                      5, 2, 75, 6, 1, False, 0, False, "",
                      prov_raster)
    processing.runalg("gdalogr:translate", prov_raster, 100,
                      True, "", 0, "", extentRepNew, False, 0,
                      0, 75, 6, 1, False, 0, False, "",
                      out_raster)

It doesn't work, instead giving me the unstyled raster as final output. I
really don't understand the problem, because the output of `writeRaster`
(the file `pipedFile`) *is* styled, so the process from the answer linked to
above is working. It's just that when I try to use the output image to run
through GDAL's `warpreproject` and `translate`, it somehow reverts to the
unstyled raster.

What have I done wrong?



--
View this message in context: http://osgeo-org.1560.x6.nabble.com/PyQGIS-save-raster-as-rendered-image-then-use-GDAL-tools-on-it-tp5244204.html
Sent from the Quantum GIS - Developer mailing list archive at Nabble.com.


More information about the Qgis-developer mailing list