001/*
002 * Import from fr.geo.convert package, a geographic coordinates converter.
003 * (https://www.i3s.unice.fr/~johan/gps/)
004 * License: GPL. For details, see LICENSE file.
005 * Copyright (C) 2002 Johan Montagnat (johan@creatis.insa-lyon.fr)
006 */
007package org.openstreetmap.josm.data.projection;
008
009import org.openstreetmap.josm.data.coor.LatLon;
010
011/**
012 * Reference ellipsoids.
013 */
014public final class Ellipsoid {
015
016    /**
017     * Airy 1830
018     */
019    public static final Ellipsoid Airy = Ellipsoid.create_a_b(6377563.396, 6356256.910);
020
021    /**
022     * Modified Airy 1849
023     */
024    public static final Ellipsoid AiryMod = Ellipsoid.create_a_b(6377340.189, 6356034.446);
025
026    /**
027     * Australian National Spheroid (Australian Natl & S. Amer. 1969)
028     * same as GRS67 Modified
029     */
030    public static final Ellipsoid AustSA = Ellipsoid.create_a_rf(6378160.0, 298.25);
031
032    /**
033     * Bessel 1841 ellipsoid
034     */
035    public static final Ellipsoid Bessel1841 = Ellipsoid.create_a_rf(6377397.155, 299.1528128);
036
037    /**
038     * Bessel 1841 (Namibia)
039     */
040    public static final Ellipsoid BesselNamibia = Ellipsoid.create_a_rf(6377483.865, 299.1528128);
041
042    /**
043     * Clarke 1866 ellipsoid
044     */
045    public static final Ellipsoid Clarke1866 = Ellipsoid.create_a_b(6378206.4, 6356583.8);
046
047    /**
048     * Clarke 1880 (modified)
049     */
050    public static final Ellipsoid Clarke1880 = Ellipsoid.create_a_rf(6378249.145, 293.4663);
051
052    /**
053     * Clarke 1880 IGN (French national geographic institute)
054     */
055    public static final Ellipsoid ClarkeIGN = Ellipsoid.create_a_b(6378249.2, 6356515.0);
056
057    /**
058     * Everest (Sabah & Sarawak)
059     */
060    public static final Ellipsoid EverestSabahSarawak = Ellipsoid.create_a_rf(6377298.556, 300.8017);
061
062    /**
063     * GRS67 ellipsoid
064     */
065    public static final Ellipsoid GRS67 = Ellipsoid.create_a_rf(6378160.0, 298.247167427);
066
067    /**
068     * GRS80 ellipsoid
069     */
070    public static final Ellipsoid GRS80 = Ellipsoid.create_a_rf(6378137.0, 298.257222101);
071
072    /**
073     * Hayford's ellipsoid 1909 (ED50 system)
074     * Also known as International 1924
075     * Proj.4 code: intl
076     */
077    public static final Ellipsoid Hayford = Ellipsoid.create_a_rf(6378388.0, 297.0);
078
079    /**
080     * Helmert 1906
081     */
082    public static final Ellipsoid Helmert = Ellipsoid.create_a_rf(6378200.0, 298.3);
083
084    /**
085     * Krassowsky 1940 ellipsoid
086     */
087    public static final Ellipsoid Krassowsky = Ellipsoid.create_a_rf(6378245.0, 298.3);
088
089    /**
090     * WGS66 ellipsoid
091     */
092    public static final Ellipsoid WGS66 = Ellipsoid.create_a_rf(6378145.0, 298.25);
093
094    /**
095     * WGS72 ellipsoid
096     */
097    public static final Ellipsoid WGS72 = Ellipsoid.create_a_rf(6378135.0, 298.26);
098
099    /**
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     * @param e first eccentricity
269     * @return isometric latitude of phi on first eccentricity (e)
270     */
271    public double latitudeIsometric(double phi, double e) {
272        double v1 = 1-e*Math.sin(phi);
273        double v2 = 1+e*Math.sin(phi);
274        return Math.log(Math.tan(Math.PI/4+phi/2)*Math.pow(v1/v2, e/2));
275    }
276
277    /**
278     * Returns isometric latitude of phi on first eccentricity (e)
279     * @param phi The local latitude (radians).
280     * @return isometric latitude of phi on first eccentricity (e)
281     */
282    public double latitudeIsometric(double phi) {
283        double v1 = 1-e*Math.sin(phi);
284        double v2 = 1+e*Math.sin(phi);
285        return Math.log(Math.tan(Math.PI/4+phi/2)*Math.pow(v1/v2, e/2));
286    }
287
288    /**
289     * Returns geographic latitude of isometric latitude of first eccentricity (e) and epsilon precision
290     * @param latIso isometric latitude
291     * @param e first eccentricity
292     * @param epsilon epsilon precision
293     * @return geographic latitude of isometric latitude of first eccentricity (e) and epsilon precision
294     */
295    public double latitude(double latIso, double e, double epsilon) {
296        double lat0 = 2*Math.atan(Math.exp(latIso))-Math.PI/2;
297        double lati = lat0;
298        double lati1 = 1.0; // random value to start the iterative processus
299        while (Math.abs(lati1-lati) >= epsilon) {
300            lati = lati1;
301            double v1 = 1+e*Math.sin(lati);
302            double v2 = 1-e*Math.sin(lati);
303            lati1 = 2*Math.atan(Math.pow(v1/v2, e/2)*Math.exp(latIso))-Math.PI/2;
304        }
305        return lati1;
306    }
307
308    /**
309     * convert cartesian coordinates to ellipsoidal coordinates
310     *
311     * @param xyz the coordinates in meters (X, Y, Z)
312     * @return The corresponding latitude and longitude in degrees
313     */
314    public LatLon cart2LatLon(double[] xyz) {
315        return cart2LatLon(xyz, 1e-11);
316    }
317
318    public LatLon cart2LatLon(double[] xyz, double epsilon) {
319        double norm = Math.sqrt(xyz[0] * xyz[0] + xyz[1] * xyz[1]);
320        double lg = 2.0 * Math.atan(xyz[1] / (xyz[0] + norm));
321        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])))));
322        double delta = 1.0;
323        while (delta > epsilon) {
324            double s2 = Math.sin(lt);
325            s2 *= s2;
326            double l = Math.atan((xyz[2] / norm)
327                    / (1.0 - (a * e2 * Math.cos(lt) / (norm * Math.sqrt(1.0 - e2 * s2)))));
328            delta = Math.abs(l - lt);
329            lt = l;
330        }
331        return new LatLon(Math.toDegrees(lt), Math.toDegrees(lg));
332    }
333
334    /**
335     * convert ellipsoidal coordinates to cartesian coordinates
336     *
337     * @param coord The Latitude and longitude in degrees
338     * @return the corresponding (X, Y Z) cartesian coordinates in meters.
339     */
340    public double[] latLon2Cart(LatLon coord) {
341        double phi = Math.toRadians(coord.lat());
342        double lambda = Math.toRadians(coord.lon());
343
344        double Rn = a / Math.sqrt(1 - e2 * Math.pow(Math.sin(phi), 2));
345        double[] xyz = new double[3];
346        xyz[0] = Rn * Math.cos(phi) * Math.cos(lambda);
347        xyz[1] = Rn * Math.cos(phi) * Math.sin(lambda);
348        xyz[2] = Rn * (1 - e2) * Math.sin(phi);
349
350        return xyz;
351    }
352}