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