[gdal-dev] Java-GDAL binding:How to convert gdal dataset to byte array
Laurențiu Nicola
lnicola at dend.ro
Thu Apr 13 04:52:04 PDT 2023
Hi,
I don't think ReadAsArray is the API you're looking for. If you do that, you'll end up with a buffer of pixel values, not with a proper TIFF or PNG tile.
Instead, you can either use the MEM driver or the /vsimem/ prefix (like in https://gdal.org/api/cpl.html#_CPPv424VSIInstallMemFileHandlerv), read the in-memory file using VSIGetMemFileBuffer, then finally unlink the file using VSIUnlink. These are C or C++ APIs, but the Java bindings must expose them under similar names.
Regards,
Laurentiu
On Thu, Apr 13, 2023, at 08:48, George Ma wrote:
> 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.
> _______________________________________________
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org
> https://lists.osgeo.org/mailman/listinfo/gdal-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20230413/d4cad09c/attachment.htm>
More information about the gdal-dev
mailing list