On 13 March 2013 16:52, Lauchlin Wilkinson <m...@lauchlin.com> wrote:

> On 12 March 2013 20:06, PabloDR <pablo.labor...@gmail.com> wrote:
>
>>
>> El 12/03/13 00:30, Lauchlin Wilkinson escribió:
>>
>> On 12 March 2013 06:02, PabloDR <pablo.labor...@gmail.com> wrote:
>>
>>>  Hi,
>>>
>>> I found this geotools api (udig) that implements several methods about
>>> getting information about ArcGridCoverage:
>>> http://udig.refractions.net/files/docs/api-geotools/index.html
>>> But in the oficial geotools repository doesn't exists.
>>>
>>> Somebody know how to implement/get this class?
>>>
>>> Thank you in advance,
>>>
>>> Pablo.
>>>
>>>
>>>
>>  Hi,
>>
>>    Hi,
>>
>>   Is this code snippet what you are after?  Once you have the Coverage2D
>> object you can just use it like any other coverage.
>> http://docs.geotools.org/latest/userguide/tutorial/raster/image.html has
>> more info about using images/rasters.
>>
>>  Cheers,
>>
>>  Lauchlin
>>
>>  import java.io.File;
>> import java.io.IOException;
>> import org.geotools.coverage.grid.GridCoverage2D;
>>  import org.geotools.coverage.grid.io.AbstractGridCoverage2DReader;
>> import org.geotools.coverage.grid.io.AbstractGridFormat;
>> import org.geotools.coverage.grid.io.GridFormatFinder;
>>
>>  public class SomeClass {
>>
>>     public void someMethod() throws IOException {
>>         /**
>>          * requires gt-arcgrid.x.x.jar add the following to your pom.xml
>>          *
>>          * <dependency>
>>          *   <groupId>org.geotools</groupId>
>>          *   <artifactId>gt-arcgrid</artifactId>
>>          *   <version>${geotools.version}</version>
>>          * </dependency>
>>          */
>>         File file = new File("/path/to/vegetation.asc");
>>         AbstractGridFormat format = GridFormatFinder.findFormat(file);
>>         AbstractGridCoverage2DReader reader = format.getReader(file);
>>
>>          GridCoverage2D vegetationCoverage = reader.read(null);
>>     }
>>
>> }
>>
>>   I just use this code to read .asc raster but with it, I can't get
>> information such us NoDataValue ...
>>
>> The link I put up isn't well, the correct link with class implements the
>> methods I like is this:
>>
>>
>> http://udig.refractions.net/files/docs/api-geotools/org/geotools/gce/arcgrid/ArcGridRaster.html
>>
>> Thanks.
>>
>
> Hi,
>
> It looks like you are looking at an old version of the api docs (2.3.x).
>  Recent (and latest) builds do not have this class.
>
>
> http://docs.geotools.org/latest/javadocs/org/geotools/gce/arcgrid/package-summary.html
>
> I actually asked a similar question a few day back because I was trying to
> get nodata, min and max values using:
>
> vegetationCoverage.getSampleDimension(0).getMinimumValue()
> vegetationCoverage.getSampleDimension(0).getMaximumValue()
> vegetationCoverage.getSampleDimension(0).getNoDataValues()
>
> etc.
>
> But these values do not work because it relies on values stored in the
> file header and ARCInfo grids don't need to store min-max values.
>
> You can however get the NoData value using.
>
> vegetationCoverage.getProperty("GC_NODATA")
>
> To get min and max I initially ended up doing something like this...which
> doesn't really work as it confuses nodata and minimum values, which is ok
> if your nodata is set to something like NaN (which I think might work)
>
> 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));
>
> So seeing as I hadn't discovered a good solution I ended up going and
> downloading the source for 2.2 which seems like it is the last version this
> Class was in.  The code that sets the min and max is the following.
>
> for (int y = 0; y < getNRows(); y++) {
>             for (int x = 0; x < getNCols(); x++) {
>                 // this call always reads the next token
>                 double d = readCell(st, x, y);
>
>                 // mask no data values with NaN
>                 if (d == getNoData()) {
>                     d = Double.NaN;
>                 } else {
>                     minValue = Math.min(minValue, d);
>                     maxValue = Math.max(maxValue, d);
>                 }
>
>                 // set the value at x,y in band 0 to the parsed value
>                 raster.setSample(x, y, 0, (float) d);
>             }
>         }
>
> I couldn't find anything in newer versions that re-implements this so
> perhaps you are best off just doing the same reimplementing this code
> snippet yourself.
>
> It would be nice if one of the devs that monitor this mailing list could
> comment on this question.
>
> - Lauchlin
>

Just adding that I quickly changed my code example above to this:

        File file = new File("/path/to/vegetation.asc");
        AbstractGridFormat format = GridFormatFinder.findFormat(file);
        AbstractGridCoverage2DReader reader = format.getReader(file);

        GridCoverage2D vegetationCoverage = reader.read(null);

        int width = (Integer) vegetationCoverage.getProperty("image_width");
        int height= (Integer)
vegetationCoverage.getProperty("image_height");
        double noData = (Double)
vegetationCoverage.getProperty("GC_NODATA");
        double[] dest = new double[1];
        double minValue = Float.MAX_VALUE;
        double maxValue = Float.MIN_VALUE;

        for(int y=0; y<height; y++) {
            for(int x=0; x<width; x++) {
                vegetationCoverage.evaluate(new GridCoordinates2D(x,y),
dest);
                 if (dest[0] != noData) {
                    minValue = Math.min(minValue, dest[0]);
                    maxValue = Math.max(maxValue, dest[0]);
                }
            }
        }


        System.out.println(minValue);
        System.out.println(maxValue);
------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_d2d_mar
_______________________________________________
GeoTools-GT2-Users mailing list
GeoTools-GT2-Users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to