final Object object = selection.getFirstElement();
try {
pm.beginTask("Collecting stats...", IProgressMonitor.UNKNOWN);
StringBuilder regionString = new StringBuilder();
GridCoverage2D geodata = null;
if (object instanceof AbstractRasterGeoResource) {
AbstractRasterGeoResource rGeo = (AbstractRasterGeoResource) object;
AbstractGridCoverage2DReader gridCoverage2DReader = rGeo.resolve(
AbstractGridCoverage2DReader.class, pm);
geodata = ((GridCoverage2D) gridCoverage2DReader.read(null));
geodata = geodata.view(ViewType.GEOPHYSICS);
Envelope envelope = geodata.getEnvelope();
DirectPosition lowerCorner = envelope.getLowerCorner();
double[] westSouth = lowerCorner.getCoordinate();
DirectPosition upperCorner = envelope.getUpperCorner();
double[] eastNorth = upperCorner.getCoordinate();
GridGeometry2D gridGeometry = geodata.getGridGeometry();
GridEnvelope2D gridRange = gridGeometry.getGridRange2D();
int rows = gridRange.height;
int cols = gridRange.width;
AffineTransform gridToCRS = (AffineTransform) gridGeometry.getGridToCRS();
double we_res = XAffineTransform.getScaleX0(gridToCRS);
double ns_res = XAffineTransform.getScaleY0(gridToCRS);
double north = eastNorth[1];
double south = westSouth[1];
double east = eastNorth[0];
double west = westSouth[0];
regionString.append("region:\nwest=");
regionString.append(west);
regionString.append("\neast=");
regionString.append(east);
regionString.append("\nsouth=");
regionString.append(south);
regionString.append("\nnorth=");
regionString.append(north);
regionString.append("\nwe_res=");
regionString.append(we_res);
regionString.append("\nns_res=");
regionString.append(ns_res);
regionString.append("\nrows=");
regionString.append(rows);
regionString.append("\ncols=");
regionString.append(cols);
} else if (object instanceof JGrassMapGeoResource) {
JGrassMapGeoResource mr = (JGrassMapGeoResource) object;
File mapFile = mr.getMapFile();
JGrassMapEnvironment mapEnvironment = new JGrassMapEnvironment(mapFile);
JGrassRegion jGrassRegion = mapEnvironment.getActiveRegion();
int cols = jGrassRegion.getCols();
int rows = jGrassRegion.getRows();
double north = jGrassRegion.getNorth();
double south = jGrassRegion.getSouth();
double east = jGrassRegion.getEast();
double west = jGrassRegion.getWest();
double we_res = jGrassRegion.getWEResolution();
double ns_res = jGrassRegion.getNSResolution();
regionString.append("region:\nwest=");
regionString.append(west);
regionString.append("\neast=");
regionString.append(east);
regionString.append("\nsouth=");
regionString.append(south);
regionString.append("\nnorth=");
regionString.append(north);
regionString.append("\nwe_res=");
regionString.append(we_res);
regionString.append("\nns_res=");
regionString.append(ns_res);
regionString.append("\nrows=");
regionString.append(rows);
regionString.append("\ncols=");
regionString.append(cols);
GeneralParameterValue[] readParams = createGridGeometryGeneralParameter(cols, rows, north, south,
east, west, mapEnvironment.getCoordinateReferenceSystem());
AbstractGridFormat format = (AbstractGridFormat) new GrassCoverageFormatFactory().createFormat();
AbstractGridCoverage2DReader reader = format.getReader(mapEnvironment.getCELL());
geodata = ((GridCoverage2D) reader.read(readParams));
geodata = geodata.view(ViewType.GEOPHYSICS);
} else {
MessageDialog.openInformation(shell, "WARNING", "Unable to read format");
return;
}
GridGeometry2D gridGeometry = geodata.getGridGeometry();
GridEnvelope2D gridRange = gridGeometry.getGridRange2D();
int rows = gridRange.height;
int cols = gridRange.width;
AffineTransform gridToCRS = (AffineTransform) gridGeometry.getGridToCRS();
double xRes = XAffineTransform.getScaleX0(gridToCRS);
double yRes = XAffineTransform.getScaleY0(gridToCRS);
RandomIter inputIter = RandomIterFactory.create(geodata.getRenderedImage(), null);
/*
* calculate mean, max and min
*/
double mean = 0.0;
double[] minMaxMeans = new double[]{Double.MAX_VALUE, Double.MIN_VALUE, 0, 0, 0};
int validCells = 0;
for( int y = 0; y < rows; y++ ) {
for( int x = 0; x < cols; x++ ) {
double value = inputIter.getSampleDouble(x, y, 0);
if (!Double.isNaN(value) && (int) value != -9999) {
if (value < minMaxMeans[0])
minMaxMeans[0] = value;
if (value > minMaxMeans[1])
minMaxMeans[1] = value;
mean = mean + value;
validCells++;
}
}
}
mean = mean / (double) validCells;
minMaxMeans[2] = mean;
minMaxMeans[3] = validCells;
minMaxMeans[4] = validCells * xRes * yRes;
/*
* print out some system out
*/
StringBuilder sb = new StringBuilder();
sb.append("Summary for the map:\n");
sb.append("\n");
sb.append("range: " + minMaxMeans[0] + " - " + minMaxMeans[1] + "\n");
sb.append("mean: " + minMaxMeans[2] + "\n");
sb.append("active cells: " + minMaxMeans[3] + "\n");
sb.append("active area (assuming metric resolution): " + minMaxMeans[4] + "\n");
sb.append(regionString.toString() + "\n");
sb.append("data crs: " + geodata.getCoordinateReferenceSystem().getName().toString());
MessageDialog.openInformation(shell, "Summary", sb.toString());
} catch (Exception e) {
String message = "An error occurred while exporting the maps.";