source: josm/trunk/src/org/openstreetmap/josm/data/projection/Ellipsoid.java@ 9105

Last change on this file since 9105 was 9104, checked in by bastiK, 8 years ago

add more datums and ellipsoids (see #12186)

  • Property svn:eol-style set to native
File size: 10.9 KB
Line 
1/*
2 * Import from fr.geo.convert package, a geographic coordinates converter.
3 * (https://www.i3s.unice.fr/~johan/gps/)
4 * License: GPL. For details, see LICENSE file.
5 * Copyright (C) 2002 Johan Montagnat (johan@creatis.insa-lyon.fr)
6 */
7package org.openstreetmap.josm.data.projection;
8
9import org.openstreetmap.josm.data.coor.LatLon;
10
11/**
12 * Reference ellipsoids.
13 */
14public final class Ellipsoid {
15
16 /**
17 * Airy 1830
18 */
19 public static final Ellipsoid Airy = Ellipsoid.create_a_b(6377563.396, 6356256.910);
20
21 /**
22 * Modified Airy 1849
23 */
24 public static final Ellipsoid AiryMod = Ellipsoid.create_a_b(6377340.189, 6356034.446);
25
26 /**
27 * Australian National Spheroid (Australian Natl & S. Amer. 1969)
28 * same as GRS67 Modified
29 */
30 public static final Ellipsoid AustSA = Ellipsoid.create_a_rf(6378160.0, 298.25);
31
32 /**
33 * Bessel 1841 ellipsoid
34 */
35 public static final Ellipsoid Bessel1841 = Ellipsoid.create_a_rf(6377397.155, 299.1528128);
36
37 /**
38 * Bessel 1841 (Namibia)
39 */
40 public static final Ellipsoid BesselNamibia = Ellipsoid.create_a_rf(6377483.865, 299.1528128);
41
42 /**
43 * Clarke 1866 ellipsoid
44 */
45 public static final Ellipsoid Clarke1866 = Ellipsoid.create_a_b(6378206.4, 6356583.8);
46
47 /**
48 * Clarke 1880 (modified)
49 */
50 public static final Ellipsoid Clarke1880 = Ellipsoid.create_a_rf(6378249.145, 293.4663);
51
52 /**
53 * Clarke 1880 IGN (French national geographic institute)
54 */
55 public static final Ellipsoid ClarkeIGN = Ellipsoid.create_a_b(6378249.2, 6356515.0);
56
57 /**
58 * Everest (Sabah & Sarawak)
59 */
60 public static final Ellipsoid EverestSabahSarawak = Ellipsoid.create_a_rf(6377298.556, 300.8017);
61
62 /**
63 * GRS67 ellipsoid
64 */
65 public static final Ellipsoid GRS67 = Ellipsoid.create_a_rf(6378160.0, 298.247167427);
66
67 /**
68 * GRS80 ellipsoid
69 */
70 public static final Ellipsoid GRS80 = Ellipsoid.create_a_rf(6378137.0, 298.257222101);
71
72 /**
73 * Hayford's ellipsoid 1909 (ED50 system)
74 * Also known as International 1924
75 * Proj.4 code: intl
76 */
77 public static final Ellipsoid Hayford = Ellipsoid.create_a_rf(6378388.0, 297.0);
78
79 /**
80 * Helmert 1906
81 */
82 public static final Ellipsoid Helmert = Ellipsoid.create_a_rf(6378200.0, 298.3);
83
84 /**
85 * Krassowsky 1940 ellipsoid
86 */
87 public static final Ellipsoid Krassowsky = Ellipsoid.create_a_rf(6378245.0, 298.3);
88
89 /**
90 * WGS66 ellipsoid
91 */
92 public static final Ellipsoid WGS66 = Ellipsoid.create_a_rf(6378145.0, 298.25);
93
94 /**
95 * WGS72 ellipsoid
96 */
97 public static final Ellipsoid WGS72 = Ellipsoid.create_a_rf(6378135.0, 298.26);
98
99 /**
100 * WGS84 ellipsoid
101 */
102 public static final Ellipsoid WGS84 = Ellipsoid.create_a_rf(6378137.0, 298.257223563);
103
104
105 /**
106 * half long axis
107 */
108 public final double a;
109
110 /**
111 * half short axis
112 */
113 public final double b;
114
115 /**
116 * first eccentricity
117 */
118 public final double e;
119
120 /**
121 * first eccentricity squared
122 */
123 public final double e2;
124
125 /**
126 * square of the second eccentricity
127 */
128 public final double eb2;
129
130 /**
131 * private constructur - use one of the create_* methods
132 *
133 * @param a semimajor radius of the ellipsoid axis
134 * @param b semiminor radius of the ellipsoid axis
135 * @param e first eccentricity of the ellipsoid ( = sqrt((a*a - b*b)/(a*a)))
136 * @param e2 first eccentricity squared
137 * @param eb2 square of the second eccentricity
138 */
139 private Ellipsoid(double a, double b, double e, double e2, double eb2) {
140 this.a = a;
141 this.b = b;
142 this.e = e;
143 this.e2 = e2;
144 this.eb2 = eb2;
145 }
146
147 /**
148 * create a new ellipsoid
149 *
150 * @param a semimajor radius of the ellipsoid axis (in meters)
151 * @param b semiminor radius of the ellipsoid axis (in meters)
152 * @return the new ellipsoid
153 */
154 public static Ellipsoid create_a_b(double a, double b) {
155 double e2 = (a*a - b*b) / (a*a);
156 double e = Math.sqrt(e2);
157 double eb2 = e2 / (1.0 - e2);
158 return new Ellipsoid(a, b, e, e2, eb2);
159 }
160
161 /**
162 * create a new ellipsoid
163 *
164 * @param a semimajor radius of the ellipsoid axis (in meters)
165 * @param es first eccentricity squared
166 * @return the new ellipsoid
167 */
168 public static Ellipsoid create_a_es(double a, double es) {
169 double b = a * Math.sqrt(1.0 - es);
170 double e = Math.sqrt(es);
171 double eb2 = es / (1.0 - es);
172 return new Ellipsoid(a, b, e, es, eb2);
173 }
174
175 /**
176 * create a new ellipsoid
177 *
178 * @param a semimajor radius of the ellipsoid axis (in meters)
179 * @param f flattening ( = (a - b) / a)
180 * @return the new ellipsoid
181 */
182 public static Ellipsoid create_a_f(double a, double f) {
183 double b = a * (1.0 - f);
184 double e2 = f * (2 - f);
185 double e = Math.sqrt(e2);
186 double eb2 = e2 / (1.0 - e2);
187 return new Ellipsoid(a, b, e, e2, eb2);
188 }
189
190 /**
191 * create a new ellipsoid
192 *
193 * @param a semimajor radius of the ellipsoid axis (in meters)
194 * @param rf inverse flattening
195 * @return the new ellipsoid
196 */
197 public static Ellipsoid create_a_rf(double a, double rf) {
198 return create_a_f(a, 1.0 / rf);
199 }
200
201 @Override
202 public String toString() {
203 return "Ellipsoid{a="+a+", b="+b+'}';
204 }
205
206 /**
207 * Returns the <i>radius of curvature in the prime vertical</i>
208 * for this reference ellipsoid at the specified latitude.
209 *
210 * @param phi The local latitude (radians).
211 * @return The radius of curvature in the prime vertical (meters).
212 */
213 public double verticalRadiusOfCurvature(final double phi) {
214 return a / Math.sqrt(1.0 - (e2 * sqr(Math.sin(phi))));
215 }
216
217 private static double sqr(final double x) {
218 return x * x;
219 }
220
221 /**
222 * Returns the meridional arc, the true meridional distance on the
223 * ellipsoid from the equator to the specified latitude, in meters.
224 *
225 * @param phi The local latitude (in radians).
226 * @return The meridional arc (in meters).
227 */
228 public double meridionalArc(final double phi) {
229 final double sin2Phi = Math.sin(2.0 * phi);
230 final double sin4Phi = Math.sin(4.0 * phi);
231 final double sin6Phi = Math.sin(6.0 * phi);
232 final double sin8Phi = Math.sin(8.0 * phi);
233 // TODO . calculate 'f'
234 //double f = 1.0 / 298.257222101; // GRS80
235 double f = 1.0 / 298.257223563; // WGS84
236 final double n = f / (2.0 - f);
237 final double n2 = n * n;
238 final double n3 = n2 * n;
239 final double n4 = n3 * n;
240 final double n5 = n4 * n;
241 final double n1n2 = n - n2;
242 final double n2n3 = n2 - n3;
243 final double n3n4 = n3 - n4;
244 final double n4n5 = n4 - n5;
245 final double ap = a * (1.0 - n + (5.0 / 4.0) * (n2n3) + (81.0 / 64.0) * (n4n5));
246 final double bp = (3.0 / 2.0) * a * (n1n2 + (7.0 / 8.0) * (n3n4) + (55.0 / 64.0) * n5);
247 final double cp = (15.0 / 16.0) * a * (n2n3 + (3.0 / 4.0) * (n4n5));
248 final double dp = (35.0 / 48.0) * a * (n3n4 + (11.0 / 16.0) * n5);
249 final double ep = (315.0 / 512.0) * a * (n4n5);
250 return ap * phi - bp * sin2Phi + cp * sin4Phi - dp * sin6Phi + ep * sin8Phi;
251 }
252
253 /**
254 * Returns the <i>radius of curvature in the meridian</i>
255 * for this reference ellipsoid at the specified latitude.
256 *
257 * @param phi The local latitude (in radians).
258 * @return The radius of curvature in the meridian (in meters).
259 */
260 public double meridionalRadiusOfCurvature(final double phi) {
261 return verticalRadiusOfCurvature(phi)
262 / (1.0 + eb2 * sqr(Math.cos(phi)));
263 }
264
265 /**
266 * Returns isometric latitude of phi on given first eccentricity (e)
267 * @param phi The local latitude (radians).
268 * @return isometric latitude of phi on first eccentricity (e)
269 */
270 public double latitudeIsometric(double phi, double e) {
271 double v1 = 1-e*Math.sin(phi);
272 double v2 = 1+e*Math.sin(phi);
273 return Math.log(Math.tan(Math.PI/4+phi/2)*Math.pow(v1/v2, e/2));
274 }
275
276 /**
277 * Returns isometric latitude of phi on first eccentricity (e)
278 * @param phi The local latitude (radians).
279 * @return isometric latitude of phi on first eccentricity (e)
280 */
281 public double latitudeIsometric(double phi) {
282 double v1 = 1-e*Math.sin(phi);
283 double v2 = 1+e*Math.sin(phi);
284 return Math.log(Math.tan(Math.PI/4+phi/2)*Math.pow(v1/v2, e/2));
285 }
286
287 /**
288 * Returns geographic latitude of isometric latitude of first eccentricity (e)
289 * and epsilon precision
290 * @return geographic latitude of isometric latitude of first eccentricity (e)
291 * and epsilon precision
292 */
293 public double latitude(double latIso, double e, double epsilon) {
294 double lat0 = 2*Math.atan(Math.exp(latIso))-Math.PI/2;
295 double lati = lat0;
296 double lati1 = 1.0; // random value to start the iterative processus
297 while (Math.abs(lati1-lati) >= epsilon) {
298 lati = lati1;
299 double v1 = 1+e*Math.sin(lati);
300 double v2 = 1-e*Math.sin(lati);
301 lati1 = 2*Math.atan(Math.pow(v1/v2, e/2)*Math.exp(latIso))-Math.PI/2;
302 }
303 return lati1;
304 }
305
306 /**
307 * convert cartesian coordinates to ellipsoidal coordinates
308 *
309 * @param xyz the coordinates in meters (X, Y, Z)
310 * @return The corresponding latitude and longitude in degrees
311 */
312 public LatLon cart2LatLon(double[] xyz) {
313 return cart2LatLon(xyz, 1e-11);
314 }
315
316 public LatLon cart2LatLon(double[] xyz, double epsilon) {
317 double norm = Math.sqrt(xyz[0] * xyz[0] + xyz[1] * xyz[1]);
318 double lg = 2.0 * Math.atan(xyz[1] / (xyz[0] + norm));
319 double lt = Math.atan(xyz[2] / (norm * (1.0 - (a * e2 / Math.sqrt(xyz[0] * xyz[0] + xyz[1] * xyz[1] + xyz[2] * xyz[2])))));
320 double delta = 1.0;
321 while (delta > epsilon) {
322 double s2 = Math.sin(lt);
323 s2 *= s2;
324 double l = Math.atan((xyz[2] / norm)
325 / (1.0 - (a * e2 * Math.cos(lt) / (norm * Math.sqrt(1.0 - e2 * s2)))));
326 delta = Math.abs(l - lt);
327 lt = l;
328 }
329 return new LatLon(Math.toDegrees(lt), Math.toDegrees(lg));
330 }
331
332 /**
333 * convert ellipsoidal coordinates to cartesian coordinates
334 *
335 * @param coord The Latitude and longitude in degrees
336 * @return the corresponding (X, Y Z) cartesian coordinates in meters.
337 */
338 public double[] latLon2Cart(LatLon coord) {
339 double phi = Math.toRadians(coord.lat());
340 double lambda = Math.toRadians(coord.lon());
341
342 double Rn = a / Math.sqrt(1 - e2 * Math.pow(Math.sin(phi), 2));
343 double[] xyz = new double[3];
344 xyz[0] = Rn * Math.cos(phi) * Math.cos(lambda);
345 xyz[1] = Rn * Math.cos(phi) * Math.sin(lambda);
346 xyz[2] = Rn * (1 - e2) * Math.sin(phi);
347
348 return xyz;
349 }
350}
Note: See TracBrowser for help on using the repository browser.