001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2005-2006, GeoTools Project Managment Committee (PMC)
005: * (C) 2005, Institut de Recherche pour le Développement
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.factory.epsg;
018:
019: // J2SE dependencies
020: import java.util.Iterator;
021: import java.util.logging.Level;
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.Identifier;
030: import org.opengis.referencing.FactoryException;
031: import org.opengis.referencing.IdentifiedObject;
032: import org.opengis.referencing.crs.CRSAuthorityFactory;
033: import org.opengis.referencing.crs.CoordinateReferenceSystem;
034: import org.opengis.referencing.operation.Conversion;
035: import org.opengis.referencing.operation.Transformation;
036: import org.opengis.referencing.operation.CoordinateOperation;
037: import org.opengis.referencing.operation.CoordinateOperationFactory;
038: import org.opengis.referencing.operation.ConcatenatedOperation;
039:
040: // Geotools dependencies
041: import org.geotools.referencing.operation.BufferedCoordinateOperationFactory;
042: import org.geotools.referencing.operation.AbstractCoordinateOperation;
043: import org.geotools.referencing.operation.AuthorityBackedFactory;
044: import org.geotools.referencing.ReferencingFactoryFinder;
045: import org.geotools.factory.Hints;
046: import org.geotools.resources.Arguments;
047: import org.geotools.resources.Utilities;
048:
049: /**
050: * Tests the usage of {@link CoordinateOperationFactory} with the help of the
051: * EPSG database. Any EPSG plugin should fit. However, this test live in the
052: * {@code plugin/epsg-hsql} module since the HSQL plugin is the only one which
053: * is garantee to work on any machine running Maven.
054: *
055: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/epsg-hsql/src/test/java/org/geotools/referencing/factory/epsg/OperationFactoryTest.java $
056: * @version $Id: OperationFactoryTest.java 27848 2007-11-12 13:10:32Z desruisseaux $
057: * @author Martin Desruisseaux
058: */
059: public class OperationFactoryTest extends TestCase {
060: /**
061: * Run the suite from the command line. If {@code "-log"} flag is specified on the
062: * command-line, then the logger will be set to {@link Level#CONFIG}. This is usefull
063: * for tracking down which data source is actually used.
064: */
065: public static void main(final String[] args) {
066: final Arguments arguments = new Arguments(args);
067: final boolean log = arguments.getFlag("-log");
068: arguments.getRemainingArguments(0);
069: org.geotools.util.logging.Logging.GEOTOOLS
070: .forceMonolineConsoleOutput(log ? Level.CONFIG : null);
071: junit.textui.TestRunner.run(suite());
072: }
073:
074: /**
075: * Returns the test suite.
076: */
077: public static Test suite() {
078: return new TestSuite(OperationFactoryTest.class);
079: }
080:
081: /**
082: * Constructs a test case with the given name.
083: */
084: public OperationFactoryTest(final String name) {
085: super (name);
086: }
087:
088: /**
089: * Returns the first identifier for the specified object.
090: */
091: private static String getIdentifier(final IdentifiedObject object) {
092: return ((Identifier) object.getIdentifiers().iterator().next())
093: .getCode();
094: }
095:
096: /**
097: * Tests the creation of an operation from EPSG:4230 to EPSG:4326. They are the same
098: * CRS than the one tested in {@link DefaultDataSourceTest#testTransformations}.
099: */
100: public void testCreate() throws FactoryException {
101: final CRSAuthorityFactory crsFactory;
102: final CoordinateOperationFactory opFactory;
103: CoordinateReferenceSystem sourceCRS;
104: CoordinateReferenceSystem targetCRS;
105: CoordinateOperation operation;
106:
107: crsFactory = ReferencingFactoryFinder.getCRSAuthorityFactory(
108: "EPSG", null);
109: opFactory = ReferencingFactoryFinder
110: .getCoordinateOperationFactory(null);
111: sourceCRS = crsFactory.createCoordinateReferenceSystem("4230");
112: targetCRS = crsFactory.createCoordinateReferenceSystem("4326");
113: operation = opFactory.createOperation(sourceCRS, targetCRS);
114:
115: assertSame(sourceCRS, operation.getSourceCRS());
116: assertSame(targetCRS, operation.getTargetCRS());
117: assertSame(operation, opFactory.createOperation(sourceCRS,
118: targetCRS));
119: assertTrue("Expected a buffered factory but got "
120: + opFactory.getClass().getName(),
121: opFactory instanceof BufferedCoordinateOperationFactory);
122: assertTrue(
123: "EPSG authority factory not found.",
124: ((BufferedCoordinateOperationFactory) opFactory)
125: .getImplementationHints().get(
126: Hints.COORDINATE_OPERATION_FACTORY) instanceof AuthorityBackedFactory);
127: assertEquals("1612", getIdentifier(operation)); // See comment in DefaultDataSourceTest.
128: assertEquals(1.0, AbstractCoordinateOperation
129: .getAccuracy(operation), 1E-6);
130: assertTrue(operation instanceof Transformation);
131: /*
132: * Tests a transformation not backed directly by an authority factory.
133: * However, the inverse transform may exist in the authority factory.
134: */
135: sourceCRS = crsFactory.createCoordinateReferenceSystem("4326");
136: targetCRS = crsFactory.createCoordinateReferenceSystem("2995");
137: operation = opFactory.createOperation(sourceCRS, targetCRS);
138: assertTrue(
139: "This test needs an operation not backed by the EPSG factory.",
140: operation.getIdentifiers().isEmpty());
141: // Should contains exactly one transformations and an arbitrary number of conversions.
142: assertTrue(operation instanceof ConcatenatedOperation);
143: int count = 0;
144: for (final Iterator it = ((ConcatenatedOperation) operation)
145: .getOperations().iterator(); it.hasNext();) {
146: final CoordinateOperation op = (CoordinateOperation) it
147: .next();
148: if (op instanceof Transformation) {
149: count++;
150: } else {
151: assertTrue(
152: "Expected Conversion but got "
153: + Utilities
154: .getShortName(AbstractCoordinateOperation
155: .getType(op)) + ". ",
156: (op instanceof Conversion));
157: }
158: }
159: assertEquals(
160: "The coordinate operation should contains exactly 1 transformation",
161: 1, count);
162: assertTrue(AbstractCoordinateOperation.getAccuracy(operation) <= 25);
163: }
164: }
|