In reply to my own query,  I ended up doing this to get the max value.

import org.jaitools.numeric.SampleStats;
import java.awt.image.Raster;
import org.apache.commons.lang3.ArrayUtils;
.
.
.
        Raster raster = coverage.getRenderedImage().getData();
        double[] data = new double[raster.getHeight()*raster.getWidth()];

        raster.getSamples(raster.getMinX(),
                raster.getMinY(),
                raster.getWidth(),
                raster.getHeight(), 0, data);
        System.out.println(SampleStats.max(ArrayUtils.toObject(data),
true));

It works, but still wondering if there is a neater way.

- Lauchlin


-----
 "The thing's hollow—it goes on forever—and—oh my God—*it's full of stars!*"


On 15 February 2013 15:06, Lauchlin Wilkinson <[email protected]> wrote:

> Hi,
>
> I'm using geotools 8.1 (with a maven project), and am trying to get the
> Minimum and Maximum values for a the single band that is contained in a
> file.
>
> The code I am using is:
>
>         File file = new File("/home/xxx/grid.asc");
>         AbstractGridFormat format = GridFormatFinder.findFormat(file);
>         reader = format.getReader(file);
>
>         GridCoverage2D coverage = reader.read(null);
>
>         System.out.println("Num Dimensions: " +
> coverage.getNumSampleDimensions());
>
>         System.out.println("Min value: " +
> coverage.getSampleDimension(0).getMinimumValue());
>         System.out.println("Max value: " +
> coverage.getSampleDimension(0).getMaximumValue());
>
> I know the max value is 1739, the min value is 1 and the NODATA value is
> -99999 by inspecting the grid in SAGA but the above code outputs:
>
> Num Dimensions: 1
> Min value: -99999.0
> Max value: -99999.0
>
> I have also tried using:
>
>         NumberRange range = coverage.getSampleDimension(0).getRange();
>
> System.out.println(coverage.getSampleDimension(0).getSampleDimensionType().identifier());
>         System.out.println("Min value: " + range.getMinimum());
>         System.out.println("Max value: " + range.getMaximum());
>
> But this also outputs the incorrect values:
>
> CV_64BIT_REAL
> Min value: -99999.0
> Max value: -99999.0
>
> When displaying the map with a ColorMap it does show correctly so the
> above output is confusing.  Am I using the correct methods or is there
> another way I could easily obtain the min/max without scanning the whole
> file?  Is this just a limitation of ascii file format?
>
> Any help would be greatly appreciated.  I hope I have provided enough
> detail.
>
> Thanks,
>
> Lauchlin
>
>
>
------------------------------------------------------------------------------
Free Next-Gen Firewall Hardware Offer
Buy your Sophos next-gen firewall before the end March 2013 
and get the hardware for free! Learn more.
http://p.sf.net/sfu/sophos-d2d-feb
_______________________________________________
GeoTools-GT2-Users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to