[gdal-dev] gdal_translate on single-band TIFF to PNG for web

Tom Chadwin tom.chadwin at nnpa.org.uk
Sat Jan 9 02:40:19 PST 2016


I'm trying to convert a single-band TIFF to a PNG for web display using
PyQGIS. I've already asked on qgis-dev and GIS.SE. 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') 
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 pyqgis rasterpipe process 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. 

The output of QgsRasterFileWriter is styled. The output
of warpreproject is not styled, but the .tif is accompanied by
a .aux.xml file which appears to have colour information in it: 

<PAMDataset> <PAMRasterBand
band="1"> <Histograms> <HistItem> <HistMin>-0.498046875</HistMin> <HistMax>255.498046875</HistMax> <BucketCount>256</BucketCount> <IncludeOutOfRange>0</IncludeOutOfRange> <Approximate>1</Approximate> <HistCounts>314|4|3|2|3|5|4|4|5|8|10|9|16|16|23|26|37|46|58|62|69|77|77|94|127|94|131|136|133|162|169|172|171|184|200|186|207|196|186|177|179|194|182|182|178|177|181|195|196|196|195|195|179|226|206|215|193|197|202|212|207|206|232|204|229|253|233|240|248|234|234|268|240|238|259|286|268|287|259|264|255|267|268|271|256|321|277|284|317|286|290|277|312|334|319|325|323|310|349|357|347|326|337|338|336|383|374|349|411|382|382|417|406|405|414|427|404|434|447|430|468|424|446|442|450|461|448|458|457|459|412|414|469|466|443|475|464|472|481|519|504|458|473|481|514|523|522|494|542|580|604|623|686|648|707|763|808|799|819|853|932|1046|1062|1118|1145|1215|1293|1393|1335|1392|1390|1317|1232|1247|1215|1088|1032|896|847|731|708|666|608|634|556|500|484|456|433|408|406|401|386|388|414|414|387|382|340|346|382|385|368|348|334|376|359|318|339|326|357|315|319|357|333|328|329|310|344|295|300|351|347|324|358|349|363|361|347|396|386|433|395|434|479|473|523|550|644|655|672|778|882|1014|1183|1400|1681|2075|2531|3529|4851|7235|10793|60773|7297|1999</HistCounts> </HistItem> </Histograms> <Metadata> <MDI
key="STATISTICS_MAXIMUM">255</MDI> <MDI
key="STATISTICS_MEAN">203.31482680479</MDI> <MDI
key="STATISTICS_MINIMUM">0</MDI> <MDI
key="STATISTICS_STDDEV">63.158702013579</MDI> </Metadata> </PAMRasterBand>

The output of translate is not styled, and has no additional file. However,
since it's a PNG not a GeoTIFF, that's perhaps the explanation. 

I've tried looking at the expand option of translate, but that gives me no
output file - I don't know why. Wondering about trying pct2rgb. 

What have I done wrong?



--
View this message in context: http://osgeo-org.1560.x6.nabble.com/gdal-translate-on-single-band-TIFF-to-PNG-for-web-tp5244372.html
Sent from the GDAL - Dev mailing list archive at Nabble.com.


More information about the gdal-dev mailing list