|
|
|
Ivan-5
|
Hi,
Does anybody know how to write a FloatBuffer, IntBuffer or ShotBuffer using the Java API? Thanks, Ivan _______________________________________________ gdal-dev mailing list [hidden email] http://lists.osgeo.org/mailman/listinfo/gdal-dev |
||||||||||||||||
|
Even Rouault
|
Selon Ivan <[hidden email]>:
The Java API only uses ByteBuffer. But you can use the asDoubleBuffer(), asFloatBuffer(), asIntBuffer() or asShortBuffer() methods to create views of the byte buffer as double, float, int or short buffer. For example : ByteBuffer byteBuffer = ByteBuffer.allocateDirect(8 * buf_xsize * buf_ysize); band.ReadRaster_Direct(0, 0, xsize, ysize, buf_xsize, buf_ysize, gdal.GDT_Float64, byteBuffer); DoubleBuffer doubleBuffer = byteBuffer.asDoubleBuffer(); > Hi, > > Does anybody know how to write a FloatBuffer, IntBuffer or ShotBuffer using > the Java API? > > Thanks, > > Ivan > _______________________________________________ > gdal-dev mailing list > [hidden email] > http://lists.osgeo.org/mailman/listinfo/gdal-dev > _______________________________________________ gdal-dev mailing list [hidden email] http://lists.osgeo.org/mailman/listinfo/gdal-dev |
||||||||||||||||
|
Ivan-5
|
Hi Even,
D'accord. I am already using this technique for reading and it seams to be working correctly. But I am not sure that I am doing the right thing when writing. See, if I have that buffer: {{{ ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4 * xsize); FloatBuffer floatBuffer = byteBuffer.asFloatBuffer(); band.WriteRaster_Direct(0, row, xsize, 1, (4 * xsize), 1, gdal.GDT_Float32, byteBuffer); }}} I got the wrong impression that the wrapper is taking "byteBuffer" to the C API as it is, byte, not as a Float32. Therefore it is wrongly converting every byte to float, one by one. Note that "band" was created as Float32. But I might be wrong. I will keep trying. Thanks, Ivan Even Rouault wrote: > Selon Ivan <[hidden email]>: > > The Java API only uses ByteBuffer. But you can use the asDoubleBuffer(), > asFloatBuffer(), asIntBuffer() or asShortBuffer() methods to create views of the > byte buffer as double, float, int or short buffer. > > For example : > > ByteBuffer byteBuffer = ByteBuffer.allocateDirect(8 * buf_xsize * buf_ysize); > band.ReadRaster_Direct(0, 0, xsize, ysize, buf_xsize, buf_ysize, > gdal.GDT_Float64, byteBuffer); > DoubleBuffer doubleBuffer = byteBuffer.asDoubleBuffer(); > >> Hi, >> >> Does anybody know how to write a FloatBuffer, IntBuffer or ShotBuffer using >> the Java API? >> >> Thanks, >> >> Ivan >> _______________________________________________ >> gdal-dev mailing list >> [hidden email] >> http://lists.osgeo.org/mailman/listinfo/gdal-dev >> > > > _______________________________________________ gdal-dev mailing list [hidden email] http://lists.osgeo.org/mailman/listinfo/gdal-dev |
||||||||||||||||
|
Ivan-5
|
In reply to this post
by Ivan-5
Even,
Thanks for you reply. I wrote this piece of code to demonstrate what is happening: {{{ public static void main(String[] args) { Dataset dataset = null; Driver driver = null; Band band = null; int xsize = 4; int ysize = 4; gdal.AllRegister(); driver = gdal.GetDriverByName("GTIFF"); dataset = driver.Create("D:\\temp\\checkit.tif", xsize, ysize, 1, gdalconstConstants.GDT_Float32); band = dataset.GetRasterBand(1); ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4 * xsize); FloatBuffer floatBuffer = byteBuffer.asFloatBuffer(); for( int i = 0; i < ysize; i++) { for( int j = 0; j < xsize; j++) { floatBuffer.put(j, (float) (i + j)); } band.WriteRaster_Direct(0, i, xsize, 1, gdalconstConstants.GDT_Float32, byteBuffer); } band.FlushCache(); dataset.FlushCache(); dataset.delete(); System.out.println("Done!"); } }}} And that is the output image dumped in Python: array([[ 0.00000000e+00, 4.60060299e-41, 8.96831017e-44, 2.30485571e-41], [ 4.60060299e-41, 8.96831017e-44, 2.30485571e-41, 4.60074312e-41], [ 8.96831017e-44, 2.30485571e-41, 4.60074312e-41, 5.74868682e-41], [ 2.30485571e-41, 4.60074312e-41, 5.74868682e-41, 6.89663052e-41]], dtype=float32)) If I change the code to produce Byte output: {{{ public static void main(String[] args) { Dataset dataset = null; Driver driver = null; Band band = null; int xsize = 4; int ysize = 4; gdal.AllRegister(); driver = gdal.GetDriverByName("GTIFF"); dataset = driver.Create("D:\\temp\\checkit2.tif", xsize, ysize, 1, gdalconstConstants.GDT_Byte); band = dataset.GetRasterBand(1); ByteBuffer byteBuffer = ByteBuffer.allocateDirect(xsize); for( int i = 0; i < ysize; i++) { for( int j = 0; j < xsize; j++) { byteBuffer.put(j, (byte) (i + j)); } band.WriteRaster_Direct(0, i, xsize, 1, gdalconstConstants.GDT_Byte, byteBuffer); } band.FlushCache(); dataset.FlushCache(); dataset.delete(); System.out.println("Done!"); } }}} That is what I got: array([[0, 1, 2, 3], [1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6]], dtype=uint8) That is correct but I need Float32 output on my real application. Can you see if I am missing something? When [1] says "It automatically takes care of data type translation if the data type" it means exactly like the others GDAL APIs? My best regards, Ivan [1] http://gdal.org/java/org/gdal/gdal/Band.html#WriteRaster_Direct%28int,%20int,%20int,%20int,%20int,%20int,%20int,%20java.nio.ByteBuffer,%20int,%20int%29 > -------Original Message------- > From: Even Rouault <[hidden email]> > Subject: Re: [gdal-dev] GDAL-Java: How to write Flot32, Int16? > Sent: Nov 03 '09 17:18 > > Selon Ivan <[hidden email]>: > > The Java API only uses ByteBuffer. But you can use the asDoubleBuffer(), > asFloatBuffer(), asIntBuffer() or asShortBuffer() methods to create views of the > byte buffer as double, float, int or short buffer. > > For example : > > ByteBuffer byteBuffer = ByteBuffer.allocateDirect(8 * buf_xsize * buf_ysize); > band.ReadRaster_Direct(0, 0, xsize, ysize, buf_xsize, buf_ysize, > gdal.GDT_Float64, byteBuffer); > DoubleBuffer doubleBuffer = byteBuffer.asDoubleBuffer(); > > > Hi, > > > > Does anybody know how to write a FloatBuffer, IntBuffer or ShotBuffer using > > the Java API? > > > > Thanks, > > > > Ivan > > _______________________________________________ > > gdal-dev mailing list > > [hidden email] > > http://lists.osgeo.org/mailman/listinfo/gdal-dev > > > > > gdal-dev mailing list [hidden email] http://lists.osgeo.org/mailman/listinfo/gdal-dev |
||||||||||||||||
|
Daniele Romagnoli
|
Hi Ivan,
Try adding this: byteBuffer.order(ByteOrder.nativeOrder()); after the allocateDirect and before the asFloatBuffer calls. That is: ------------------------------- ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4 * xsize); byteBuffer.order(ByteOrder.nativeOrder()); FloatBuffer floatBuffer = byteBuffer.asFloatBuffer(); ------------------------------- Let me know whether this does work. Best Regards, Daniele On Wed, Nov 4, 2009 at 5:44 PM, Ivan <[hidden email]> wrote: Even, -- ------------------------------------------------------- Eng. Daniele Romagnoli Software Engineer GeoSolutions S.A.S. Via Carignoni 51 55041 Camaiore (LU) Italy phone: +39 0584983027 fax: +39 0584983027 mob: +39 328 0559267 http://www.geo-solutions.it ------------------------------------------------------- _______________________________________________ gdal-dev mailing list [hidden email] http://lists.osgeo.org/mailman/listinfo/gdal-dev |
||||||||||||||||
|
Ivan-5
|
In reply to this post
by Ivan-5
Thanks Daniele. That takes care of the problem.
> -------Original Message------- > From: Daniele Romagnoli <[hidden email]> > Subject: Re: [gdal-dev] GDAL-Java: How to write Flot32, Int16? > Sent: Nov 04 '09 12:36 > > Hi Ivan, > > Try adding this: > byteBuffer.order(ByteOrder.nativeOrder()); > > after the allocateDirect and before the asFloatBuffer calls. > > That is: > ------------------------------- > ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4 * xsize); > byteBuffer.order(ByteOrder.nativeOrder()); > FloatBuffer floatBuffer = byteBuffer.asFloatBuffer(); > ------------------------------- > > Let me know whether this does work. > Best Regards, > Daniele > > > On Wed, Nov 4, 2009 at 5:44 PM, Ivan <[LINK: > mailto:[hidden email]] [hidden email]> wrote: > Even, > > Thanks for you reply. > > I wrote this piece of code to demonstrate what is happening: > > {{{ > public static void main(String[] args) { > > Dataset dataset = null; > Driver driver = null; > Band band = null; > > int xsize = 4; > int ysize = 4; > > gdal.AllRegister(); > > driver = gdal.GetDriverByName("GTIFF"); > dataset = driver.Create("D:\\temp\\checkit.tif", xsize, > ysize, 1, gdalconstConstants.GDT_Float32); > band = dataset.GetRasterBand(1); > > > ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4 * xsize); > FloatBuffer floatBuffer = byteBuffer.asFloatBuffer(); > > for( int i = 0; i < ysize; i++) { > for( int j = 0; j < xsize; j++) { > floatBuffer.put(j, (float) (i + j)); > } > band.WriteRaster_Direct(0, i, xsize, 1, > gdalconstConstants.GDT_Float32, byteBuffer); > } > > band.FlushCache(); > dataset.FlushCache(); > dataset.delete(); > > System.out.println("Done!"); > } > }}} > > And that is the output image dumped in Python: > > array([[ 0.00000000e+00, 4.60060299e-41, 8.96831017e-44, > 2.30485571e-41], > [ 4.60060299e-41, 8.96831017e-44, 2.30485571e-41, > 4.60074312e-41], > [ 8.96831017e-44, 2.30485571e-41, 4.60074312e-41, > 5.74868682e-41], > [ 2.30485571e-41, 4.60074312e-41, 5.74868682e-41, > 6.89663052e-41]], dtype=float32)) > > If I change the code to produce Byte output: > > {{{ > public static void main(String[] args) { > > Dataset dataset = null; > Driver driver = null; > Band band = null; > > int xsize = 4; > int ysize = 4; > > gdal.AllRegister(); > > driver = gdal.GetDriverByName("GTIFF"); > dataset = driver.Create("D:\\temp\\checkit2.tif", xsize, > ysize, 1, gdalconstConstants.GDT_Byte); > band = dataset.GetRasterBand(1); > > ByteBuffer byteBuffer = ByteBuffer.allocateDirect(xsize); > > for( int i = 0; i < ysize; i++) { > for( int j = 0; j < xsize; j++) { > byteBuffer.put(j, (byte) (i + j)); > } > band.WriteRaster_Direct(0, i, xsize, 1, > gdalconstConstants.GDT_Byte, byteBuffer); > } > > band.FlushCache(); > dataset.FlushCache(); > dataset.delete(); > > System.out.println("Done!"); > } > }}} > > That is what I got: > > array([[0, 1, 2, 3], > [1, 2, 3, 4], > [2, 3, 4, 5], > [3, 4, 5, 6]], dtype=uint8) > > That is correct but I need Float32 output on my real application. > > Can you see if I am missing something? > > When [1] says "It automatically takes care of data type translation if > the data type" it means exactly like the others GDAL APIs? > > My best regards, > > Ivan > > [1] [LINK: http://gdal.org/java/org/gdal/gdal/Band.html] > http://gdal.org/java/org/gdal/gdal/Band.html#WriteRaster_Direct%28int,%20int,%20int,%20int,%20int,%20int,%20int,%20java.nio.ByteBuffer,%20int,%20int%29 > > > > -------Original Message------- > > From: Even Rouault <[LINK: mailto:[hidden email]] > [hidden email]> > > Subject: Re: [gdal-dev] GDAL-Java: How to write Flot32, Int16? > > Sent: Nov 03 '09 17:18 > > > > Selon Ivan <[LINK: mailto:[hidden email]] > [hidden email]>: > > > > The Java API only uses ByteBuffer. But you can use the > asDoubleBuffer(), > > asFloatBuffer(), asIntBuffer() or asShortBuffer() methods to create > views of the > > byte buffer as double, float, int or short buffer. > > > > For example : > > > > ByteBuffer byteBuffer = ByteBuffer.allocateDirect(8 * buf_xsize * > buf_ysize); > > band.ReadRaster_Direct(0, 0, xsize, ysize, buf_xsize, buf_ysize, > > gdal.GDT_Float64, byteBuffer); > > DoubleBuffer doubleBuffer = byteBuffer.asDoubleBuffer(); > > > > > Hi, > > > > > > Does anybody know how to write a FloatBuffer, IntBuffer or > ShotBuffer using > > > the Java API? > > > > > > Thanks, > > > > > > Ivan > > > _______________________________________________ > > > gdal-dev mailing list > > > [LINK: mailto:[hidden email]] [hidden email] > > > [LINK: http://lists.osgeo.org/mailman/listinfo/gdal-dev] > http://lists.osgeo.org/mailman/listinfo/gdal-dev > > > > > > > > > > _______________________________________________ > gdal-dev mailing list > [LINK: mailto:[hidden email]] [hidden email] > [LINK: http://lists.osgeo.org/mailman/listinfo/gdal-dev] > http://lists.osgeo.org/mailman/listinfo/gdal-dev > > > -- > ------------------------------------------------------- > Eng. Daniele Romagnoli > Software Engineer > > GeoSolutions S.A.S. > Via Carignoni 51 > 55041 Camaiore (LU) > Italy > > phone: +39 0584983027 > fax: +39 0584983027 > mob: +39 328 0559267 > > > [LINK: http://www.geo-solutions.it] http://www.geo-solutions.it > > ------------------------------------------------------- gdal-dev mailing list [hidden email] http://lists.osgeo.org/mailman/listinfo/gdal-dev |
||||||||||||||||
|
Even Rouault
|
Selon Ivan <[hidden email]>:
I realize I hadn't yet tested with multibyte data types. I've improved the javadoc of the ReadRaster_Direct and WriteRaster_Direct methods to mention the byte ordering issue. > Thanks Daniele. That takes care of the problem. > > > > -------Original Message------- > > From: Daniele Romagnoli <[hidden email]> > > Subject: Re: [gdal-dev] GDAL-Java: How to write Flot32, Int16? > > Sent: Nov 04 '09 12:36 > > > > Hi Ivan, > > > > Try adding this: > > byteBuffer.order(ByteOrder.nativeOrder()); > > > > after the allocateDirect and before the asFloatBuffer calls. > > > > That is: > > ------------------------------- > > ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4 * xsize); > > byteBuffer.order(ByteOrder.nativeOrder()); > > FloatBuffer floatBuffer = byteBuffer.asFloatBuffer(); > > ------------------------------- > > > > Let me know whether this does work. > > Best Regards, > > Daniele > > > > > > On Wed, Nov 4, 2009 at 5:44 PM, Ivan <[LINK: > > mailto:[hidden email]] [hidden email]> wrote: > > Even, > > > > Thanks for you reply. > > > > I wrote this piece of code to demonstrate what is happening: > > > > {{{ > > public static void main(String[] args) { > > > > Dataset dataset = null; > > Driver driver = null; > > Band band = null; > > > > int xsize = 4; > > int ysize = 4; > > > > gdal.AllRegister(); > > > > driver = gdal.GetDriverByName("GTIFF"); > > dataset = driver.Create("D:\\temp\\checkit.tif", xsize, > > ysize, 1, gdalconstConstants.GDT_Float32); > > band = dataset.GetRasterBand(1); > > > > > > ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4 * xsize); > > FloatBuffer floatBuffer = byteBuffer.asFloatBuffer(); > > > > for( int i = 0; i < ysize; i++) { > > for( int j = 0; j < xsize; j++) { > > floatBuffer.put(j, (float) (i + j)); > > } > > band.WriteRaster_Direct(0, i, xsize, 1, > > gdalconstConstants.GDT_Float32, byteBuffer); > > } > > > > band.FlushCache(); > > dataset.FlushCache(); > > dataset.delete(); > > > > System.out.println("Done!"); > > } > > }}} > > > > And that is the output image dumped in Python: > > > > array([[ 0.00000000e+00, 4.60060299e-41, 8.96831017e-44, > > 2.30485571e-41], > > [ 4.60060299e-41, 8.96831017e-44, 2.30485571e-41, > > 4.60074312e-41], > > [ 8.96831017e-44, 2.30485571e-41, 4.60074312e-41, > > 5.74868682e-41], > > [ 2.30485571e-41, 4.60074312e-41, 5.74868682e-41, > > 6.89663052e-41]], dtype=float32)) > > > > If I change the code to produce Byte output: > > > > {{{ > > public static void main(String[] args) { > > > > Dataset dataset = null; > > Driver driver = null; > > Band band = null; > > > > int xsize = 4; > > int ysize = 4; > > > > gdal.AllRegister(); > > > > driver = gdal.GetDriverByName("GTIFF"); > > dataset = driver.Create("D:\\temp\\checkit2.tif", xsize, > > ysize, 1, gdalconstConstants.GDT_Byte); > > band = dataset.GetRasterBand(1); > > > > ByteBuffer byteBuffer = ByteBuffer.allocateDirect(xsize); > > > > for( int i = 0; i < ysize; i++) { > > for( int j = 0; j < xsize; j++) { > > byteBuffer.put(j, (byte) (i + j)); > > } > > band.WriteRaster_Direct(0, i, xsize, 1, > > gdalconstConstants.GDT_Byte, byteBuffer); > > } > > > > band.FlushCache(); > > dataset.FlushCache(); > > dataset.delete(); > > > > System.out.println("Done!"); > > } > > }}} > > > > That is what I got: > > > > array([[0, 1, 2, 3], > > [1, 2, 3, 4], > > [2, 3, 4, 5], > > [3, 4, 5, 6]], dtype=uint8) > > > > That is correct but I need Float32 output on my real application. > > > > Can you see if I am missing something? > > > > When [1] says "It automatically takes care of data type translation if > > the data type" it means exactly like the others GDAL APIs? > > > > My best regards, > > > > Ivan > > > > [1] [LINK: http://gdal.org/java/org/gdal/gdal/Band.html] > > > > > > > > > > -------Original Message------- > > > From: Even Rouault <[LINK: mailto:[hidden email]] > > [hidden email]> > > > Subject: Re: [gdal-dev] GDAL-Java: How to write Flot32, Int16? > > > Sent: Nov 03 '09 17:18 > > > > > > Selon Ivan <[LINK: mailto:[hidden email]] > > [hidden email]>: > > > > > > The Java API only uses ByteBuffer. But you can use the > > asDoubleBuffer(), > > > asFloatBuffer(), asIntBuffer() or asShortBuffer() methods to create > > views of the > > > byte buffer as double, float, int or short buffer. > > > > > > For example : > > > > > > ByteBuffer byteBuffer = ByteBuffer.allocateDirect(8 * buf_xsize * > > buf_ysize); > > > band.ReadRaster_Direct(0, 0, xsize, ysize, buf_xsize, buf_ysize, > > > gdal.GDT_Float64, byteBuffer); > > > DoubleBuffer doubleBuffer = byteBuffer.asDoubleBuffer(); > > > > > > > Hi, > > > > > > > > Does anybody know how to write a FloatBuffer, IntBuffer or > > ShotBuffer using > > > > the Java API? > > > > > > > > Thanks, > > > > > > > > Ivan > > > > _______________________________________________ > > > > gdal-dev mailing list > > > > [LINK: mailto:[hidden email]] [hidden email] > > > > [LINK: http://lists.osgeo.org/mailman/listinfo/gdal-dev] > > http://lists.osgeo.org/mailman/listinfo/gdal-dev > > > > > > > > > > > > > > > _______________________________________________ > > gdal-dev mailing list > > [LINK: mailto:[hidden email]] [hidden email] > > [LINK: http://lists.osgeo.org/mailman/listinfo/gdal-dev] > > http://lists.osgeo.org/mailman/listinfo/gdal-dev > > > > > > -- > > ------------------------------------------------------- > > Eng. Daniele Romagnoli > > Software Engineer > > > > GeoSolutions S.A.S. > > Via Carignoni 51 > > 55041 Camaiore (LU) > > Italy > > > > phone: +39 0584983027 > > fax: +39 0584983027 > > mob: +39 328 0559267 > > > > > > [LINK: http://www.geo-solutions.it] http://www.geo-solutions.it > > > > ------------------------------------------------------- > _______________________________________________ gdal-dev mailing list [hidden email] http://lists.osgeo.org/mailman/listinfo/gdal-dev |
||||||||||||||||
| Free Embeddable Forum Powered by Nabble | Help |