@Test
public void testPolygonZoneGlobalStats() throws Exception {
final File tiff = TestData.file(this, "test.tif");
final File tfw = TestData.file(this, "test.tfw");
final TIFFImageReader reader = (it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReader) new TIFFImageReaderSpi()
.createReaderInstance();
reader.setInput(ImageIO.createImageInputStream(tiff));
final BufferedImage image = reader.read(0);
reader.dispose();
final MathTransform transform = new WorldFileReader(tfw).getTransform();
final GridCoverage2D coverage2D = CoverageFactoryFinder.getGridCoverageFactory(null)
.create("coverage",
image,
new GridGeometry2D(new GridEnvelope2D(PlanarImage.wrapRenderedImage(image)
.getBounds()), transform, DefaultGeographicCRS.WGS84),
new GridSampleDimension[] { new GridSampleDimension("coverage") }, null,
null);
final File fileshp = TestData.file(this, "testpolygon.shp");
final DataStore store = FileDataStoreFinder.getDataStore(fileshp.toURI().toURL());
FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = store
.getFeatureSource(store.getNames().get(0));
FeatureCollection<SimpleFeatureType, SimpleFeature> featureCollection = featureSource
.getFeatures();
List<SimpleFeature> polygonList = new ArrayList<SimpleFeature>();
FeatureIterator<SimpleFeature> featureIterator = featureCollection.features();
while (featureIterator.hasNext()) {
SimpleFeature feature = featureIterator.next();
polygonList.add(feature);
}
featureIterator.close();
// choose the stats
Set<Statistic> statsSet = new LinkedHashSet<Statistic>();
statsSet.add(Statistic.MIN);
statsSet.add(Statistic.MAX);
statsSet.add(Statistic.MEAN);
statsSet.add(Statistic.VARIANCE);
statsSet.add(Statistic.SDEV);
statsSet.add(Statistic.RANGE);
// select the bands to work on
Integer[] bands = new Integer[] { 0 };
List<Range<Double>> inclusionRanges = new ArrayList<Range<Double>>();
inclusionRanges
.add(new Range<Double>(Double.valueOf(0), false, Double.valueOf(1300), true));
inclusionRanges.add(new Range<Double>(Double.valueOf(1370), true, Double.valueOf(1600),
true));
// create the proper instance
StatisticsTool statisticsTool = new StatisticsTool(statsSet, coverage2D, bands,
polygonList, inclusionRanges, Type.INCLUDE, false);
// do analysis
statisticsTool.run();
// get the results
String id = "testpolygon.1";
Map<Statistic, List<Result>> statistics = statisticsTool.getStatistics(id);
LOGGER.info(id + statistics.toString());
assertEquals(statistics.get(Statistic.RANGE).get(0).getValue().doubleValue(), 343.0, DELTA);
assertEquals(statistics.get(Statistic.SDEV).get(0).getValue().doubleValue(), 88.7358, DELTA);
assertEquals(statistics.get(Statistic.MIN).get(0).getValue().doubleValue(), 1255.0, DELTA);
assertEquals(statistics.get(Statistic.MEAN).get(0).getValue().doubleValue(), 1380.5423,
DELTA);
assertEquals(statistics.get(Statistic.VARIANCE).get(0).getValue().doubleValue(), 7874.0598,
DELTA);
assertEquals(statistics.get(Statistic.MAX).get(0).getValue().doubleValue(), 1598.0, DELTA);
id = "testpolygon.2";
statistics = statisticsTool.getStatistics(id);
LOGGER.info(id + statistics.toString());
assertEquals(statistics.get(Statistic.RANGE).get(0).getValue().doubleValue(), 216.0, DELTA);
assertEquals(statistics.get(Statistic.SDEV).get(0).getValue().doubleValue(), 36.7996, DELTA);
assertEquals(statistics.get(Statistic.MIN).get(0).getValue().doubleValue(), 1192.0, DELTA);
assertEquals(statistics.get(Statistic.MEAN).get(0).getValue().doubleValue(), 1248.3870,
DELTA);
assertEquals(statistics.get(Statistic.VARIANCE).get(0).getValue().doubleValue(), 1354.2150,
DELTA);
assertEquals(statistics.get(Statistic.MAX).get(0).getValue().doubleValue(), 1408.0, DELTA);
id = "testpolygon.3";
statistics = statisticsTool.getStatistics(id);
LOGGER.info(id + statistics.toString());
assertEquals(statistics.get(Statistic.RANGE).get(0).getValue().doubleValue(), 127.0000,
DELTA);
assertEquals(statistics.get(Statistic.SDEV).get(0).getValue().doubleValue(), 30.9412, DELTA);
assertEquals(statistics.get(Statistic.MIN).get(0).getValue().doubleValue(), 1173.0, DELTA);
assertEquals(statistics.get(Statistic.MEAN).get(0).getValue().doubleValue(), 1266.3876,
DELTA);
assertEquals(statistics.get(Statistic.VARIANCE).get(0).getValue().doubleValue(), 957.3594,
DELTA);
assertEquals(statistics.get(Statistic.MAX).get(0).getValue().doubleValue(), 1300.0, DELTA);
reader.dispose();
coverage2D.dispose(true);
image.flush();
}