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 @ 805

History | View | Annotate | Download (5.62 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 java.util.Arrays;
26

    
27
import org.gvsig.proj.catalogue.CRSDefinition;
28
import org.gvsig.proj.catalogue.TransformationDefinition;
29
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase;
30

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

    
39
    protected CoordinateReferenceSystem source;
40
    protected CoordinateReferenceSystem target;
41
    protected CoordinateTransformation transformation;
42
    protected CoordinateReferenceSystemManager manager;
43

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

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

    
77
    protected abstract CoordinateReferenceSystem createTargetProjection()
78
        throws Exception;
79

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

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

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

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