I have raster data in a postgis server (I am able to see it in qgis). When I retrieve it and form gtiff image out of it using gdal, the generated image is not correct (there is a coloring problem). I am able to retrieve the metadata correctly.

For raster I am doing st_AsBinary(rast). And then passing it to gdal raster io buffer parameter

Metadata is read first successfully. Image was stored as tiles


for(int row=0;row<99;row++){
int xoff,yoff;
        yoff=row*metadata.tilesizeY ;
        for(int col=0;col<99;col++){
              query<<"select st_asbinary(rast) from public.tkptest where rid ="<<(row*99)+col+1;
              retval = PQexec(options.connection,query.str().c_str());
              res= PQresultStatus(retval);
                    std::cout<<"We did not get any data\n";
                    return res;
              dstDS->RasterIO(GF_Write,xoff,yoff,metadata.tilesizeX ,metadata.tilesizeY ,PQgetvalue(retval,0,0),metadata.tilesizeX *metadata.datasize,metadata.tilesizeY *metadata.datasize  ,metadata.dtype,metadata.numBands,panBandMap,0,0,0);


3 Answers 3


ST_AsBinary return raster info in wkb format to store one need to convert it to char array.


Apart from ST_AsBinary, the other option is to use GDAL, as you said. The problem with that is the GDAL PostGIS Raster driver. I'm actually working on it (I'm the driver's developer). Results expected before September.


My guess would be that you need to extract the data from WKB into something GDAL knows. Furthermore you should be aware what the output of your database looks like, see http://www.postgresql.org/docs/9.1/static/datatype-binary.html for more information.

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.