DataBuffer buffer = new DataBufferInt(data, data.length);

should be

DataBuffer buffer = new DataBufferInt(data, data.length*data[0].length);

Ciao
Andrea


On Sun, Dec 6, 2009 at 5:26 PM, Jan Vosecky <[email protected]> wrote:
> Hi Michael,
> Thanks for your answer. I have tried the first method and made the sample
> model 1000 x 1000. But I still get the same out-of-bounds exception.
> I further simplified the program to just deal with 100 x 100 size array and
> image and still get the same exception
> (java.lang.ArrayIndexOutOfBoundsException: 100)
> For reference, the code is:
>         int[][] data = new int[100][100];
>         ColorModel colorModel = new ComponentColorModel(
>                 ColorSpace.getInstance(ColorSpace.CS_GRAY), false, false,
>                 ColorModel.TRANSLUCENT, DataBuffer.TYPE_INT);
>         SampleModel model = new BandedSampleModel(DataBuffer.TYPE_INT, 100,
> 100, 1);
>         DataBuffer buffer = new DataBufferInt(data, data.length);
>         WritableRaster raster = Raster.createWritableRaster(model, buffer,
> null);
>         BufferedImage img = new BufferedImage(colorModel, raster, false,
> null);
>         GridCoverageFactory factory =
> CoverageFactoryFinder.getGridCoverageFactory(null);
>         Envelope2D envelope = new
> Envelope2D(CRS.getProjectedCRS(DefaultGeographicCRS.WGS84), 0, 0, 100, 100);
>         GridCoverage2D cov = factory.create("Coverage", img, envelope);
>         File outputFile = new File("D:\\Output.tif");
>         GeoTiffFormat format = new GeoTiffFormat();
>         GeoTiffWriter writer = (GeoTiffWriter)format.getWriter(outputFile);
>         writer.write(cov, null);
> Do you see any problem with this code?
> Out of interest, I made the array 2000 x 2000 while keeping the rest the
> same, and still get an out-of-bounds exception at 2000.
> Many thanks,
> Jan
> On Sun, Dec 6, 2009 at 8:45 AM, Michael Bedward <[email protected]>
> wrote:
>>
>> Hi Jan,
>>
>> It looks like a mix up between your data array size and your image size.
>>
>> BufferedImages only have a single image tile so when you create your
>> sample model like this...
>>
>> >         SampleModel model = new
>> >         BandedSampleModel(DataBuffer.TYPE_INT, 100, 100, 1);
>>
>> And use that to create your image...
>>
>> >         BufferedImage img = new BufferedImage(colorModel, raster, false,
>> > null);
>>
>> You are making a 100 x 100 image.
>>
>> The problem is that you used your data array size to create the data
>> buffer and raster...
>>
>> Then you use your 1000 x 1000 data matrix to create a data buffer and
>> raster
>>
>> >         DataBuffer buffer = new DataBufferInt(data, data.length);
>> >         WritableRaster raster = Raster.createWritableRaster(model,
>> > buffer, null);
>>
>> So when you try to use the image by writing our the coverage to a
>> GeoTiff, you get the array out of bounds message.
>>
>> To fix the problem you can either make your sample model 1000 x 1000
>> or use a different kind of image that supports multiple image tiles.
>> The former is easier, the latter can make your app less of a memory
>> hog depending on how you use the data.
>>
>> Hope this helps - get back to us if you need more info.
>>
>> Michael
>
>
> ------------------------------------------------------------------------------
> Join us December 9, 2009 for the Red Hat Virtual Experience,
> a free event focused on virtualization and cloud computing.
> Attend in-depth sessions from your desk. Your couch. Anywhere.
> http://p.sf.net/sfu/redhat-sfdev2dev
> _______________________________________________
> Geotools-gt2-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
>
>

------------------------------------------------------------------------------
Join us December 9, 2009 for the Red Hat Virtual Experience,
a free event focused on virtualization and cloud computing. 
Attend in-depth sessions from your desk. Your couch. Anywhere.
http://p.sf.net/sfu/redhat-sfdev2dev
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to