Re: [gdal-dev] GDAL-Java: How to write Flot32, Int16?
Ivan
ivan.lucena at pmldnet.com
Wed Nov 4 11:44:53 EST 2009
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 <even.rouault at mines-paris.org>
> Subject: Re: [gdal-dev] GDAL-Java: How to write Flot32, Int16?
> Sent: Nov 03 '09 17:18
>
> Selon Ivan <ivan.lucena at pmldnet.com>:
>
> 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
> > gdal-dev at lists.osgeo.org
> > http://lists.osgeo.org/mailman/listinfo/gdal-dev
> >
>
>
>
More information about the gdal-dev
mailing list