001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2003-2006, Geotools Project Managment Committee (PMC)
005: * (C) 2002, Institut de Recherche pour le Développement
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.grid;
018:
019: // J2SE dependencies and extensions
020: import java.awt.geom.Point2D;
021: import java.awt.image.Raster;
022: import javax.media.jai.Interpolation;
023:
024: // JUnit dependencies
025: import junit.framework.Test;
026: import junit.framework.TestSuite;
027:
028: // OpenGIS dependencies
029: import org.opengis.coverage.grid.GridRange;
030: import org.opengis.geometry.Envelope;
031:
032: // Geotools dependencies
033: import org.geotools.coverage.grid.Interpolator2D;
034:
035: /**
036: * Tests the {@link Interpolator2D} implementation. This method inherit all tests from
037: * {@link GridCoverageTest}. Because we override {@link #transform}, tests will be performed
038: * on {@link Interpolator2D} objects instead of default {@link GridCoverage2D}.
039: *
040: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/coverage/src/test/java/org/geotools/coverage/grid/InterpolatorTest.java $
041: * @version $Id: InterpolatorTest.java 25591 2007-05-20 10:43:59Z desruisseaux $
042: * @author Martin Desruisseaux
043: */
044: public class InterpolatorTest extends GridCoverageTest {
045: /**
046: * Small value for comparaison of sample values. Since most grid coverage implementation in
047: * Geotools 2 store geophysics values as {@code float} numbers, this {@code EPS} value must
048: * be of the order of {@code float} relative precision, not {@code double}.
049: */
050: private static final double EPS = 1E-5;
051:
052: /**
053: * The interpolators to use.
054: */
055: private Interpolation[] interpolations;
056:
057: /**
058: * Run the suite from the command line.
059: */
060: public static void main(final String[] args) {
061: junit.textui.TestRunner.run(suite());
062: }
063:
064: /**
065: * Returns the test suite.
066: */
067: public static Test suite() {
068: return new TestSuite(InterpolatorTest.class);
069: }
070:
071: /**
072: * Constructs a test case with the given name.
073: */
074: public InterpolatorTest(final String name) {
075: super (name);
076: final int[] types = { Interpolation.INTERP_BICUBIC,
077: Interpolation.INTERP_BILINEAR,
078: Interpolation.INTERP_NEAREST };
079: interpolations = new Interpolation[types.length];
080: for (int i = 0; i < interpolations.length; i++) {
081: interpolations[i] = Interpolation.getInstance(types[i]);
082: }
083: }
084:
085: /**
086: * Applies an operation on the specified coverage, if wanted.
087: * The default implementation applies a set of interpolations
088: * on <code>coverage</code>.
089: */
090: //@Override
091: protected GridCoverage2D transform(final GridCoverage2D coverage) {
092: return Interpolator2D.create(coverage, interpolations);
093: }
094:
095: /**
096: * Tests the interpolations. Since <code>testGridCoverage()</code> tests value
097: * at the center of pixels, all interpolations results should be identical to
098: * a result without interpolation.
099: */
100: //@Override
101: public void testGridCoverage() {
102: final GridCoverage2D coverage = getRandomCoverage();
103: assertTrue(coverage instanceof Interpolator2D);
104: assertTrue(coverage.geophysics(true) instanceof Interpolator2D);
105: assertTrue(coverage.geophysics(false) instanceof Interpolator2D);
106: }
107:
108: /**
109: * Tests bilinear intersection at pixel edges. It should be equals
110: * to the average of the four pixels around.
111: */
112: public void testInterpolationAtEdges() {
113: // Following constant is pixel size (in degrees).
114: // This constant must be identical to the one defined in 'getRandomCoverage()'
115: final double PIXEL_SIZE = 0.25;
116:
117: final GridCoverage2D coverage = Interpolator2D.create(
118: getRandomCoverage().geophysics(true),
119: new Interpolation[] { Interpolation
120: .getInstance(Interpolation.INTERP_BILINEAR) });
121:
122: final int band = 0; // Band to test.
123: double[] buffer = null;
124: final Raster data = coverage.getRenderedImage().getData();
125: final Envelope envelope = coverage.getEnvelope();
126: final GridRange range = coverage.getGridGeometry()
127: .getGridRange();
128: final double left = envelope.getMinimum(0);
129: final double upper = envelope.getMaximum(1);
130: final Point2D.Double point = new Point2D.Double(); // Will maps to pixel upper-left corner
131: for (int j = range.getLength(1); --j >= 1;) {
132: for (int i = range.getLength(0); --i >= 1;) {
133: point.x = left + PIXEL_SIZE * i;
134: point.y = upper - PIXEL_SIZE * j;
135: buffer = coverage.evaluate(point, buffer);
136: double t = buffer[band];
137:
138: // Computes the expected value:
139: double r00 = data.getSampleDouble(i - 0, j - 0, band);
140: double r01 = data.getSampleDouble(i - 0, j - 1, band);
141: double r10 = data.getSampleDouble(i - 1, j - 0, band);
142: double r11 = data.getSampleDouble(i - 1, j - 1, band);
143: double r = (r00 + r01 + r10 + r11) / 4;
144: if (Double.isNaN(r)) {
145: assertTrue("NaN", Double.isNaN(t));
146: } else {
147: assertEquals(r, t, EPS);
148: }
149: }
150: }
151: }
152: }
|