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:
022: // JUnit dependencies
023: import junit.framework.Test;
024: import junit.framework.TestCase;
025: import junit.framework.TestSuite;
026:
027: // OpenGIS dependencies
028: import org.opengis.referencing.cs.AxisDirection;
029: import org.opengis.referencing.cs.CoordinateSystem;
030:
031: // Geotools dependencies
032: import org.geotools.referencing.CRS;
033:
034: /**
035: * Tests the {@link CartesianCS} class.
036: *
037: * @version $Id: DefaultCartesianCSTest.java 24683 2007-03-06 06:02:15Z desruisseaux $
038: * @author Martin Desruisseaux
039: */
040: public class DefaultCartesianCSTest extends TestCase {
041: /**
042: * Run the suite from the command line.
043: */
044: public static void main(final String[] args) {
045: junit.textui.TestRunner.run(suite());
046: }
047:
048: /**
049: * Returns the test suite.
050: */
051: public static Test suite() {
052: return new TestSuite(DefaultCartesianCSTest.class);
053: }
054:
055: /**
056: * Constructs a test case with the given name.
057: */
058: public DefaultCartesianCSTest(final String name) {
059: super (name);
060: }
061:
062: /**
063: * Tests the creation of a cartesian CS with legal and illegal axis.
064: */
065: public void testAxis() {
066: DefaultCartesianCS cs;
067: try {
068: cs = new DefaultCartesianCS("Test",
069: DefaultCoordinateSystemAxis.LONGITUDE,
070: DefaultCoordinateSystemAxis.LATITUDE);
071: fail("Angular units should not be accepted.");
072: } catch (IllegalArgumentException e) {
073: // Expected exception: illegal angular units.
074: }
075:
076: // Legal CS (the most usual one).
077: cs = new DefaultCartesianCS("Test",
078: DefaultCoordinateSystemAxis.EASTING,
079: DefaultCoordinateSystemAxis.NORTHING);
080:
081: try {
082: cs = new DefaultCartesianCS("Test",
083: DefaultCoordinateSystemAxis.SOUTHING,
084: DefaultCoordinateSystemAxis.NORTHING);
085: fail("Colinear units should not be accepted.");
086: } catch (IllegalArgumentException e) {
087: // Expected exception: colinear axis.
088: }
089:
090: // Legal CS rotated 45°
091: cs = create(AxisDirection.NORTH_EAST, AxisDirection.SOUTH_EAST);
092:
093: try {
094: cs = create(AxisDirection.NORTH_EAST, AxisDirection.EAST);
095: fail("Non-perpendicular axis should not be accepted.");
096: } catch (IllegalArgumentException e) {
097: // Expected exception: non-perpendicular axis.
098: }
099:
100: // Legal CS, but no perpendicularity check.
101: cs = create(AxisDirection.NORTH_EAST, AxisDirection.UP);
102:
103: // Inconsistent axis direction.
104: try {
105: cs = new DefaultCartesianCS("Test",
106: DefaultCoordinateSystemAxis.EASTING,
107: new DefaultCoordinateSystemAxis("Northing",
108: AxisDirection.SOUTH, SI.METER));
109: } catch (IllegalArgumentException e) {
110: // Expected exception: inconsistent direction.
111: }
112: }
113:
114: /**
115: * Tests {@link AbstractCS#standard} with cartesian CS, especially
116: * the ones that leads to the creation of right-handed CS.
117: */
118: public void testStandard() {
119: // ----------- Axis to test ------ Expected axis --
120: assertOrdered("East", "North", "East", "North");
121: assertOrdered("North", "East", "East", "North");
122: assertOrdered("South", "East", "East", "North");
123: assertOrdered("South", "West", "East", "North");
124:
125: assertOrdered("East", "North");
126: assertOrdered("South-East", "North-East");
127: assertOrdered("North along 90 deg East", "North along 0 deg");
128: assertOrdered("North along 90 deg East", "North along 0 deg");
129: assertOrdered("North along 75 deg West",
130: "North along 165 deg West");
131: assertOrdered("South along 90 deg West", "South along 0 deg");
132: assertOrdered("South along 180 deg", "South along 90 deg West");
133: assertOrdered("North along 130 deg West",
134: "North along 140 deg East");
135: }
136:
137: /**
138: * Creates an axis for testing purpose for the specified direction.
139: */
140: private static DefaultCoordinateSystemAxis create(
141: final AxisDirection direction) {
142: if (direction.equals(AxisDirection.NORTH)) {
143: return DefaultCoordinateSystemAxis.NORTHING;
144: }
145: if (direction.equals(AxisDirection.EAST)) {
146: return DefaultCoordinateSystemAxis.EASTING;
147: }
148: if (direction.equals(AxisDirection.SOUTH)) {
149: return DefaultCoordinateSystemAxis.SOUTHING;
150: }
151: if (direction.equals(AxisDirection.WEST)) {
152: return DefaultCoordinateSystemAxis.WESTING;
153: }
154: return new DefaultCoordinateSystemAxis("Test", direction,
155: SI.METER);
156: }
157:
158: /**
159: * Creates a coordinate system with the specified axis directions.
160: */
161: private static DefaultCartesianCS create(final AxisDirection x,
162: final AxisDirection y) {
163: return new DefaultCartesianCS("Test", create(x), create(y));
164: }
165:
166: /**
167: * Creates a coordinate system with the specified axis directions.
168: */
169: private static DefaultCartesianCS create(final String x,
170: final String y) {
171: return create(DefaultCoordinateSystemAxis.getDirection(x),
172: DefaultCoordinateSystemAxis.getDirection(y));
173: }
174:
175: /**
176: * Tests ordering with a CS created from the specified axis.
177: */
178: private static void assertOrdered(final String expectedX,
179: final String expectedY) {
180: assertOrdered(expectedY, expectedX, expectedX, expectedY);
181: assertOrdered(expectedX, expectedY, expectedX, expectedY);
182: }
183:
184: /**
185: * Creates a cartesian CS using the provided test axis, invoke {@link AbstractCS#standard}
186: * with it and compare with the expected axis.
187: */
188: private static void assertOrdered(final String testX,
189: final String testY, final String expectedX,
190: final String expectedY) {
191: final CoordinateSystem cs = AbstractCS.standard(create(testX,
192: testY));
193: assertTrue(CRS.equalsIgnoreMetadata(
194: create(expectedX, expectedY), cs));
195: }
196: }
|