[Gdal-dev] Re: GDAL data writing problems (using Java SWIG bindings)

Daniele Romagnoli dany.geotools at gmail.com
Wed Jan 3 11:48:10 EST 2007


Hi list,
I solved my problem debugging gdalJNI.dll.

Since the SWIG Java Bindings use the method "GetDirectBufferAddress(...)"
which requires a direct buffer,
I fixed the code:
------------------------------------------------------------------------------------------------------------------------------------------
...
byte [] byteRaster =
((DataBufferByte)b0.getData().getDataBuffer()).getData();
ByteBuffer bb=ByteBuffer.allocateDirect(byteRaster.length);
bb.put(byteRaster);
...
------------------------------------------------------------------------------------------------------------------------------------------

The old code was:
------------------------------------------------------------------------------------------------------------------------------------------
...
ByteBuffer bb0 = ByteBuffer.wrap(((DataBufferByte)
b0.getData().getDataBuffer()).getData(0));

...

which uses a HeapByteBuffer. In such a case, GetDirectBufferAddress returns
NULL.
------------------------------------------------------------------------------------------------------------------------------------------


With these changes, the final code becomes:
------------------------------------------------------------------------------------------------------------------------------------------
        final File outFile = new
File("D:\\DatiPlugin\\geotiff\\mysample.tif");
        final BufferedImage im = ImageIO.read(new
File("D:\\DatiPlugin\\bogota.tif"));
        Driver gtiffDriver = gdal.GetDriverByName("GTiff");
        final int width = im.getWidth();
        final int height = im.getHeight();

        Dataset ds = gtiffDriver.Create(outFile.getAbsolutePath(), width,
height, 1, gdalconstConstants.GDT_Byte,null);
        RenderedOp b0 = BandSelectDescriptor.create(im, new int[] { 0 },
null);

        byte [] byteRaster =
((DataBufferByte)b0.getData().getDataBuffer()).getData();
        ByteBuffer bb=ByteBuffer.allocateDirect(byteRaster.length);
        bb.put(byteRaster);
        ds.GetRasterBand
(1).WriteRaster_Direct(0,0,width,height,width,height,
gdalconstConstants.GDT_Byte,bb);

        ds.FlushCache();
        ds.delete();
------------------------------------------------------------------------------------------------------------------------------------------







On 12/27/06, Daniele Romagnoli < dany.geotools at gmail.com> wrote:
>
> Hi list,
>
> I'm unsuccessfully trying to write some raster data using  GDAL Java
> Bindings, by mean of the writeRaster_Direct method.
>
> This is my code:
> ------------------------------------------------------------
> final File outFile = new File("d:\\DatiPlugin\\geotiff\\mysample22.tif");
> final BufferedImage im = ImageIO.read(new
> File("D:\\DatiPlugin\\bogota.tif"));
>
> Driver gtiffDriver = gdal.GetDriverByName("GTiff");
> final int width = im.getWidth();
> final int height = im.getHeight ();
>
> Dataset ds = gtiffDriver.Create(outFile.getAbsolutePath(), width,
> height, 1, gdalconstConstants.GDT_Byte,null);
>
> RenderedOp b0 = BandSelectDescriptor.create(im, new int[] { 0 }, null);
>
> ByteBuffer bb0 = ByteBuffer.wrap(((DataBufferByte)
> b0.getData().getDataBuffer()).getData(0));
>
> ByteBuffer scanline;
> byte  miniBuffer[] = new byte [width];
>
> for (int i=0; i<height;i++){
>    bb0.get(miniBuffer, width*i, width);
>    scanline = ByteBuffer.wrap(miniBuffer);
>    ds.GetRasterBand (1).WriteRaster_Direct(0,height*i,width,1,width,1,
> gdalconstConstants.GDT_Byte,scanline);
> }
>
>
> ds.FlushCache();
> ds.delete();
> ------------------------------------------------------------
>
>
>
> This code, generates this exception:
> -------------------------------------------------------
> An unexpected exception has been detected in native code outside the VM.
> Unexpected Signal : EXCEPTION_ACCESS_VIOLATION (0xc0000005) occurred
> at PC=0x7C342EEE
> Function=memcpy+0x33
> Library=C:\WINDOWS\system32\MSVCR71.dll
> Source file = F:\VS70Builds\3052\vc\crtbld\crt\src\intel\memcpy.asm : 171
>
>
> Current Java thread:
>    at org.gdal.gdal.gdalJNI.Band_WriteRaster_Direct (Native Method)
>    at org.gdal.gdal.Band.WriteRaster_Direct(Band.java:127)
>    at javax.imageio.plugins.jp2k.TestJaiRead.testWrite(TestJaiRead.java
> :178)
>    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>    at sun.reflect.NativeMethodAccessorImpl.invoke(
> NativeMethodAccessorImpl.java:39)
>    at sun.reflect.DelegatingMethodAccessorImpl.invoke(
> DelegatingMethodAccessorImpl.java:25)
>    at java.lang.reflect.Method.invoke (Method.java:324)
>    at junit.framework.TestCase.runTest(TestCase.java:154)
>    at junit.framework.TestCase.runBare(TestCase.java:127)
>    at junit.framework.TestResult$1.protect(TestResult.java:106)
>    at junit.framework.TestResult.runProtected (TestResult.java:124)
>    at junit.framework.TestResult.run(TestResult.java:109)
>    at junit.framework.TestCase.run(TestCase.java:118)
>    at junit.framework.TestSuite.runTest (TestSuite.java:208)
>    at junit.framework.TestSuite.run (TestSuite.java:203)
>    at junit.textui.TestRunner.doRun(TestRunner.java:116)
>    at junit.textui.TestRunner.doRun(TestRunner.java:109)
>    at junit.textui.TestRunner.run (TestRunner.java:72)
>    at junit.textui.TestRunner.run (TestRunner.java:57)
>    at javax.imageio.plugins.jp2k.TestJaiRead.main(TestJaiRead.java:76)
> ------------------------------------------
>
>
> Does anybody have any clues about what I'm doing wrong?
>
> Moreover, I have one question. Did anybody tried to write data using
> the java bindings directly?  I mean without using DataSet CreateCopy or
> the
> like but feeding data directly into a RasterBand?
>
>
> Best regards,
> Daniele
>
>
>
>
>
>
>
>
>
> --
> Best Regards,
> Daniele




-- 
Best Regards,
Daniele
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/gdal-dev/attachments/20070103/781a406a/attachment.html


More information about the Gdal-dev mailing list