001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2006, Geotools Project Managment Committee (PMC)
005: * (C) 2006, Geomatys
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.referencing.operation.builder;
018:
019: // J2SE dependencies
020: import java.util.Arrays;
021: import java.awt.geom.Point2D;
022: import java.awt.geom.AffineTransform;
023: import java.awt.geom.NoninvertibleTransformException;
024:
025: // JUnit dependencies
026: import junit.framework.Test;
027: import junit.framework.TestCase;
028: import junit.framework.TestSuite;
029:
030: // OpenGIS dependencies
031: import org.opengis.geometry.MismatchedDimensionException;
032:
033: // Geotools dependencies
034: import org.geotools.coverage.grid.GeneralGridRange;
035: import org.geotools.referencing.cs.DefaultCoordinateSystemAxis;
036: import org.geotools.referencing.cs.DefaultEllipsoidalCS;
037: import org.geotools.referencing.crs.DefaultGeographicCRS;
038: import org.geotools.referencing.datum.DefaultGeodeticDatum;
039: import org.geotools.referencing.operation.matrix.XAffineTransform;
040: import org.geotools.geometry.GeneralEnvelope;
041:
042: /**
043: * Tests {@link GridToEnvelopeMapper}. This test appears in the coverage module instead
044: * of the referencing module because it need an implementation of {@link GridRange}.
045: *
046: * @since 2.3
047: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/coverage/src/test/java/org/geotools/referencing/operation/builder/GridToEnvelopeMapperTest.java $
048: * @version $Id: GridToEnvelopeMapperTest.java 24925 2007-03-27 20:12:08Z jgarnett $
049: * @author Martin Desruisseaux
050: */
051: public class GridToEnvelopeMapperTest extends TestCase {
052: /**
053: * Tolerance factor for the comparaison of floating point numbers.
054: */
055: private static final double EPS = 1E-10;
056:
057: /**
058: * Run the suite from the command line.
059: */
060: public static void main(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(GridToEnvelopeMapperTest.class);
069: }
070:
071: /**
072: * Constructs a test case with the given name.
073: */
074: public GridToEnvelopeMapperTest(final String name) {
075: super (name);
076: }
077:
078: /**
079: * Various tests.
080: */
081: public void testMapper() throws NoninvertibleTransformException {
082: ///////////////////////////////////////////////////////////////
083: /// Tests the initial state.
084: ///
085: final GridToEnvelopeMapper mapper = new GridToEnvelopeMapper();
086: assertTrue(mapper.isAutomatic(GridToEnvelopeMapper.SWAP_XY));
087: assertTrue(mapper
088: .isAutomatic(GridToEnvelopeMapper.REVERSE_AXIS));
089: assertFalse(mapper.getSwapXY());
090: assertNull(mapper.getReverseAxis());
091: try {
092: mapper.getGridRange();
093: fail();
094: } catch (IllegalStateException e) {
095: // This is the expected exception.
096: }
097: try {
098: mapper.getEnvelope();
099: fail();
100: } catch (IllegalStateException e) {
101: // This is the expected exception.
102: }
103: try {
104: mapper.createTransform();
105: fail();
106: } catch (IllegalStateException e) {
107: // This is the expected exception.
108: }
109:
110: ///////////////////////////////////////////////////////////////
111: /// Tests the setting of grid range and envelope.
112: ///
113: Point2D.Double point = new Point2D.Double();
114: GeneralGridRange gridRange;
115: GeneralEnvelope envelope;
116: gridRange = new GeneralGridRange(new int[] { 10, 20 },
117: new int[] { 110, 220 });
118: envelope = new GeneralEnvelope(new double[] { 1, 4, 6 },
119: new double[] { 11, 44, 66 });
120: mapper.setGridRange(gridRange);
121: assertSame(gridRange, mapper.getGridRange());
122: try {
123: mapper.getEnvelope();
124: fail();
125: } catch (IllegalStateException e) {
126: // This is the expected exception.
127: }
128: try {
129: mapper.setEnvelope(envelope);
130: fail();
131: } catch (MismatchedDimensionException e) {
132: // This is the expected exception.
133: }
134: try {
135: new GridToEnvelopeMapper(gridRange, envelope);
136: fail();
137: } catch (MismatchedDimensionException e) {
138: // This is the expected exception.
139: }
140: envelope = envelope.getSubEnvelope(0, 2);
141: mapper.setEnvelope(envelope);
142: assertSame(envelope, mapper.getEnvelope());
143:
144: ///////////////////////////////////////////////////////////////
145: /// Tests the creation when no CRS is available.
146: ///
147: assertFalse(mapper.getSwapXY());
148: assertNull(mapper.getReverseAxis());
149: final AffineTransform tr1 = mapper.createAffineTransform();
150: assertEquals(AffineTransform.TYPE_GENERAL_SCALE
151: | AffineTransform.TYPE_TRANSLATION, tr1.getType());
152: assertEquals(0.1, tr1.getScaleX(), EPS);
153: assertEquals(0.2, tr1.getScaleY(), EPS);
154: assertEquals(0.05, tr1.getTranslateX(), EPS);
155: assertEquals(0.10, tr1.getTranslateY(), EPS);
156: assertSame("Transform should be cached", tr1, mapper
157: .createAffineTransform());
158:
159: // Tests a coordinate transformation.
160: point.x = 10 - 0.5;
161: point.y = 20 - 0.5;
162: assertSame(point, tr1.transform(point, point));
163: assertEquals(1, point.x, EPS);
164: assertEquals(4, point.y, EPS);
165:
166: ///////////////////////////////////////////////////////////////
167: /// Tests the creation when a CRS is available.
168: ///
169: envelope = (GeneralEnvelope) envelope.clone();
170: envelope
171: .setCoordinateReferenceSystem(DefaultGeographicCRS.WGS84);
172: mapper.setEnvelope(envelope);
173: assertFalse(mapper.getSwapXY());
174: assertTrue(Arrays.equals(new boolean[] { false, true }, mapper
175: .getReverseAxis()));
176: final AffineTransform tr2 = mapper.createAffineTransform();
177: assertNotSame("Should be a new transform", tr1, tr2);
178: assertEquals(AffineTransform.TYPE_GENERAL_SCALE
179: | AffineTransform.TYPE_TRANSLATION
180: | AffineTransform.TYPE_FLIP, tr2.getType());
181: assertEquals(0.1, tr2.getScaleX(), EPS);
182: assertEquals(-0.2, tr2.getScaleY(), EPS);
183: assertSame("Transform should be cached", tr2, mapper
184: .createAffineTransform());
185:
186: // Tests a coordinate transformation.
187: point.x = 10 - 0.5;
188: point.y = 20 - 0.5;
189: assertSame(point, tr2.transform(point, point));
190: assertEquals(1, point.x, EPS);
191: assertEquals(44, point.y, EPS);
192:
193: ///////////////////////////////////////////////////////////////
194: /// Tests the creation with a (latitude, longitude) CRS.
195: ///
196: envelope = (GeneralEnvelope) envelope.clone();
197: envelope.setCoordinateReferenceSystem(new DefaultGeographicCRS(
198: "WGS84", DefaultGeodeticDatum.WGS84,
199: new DefaultEllipsoidalCS("WGS84",
200: DefaultCoordinateSystemAxis.LATITUDE,
201: DefaultCoordinateSystemAxis.LONGITUDE)));
202: mapper.setEnvelope(envelope);
203: assertTrue(mapper.getSwapXY());
204: assertTrue(Arrays.equals(new boolean[] { true, false }, mapper
205: .getReverseAxis()));
206: final AffineTransform tr3 = mapper.createAffineTransform();
207: assertNotSame("Should be a new transform", tr2, tr3);
208: assertEquals(AffineTransform.TYPE_QUADRANT_ROTATION
209: | AffineTransform.TYPE_GENERAL_SCALE
210: | AffineTransform.TYPE_TRANSLATION, tr3.getType());
211: assertEquals(0.0, tr3.getScaleX(), EPS);
212: assertEquals(0.0, tr3.getScaleY(), EPS);
213: assertEquals(-0.05, tr3.getShearX(), EPS);
214: assertEquals(0.4, tr3.getShearY(), EPS);
215: assertEquals(0.05, XAffineTransform.getScaleX0(tr3), EPS);
216: assertEquals(0.4, XAffineTransform.getScaleY0(tr3), EPS);
217: assertSame("Transform should be cached", tr3, mapper
218: .createAffineTransform());
219:
220: // Tests a coordinate transformation.
221: point.x = 10 - 0.5;
222: point.y = 20 - 0.5;
223: assertSame(point, tr3.transform(point, point));
224: assertEquals(4, point.y, EPS);
225: assertEquals(11, point.x, EPS);
226:
227: // Tests matrix inversion. Note that compared to the 'tr3' transform, the
228: // factors are not only inversed (1/0.05 = 20, 1/0.4 = 2.5). In addition,
229: // shearX and shearY are interchanged.
230: final AffineTransform tr3i = tr3.createInverse();
231: assertEquals(0.0, tr3i.getScaleX(), EPS);
232: assertEquals(0.0, tr3i.getScaleY(), EPS);
233: assertEquals(2.5, tr3i.getShearX(), EPS);
234: assertEquals(-20, tr3i.getShearY(), EPS);
235: assertEquals(2.5, XAffineTransform.getScaleX0(tr3i), EPS);
236: assertEquals(20, XAffineTransform.getScaleY0(tr3i), EPS);
237:
238: ///////////////////////////////////////////////////////////////
239: /// Tests explicit axis reversal and swapping
240: ///
241: assertTrue(mapper.isAutomatic(GridToEnvelopeMapper.SWAP_XY));
242: assertTrue(mapper
243: .isAutomatic(GridToEnvelopeMapper.REVERSE_AXIS));
244: assertTrue(mapper.getSwapXY());
245: mapper.setSwapXY(false);
246: assertFalse(mapper.isAutomatic(GridToEnvelopeMapper.SWAP_XY));
247: assertTrue(mapper
248: .isAutomatic(GridToEnvelopeMapper.REVERSE_AXIS));
249: assertFalse(mapper.getSwapXY());
250: assertNotSame(tr3, mapper.createAffineTransform());
251: mapper.setReverseAxis(null);
252: assertFalse(mapper.isAutomatic(GridToEnvelopeMapper.SWAP_XY));
253: assertFalse(mapper
254: .isAutomatic(GridToEnvelopeMapper.REVERSE_AXIS));
255: assertEquals(tr1, mapper.createAffineTransform());
256: }
257: }
|