<!DOCTYPE html><html><head><title></title><style type="text/css">p.MsoNormal,p.MsoNoSpacing{margin:0}</style></head><body><div style="font-family:Arial;">Hi,<br></div><div style="font-family:Arial;"><br></div><div style="font-family:Arial;">I don't think <span class="font" style="font-family:menlo, consolas, monospace, sans-serif;">ReadAsArray</span> 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.<br></div><div style="font-family:Arial;"><br></div><div style="font-family:Arial;">Instead, you can either use the <span class="font" style="font-family:menlo, consolas, monospace, sans-serif;">MEM</span> driver or the <span class="font" style="font-family:menlo, consolas, monospace, sans-serif;">/vsimem/</span> prefix (like in <a href="https://gdal.org/api/cpl.html#_CPPv424VSIInstallMemFileHandlerv">https://gdal.org/api/cpl.html#_CPPv424VSIInstallMemFileHandlerv</a>), read the in-memory file using <span class="font" style="font-family:menlo, consolas, monospace, sans-serif;">VSIGetMemFileBuffer</span>, then finally unlink the file using <span class="font" style="font-family:menlo, consolas, monospace, sans-serif;">VSIUnlink</span>. These are C or C++ APIs, but the Java bindings must expose them under similar names.<br></div><div style="font-family:Arial;"><br></div><div style="font-family:Arial;">Regards,<br>Laurentiu<br></div><div style="font-family:Arial;"><br></div><div>On Thu, Apr 13, 2023, at 08:48, George Ma wrote:<br></div><blockquote type="cite" id="qt" style=""><div dir="ltr"><div>Hello everyone.<br></div><div>I have a trouble about java-gdal when I process raster geotif data.<br></div><div>I want to convert Dataset type object to Byte array in order to return HttpResponse.<br></div><div>But I dont know how to convert.<br></div><div>If I write file on disk firstly,then I read the file into FileInputStream,I can convert to byte[],like this:<br></div><div><br></div><div>####code#####<br></div><div>String resTifPath = storageLocation + zoom + "-" + col + "-" + row + ".tif";  <br></div><div>Driver tifDriver = gdal.GetDriverByName("GTiff");<br></div><div>Dataset datasetTifNew = tifDriver.CreateCopy(resTifPath, datasetTif); //write datasetTif to file on disk<br></div><div>Byte[] res =null ;<br></div><div>File file = new File(resTifPath);<br></div><div>FileInputStream in = null;<br></div><div>res = new byte[(int) file.length()];<br></div><div>in = new FileInputStream(file);<br></div><div>in.read(res);<br></div><div>return res; //It can return byte[] to HttpResponse normally.<br></div><div>#####<br></div><div>I found it can use "data_bytes = dataset.ReadAsArray().tobytes()" in python-gdal.<br></div><div>But who can tell me how to convert Dataset type object to Byte array directly in Java-gdal?<br></div><div><br></div><div>Best Regurds.<br></div><div><br></div><div>Thank you very much.<br></div></div><div>_______________________________________________<br></div><div>gdal-dev mailing list<br></div><div><a href="mailto:gdal-dev@lists.osgeo.org">gdal-dev@lists.osgeo.org</a><br></div><div><a href="https://lists.osgeo.org/mailman/listinfo/gdal-dev">https://lists.osgeo.org/mailman/listinfo/gdal-dev</a><br></div><div><br></div></blockquote><div style="font-family:Arial;"><br></div></body></html>