001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2003-2006, Geotools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.referencing.operation.projection;
017:
018: // JUnit dependencies
019: import junit.framework.Test;
020: import junit.framework.TestCase;
021: import junit.framework.TestSuite;
022:
023: // OpenGIS dependencies
024: import org.opengis.referencing.FactoryException;
025: import org.opengis.referencing.cs.AxisDirection;
026: import org.opengis.referencing.crs.CoordinateReferenceSystem;
027: import org.opengis.referencing.crs.ProjectedCRS;
028: import org.opengis.referencing.operation.Matrix;
029: import org.opengis.referencing.operation.MathTransform;
030:
031: // Geotools dependencies
032: import org.geotools.referencing.CRS;
033: import org.geotools.referencing.operation.LinearTransform;
034: import org.geotools.referencing.operation.transform.ConcatenatedTransform;
035:
036: /**
037: * Tests some south-oriented map projections.
038: *
039: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/referencing/src/test/java/org/geotools/referencing/operation/projection/SouthOrientedTest.java $
040: * @version $Id: SouthOrientedTest.java 24683 2007-03-06 06:02:15Z desruisseaux $
041: * @author Martin Desruisseaux
042: */
043: public final class SouthOrientedTest extends TestCase {
044: /**
045: * Small number for matrix element comparaisons.
046: */
047: private static final double EPS = 1E-10;
048:
049: /**
050: * Constructs a test with the given name.
051: */
052: public SouthOrientedTest(final String name) {
053: super (name);
054: }
055:
056: /**
057: * Uses reflection to dynamically create a test suite containing all
058: * the {@code testXXX()} methods - from the JUnit FAQ.
059: */
060: public static Test suite() {
061: return new TestSuite(SouthOrientedTest.class);
062: }
063:
064: /**
065: * Runs the tests with the textual test runner.
066: */
067: public static void main(final String args[]) {
068: junit.textui.TestRunner.run(suite());
069: }
070:
071: /**
072: * Parse a test CRS north or south oriented. If the CRS is fully south-oriented
073: * with 0.0 northing, then it should be the EPSG:22285 one.
074: */
075: private static ProjectedCRS parseTransverseMercator(
076: final boolean methodSouth, final boolean axisSouth,
077: final double northing) throws FactoryException {
078: final String method = methodSouth ? "Transverse Mercator (South Orientated)"
079: : "Transverse Mercator";
080: final String axis = axisSouth ? "\"Southing\", SOUTH"
081: : "\"Northing\", NORTH";
082: return (ProjectedCRS) CRS
083: .parseWKT("PROJCS[\"South African Coordinate System zone 25\", "
084: + "GEOGCS[\"Cape\", "
085: + "DATUM[\"Cape\", "
086: + "SPHEROID[\"Clarke 1880 (Arc)\", 6378249.145, 293.4663077, AUTHORITY[\"EPSG\",\"7013\"]], "
087: + "TOWGS84[-136.0, -108.0, -292.0, 0.0, 0.0, 0.0, 0.0], "
088: + "AUTHORITY[\"EPSG\",\"6222\"]], "
089: + "PRIMEM[\"Greenwich\", 0.0, AUTHORITY[\"EPSG\",\"8901\"]], "
090: + "UNIT[\"degree\", 0.017453292519943295], "
091: + "AXIS[\"Geodetic latitude\", NORTH], "
092: + "AXIS[\"Geodetic longitude\", EAST], "
093: + "AUTHORITY[\"EPSG\",\"4222\"]], "
094: + "PROJECTION[\""
095: + method
096: + "\"], "
097: + "PARAMETER[\"central_meridian\", 25.0], "
098: + "PARAMETER[\"latitude_of_origin\", 0.0], "
099: + "PARAMETER[\"scale_factor\", 1.0], "
100: + "PARAMETER[\"false_easting\", 0.0], "
101: + "PARAMETER[\"false_northing\", "
102: + northing
103: + "], "
104: + "UNIT[\"m\", 1.0], "
105: + "AXIS[\"Westing\", WEST], "
106: + "AXIS["
107: + axis
108: + "]]");
109: }
110:
111: /**
112: * Tests the Transverse Mercator South-Oriented case.
113: */
114: public void testTransverseMercator() throws FactoryException {
115: /*
116: * Tests "Transverse Mercator" (not south-oriented) with an axis oriented toward south.
117: */
118: ProjectedCRS north = parseTransverseMercator(false, false, 1000);
119: assertEquals(AxisDirection.WEST, north.getCoordinateSystem()
120: .getAxis(0).getDirection());
121: assertEquals(AxisDirection.NORTH, north.getCoordinateSystem()
122: .getAxis(1).getDirection());
123:
124: ProjectedCRS south = parseTransverseMercator(false, true, 1000);
125: assertEquals(AxisDirection.WEST, south.getCoordinateSystem()
126: .getAxis(0).getDirection());
127: assertEquals(AxisDirection.SOUTH, south.getCoordinateSystem()
128: .getAxis(1).getDirection());
129:
130: MathTransform transform = CRS.findMathTransform(north, south);
131: assertTrue(transform instanceof LinearTransform);
132: Matrix matrix = ((LinearTransform) transform).getMatrix();
133: assertDiagonal(matrix);
134: assertFalse(matrix.isIdentity());
135: assertEquals("West direction should be unchanged. ", +1, matrix
136: .getElement(0, 0), EPS);
137: assertEquals("North-South direction should be reverted.", -1,
138: matrix.getElement(1, 1), EPS);
139: assertEquals("No easting expected.", 0,
140: matrix.getElement(0, 2), EPS);
141: assertEquals("No northing expected.", 0, matrix
142: .getElement(1, 2), EPS);
143:
144: /*
145: * Tests "Transverse Mercator South Oriented"
146: */
147: south = parseTransverseMercator(true, true, 1000);
148: assertEquals(AxisDirection.WEST, south.getCoordinateSystem()
149: .getAxis(0).getDirection());
150: assertEquals(AxisDirection.SOUTH, south.getCoordinateSystem()
151: .getAxis(1).getDirection());
152: transform = CRS.findMathTransform(north, south);
153: assertTrue(transform instanceof LinearTransform);
154: matrix = ((LinearTransform) transform).getMatrix();
155: assertDiagonal(matrix);
156: assertFalse(matrix.isIdentity());
157: assertEquals("West direction should be unchanged. ", +1, matrix
158: .getElement(0, 0), EPS);
159: assertEquals("North-South direction should be reverted.", -1,
160: matrix.getElement(1, 1), EPS);
161: assertEquals("No easting expected.", 0,
162: matrix.getElement(0, 2), EPS);
163: assertEquals("No northing expected.", 0, matrix
164: .getElement(1, 2), EPS);
165:
166: /*
167: * Tries with a different northing.
168: */
169: north = parseTransverseMercator(false, false, 3000);
170: transform = CRS.findMathTransform(north, south);
171: assertTrue(transform instanceof LinearTransform);
172: matrix = ((LinearTransform) transform).getMatrix();
173: assertFalse(matrix.isIdentity());
174: assertEquals("West direction should be unchanged. ", +1, matrix
175: .getElement(0, 0), EPS);
176: assertEquals("North-South direction should be reverted.", -1,
177: matrix.getElement(1, 1), EPS);
178: assertEquals("No easting expected.", 0,
179: matrix.getElement(0, 2), EPS);
180: assertEquals("Northing expected.", 2000, matrix
181: .getElement(1, 2), EPS);
182: }
183:
184: /**
185: * Tests a Krovak projection with (SOUTH,WEST) axis.
186: */
187: public void testKrovak() throws FactoryException {
188: final String geoWKT = "GEOGCS[\"S-JTSK (Ferro)\", "
189: + "DATUM[\"S_JTSK_Ferro\", "
190: + "SPHEROID[\"Bessel 1841\", 6377397.155, 299.1528128], "
191: + "TOWGS84[570.8,85.7,462.8,4.998,1.587,5.261,3.56]], "
192: + "PRIMEM[\"Greenwich\",0], "
193: + "UNIT[\"degree\",0.0174532925199433]]";
194: final String prjWKT = "PROJCS[\"S-JTSK(Ferro) / krovak\", "
195: + geoWKT + ", " + "PROJECTION[\"Krovak\"], "
196: + "UNIT[\"metre\",1.0], " + "AXIS[\"y\",WEST], "
197: + "AXIS[\"x\",SOUTH]]";
198:
199: final CoordinateReferenceSystem sourceCRS = CRS
200: .parseWKT(geoWKT);
201: final CoordinateReferenceSystem targetCRS = CRS
202: .parseWKT(prjWKT);
203: final MathTransform transform = CRS.findMathTransform(
204: sourceCRS, targetCRS);
205: assertTrue(transform instanceof ConcatenatedTransform);
206: final ConcatenatedTransform ct = (ConcatenatedTransform) transform;
207: assertTrue(ct.transform2 instanceof LinearTransform);
208: final Matrix matrix = ((LinearTransform) ct.transform2)
209: .getMatrix();
210: assertDiagonal(matrix);
211: assertEquals("East-West direction should be reverted. ", -1,
212: matrix.getElement(0, 0), EPS);
213: assertEquals("North-South direction should be reverted.", -1,
214: matrix.getElement(1, 1), EPS);
215: assertEquals("No easting expected.", 0,
216: matrix.getElement(0, 2), EPS);
217: assertEquals("No northing expected.", 0, matrix
218: .getElement(1, 2), EPS);
219: }
220:
221: /**
222: * Asserts that the specified matrix is diagonal.
223: */
224: private static void assertDiagonal(final Matrix matrix) {
225: final int nrow = matrix.getNumRow();
226: final int ncol = matrix.getNumCol();
227: for (int j = 0; j < nrow; j++) {
228: for (int i = 0; i < ncol; i++) {
229: if (i != j) {
230: assertEquals("row " + j + ", col " + i, 0.0, matrix
231: .getElement(j, i), EPS);
232: }
233: }
234: }
235: }
236: }
|