Re: [gdal-dev] GDAL-Java: How to write Flot32, Int16?

Ivan ivan.lucena at pmldnet.com
Wed Nov 4 13:58:50 EST 2009


Thanks Daniele. That takes care of the problem.


>  -------Original Message-------
>  From: Daniele Romagnoli <daniele.romagnoli at geo-solutions.it>
>  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:ivan.lucena at pmldnet.com] ivan.lucena at pmldnet.com> 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:even.rouault at mines-paris.org]
>  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 <[LINK: mailto:ivan.lucena at pmldnet.com]
>  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
>  >  > [LINK: mailto:gdal-dev at lists.osgeo.org] gdal-dev at lists.osgeo.org
>  >  > [LINK: http://lists.osgeo.org/mailman/listinfo/gdal-dev]
>  http://lists.osgeo.org/mailman/listinfo/gdal-dev
>  >  >
>  >
>  >
>  >
>  _______________________________________________
>  gdal-dev mailing list
>  [LINK: mailto:gdal-dev at lists.osgeo.org] gdal-dev at lists.osgeo.org
>  [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
>  
>  -------------------------------------------------------


More information about the gdal-dev mailing list