Statistics
| Revision:

root / org.gvsig.proj / branches / refactor2018 / org.gvsig.proj / org.gvsig.proj.lib / org.gvsig.proj.lib.api / src / test / java / org / gvsig / proj / CoordinateTransformationIT.java @ 814

History | View | Annotate | Download (5.79 KB)

1
/* gvSIG. Desktop Geographic Information System.
2
 *
3
 * Copyright ? 2007-2012 gvSIG Association
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18
 * MA  02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us
21
 * at info AT gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.proj;
24

    
25
import org.gvsig.proj.catalog.TransformationDefinition;
26
import org.gvsig.proj.catalog.exception.NoninvertibleTransformException;
27
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase;
28

    
29
/**
30
 * {@link CoordinateTransformation} API acceptance tests.
31
 * 
32
 * @author gvSIG Team
33
 */
34
public abstract class CoordinateTransformationIT extends
35
    AbstractLibraryAutoInitTestCase {
36

    
37
    protected CoordinateReferenceSystem source;
38
    protected CoordinateReferenceSystem target;
39
    protected CoordinateTransformation transformation;
40
    protected CoordinateReferenceSystemManager manager;
41

    
42
    protected void doSetUp() throws Exception {
43
        manager = createProjectionManager();
44
        source = createSourceProjection();
45
        target = createTargetProjection();
46
        transformation = createTransformationFromDefinition();
47
    }
48
    
49
    /**
50
     * Creates a {@link CoordinateReferenceSystemManager} instance.
51
     * 
52
     * @return the {@link CoordinateReferenceSystemManager} instance
53
     */
54
    protected abstract CoordinateReferenceSystemManager createProjectionManager();
55
    
56
    /**
57
     * Returns the transformation definition to create the CoordinateTransformation
58
     * to test.
59
     * 
60
     * @return the authority name
61
     */
62
    protected abstract TransformationDefinition getTransformationDefinition();
63

    
64
    /**
65
     * Creates the transformation to be tested
66
     */
67
    public CoordinateTransformation createTransformationFromDefinition() throws Exception {
68
            TransformationDefinition definition = getTransformationDefinition();
69
        return manager.getCoordinateTransformation(definition);
70
    }
71
    
72
    protected abstract CoordinateReferenceSystem createSourceProjection()
73
        throws Exception;
74

    
75
    protected abstract CoordinateReferenceSystem createTargetProjection()
76
        throws Exception;
77

    
78
    /**
79
     * Test method for
80
     * {@link org.gvsig.proj.CoordinateTransformation#getSource()}.
81
     */
82
    public void testGetSourceProjection() {
83
        assertEquals(source, transformation.getSource());
84
    }
85

    
86
    /**
87
     * Test method for
88
     * {@link org.gvsig.proj.CoordinateTransformation#getTarget()}.
89
     */
90
    public void testGetTargetProjection() {
91
        assertEquals(target, transformation.getTarget());
92
    }
93

    
94
    /**
95
     * Test method for
96
     * {@link org.gvsig.proj.CoordinateTransformation#getInverse()}.
97
     * @throws NoninvertibleTransformException 
98
     */
99
    public void testGetReverse() throws NoninvertibleTransformException {
100
        CoordinateTransformation reverseTransformation =
101
            transformation.getInverse();
102
        assertEquals(target, reverseTransformation.getSource());
103
        assertEquals(source, reverseTransformation.getTarget());
104
    }
105

    
106
    /**
107
     * The input point to test the transformation
108
     * 
109
     * @return
110
     */
111
    public abstract double[] getInputPoint();
112
    /**
113
     * The output point to thest the transformation. This point
114
     * should be a known result of applying the transformation on
115
     * getInputPoint()
116
     * @return
117
     */
118
    public abstract double[] getResultPoint();
119
    /**
120
     * Gets the maximum numeric tolerance to accept the result of
121
     * the transformation as valid
122
     * 
123
     * @return
124
     */
125
    public abstract double getResultTolerance();
126
    
127
    /**
128
     * Test method for
129
     * {@link org.gvsig.proj.CoordinateTransformation#apply(double[])}.
130
     */
131
    public void testApplyTransformationInline() {
132
            double[] input = getInputPoint();
133
            transformation.apply(input);
134
            assertTrue(pointsEquals(getResultPoint(), input, getResultTolerance()));
135
    }
136
    
137
    public boolean pointsEquals(double[] point1, double[] point2, double tolerance) {
138
            for (int i=0; i<point1.length; i++) {
139
                    if (Math.abs(point1[i]-point2[i])>tolerance) {
140
                            return false;
141
                    }
142
            }
143
            return true;
144
    }
145
    
146
    /**
147
     * Test method for
148
     * {@link org.gvsig.proj.CoordinateTransformation#apply(double[])}.
149
     */
150
    public void testApplyTransformation() {
151
            double[] input = getInputPoint();
152
            double[] transformed = new double[input.length];
153
            transformation.apply(input, transformed);
154
            assertTrue(pointsEquals(getResultPoint(), transformed, getResultTolerance()));
155
    }
156
    
157
    /**
158
     * Test method for
159
     * {@link org.gvsig.proj.CoordinateTransformation#getInverse()}
160
     * and
161
     * {@link org.gvsig.proj.CoordinateTransformation#apply(double[])}.
162
     * @throws NoninvertibleTransformException 
163
     */
164
    public void testApplyInverseTransformation() throws NoninvertibleTransformException {
165
            double[] input = getInputPoint();
166
            double[] transformed = new double[input.length];
167
            transformation.apply(input, transformed);
168
            CoordinateTransformation reverseTransformation =
169
                transformation.getInverse();
170
            reverseTransformation.apply(transformed);
171
            assertTrue(pointsEquals(input, transformed, getResultTolerance()));
172
    }
173
}