001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2005-2007, Geotools Project Managment Committee (PMC)
005: * (C) 2007, GeoSolutions S.A.S.
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.coverage.processing.operation;
018:
019: import java.awt.Shape;
020: import java.awt.image.RenderedImage;
021: import java.util.Collections;
022: import java.util.HashMap;
023: import java.util.Map;
024: import java.util.logging.Logger;
025:
026: import javax.media.jai.JAI;
027: import javax.media.jai.RenderedOp;
028: import javax.media.jai.operator.ExtremaDescriptor;
029:
030: import org.geotools.coverage.grid.GridCoverage2D;
031: import org.geotools.coverage.processing.AbstractStatisticsOperationJAI;
032: import org.opengis.coverage.processing.OperationNotFoundException;
033: import org.opengis.parameter.ParameterValueGroup;
034: import org.opengis.referencing.crs.CoordinateReferenceSystem;
035: import org.opengis.referencing.operation.MathTransform;
036: import org.opengis.util.InternationalString;
037:
038: /**
039: * This operation simply wraps JAI Extrema operations described by
040: * {@link ExtremaDescriptor} inside a GeoTools operation in order to make it
041: * spatial-aware.
042: *
043: * <p>
044: * For the moment this is a very simple wrap. Plans on the 2.4 na successive
045: * versions of this operation are to add the ability to to use spatial ROIs and
046: * to specific Spatial subsampling. As of now, ROI has to be a Java2D
047: * {@link Shape} subclass and the parameters to control x and y subsamplings got
048: * to be Integer, which means pixel-aware.
049: *
050: * <p>
051: * For more information on how the underlying {@link JAI} operators works you
052: * can have a look here: <a
053: * href="http://download.java.net/media/jai/javadoc/1.1.3/jai-apidocs/javax/media/jai/operator/ExtremaDescriptor.html">ExtremaDescriptor</a>
054: *
055: * <p>
056: * <strong>How to use this operation</strong> Here is a very simple example on
057: * how to use this operation in order to the minimum and maixumum of the source
058: * coverage.
059: *
060: * <code>
061: * final OperationJAI op=new OperationJAI("Extrema");
062: * ParameterValueGroup params = op.getParameters();
063: * params.parameter("Source").setValue(coverage);
064: * coverage=(GridCoverage2D) op.doOperation(params,null);
065: * System.out.println(((double[])coverage.getProperty("minimum"))[0]);
066: * System.out.println(((double[])coverage.getProperty("minimum"))[1]);
067: * System.out.println(((double[])coverage.getProperty("minimum"))[2]);
068: * System.out.println(((double[])coverage.getProperty("maximum"))[0]);
069: * System.out.println(((double[])coverage.getProperty("maximum"))[1]);
070: * System.out.println(((double[])coverage.getProperty("maximum"))[2]);
071: * </code>
072: *
073: * @author Simone Giannecchini
074: * @since 2.4
075: *
076: */
077: public class Extrema extends AbstractStatisticsOperationJAI {
078:
079: /**
080: * Serial number for interoperability with different versions.
081: */
082: private static final long serialVersionUID = 7731039381590398047L;
083:
084: /** {@link Logger} for this class. */
085: public final static Logger LOGGER = org.geotools.util.logging.Logging
086: .getLogger("org.geotools.coverage.processing.operation");
087:
088: /** {@link String} key for getting the minimum vector. */
089: public final static String GT_SYNTHETIC_PROPERTY_MINIMUM = "minimum";
090:
091: /** {@link String} key for getting the maximum vector. */
092: public final static String GT_SYNTHETIC_PROPERTY_MAXIMUM = "maximum";
093:
094: /**
095: * Constructs a default {@code "Extrema"} operation.
096: */
097: public Extrema() throws OperationNotFoundException {
098: super (getOperationDescriptor("Extrema"));
099:
100: }
101:
102: /**
103: * This operation MUST be performed on the geophysics data for this
104: * {@link GridCoverage2D}.
105: *
106: * @param parameters
107: * {@link ParameterValueGroup} that describes this operation
108: * @return always true.
109: */
110: protected boolean computeOnGeophysicsValues(
111: ParameterValueGroup parameters) {
112: return true;
113: }
114:
115: /**
116: * Prepare the minimum and maximum properties for this extream operation.
117: *
118: * <p>
119: * See <a
120: * href="http://download.java.net/media/jai/javadoc/1.1.3/jai-apidocs/javax/media/jai/operator/ExtremaDescriptor.html">ExtremaDescriptor</a>
121: * for more info.
122: *
123: * @see OperationJAI#getProperties(RenderedImage, CoordinateReferenceSystem,
124: * InternationalString, MathTransform, GridCoverage2D[],
125: * org.geotools.coverage.processing.OperationJAI.Parameters),
126: */
127: protected Map getProperties(RenderedImage data,
128: CoordinateReferenceSystem crs, InternationalString name,
129: MathTransform toCRS, GridCoverage2D[] sources,
130: Parameters parameters) {
131: // /////////////////////////////////////////////////////////////////////
132: //
133: // If and only if data is a RenderedOp we prepara the properties for
134: // minimum and maximum as the output of the extrema operation.
135: //
136: // /////////////////////////////////////////////////////////////////////
137: if (data instanceof RenderedOp) {
138: // XXX remove me with 1.5
139: final RenderedOp result = (RenderedOp) data;
140:
141: // get the properties
142: final double[] maximums = (double[]) result
143: .getProperty(GT_SYNTHETIC_PROPERTY_MAXIMUM);
144: final double[] minimums = (double[]) result
145: .getProperty(GT_SYNTHETIC_PROPERTY_MINIMUM);
146:
147: // return the map
148: final Map synthProp = new HashMap(2);
149: synthProp.put(GT_SYNTHETIC_PROPERTY_MINIMUM, minimums);
150: synthProp.put(GT_SYNTHETIC_PROPERTY_MAXIMUM, maximums);
151: return Collections.unmodifiableMap(synthProp);
152:
153: }
154: return super.getProperties(data, crs, name, toCRS, sources,
155: parameters);
156: }
157: }
|