[gdal-dev] Java-GDAL binding:How to convert gdal dataset to byte array

George Ma chaoboma at gmail.com
Wed Apr 12 22:48:00 PDT 2023


Hello everyone.
I have a trouble about java-gdal when I process raster geotif data.
I want to convert Dataset type object to Byte array in order to return
HttpResponse.
But I dont know how to convert.
If I write file on disk firstly,then I read the file into FileInputStream,I
can convert to byte[],like this:

####code#####
String resTifPath = storageLocation + zoom + "-" + col + "-" + row +
".tif";
Driver tifDriver = gdal.GetDriverByName("GTiff");
Dataset datasetTifNew = tifDriver.CreateCopy(resTifPath, datasetTif);
//write datasetTif to file on disk
Byte[] res =null ;
File file = new File(resTifPath);
FileInputStream in = null;
res = new byte[(int) file.length()];
in = new FileInputStream(file);
in.read(res);
return res; //It can return byte[] to HttpResponse normally.
#####
I found it can use "data_bytes = dataset.ReadAsArray().tobytes()" in
python-gdal.
But who can tell me how to convert Dataset type object to Byte array
directly in Java-gdal?

Best Regurds.

Thank you very much.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20230413/9371d1e4/attachment.htm>


More information about the gdal-dev mailing list