Hi Ivan,<br><br>Try adding this:<br>byteBuffer.order(ByteOrder.nativeOrder());<br><br>after the allocateDirect and before the asFloatBuffer calls.<br><br>That is:<br>-------------------------------<br>ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4 * xsize);<br>
byteBuffer.order(ByteOrder.nativeOrder());<br>
FloatBuffer floatBuffer = byteBuffer.asFloatBuffer();<br>-------------------------------<br><br>Let me know whether this does work.<br>Best Regards,<br>Daniele<br><br><br><div class="gmail_quote">On Wed, Nov 4, 2009 at 5:44 PM, Ivan <span dir="ltr"><<a href="mailto:ivan.lucena@pmldnet.com">ivan.lucena@pmldnet.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Even,<br>
<br>
Thanks for you reply.<br>
<br>
I wrote this piece of code to demonstrate what is happening:<br>
<br>
{{{<br>
public static void main(String[] args) {<br>
<br>
Dataset dataset = null;<br>
Driver driver = null;<br>
Band band = null;<br>
<br>
int xsize = 4;<br>
int ysize = 4;<br>
<br>
gdal.AllRegister();<br>
<br>
driver = gdal.GetDriverByName("GTIFF");<br>
dataset = driver.Create("D:\\temp\\checkit.tif", xsize, ysize, 1, gdalconstConstants.GDT_Float32);<br>
band = dataset.GetRasterBand(1);<br>
<div class="im"><br>
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4 * xsize);<br>
FloatBuffer floatBuffer = byteBuffer.asFloatBuffer();<br>
<br>
</div> for( int i = 0; i < ysize; i++) {<br>
for( int j = 0; j < xsize; j++) {<br>
floatBuffer.put(j, (float) (i + j));<br>
}<br>
band.WriteRaster_Direct(0, i, xsize, 1, gdalconstConstants.GDT_Float32, byteBuffer);<br>
}<br>
<br>
band.FlushCache();<br>
dataset.FlushCache();<br>
dataset.delete();<br>
<br>
System.out.println("Done!");<br>
}<br>
}}}<br>
<br>
And that is the output image dumped in Python:<br>
<br>
array([[ 0.00000000e+00, 4.60060299e-41, 8.96831017e-44, 2.30485571e-41],<br>
[ 4.60060299e-41, 8.96831017e-44, 2.30485571e-41, 4.60074312e-41],<br>
[ 8.96831017e-44, 2.30485571e-41, 4.60074312e-41, 5.74868682e-41],<br>
[ 2.30485571e-41, 4.60074312e-41, 5.74868682e-41, 6.89663052e-41]], dtype=float32))<br>
<br>
If I change the code to produce Byte output:<br>
<br>
{{{<br>
public static void main(String[] args) {<br>
<br>
Dataset dataset = null;<br>
Driver driver = null;<br>
Band band = null;<br>
<br>
int xsize = 4;<br>
int ysize = 4;<br>
<br>
gdal.AllRegister();<br>
<br>
driver = gdal.GetDriverByName("GTIFF");<br>
dataset = driver.Create("D:\\temp\\checkit2.tif", xsize, ysize, 1, gdalconstConstants.GDT_Byte);<br>
band = dataset.GetRasterBand(1);<br>
<br>
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(xsize);<br>
<br>
for( int i = 0; i < ysize; i++) {<br>
for( int j = 0; j < xsize; j++) {<br>
byteBuffer.put(j, (byte) (i + j));<br>
}<br>
band.WriteRaster_Direct(0, i, xsize, 1, gdalconstConstants.GDT_Byte, byteBuffer);<br>
}<br>
<br>
band.FlushCache();<br>
dataset.FlushCache();<br>
dataset.delete();<br>
<br>
System.out.println("Done!");<br>
}<br>
}}}<br>
<br>
That is what I got:<br>
<br>
array([[0, 1, 2, 3],<br>
[1, 2, 3, 4],<br>
[2, 3, 4, 5],<br>
[3, 4, 5, 6]], dtype=uint8)<br>
<br>
That is correct but I need Float32 output on my real application.<br>
<br>
Can you see if I am missing something?<br>
<br>
When [1] says "It automatically takes care of data type translation if the data type" it means exactly like the others GDAL APIs?<br>
<br>
My best regards,<br>
<br>
Ivan<br>
<br>
[1] <a href="http://gdal.org/java/org/gdal/gdal/Band.html#WriteRaster_Direct%28int,%20int,%20int,%20int,%20int,%20int,%20int,%20java.nio.ByteBuffer,%20int,%20int%29" target="_blank">http://gdal.org/java/org/gdal/gdal/Band.html#WriteRaster_Direct%28int,%20int,%20int,%20int,%20int,%20int,%20int,%20java.nio.ByteBuffer,%20int,%20int%29</a><br>
<div><div></div><div class="h5"><br>
<br>
> -------Original Message-------<br>
> From: Even Rouault <<a href="mailto:even.rouault@mines-paris.org">even.rouault@mines-paris.org</a>><br>
> Subject: Re: [gdal-dev] GDAL-Java: How to write Flot32, Int16?<br>
> Sent: Nov 03 '09 17:18<br>
><br>
> Selon Ivan <<a href="mailto:ivan.lucena@pmldnet.com">ivan.lucena@pmldnet.com</a>>:<br>
><br>
> The Java API only uses ByteBuffer. But you can use the asDoubleBuffer(),<br>
> asFloatBuffer(), asIntBuffer() or asShortBuffer() methods to create views of the<br>
> byte buffer as double, float, int or short buffer.<br>
><br>
> For example :<br>
><br>
> ByteBuffer byteBuffer = ByteBuffer.allocateDirect(8 * buf_xsize * buf_ysize);<br>
> band.ReadRaster_Direct(0, 0, xsize, ysize, buf_xsize, buf_ysize,<br>
> gdal.GDT_Float64, byteBuffer);<br>
> DoubleBuffer doubleBuffer = byteBuffer.asDoubleBuffer();<br>
><br>
> > Hi,<br>
> ><br>
> > Does anybody know how to write a FloatBuffer, IntBuffer or ShotBuffer using<br>
> > the Java API?<br>
> ><br>
> > Thanks,<br>
> ><br>
> > Ivan<br>
> > _______________________________________________<br>
> > gdal-dev mailing list<br>
> > <a href="mailto:gdal-dev@lists.osgeo.org">gdal-dev@lists.osgeo.org</a><br>
> > <a href="http://lists.osgeo.org/mailman/listinfo/gdal-dev" target="_blank">http://lists.osgeo.org/mailman/listinfo/gdal-dev</a><br>
> ><br>
><br>
><br>
><br>
_______________________________________________<br>
gdal-dev mailing list<br>
<a href="mailto:gdal-dev@lists.osgeo.org">gdal-dev@lists.osgeo.org</a><br>
<a href="http://lists.osgeo.org/mailman/listinfo/gdal-dev" target="_blank">http://lists.osgeo.org/mailman/listinfo/gdal-dev</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>-------------------------------------------------------<br>Eng. Daniele Romagnoli <br>Software Engineer<br><br>GeoSolutions S.A.S.<br>Via Carignoni 51<br>55041 Camaiore (LU)<br>
Italy<br><br>phone: +39 0584983027<br>fax: +39 0584983027<br>mob: +39 328 0559267<br><br><br><a href="http://www.geo-solutions.it">http://www.geo-solutions.it</a><br><br>-------------------------------------------------------<br>
<br>