[GIS] POSTGIS raster/Java with GDAL bindings – Load byte array in Gdal Dataset Object

gdalpostgis

I would like to load a raster as java.awt.image.BufferedImage (or equivalent class that would allow to perform some display) out of my PostGIS database. My Raster is inDb (loaded with raster2pgsql)

I already created a proper DAO to "connect" the Postgis database and my Java package.
So far, I managed to import an display tiles from my raster using POSTGIS ST_AsPNG queries as presented in the code extract below.

public BufferedImage test_raster() {

    byte[] content = null;
    Dataset dattaset = null;

    try {

        ResultSet result = this .connect
                                .createStatement(
                                            ResultSet.TYPE_SCROLL_INSENSITIVE, 
                                                ResultSet.CONCUR_UPDATABLE
                                         ).executeQuery(
             "SELECT ST_AsPNG(rast) as tmp FROM bath50m  WHERE ST_Intersects(rast, ST_GeomFromText('MULTIPOLYGON(((50 50, 50 0, 0 0, 0 50, 50 50)))',4326))"
                                         );
        while(result.next()){
            if (result.absolute(result.getRow())){

            content = result.getBytes("tmp"); 
                ByteArrayInputStream bis = new ByteArrayInputStream(content);
            BufferedImage image = ImageIO.read(bis);

            }
        }   

    } catch (SQLException e) {
                e.printStackTrace();
    }   
    return image;
}

That is interresting but I'd like to keep the geographic metadata that are embedded with the raster.

I decided to give a try to extract tiles using the POSTGIS ST_AsGdalRaster so metadata would be passed along with the image. I properly compile GDAL to get the java bindings and set PATH so that I can use it (I tried succesfully the Nasa Worldwind GDALtest.java)

According to the doc, it returns the raster tile in the designated GDAL Raster format. Raster formats are one of those supported by your compiled library. Use ST_GDALRasters() to get a list of formats supported by your library.

Actually ST_AsGdalRaster returns a bytes array that I can't cast to the Gdal Dataset type. My intention was to adapt the openfile method from the GDALtest.java to pass byte array or whatever I could.

I am not sure where I am going with that. I know I am doing something wrong but I miss support/experience to realize it.

Any help would be welcome.

Thanks for reading me

edit: I realize I need to clarify meaning. I try to use the GDALDataset Class with a byte array as input. Is it possible?

Best Answer

After looking more carefully at the GDAL reference and stepped into that IT-usenet thread, I finally manage to create a proper Gdaldataset from an in-memory byte array.

That operation requires to load all gdal drivers to get access to the supported Gdal file format. Then you need and to load it in a virtual file, allowed by the gdal FileFromMemBuffer function.

gdal.AllRegister();

//link a virtual file to your byte stream

gdal.FileFromMemBuffer("/vsimem/tiffinmem", content);

//open the dataset as it was a file using the gdal Open function

Dataset dataset = gdal.Open("/vsimem/tiffinmem");

All specific geographic metadatas an more are then conveniently accessible.

Cheers!