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

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

add Albers Equal Area Projection and Polar Stereographic Projection (see #12186)
(imports pieces of code from the Geotools project)

  • Property svn:eol-style set to native
File size: 5.6 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 * Relative iteration precision used in the <code>mlfn</code> method
28 */
29 private static final double MLFN_TOL = 1E-11;
30
31 /**
32 * Constants used to calculate {@link #en0}, {@link #en1},
33 * {@link #en2}, {@link #en3}, {@link #en4}.
34 */
35 private static final double C00 = 1.0,
36 C02 = 0.25,
37 C04 = 0.046875,
38 C06 = 0.01953125,
39 C08 = 0.01068115234375,
40 C22 = 0.75,
41 C44 = 0.46875,
42 C46 = 0.01302083333333333333,
43 C48 = 0.00712076822916666666,
44 C66 = 0.36458333333333333333,
45 C68 = 0.00569661458333333333,
46 C88 = 0.3076171875;
47
48 /**
49 * Constant needed for the <code>mlfn</code> method.
50 * Setup at construction time.
51 */
52 protected double en0, en1, en2, en3, en4;
53
54 /**
55 * Ellipsoid excentricity, equals to <code>sqrt({@link #excentricitySquared})</code>.
56 * Value 0 means that the ellipsoid is spherical.
57 *
58 * @see #excentricitySquared
59 */
60 protected double e;
61
62 /**
63 * The square of excentricity: e² = (a²-b²)/a² where
64 * <var>e</var> is the excentricity,
65 * <var>a</var> is the semi major axis length and
66 * <var>b</var> is the semi minor axis length.
67 */
68 protected double e2;
69
70 @Override
71 public void initialize(ProjParameters params) throws ProjectionConfigurationException {
72 e2 = params.ellps.e2;
73 e = params.ellps.e;
74 // Compute constants for the mlfn
75 double t;
76 en0 = C00 - e2 * (C02 + e2 *
77 (C04 + e2 * (C06 + e2 * C08)));
78 en1 = e2 * (C22 - e2 *
79 (C04 + e2 * (C06 + e2 * C08)));
80 en2 = (t = e2 * e2) *
81 (C44 - e2 * (C46 + e2 * C48));
82 en3 = (t *= e2) * (C66 - e2 * C68);
83 en4 = t * e2 * C88;
84 }
85
86 /**
87 * Calculates the meridian distance. This is the distance along the central
88 * meridian from the equator to {@code phi}. Accurate to &lt; 1e-5 meters
89 * when used in conjuction with typical major axis values.
90 *
91 * @param phi latitude to calculate meridian distance for.
92 * @param sphi sin(phi).
93 * @param cphi cos(phi).
94 * @return meridian distance for the given latitude.
95 */
96 protected final double mlfn(final double phi, double sphi, double cphi) {
97 cphi *= sphi;
98 sphi *= sphi;
99 return en0 * phi - cphi *
100 (en1 + sphi *
101 (en2 + sphi *
102 (en3 + sphi *
103 (en4))));
104 }
105
106 /**
107 * Calculates the latitude ({@code phi}) from a meridian distance.
108 * Determines phi to TOL (1e-11) radians, about 1e-6 seconds.
109 *
110 * @param arg meridian distance to calulate latitude for.
111 * @return the latitude of the meridian distance.
112 * @throws RuntimeException if the itteration does not converge.
113 */
114 protected final double inv_mlfn(double arg) {
115 double s, t, phi, k = 1.0/(1.0 - e2);
116 int i;
117 phi = arg;
118 for (i = MAXIMUM_ITERATIONS; true;) { // rarely goes over 5 iterations
119 if (--i < 0) {
120 throw new RuntimeException("Too many iterations");
121 }
122 s = Math.sin(phi);
123 t = 1.0 - e2 * s * s;
124 t = (mlfn(phi, s, Math.cos(phi)) - arg) * (t * Math.sqrt(t)) * k;
125 phi -= t;
126 if (Math.abs(t) < MLFN_TOL) {
127 return phi;
128 }
129 }
130 }
131
132 public static double normalizeLon(double lon) {
133 if (lon >= -Math.PI && lon <= Math.PI)
134 return lon;
135 else {
136 lon = lon % (2 * Math.PI);
137 if (lon > Math.PI) {
138 return lon - 2 * Math.PI;
139 } else if (lon < -Math.PI) {
140 return lon + 2 * Math.PI;
141 }
142 return lon;
143 }
144 }
145
146 /**
147 * Computes function <code>f(s,c,e²) = c/sqrt(1 - s²&times;e²)</code> needed for the true scale
148 * latitude (Snyder 14-15), where <var>s</var> and <var>c</var> are the sine and cosine of
149 * the true scale latitude, and <var>e²</var> is the {@linkplain #excentricitySquared
150 * eccentricity squared}.
151 */
152 final double msfn(final double s, final double c) {
153 return c / Math.sqrt(1.0 - (s*s) * e2);
154 }
155
156 /**
157 * Computes function (15-9) and (9-13) from Snyder.
158 * Equivalent to negative of function (7-7).
159 */
160 final double tsfn(final double lat, double sinlat) {
161 sinlat *= e;
162 /*
163 * NOTE: change sign to get the equivalent of Snyder (7-7).
164 */
165 return Math.tan(0.5 * (Math.PI/2 - lat)) / Math.pow((1 - sinlat) / (1 + sinlat), 0.5*e);
166 }
167}
Note: See TracBrowser for help on using the repository browser.