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

Last change on this file since 8390 was 8378, checked in by Don-vip, 9 years ago

fix copyright/license headers globally

  • Property svn:eol-style set to native
File size: 9.5 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 * Clarke 1866 ellipsoid
18 */
19 public static final Ellipsoid clarke1866 = Ellipsoid.create_a_b(6378206.4, 6356583.8);
20
21 /**
22 * Clarke 1880 IGN (French national geographic institute)
23 */
24 public static final Ellipsoid clarkeIGN = Ellipsoid.create_a_b(6378249.2, 6356515.0);
25
26 /**
27 * Hayford's ellipsoid 1909 (ED50 system)<br>
28 * Proj.4 code: intl
29 */
30 public static final Ellipsoid hayford = Ellipsoid.create_a_rf(6378388.0, 297.0);
31
32 /**
33 * GRS67 ellipsoid
34 */
35 public static final Ellipsoid GRS67 = Ellipsoid.create_a_rf(6378160.0, 298.247167472);
36
37 /**
38 * GRS80 ellipsoid
39 */
40 public static final Ellipsoid GRS80 = Ellipsoid.create_a_rf(6378137.0, 298.257222101);
41
42 /**
43 * WGS84 ellipsoid
44 */
45 public static final Ellipsoid WGS84 = Ellipsoid.create_a_rf(6378137.0, 298.257223563);
46
47 /**
48 * Bessel 1841 ellipsoid
49 */
50 public static final Ellipsoid Bessel1841 = Ellipsoid.create_a_rf(6377397.155, 299.1528128);
51
52 /**
53 * half long axis
54 */
55 public final double a;
56
57 /**
58 * half short axis
59 */
60 public final double b;
61
62 /**
63 * first eccentricity
64 */
65 public final double e;
66
67 /**
68 * first eccentricity squared
69 */
70 public final double e2;
71
72 /**
73 * square of the second eccentricity
74 */
75 public final double eb2;
76
77 /**
78 * private constructur - use one of the create_* methods
79 *
80 * @param a semimajor radius of the ellipsoid axis
81 * @param b semiminor radius of the ellipsoid axis
82 * @param e first eccentricity of the ellipsoid ( = sqrt((a*a - b*b)/(a*a)))
83 * @param e2 first eccentricity squared
84 * @param eb2 square of the second eccentricity
85 */
86 private Ellipsoid(double a, double b, double e, double e2, double eb2) {
87 this.a = a;
88 this.b = b;
89 this.e = e;
90 this.e2 = e2;
91 this.eb2 = eb2;
92 }
93
94 /**
95 * create a new ellipsoid
96 *
97 * @param a semimajor radius of the ellipsoid axis (in meters)
98 * @param b semiminor radius of the ellipsoid axis (in meters)
99 * @return the new ellipsoid
100 */
101 public static Ellipsoid create_a_b(double a, double b) {
102 double e2 = (a*a - b*b) / (a*a);
103 double e = Math.sqrt(e2);
104 double eb2 = e2 / (1.0 - e2);
105 return new Ellipsoid(a, b, e, e2, eb2);
106 }
107
108 /**
109 * create a new ellipsoid
110 *
111 * @param a semimajor radius of the ellipsoid axis (in meters)
112 * @param es first eccentricity squared
113 * @return the new ellipsoid
114 */
115 public static Ellipsoid create_a_es(double a, double es) {
116 double b = a * Math.sqrt(1.0 - es);
117 double e = Math.sqrt(es);
118 double eb2 = es / (1.0 - es);
119 return new Ellipsoid(a, b, e, es, eb2);
120 }
121
122 /**
123 * create a new ellipsoid
124 *
125 * @param a semimajor radius of the ellipsoid axis (in meters)
126 * @param f flattening ( = (a - b) / a)
127 * @return the new ellipsoid
128 */
129 public static Ellipsoid create_a_f(double a, double f) {
130 double b = a * (1.0 - f);
131 double e2 = f * (2 - f);
132 double e = Math.sqrt(e2);
133 double eb2 = e2 / (1.0 - e2);
134 return new Ellipsoid(a, b, e, e2, eb2);
135 }
136
137 /**
138 * create a new ellipsoid
139 *
140 * @param a semimajor radius of the ellipsoid axis (in meters)
141 * @param rf inverse flattening
142 * @return the new ellipsoid
143 */
144 public static Ellipsoid create_a_rf(double a, double rf) {
145 return create_a_f(a, 1.0 / rf);
146 }
147
148 @Override
149 public String toString() {
150 return "Ellipsoid{a="+a+", b="+b+"}";
151 }
152
153 /**
154 * Returns the <i>radius of curvature in the prime vertical</i>
155 * for this reference ellipsoid at the specified latitude.
156 *
157 * @param phi The local latitude (radians).
158 * @return The radius of curvature in the prime vertical (meters).
159 */
160 public double verticalRadiusOfCurvature(final double phi) {
161 return a / Math.sqrt(1.0 - (e2 * sqr(Math.sin(phi))));
162 }
163
164 private static double sqr(final double x) {
165 return x * x;
166 }
167
168 /**
169 * Returns the meridional arc, the true meridional distance on the
170 * ellipsoid from the equator to the specified latitude, in meters.
171 *
172 * @param phi The local latitude (in radians).
173 * @return The meridional arc (in meters).
174 */
175 public double meridionalArc(final double phi) {
176 final double sin2Phi = Math.sin(2.0 * phi);
177 final double sin4Phi = Math.sin(4.0 * phi);
178 final double sin6Phi = Math.sin(6.0 * phi);
179 final double sin8Phi = Math.sin(8.0 * phi);
180 // TODO . calculate 'f'
181 //double f = 1.0 / 298.257222101; // GRS80
182 double f = 1.0 / 298.257223563; // WGS84
183 final double n = f / (2.0 - f);
184 final double n2 = n * n;
185 final double n3 = n2 * n;
186 final double n4 = n3 * n;
187 final double n5 = n4 * n;
188 final double n1n2 = n - n2;
189 final double n2n3 = n2 - n3;
190 final double n3n4 = n3 - n4;
191 final double n4n5 = n4 - n5;
192 final double ap = a * (1.0 - n + (5.0 / 4.0) * (n2n3) + (81.0 / 64.0) * (n4n5));
193 final double bp = (3.0 / 2.0) * a * (n1n2 + (7.0 / 8.0) * (n3n4) + (55.0 / 64.0) * n5);
194 final double cp = (15.0 / 16.0) * a * (n2n3 + (3.0 / 4.0) * (n4n5));
195 final double dp = (35.0 / 48.0) * a * (n3n4 + (11.0 / 16.0) * n5);
196 final double ep = (315.0 / 512.0) * a * (n4n5);
197 return ap * phi - bp * sin2Phi + cp * sin4Phi - dp * sin6Phi + ep * sin8Phi;
198 }
199
200 /**
201 * Returns the <i>radius of curvature in the meridian</i>
202 * for this reference ellipsoid at the specified latitude.
203 *
204 * @param phi The local latitude (in radians).
205 * @return The radius of curvature in the meridian (in meters).
206 */
207 public double meridionalRadiusOfCurvature(final double phi) {
208 return verticalRadiusOfCurvature(phi)
209 / (1.0 + eb2 * sqr(Math.cos(phi)));
210 }
211
212 /**
213 * Returns isometric latitude of phi on given first eccentricity (e)
214 * @param phi The local latitude (radians).
215 * @return isometric latitude of phi on first eccentricity (e)
216 */
217 public double latitudeIsometric(double phi, double e) {
218 double v1 = 1-e*Math.sin(phi);
219 double v2 = 1+e*Math.sin(phi);
220 return Math.log(Math.tan(Math.PI/4+phi/2)*Math.pow(v1/v2,e/2));
221 }
222
223 /**
224 * Returns isometric latitude of phi on first eccentricity (e)
225 * @param phi The local latitude (radians).
226 * @return isometric latitude of phi on first eccentricity (e)
227 */
228 public double latitudeIsometric(double phi) {
229 double v1 = 1-e*Math.sin(phi);
230 double v2 = 1+e*Math.sin(phi);
231 return Math.log(Math.tan(Math.PI/4+phi/2)*Math.pow(v1/v2,e/2));
232 }
233
234 /**
235 * Returns geographic latitude of isometric latitude of first eccentricity (e)
236 * and epsilon precision
237 * @return geographic latitude of isometric latitude of first eccentricity (e)
238 * and epsilon precision
239 */
240 public double latitude(double latIso, double e, double epsilon) {
241 double lat0 = 2*Math.atan(Math.exp(latIso))-Math.PI/2;
242 double lati = lat0;
243 double lati1 = 1.0; // random value to start the iterative processus
244 while(Math.abs(lati1-lati)>=epsilon) {
245 lati = lati1;
246 double v1 = 1+e*Math.sin(lati);
247 double v2 = 1-e*Math.sin(lati);
248 lati1 = 2*Math.atan(Math.pow(v1/v2,e/2)*Math.exp(latIso))-Math.PI/2;
249 }
250 return lati1;
251 }
252
253 /**
254 * convert cartesian coordinates to ellipsoidal coordinates
255 *
256 * @param xyz the coordinates in meters (X, Y, Z)
257 * @return The corresponding latitude and longitude in degrees
258 */
259 public LatLon cart2LatLon(double[] xyz) {
260 return cart2LatLon(xyz, 1e-11);
261 }
262
263 public LatLon cart2LatLon(double[] xyz, double epsilon) {
264 double norm = Math.sqrt(xyz[0] * xyz[0] + xyz[1] * xyz[1]);
265 double lg = 2.0 * Math.atan(xyz[1] / (xyz[0] + norm));
266 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])))));
267 double delta = 1.0;
268 while (delta > epsilon) {
269 double s2 = Math.sin(lt);
270 s2 *= s2;
271 double l = Math.atan((xyz[2] / norm)
272 / (1.0 - (a * e2 * Math.cos(lt) / (norm * Math.sqrt(1.0 - e2 * s2)))));
273 delta = Math.abs(l - lt);
274 lt = l;
275 }
276 return new LatLon(Math.toDegrees(lt), Math.toDegrees(lg));
277 }
278
279 /**
280 * convert ellipsoidal coordinates to cartesian coordinates
281 *
282 * @param coord The Latitude and longitude in degrees
283 * @return the corresponding (X, Y Z) cartesian coordinates in meters.
284 */
285 public double[] latLon2Cart(LatLon coord) {
286 double phi = Math.toRadians(coord.lat());
287 double lambda = Math.toRadians(coord.lon());
288
289 double Rn = a / Math.sqrt(1 - e2 * Math.pow(Math.sin(phi), 2));
290 double[] xyz = new double[3];
291 xyz[0] = Rn * Math.cos(phi) * Math.cos(lambda);
292 xyz[1] = Rn * Math.cos(phi) * Math.sin(lambda);
293 xyz[2] = Rn * (1 - e2) * Math.sin(phi);
294
295 return xyz;
296 }
297}
Note: See TracBrowser for help on using the repository browser.