001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2007, 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;
009: * version 2.1 of the License.
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;
017:
018: // J2SE dependencies
019: import java.util.Iterator;
020: import java.util.Locale;
021: import java.util.Set;
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.metadata.citation.Citation;
030: import org.opengis.referencing.FactoryException;
031: import org.opengis.referencing.NoSuchAuthorityCodeException;
032: import org.opengis.referencing.cs.CoordinateSystem;
033: import org.opengis.referencing.cs.CoordinateSystemAxis;
034: import org.opengis.referencing.crs.CRSAuthorityFactory;
035: import org.opengis.referencing.crs.CoordinateReferenceSystem;
036:
037: // Geotools dependencies
038: import org.geotools.factory.Hints;
039: import org.geotools.factory.GeoTools;
040: import org.geotools.resources.Arguments;
041: import org.geotools.referencing.crs.DefaultGeographicCRS;
042: import org.geotools.referencing.factory.OrderedAxisAuthorityFactory;
043:
044: /**
045: * Tests if the CRS utility class is functioning correctly when using HSQL datastore.
046: *
047: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/epsg-hsql/src/test/java/org/geotools/referencing/CRSTest.java $
048: * @version $Id: CRSTest.java 25406 2007-05-03 18:08:10Z desruisseaux $
049: * @author Jody Garnett
050: * @author Martin Desruisseaux
051: */
052: public class CRSTest extends TestCase {
053: /**
054: * {@code true} for tracing operations on the standard output.
055: */
056: private static boolean verbose;
057:
058: /**
059: * Run the suite from the command line.
060: */
061: public static void main(final String[] args) {
062: final Arguments arguments = new Arguments(args);
063: verbose = arguments.getFlag("-verbose");
064: arguments.getRemainingArguments(0);
065: junit.textui.TestRunner.run(suite());
066: }
067:
068: /**
069: * Returns the test suite.
070: */
071: public static Test suite() {
072: return new TestSuite(CRSTest.class);
073: }
074:
075: /**
076: * Constructs a test case with the given name.
077: */
078: public CRSTest(final String name) {
079: super (name);
080: }
081:
082: /**
083: * Tests the (latitude, longitude) axis order for EPSG:4326.
084: */
085: public void testCorrectAxisOrder()
086: throws NoSuchAuthorityCodeException, FactoryException {
087: final CoordinateReferenceSystem crs = CRS.decode("EPSG:4326");
088: final CoordinateSystem cs = crs.getCoordinateSystem();
089: assertEquals(2, cs.getDimension());
090:
091: CoordinateSystemAxis axis0 = cs.getAxis(0);
092: assertEquals("Lat", axis0.getAbbreviation());
093:
094: CoordinateSystemAxis axis1 = cs.getAxis(1);
095: assertEquals("Long", axis1.getAbbreviation());
096: }
097:
098: /**
099: * Tests the (longitude, latitude) axis order for EPSG:4326.
100: */
101: public void testForcedAxisOrder()
102: throws NoSuchAuthorityCodeException, FactoryException {
103: final CoordinateReferenceSystem crs = CRS.decode("EPSG:4326",
104: true);
105: final CoordinateSystem cs = crs.getCoordinateSystem();
106: assertEquals(2, cs.getDimension());
107:
108: CoordinateSystemAxis axis0 = cs.getAxis(0);
109: assertEquals("Long", axis0.getAbbreviation());
110:
111: CoordinateSystemAxis axis1 = cs.getAxis(1);
112: assertEquals("Lat", axis1.getAbbreviation());
113:
114: final CoordinateReferenceSystem standard = CRS
115: .decode("EPSG:4326");
116: assertFalse("Should not be (long,lat) axis order.", CRS
117: .equalsIgnoreMetadata(crs, standard));
118:
119: final CoordinateReferenceSystem def;
120: try {
121: assertNull(Hints.putSystemDefault(
122: Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER,
123: Boolean.TRUE));
124: def = CRS.decode("EPSG:4326");
125: } finally {
126: assertEquals(
127: Boolean.TRUE,
128: Hints
129: .removeSystemDefault(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER));
130: }
131: assertEquals("Expected (long,lat) axis order.", crs, def);
132: assertSame("Should be back to (lat,long) axis order.",
133: standard, CRS.decode("EPSG:4326"));
134: }
135:
136: /**
137: * Tests again EPSG:4326, but forced to (longitude, latitude) axis order.
138: *
139: * @todo Uncomment when we will be allowed to compile for J2SE 1.5.
140: * Call to {@link System#clearProperty} is mandatory for this test.
141: */
142: public void testSystemPropertyToForceXY()
143: throws NoSuchAuthorityCodeException, FactoryException {
144: assertNull(System
145: .getProperty(GeoTools.FORCE_LONGITUDE_FIRST_AXIS_ORDER));
146: // System.setProperty(GeoTools.FORCE_LONGITUDE_FIRST_AXIS_ORDER, "true");
147: // try {
148: // CoordinateReferenceSystem crs = CRS.decode("EPSG:4326");
149: // final CoordinateSystem cs = crs.getCoordinateSystem();
150: // assertEquals(2, cs.getDimension());
151: //
152: // CoordinateSystemAxis axis0 = cs.getAxis(0);
153: // assertEquals("forceXY did not work", "Long", axis0.getAbbreviation());
154: //
155: // CoordinateSystemAxis axis1 = cs.getAxis(1);
156: // assertEquals("forceXY did not work", "Lat", axis1.getAbbreviation());
157: // } finally {
158: // System.clearProperty(GeoTools.FORCE_LONGITUDE_FIRST_AXIS_ORDER);
159: // }
160: }
161:
162: /**
163: * Tests {@link CRS#lookupIdentifier}.
164: */
165: public void testFind() throws FactoryException {
166: CoordinateReferenceSystem crs = getED50("ED50");
167: assertEquals("Should find without scan thanks to the name.",
168: "EPSG:4230", CRS.lookupIdentifier(crs, false));
169:
170: crs = getED50("ED50 with unknown name");
171: assertNull("Should not find the CRS without a scan.", CRS
172: .lookupIdentifier(crs, false));
173:
174: assertEquals("With scan allowed, should find the CRS.",
175: "EPSG:4230", CRS.lookupIdentifier(crs, true));
176: }
177:
178: /**
179: * Returns a ED50 CRS with the specified name.
180: */
181: private static CoordinateReferenceSystem getED50(final String name)
182: throws FactoryException {
183: final String wkt = "GEOGCS[\""
184: + name
185: + "\",\n"
186: + " DATUM[\"European Datum 1950\",\n"
187: + " SPHEROID[\"International 1924\", 6378388.0, 297.0]],\n"
188: + "PRIMEM[\"Greenwich\", 0.0],\n"
189: + "UNIT[\"degree\", 0.017453292519943295]]";
190: return CRS.parseWKT(wkt);
191: }
192:
193: // -------------------------------------------------------------------------
194: // The following tests are copied from the legacy plugin/epsg-wkt test suite
195: // -------------------------------------------------------------------------
196:
197: /**
198: * Makes sure that the authority factory has the proper name.
199: */
200: public void testAuthority() {
201: CRSAuthorityFactory factory;
202: Citation authority;
203:
204: // Tests the official factory.
205: factory = ReferencingFactoryFinder.getCRSAuthorityFactory(
206: "EPSG", null);
207: authority = factory.getAuthority();
208: assertNotNull(authority);
209: assertEquals("European Petroleum Survey Group", authority
210: .getTitle().toString(Locale.US));
211: assertTrue(authority.getIdentifiers().contains("EPSG"));
212:
213: // Tests the modified factory.
214: factory = new OrderedAxisAuthorityFactory("EPSG", null, null);
215: authority = factory.getAuthority();
216: assertNotNull(authority);
217: assertTrue(authority.getIdentifiers().contains("EPSG"));
218: }
219:
220: /**
221: * Tests the vendor name.
222: */
223: public void testVendor() {
224: CRSAuthorityFactory factory;
225: Citation vendor;
226:
227: factory = new OrderedAxisAuthorityFactory("EPSG", null, null);
228: vendor = factory.getVendor();
229: assertNotNull(vendor);
230: assertEquals("Geotools", vendor.getTitle().toString(Locale.US));
231: assertFalse(vendor.getIdentifiers().contains("EPSG"));
232: }
233:
234: /**
235: * Tests the amount of codes available.
236: */
237: public void testCodes() throws FactoryException {
238: final CRSAuthorityFactory factory = new OrderedAxisAuthorityFactory(
239: "EPSG", null, null);
240: final Set codes = factory
241: .getAuthorityCodes(CoordinateReferenceSystem.class);
242: assertNotNull(codes);
243: assertTrue(codes.size() >= 3000);
244: }
245:
246: /**
247: * A random CRS for fun.
248: */
249: public void test26910() throws FactoryException {
250: final CRSAuthorityFactory factory = new OrderedAxisAuthorityFactory(
251: "EPSG", null, null);
252: final CoordinateReferenceSystem crs = factory
253: .createCoordinateReferenceSystem("EPSG:26910");
254: assertNotNull(crs);
255: assertSame(crs, factory.createObject("EPSG:26910"));
256: }
257:
258: /**
259: * UDIG requires this to work.
260: */
261: public void test4326() throws FactoryException {
262: final CRSAuthorityFactory factory = new OrderedAxisAuthorityFactory(
263: "EPSG", null, null);
264: final CoordinateReferenceSystem crs = factory
265: .createCoordinateReferenceSystem("EPSG:4326");
266: assertNotNull(crs);
267: assertSame(crs, factory.createObject("EPSG:4326"));
268: }
269:
270: /**
271: * UDIG requires this to work.
272: */
273: public void test4269() throws FactoryException {
274: final CRSAuthorityFactory factory = new OrderedAxisAuthorityFactory(
275: "EPSG", null, null);
276: final CoordinateReferenceSystem crs = factory
277: .createCoordinateReferenceSystem("EPSG:4269");
278: assertNotNull(crs);
279: assertSame(crs, factory.createObject("EPSG:4269"));
280: }
281:
282: /**
283: * A random CRS for fun.
284: */
285: public void test26910Lower() throws FactoryException {
286: CoordinateReferenceSystem crs = CRS.decode("epsg:26910");
287: assertNotNull(crs);
288: }
289:
290: /**
291: * A random CRS for fun.
292: */
293: public void test26986Lower() throws FactoryException {
294: CoordinateReferenceSystem crs = CRS.decode("epsg:26986");
295: assertNotNull(crs);
296: }
297:
298: /**
299: * WFS requires this to work.
300: */
301: public void test4326Lower() throws FactoryException {
302: CoordinateReferenceSystem crs = CRS.decode("epsg:4326");
303: assertNotNull(crs);
304: }
305:
306: /**
307: * WFS requires this to work.
308: */
309: public void test26742Lower() throws FactoryException {
310: CoordinateReferenceSystem crs = CRS.decode("epsg:26742");
311: assertNotNull(crs);
312: }
313:
314: /**
315: * WFS requires this to work.
316: */
317: public void test4269Lower() throws FactoryException {
318: CoordinateReferenceSystem crs = CRS.decode("epsg:4269");
319: assertNotNull(crs);
320: }
321:
322: /**
323: * Tests the number of CRS that can be created. This test will be executed only if this test
324: * suite is run with the {@code -verbose} option provided on the command line.
325: */
326: public void testSuccess() throws FactoryException {
327: if (!verbose) {
328: return;
329: }
330: final CRSAuthorityFactory factory = new OrderedAxisAuthorityFactory(
331: "EPSG", null, null);
332: Set codes = factory
333: .getAuthorityCodes(CoordinateReferenceSystem.class);
334: int total = codes.size();
335: int count = 0;
336: for (Iterator i = codes.iterator(); i.hasNext();) {
337: CoordinateReferenceSystem crs;
338: String code = (String) i.next();
339: try {
340: crs = factory.createCoordinateReferenceSystem(code);
341: assertNotNull(crs);
342: count++;
343: } catch (FactoryException e) {
344: System.err.println("WARNING (CRS: " + code + " ):"
345: + e.getMessage());
346: }
347: }
348: System.out.println("Success: " + count + "/" + total + " ("
349: + (count * 100) / total + "%)");
350: }
351: }
|