001: /*
002: * Geotools2 - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-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;
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.operation.builder;
017:
018: import junit.framework.Test;
019: import junit.framework.TestCase;
020: import junit.framework.TestSuite;
021: import org.geotools.geometry.GeneralDirectPosition;
022: import org.geotools.referencing.datum.BursaWolfParameters;
023: import org.geotools.referencing.operation.transform.GeocentricTranslation;
024: import org.opengis.referencing.FactoryException;
025: import org.opengis.referencing.operation.TransformException;
026: import org.opengis.geometry.DirectPosition;
027:
028: // J2SE and extensions
029: import java.util.ArrayList;
030: import java.util.List;
031: import java.util.Random;
032:
033: public class BursaWolfTransformBuilderTest extends TestCase {
034: /**
035: * Run the suite from the command line.
036: *
037: */
038: public static void main(String[] args) {
039: junit.textui.TestRunner.run(suite());
040: }
041:
042: /**
043: * Returns the test suite.
044: *
045: */
046: public static Test suite() {
047: return new TestSuite(BursaWolfTransformBuilderTest.class);
048: }
049:
050: /**
051: * Test {@link BursaWolfTransformBuilder}.
052: *
053: */
054: public void testBursaWolfParamCalculaterXrotation()
055: throws FactoryException, TransformException {
056: Random random = new Random(773418718);
057:
058: double R = 6370000;
059: double angle = (((random.nextDouble() * 10) / 3600) * Math.PI) / 180;
060: double cos = Math.cos(angle);
061: double sin = Math.sin(angle);
062:
063: List /*<MappedPosition>*/vectors = new ArrayList();
064:
065: vectors.add(new MappedPosition(new GeneralDirectPosition(R, 0,
066: 0), new GeneralDirectPosition(R, 0, 0)));
067:
068: vectors.add(new MappedPosition(new GeneralDirectPosition(0, cos
069: * R, -sin * R), new GeneralDirectPosition(0, R, 0)));
070:
071: vectors.add(new MappedPosition(new GeneralDirectPosition(0, sin
072: * R, cos * R), new GeneralDirectPosition(0, 0, R)));
073:
074: double[] points = new double[vectors.size() * 3];
075:
076: for (int i = 0; i < vectors.size(); i++) {
077: points[i * 3] = ((MappedPosition) vectors.get(i))
078: .getSource().getCoordinates()[0];
079: points[(i * 3) + 1] = ((MappedPosition) vectors.get(i))
080: .getSource().getCoordinates()[1];
081: points[(i * 3) + 2] = ((MappedPosition) vectors.get(i))
082: .getSource().getCoordinates()[2];
083: }
084:
085: double[] dstPoints = new double[points.length];
086:
087: MathTransformBuilder BWPT = new BursaWolfTransformBuilder(
088: vectors);
089: BWPT.getMathTransform().transform(points, 0, dstPoints, 0,
090: (points.length / 3));
091:
092: for (int i = 0; i < vectors.size(); i++) {
093: assertEquals(dstPoints[i * 3], ((MappedPosition) vectors
094: .get(i)).getTarget().getCoordinates()[0], 1E-2);
095: assertEquals(dstPoints[(i * 3) + 1],
096: ((MappedPosition) vectors.get(i)).getTarget()
097: .getCoordinates()[1], 1E-2);
098: assertEquals(dstPoints[(i * 3) + 2],
099: ((MappedPosition) vectors.get(i)).getTarget()
100: .getCoordinates()[2], 1E-2);
101: }
102: }
103:
104: /**
105: * The test that generates random transformation parameters and
106: * source points. The destination points are calculated using generated
107: * parameters. Then the parameters are computed by the builder and
108: * compared against original.
109: *
110: * @throws FactoryException DOCUMENT ME!
111: * @throws TransformException
112: */
113: public void test2BursaWolfParamCalculater()
114: throws FactoryException, TransformException {
115: double R = 6370000;
116: Random random = new Random(143477662);
117:
118: DirectPosition ptSrc;
119: DirectPosition ptDst;
120: List /*<MappedPosition>*/vectors = new ArrayList();
121:
122: BursaWolfParameters bwp = new BursaWolfParameters(null);
123: bwp.dx = random.nextDouble() * 100;
124: bwp.dy = random.nextDouble() * 100;
125: bwp.dz = random.nextDouble() * 100;
126: bwp.ex = random.nextDouble() * 10;
127: bwp.ey = random.nextDouble() * 10;
128: bwp.ez = random.nextDouble() * 10;
129: bwp.ppm = random.nextDouble() * 10;
130:
131: GeocentricTranslation gt = new GeocentricTranslation(bwp);
132:
133: for (int i = 0; i < (3 * 10); i++) {
134: double gamma = ((45 + (random.nextDouble() * 10)) * Math.PI) / 180;
135: double alfa = ((45 + (random.nextDouble() * 10)) * Math.PI) / 180;
136:
137: // generate source points
138: ptSrc = new GeneralDirectPosition(R * Math.sin(gamma)
139: * Math.cos(alfa), R * Math.sin(gamma)
140: * Math.cos(alfa), R * Math.cos(gamma));
141:
142: double[] pom = new double[3];
143:
144: // generates destination points
145: gt.transform(ptSrc.getCoordinates(), 0, pom, 0, 1);
146: ptDst = new GeneralDirectPosition(pom);
147: vectors.add(new MappedPosition(ptSrc, ptDst));
148: }
149:
150: BursaWolfTransformBuilder BWPT = new BursaWolfTransformBuilder(
151: vectors);
152: assertEquals(BWPT.getBursaWolfParameters(null).dx, bwp.dx, 1E-2);
153: assertEquals(BWPT.getBursaWolfParameters(null).dy, bwp.dy, 1E-2);
154: assertEquals(BWPT.getBursaWolfParameters(null).dz, bwp.dz, 1E-2);
155: assertEquals(BWPT.getBursaWolfParameters(null).ex, bwp.ex, 1E-2);
156: assertEquals(BWPT.getBursaWolfParameters(null).ey, bwp.ey, 1E-2);
157: assertEquals(BWPT.getBursaWolfParameters(null).ez, bwp.ez, 1E-2);
158: assertEquals(BWPT.getBursaWolfParameters(null).ppm, bwp.ppm,
159: 1E-2);
160:
161: assertEquals(BWPT.getErrorStatistics().rms(), 0, 1E-2);
162: }
163: }
|