001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-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.geometry.jts;
017:
018: // J2SE dependencies
019: import java.util.Random;
020:
021: // JTS dependencies
022: import com.vividsolutions.jts.geom.Coordinate;
023: import com.vividsolutions.jts.geom.CoordinateSequence;
024: import com.vividsolutions.jts.geom.CoordinateSequenceFactory;
025: import com.vividsolutions.jts.geom.DefaultCoordinateSequenceFactory;
026:
027: // OpenGIS dependencies
028: import org.opengis.referencing.FactoryException;
029: import org.opengis.referencing.operation.MathTransform;
030: import org.opengis.referencing.operation.MathTransform2D;
031: import org.opengis.referencing.operation.TransformException;
032: import org.opengis.referencing.crs.CoordinateReferenceSystem;
033:
034: // Geotools dependencies
035: import org.geotools.referencing.ReferencingFactoryFinder;
036: import org.geotools.referencing.crs.DefaultGeographicCRS;
037:
038: // JUnit dependencies
039: import junit.framework.Test;
040: import junit.framework.TestCase;
041: import junit.framework.TestSuite;
042:
043: /**
044: * Tests the {@link DefaultCoordinateSequenceTransformer} implementation.
045: *
046: * @since 2.2
047: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/test/java/org/geotools/geometry/jts/CoordinateSequenceTransformerTest.java $
048: * @version $Id: CoordinateSequenceTransformerTest.java 27848 2007-11-12 13:10:32Z desruisseaux $
049: * @author Martin Desruisseaux
050: */
051: public class CoordinateSequenceTransformerTest extends TestCase {
052: /**
053: * The coordinate sequence factory to use.
054: */
055: private final CoordinateSequenceFactory csFactory = DefaultCoordinateSequenceFactory
056: .instance();
057:
058: /**
059: * Run the suite from the command line.
060: */
061: public static void main(String[] args) {
062: org.geotools.util.logging.Logging.GEOTOOLS
063: .forceMonolineConsoleOutput();
064: junit.textui.TestRunner.run(suite());
065: }
066:
067: /**
068: * Returns the test suite.
069: */
070: public static Test suite() {
071: return new TestSuite(CoordinateSequenceTransformerTest.class);
072: }
073:
074: /**
075: * Compares the current implementation with a simplier one.
076: */
077: public void testTransform() throws FactoryException,
078: TransformException {
079: final MathTransform2D t;
080: final CoordinateReferenceSystem crs;
081: crs = ReferencingFactoryFinder.getCRSFactory(null)
082: .createFromWKT(JTSTest.UTM_ZONE_10N);
083: t = (MathTransform2D) ReferencingFactoryFinder
084: .getCoordinateOperationFactory(null).createOperation(
085: DefaultGeographicCRS.WGS84, crs)
086: .getMathTransform();
087: final Random random = new Random(546757437746704345L);
088:
089: // Tries with different coordinate sequence length.
090: final int[] size = { 12, 1000 };
091: for (int j = 0; j < size.length; j++) {
092: final Coordinate[] source = new Coordinate[size[j]];
093: for (int i = 0; i < source.length; i++) {
094: source[i] = new Coordinate(-121 - 4
095: * random.nextDouble(), -45 + 90
096: * random.nextDouble(), 500 * random
097: .nextDouble());
098: }
099: final CoordinateSequence sourceCS = csFactory
100: .create(source);
101: final CoordinateSequence targetCS = transform(sourceCS, t);
102: assertNotSame(sourceCS, targetCS);
103: assertEquals(sourceCS.size(), targetCS.size());
104: for (int i = sourceCS.size(); --i >= 0;) {
105: assertFalse(sourceCS.getCoordinate(i).equals(
106: targetCS.getCoordinate(i)));
107: }
108:
109: final CoordinateSequenceTransformer transformer = new DefaultCoordinateSequenceTransformer();
110: final CoordinateSequence testCS = transformer.transform(
111: sourceCS, t);
112: assertNotSame(sourceCS, testCS);
113: assertNotSame(targetCS, testCS);
114: assertEquals(sourceCS.size(), testCS.size());
115: for (int i = targetCS.size(); --i >= 0;) {
116: assertEquals(targetCS.getCoordinate(i), testCS
117: .getCoordinate(i));
118: }
119: }
120: }
121:
122: /**
123: * The following is basically a copy-and-paste of a previous implementation by Andrea Aime.
124: */
125: private CoordinateSequence transform(final CoordinateSequence cs,
126: final MathTransform transform) throws TransformException {
127: double[] coords = new double[100];
128: final Coordinate[] scs = cs.toCoordinateArray();
129: final Coordinate[] tcs = new Coordinate[scs.length];
130: if (coords.length < (scs.length * 2)) {
131: coords = new double[scs.length * 2];
132: }
133: for (int i = 0; i < scs.length; i++) {
134: coords[i * 2] = scs[i].x;
135: coords[i * 2 + 1] = scs[i].y;
136: }
137: transform.transform(coords, 0, coords, 0, scs.length);
138: for (int i = 0; i < tcs.length; i++) {
139: tcs[i] = new Coordinate(coords[i * 2], coords[i * 2 + 1]);
140: }
141: return csFactory.create(tcs);
142: }
143: }
|