source: josm/trunk/src/org/openstreetmap/josm/data/projection/proj/AbstractProj.java@ 9565

Last change on this file since 9565 was 9565, checked in by bastiK, 9 years ago

add 2 standard parallel & non-spherical variants for Mercator projection (see #12186)
(imports pieces of code from the Geotools project)

  • Property svn:eol-style set to native
File size: 6.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.projection.proj;
3
4import org.openstreetmap.josm.data.projection.ProjectionConfigurationException;
5
6/**
7 * Abstract base class providing utilities for implementations of the Proj
8 * interface.
9 *
10 * This class has been derived from the implementation of the Geotools project;
11 * git 8cbf52d, org.geotools.referencing.operation.projection.MapProjection
12 * at the time of migration.
13 * <p>
14 *
15 * @author André Gosselin
16 * @author Martin Desruisseaux (PMO, IRD)
17 * @author Rueben Schulz
18*/
19public abstract class AbstractProj implements Proj {
20
21 /**
22 * Maximum number of iterations for iterative computations.
23 */
24 private static final int MAXIMUM_ITERATIONS = 15;
25
26 /**
27 * Difference allowed in iterative computations.
28 */
29 private static final double ITERATION_TOLERANCE = 1E-10;
30
31 /**
32 * Relative iteration precision used in the <code>mlfn</code> method
33 */
34 private static final double MLFN_TOL = 1E-11;
35
36 /**
37 * Constants used to calculate {@link #en0}, {@link #en1},
38 * {@link #en2}, {@link #en3}, {@link #en4}.
39 */
40 private static final double C00 = 1.0,
41 C02 = 0.25,
42 C04 = 0.046875,
43 C06 = 0.01953125,
44 C08 = 0.01068115234375,
45 C22 = 0.75,
46 C44 = 0.46875,
47 C46 = 0.01302083333333333333,
48 C48 = 0.00712076822916666666,
49 C66 = 0.36458333333333333333,
50 C68 = 0.00569661458333333333,
51 C88 = 0.3076171875;
52
53 /**
54 * Constant needed for the <code>mlfn</code> method.
55 * Setup at construction time.
56 */
57 protected double en0, en1, en2, en3, en4;
58
59 /**
60 * Ellipsoid excentricity, equals to <code>sqrt({@link #e2 excentricity squared})</code>.
61 * Value 0 means that the ellipsoid is spherical.
62 *
63 * @see #e2
64 */
65 protected double e;
66
67 /**
68 * The square of excentricity: e² = (a²-b²)/a² where
69 * <var>e</var> is the excentricity,
70 * <var>a</var> is the semi major axis length and
71 * <var>b</var> is the semi minor axis length.
72 *
73 * @see #e
74 */
75 protected double e2;
76
77 /**
78 * is ellipsoid spherical?
79 * @see Ellisoid.spherical
80 */
81 protected boolean spherical;
82
83 @Override
84 public void initialize(ProjParameters params) throws ProjectionConfigurationException {
85 e2 = params.ellps.e2;
86 e = params.ellps.e;
87 spherical = params.ellps.spherical;
88 // Compute constants for the mlfn
89 double t;
90 en0 = C00 - e2 * (C02 + e2 *
91 (C04 + e2 * (C06 + e2 * C08)));
92 en1 = e2 * (C22 - e2 *
93 (C04 + e2 * (C06 + e2 * C08)));
94 en2 = (t = e2 * e2) *
95 (C44 - e2 * (C46 + e2 * C48));
96 en3 = (t *= e2) * (C66 - e2 * C68);
97 en4 = t * e2 * C88;
98 }
99
100 /**
101 * Calculates the meridian distance. This is the distance along the central
102 * meridian from the equator to {@code phi}. Accurate to &lt; 1e-5 meters
103 * when used in conjuction with typical major axis values.
104 *
105 * @param phi latitude to calculate meridian distance for.
106 * @param sphi sin(phi).
107 * @param cphi cos(phi).
108 * @return meridian distance for the given latitude.
109 */
110 protected final double mlfn(final double phi, double sphi, double cphi) {
111 cphi *= sphi;
112 sphi *= sphi;
113 return en0 * phi - cphi *
114 (en1 + sphi *
115 (en2 + sphi *
116 (en3 + sphi *
117 (en4))));
118 }
119
120 /**
121 * Calculates the latitude ({@code phi}) from a meridian distance.
122 * Determines phi to TOL (1e-11) radians, about 1e-6 seconds.
123 *
124 * @param arg meridian distance to calulate latitude for.
125 * @return the latitude of the meridian distance.
126 * @throws RuntimeException if the itteration does not converge.
127 */
128 protected final double inv_mlfn(double arg) {
129 double s, t, phi, k = 1.0/(1.0 - e2);
130 int i;
131 phi = arg;
132 for (i = MAXIMUM_ITERATIONS; true;) { // rarely goes over 5 iterations
133 if (--i < 0) {
134 throw new RuntimeException("Too many iterations");
135 }
136 s = Math.sin(phi);
137 t = 1.0 - e2 * s * s;
138 t = (mlfn(phi, s, Math.cos(phi)) - arg) * (t * Math.sqrt(t)) * k;
139 phi -= t;
140 if (Math.abs(t) < MLFN_TOL) {
141 return phi;
142 }
143 }
144 }
145
146 // Iteratively solve equation (7-9) from Snyder.
147 final double cphi2(final double ts) {
148 final double eccnth = 0.5 * e;
149 double phi = (Math.PI/2) - 2.0 * Math.atan(ts);
150 for (int i = 0; i < MAXIMUM_ITERATIONS; i++) {
151 final double con = e * Math.sin(phi);
152 final double dphi = (Math.PI/2) - 2.0*Math.atan(ts * Math.pow((1-con)/(1+con), eccnth)) - phi;
153 phi += dphi;
154 if (Math.abs(dphi) <= ITERATION_TOLERANCE) {
155 return phi;
156 }
157 }
158 throw new RuntimeException("no convergence");
159 }
160
161 /**
162 * Computes function <code>f(s,c,e²) = c/sqrt(1 - s²&times;e²)</code> needed for the true scale
163 * latitude (Snyder 14-15), where <var>s</var> and <var>c</var> are the sine and cosine of
164 * the true scale latitude, and <var>e²</var> is the {@linkplain #e2 eccentricity squared}.
165 * @param s sine of the true scale latitude
166 * @param c cosine of the true scale latitude
167 * @return <code>c/sqrt(1 - s²&times;e²)</code>
168 */
169 final double msfn(final double s, final double c) {
170 return c / Math.sqrt(1.0 - (s*s) * e2);
171 }
172
173 /**
174 * Computes function (15-9) and (9-13) from Snyder.
175 * Equivalent to negative of function (7-7).
176 * @param lat the latitude
177 * @param sinlat sine of the latitude
178 * @return auxiliary value computed from <code>lat</code> and <code>sinlat</code>
179 */
180 final double tsfn(final double lat, double sinlat) {
181 sinlat *= e;
182 // NOTE: change sign to get the equivalent of Snyder (7-7).
183 return Math.tan(0.5 * (Math.PI/2 - lat)) / Math.pow((1 - sinlat) / (1 + sinlat), 0.5*e);
184 }
185}
Note: See TracBrowser for help on using the repository browser.