001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2007, GeoTools Project Managment Committee (PMC)
005: * (C) 2007, 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;
010: * version 2.1 of the License.
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.cs;
018:
019: // J2SE dependencies and extensions
020: import javax.units.SI;
021: import javax.units.Unit;
022:
023: // JUnit dependencies
024: import junit.framework.Test;
025: import junit.framework.TestCase;
026: import junit.framework.TestSuite;
027:
028: // OpenGIS dependencies
029: import org.opengis.referencing.cs.AxisDirection;
030: import org.opengis.referencing.cs.CoordinateSystem;
031: import org.opengis.referencing.cs.CoordinateSystemAxis;
032: import org.opengis.referencing.operation.Matrix;
033:
034: // Geotools dependencies
035: import org.geotools.referencing.operation.matrix.GeneralMatrix;
036:
037: /**
038: * Tests the {@link AbstractCS} class.
039: *
040: * @version $Id: AbstractCSTest.java 24683 2007-03-06 06:02:15Z desruisseaux $
041: * @author Martin Desruisseaux
042: */
043: public class AbstractCSTest extends TestCase {
044: /**
045: * Run the suite from the command line.
046: */
047: public static void main(final String[] args) {
048: junit.textui.TestRunner.run(suite());
049: }
050:
051: /**
052: * Returns the test suite.
053: */
054: public static Test suite() {
055: return new TestSuite(AbstractCSTest.class);
056: }
057:
058: /**
059: * Constructs a test case with the given name.
060: */
061: public AbstractCSTest(final String name) {
062: super (name);
063: }
064:
065: /**
066: * Tests the swapping of axis.
067: */
068: public void testAxisSwapping() {
069: CoordinateSystem cs1, cs2;
070: cs1 = new DefaultEllipsoidalCS("cs1",
071: DefaultCoordinateSystemAxis.GEODETIC_LONGITUDE,
072: DefaultCoordinateSystemAxis.GEODETIC_LATITUDE);
073: cs2 = new DefaultEllipsoidalCS("cs2",
074: DefaultCoordinateSystemAxis.GEODETIC_LATITUDE,
075: DefaultCoordinateSystemAxis.GEODETIC_LONGITUDE);
076: assertTrue(AbstractCS.swapAndScaleAxis(cs1, cs1).isIdentity());
077: assertTrue(AbstractCS.swapAndScaleAxis(cs2, cs2).isIdentity());
078: compareMatrix(cs1, cs2, new double[] { 0, 1, 0, 1, 0, 0, 0, 0,
079: 1 });
080:
081: cs1 = new DefaultEllipsoidalCS("cs1",
082: DefaultCoordinateSystemAxis.GEODETIC_LONGITUDE,
083: DefaultCoordinateSystemAxis.GEODETIC_LATITUDE,
084: DefaultCoordinateSystemAxis.ELLIPSOIDAL_HEIGHT);
085: cs2 = new DefaultEllipsoidalCS("cs2",
086: DefaultCoordinateSystemAxis.GEODETIC_LATITUDE,
087: DefaultCoordinateSystemAxis.GEODETIC_LONGITUDE,
088: DefaultCoordinateSystemAxis.ELLIPSOIDAL_HEIGHT);
089: compareMatrix(cs1, cs2, new double[] { 0, 1, 0, 0, 1, 0, 0, 0,
090: 0, 0, 1, 0, 0, 0, 0, 1 });
091:
092: cs1 = new DefaultCartesianCS("cs1",
093: DefaultCoordinateSystemAxis.ELLIPSOIDAL_HEIGHT,
094: DefaultCoordinateSystemAxis.EASTING,
095: DefaultCoordinateSystemAxis.NORTHING);
096: cs2 = new DefaultCartesianCS("cs2",
097: DefaultCoordinateSystemAxis.SOUTHING,
098: DefaultCoordinateSystemAxis.EASTING,
099: DefaultCoordinateSystemAxis.ELLIPSOIDAL_HEIGHT);
100: compareMatrix(cs1, cs2, new double[] { 0, 0, -1, 0, 0, 1, 0, 0,
101: 1, 0, 0, 0, 0, 0, 0, 1 });
102: }
103:
104: /**
105: * Compares the matrix computes by {@link AbstractCS#swapAndScaleAxis} with the specified one.
106: */
107: private static void compareMatrix(final CoordinateSystem cs1,
108: final CoordinateSystem cs2, final double[] expected) {
109: final Matrix matrix = AbstractCS.swapAndScaleAxis(cs1, cs2);
110: final int numRow = matrix.getNumRow();
111: final int numCol = matrix.getNumCol();
112: assertEquals(expected.length, numRow * numCol);
113: final Matrix em = new GeneralMatrix(numRow, numCol, expected);
114: assertEquals(em, matrix);
115: }
116:
117: /**
118: * Tests {@link AbstractCS#axisUsingUnit}.
119: */
120: public void testAxisUsingUnit() {
121: assertNull("Should detect that no axis change is needed",
122: DefaultCartesianCS.PROJECTED.axisUsingUnit(SI.METER));
123:
124: final Unit KILOMETER = SI.KILO(SI.METER);
125: final CoordinateSystemAxis[] axis = DefaultCartesianCS.PROJECTED
126: .axisUsingUnit(KILOMETER);
127: assertNotNull(axis);
128: assertEquals("Expected two-dimensional", 2, axis.length);
129: assertEquals(KILOMETER, axis[0].getUnit());
130: assertEquals(KILOMETER, axis[1].getUnit());
131: assertEquals(AxisDirection.EAST, axis[0].getDirection());
132: assertEquals(AxisDirection.NORTH, axis[1].getDirection());
133: assertEquals("Easting", axis[0].getName().getCode());
134: assertEquals("Northing", axis[1].getName().getCode());
135: }
136:
137: /**
138: * Tests {@link AbstractCS#standard}.
139: */
140: public void testStandards() {
141: CoordinateSystem cs;
142: cs = DefaultCartesianCS.GRID;
143: assertSame(cs, AbstractCS.standard(cs));
144: cs = DefaultCartesianCS.GEOCENTRIC;
145: assertSame(cs, AbstractCS.standard(cs));
146: cs = DefaultCartesianCS.GENERIC_2D;
147: assertSame(cs, AbstractCS.standard(cs));
148: cs = DefaultCartesianCS.GENERIC_3D;
149: assertSame(cs, AbstractCS.standard(cs));
150: cs = DefaultCartesianCS.PROJECTED;
151: assertSame(cs, AbstractCS.standard(cs));
152: cs = DefaultEllipsoidalCS.GEODETIC_2D;
153: assertSame(cs, AbstractCS.standard(cs));
154: cs = DefaultEllipsoidalCS.GEODETIC_3D;
155: assertSame(cs, AbstractCS.standard(cs));
156: cs = DefaultSphericalCS.GEOCENTRIC;
157: assertSame(cs, AbstractCS.standard(cs));
158: cs = DefaultTimeCS.DAYS;
159: assertSame(cs, AbstractCS.standard(cs));
160: cs = DefaultVerticalCS.ELLIPSOIDAL_HEIGHT;
161: assertSame(cs, AbstractCS.standard(cs));
162: cs = DefaultVerticalCS.GRAVITY_RELATED;
163: assertSame(
164: "\"Standard\" vertical axis should be forced to ellipsoidal height.",
165: DefaultVerticalCS.ELLIPSOIDAL_HEIGHT, AbstractCS
166: .standard(cs));
167: }
168: }
|