Hello,

It looks like you are using the coordinates from the coverage envelope
(world coordinates) directly with the Raster.getPixels method.
However, the latter method expects image coordinates. You can retrieve
the bounding image coordinates with the RenderedImage functions
getMinX(), getMinY(), getWidth() and getHeight().

Also, it is generally better practice to use an image iterator to
retrieve values rather than loading the whole image into memory - that
way your code will still work with large images. GeoTools
automagically includes the JAI and JAITools libraries, so you could
use the JAI RectIter class or the slightly easier to use JAITools
SimpleIterator class. Here are the links to the javadocs if you want
to try that:

http://docs.oracle.com/cd/E17802_01/products/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/iterator/RectIter.html

http://jaitools.org/docs/jaitools/stable/apidocs/org/jaitools/imageutils/SimpleIterator.html

Michael


On 1 June 2012 10:51, agawrons <[email protected]> wrote:
> I'm new to geomatics and GeoTools so this might be a silly question, but I'm
> really stuck. I want to extract pixel data from a geoTiff image as a double
> array but no matter what I try, all I get are zeros.
>
> Here is my code:
>
> File file = new File("./input/ASAR_04232010.tif");
>
> AbstractGridFormat format = GridFormatFinder.findFormat(file);
> AbstractGridCoverage2DReader reader = format.getReader(file);
>
> GridCoverage2D coverage = reader.read(null);
>
> RenderedImage rendImage = coverage.getRenderedImage();
> Raster raster = rendImage.getData();
> Envelope2D dim = coverage.getEnvelope2D();
> double[] array = new double[100];
> array = raster.getPixels((int)dim.x + 1, (int)dim.y + 1, 100, 0, array);
>
> for(double num : array){
>     System.out.println(num + "");
> }
>
> I tried a different file and got the same result. Unfortunately the files
> are too large to attach.
>
> Thanks in advance!
> Alex
>
> --
> View this message in context: 
> http://osgeo-org.1560.n6.nabble.com/Retrieving-Pixel-Data-tp4978427.html
> Sent from the geotools-gt2-users mailing list archive at Nabble.com.
>
> ------------------------------------------------------------------------------
> Live Security Virtual Conference
> Exclusive live event will cover all the ways today's security and
> threat landscape has changed and how IT managers can respond. Discussions
> will include endpoint security, mobile security and the latest in malware
> threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
> _______________________________________________
> GeoTools-GT2-Users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
GeoTools-GT2-Users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to