001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.projection.proj;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import org.openstreetmap.josm.data.Bounds;
007import org.openstreetmap.josm.data.projection.ProjectionConfigurationException;
008
009/**
010 * The polar case of the stereographic projection.
011 * <p>
012 * In the proj.4 library, the code "stere" covers several variants of the
013 * Stereographic projection, depending on the latitude of natural origin
014 * (parameter lat_0).
015 * <p>
016 *
017 * In this file, only the polar case is implemented. This corresponds to
018 * EPSG:9810 (Polar Stereographic Variant A) and EPSG:9829 (Polar Stereographic
019 * Variant B).
020 * <p>
021 *
022 * It is required, that the latitude of natural origin has the value +/-90 degrees.
023 * <p>
024 *
025 * This class has been derived from the implementation of the Geotools project;
026 * git 8cbf52d, org.geotools.referencing.operation.projection.PolarStereographic
027 * at the time of migration.
028 * <p>
029 *
030 * <b>References:</b>
031 * <ul>
032 *   <li>John P. Snyder (Map Projections - A Working Manual,<br>
033 *       U.S. Geological Survey Professional Paper 1395, 1987)</li>
034 *   <li>"Coordinate Conversions and Transformations including Formulas",<br>
035 *       EPSG Guidence Note Number 7, Version 19.</li>
036 *   <li>Gerald Evenden. <A HREF="http://members.bellatlantic.net/~vze2hc4d/proj4/sterea.pdf">
037 *       "Supplementary PROJ.4 Notes - Oblique Stereographic Alternative"</A></li>
038 *   <li>Krakiwsky, E.J., D.B. Thomson, and R.R. Steeves. 1977. A Manual
039 *       For Geodetic Coordinate Transformations in the Maritimes.
040 *       Geodesy and Geomatics Engineering, UNB. Technical Report No. 48.</li>
041 *   <li>Thomson, D.B., M.P. Mepham and R.R. Steeves. 1977.
042 *       The Stereographic Double Projection.
043 *       Geodesy and Geomatics Engineereng, UNB. Technical Report No. 46.</li>
044 * </ul>
045 *
046 * @author André Gosselin
047 * @author Martin Desruisseaux (PMO, IRD)
048 * @author Rueben Schulz
049 *
050 * @see <A HREF="http://mathworld.wolfram.com/StereographicProjection.html">Stereographic projection on MathWorld</A>
051 * @see <A HREF="http://www.remotesensing.org/geotiff/proj_list/polar_stereographic.html">Polar_Stereographic</A>
052 * @see <A HREF="http://www.remotesensing.org/geotiff/proj_list/oblique_stereographic.html">Oblique_Stereographic</A>
053 * @see <A HREF="http://www.remotesensing.org/geotiff/proj_list/stereographic.html">Stereographic</A>
054 * @see <A HREF="http://www.remotesensing.org/geotiff/proj_list/random_issues.html#stereographic">Some Random Stereographic Issues</A>
055 *
056 * @see DoubleStereographic
057 * @since 9419
058 */
059public class PolarStereographic extends AbstractProj {
060    /**
061     * Maximum number of iterations for iterative computations.
062     */
063    private static final int MAXIMUM_ITERATIONS = 15;
064
065    /**
066     * Difference allowed in iterative computations.
067     */
068    private static final double ITERATION_TOLERANCE = 1E-10;
069
070    /**
071     * Maximum difference allowed when comparing real numbers.
072     */
073    private static final double EPSILON = 1E-8;
074
075    /**
076     * A constant used in the transformations.
077     */
078    private double k0;
079
080    /**
081     * {@code true} if this projection is for the south pole, or {@code false}
082     * if it is for the north pole.
083     */
084    boolean southPole;
085
086    @Override
087    public String getName() {
088        return tr("Polar Stereographic");
089    }
090
091    @Override
092    public String getProj4Id() {
093        return "stere";
094    }
095
096    @Override
097    public void initialize(ProjParameters params) throws ProjectionConfigurationException {
098        super.initialize(params);
099        if (params.lat0 == null)
100            throw new ProjectionConfigurationException(tr("Parameter ''{0}'' required.", "lat_0"));
101        if (params.lat0 != 90.0 && params.lat0 != -90.0)
102            throw new ProjectionConfigurationException(
103                    tr("Polar Stereographic: Parameter ''{0}'' must be 90 or -90.", "lat_0"));
104        // Latitude of true scale, in radians;
105        double latitudeTrueScale;
106        if (params.lat_ts == null) {
107            latitudeTrueScale = (params.lat0 < 0) ? -Math.PI/2 : Math.PI/2;
108        } else {
109            latitudeTrueScale = Math.toRadians(params.lat_ts);
110        }
111        southPole = latitudeTrueScale < 0;
112
113        // Computes coefficients.
114        double latitudeTrueScaleAbs = Math.abs(latitudeTrueScale);
115        if (Math.abs(latitudeTrueScaleAbs - Math.PI/2) >= EPSILON) {
116            final double t = Math.sin(latitudeTrueScaleAbs);
117            k0 = msfn(t, Math.cos(latitudeTrueScaleAbs)) /
118                 tsfn(latitudeTrueScaleAbs, t); // Derives from (21-32 and 21-33)
119        } else {
120            // True scale at pole (part of (21-33))
121            k0 = 2.0 / Math.sqrt(Math.pow(1+e, 1+e)*
122                                 Math.pow(1-e, 1-e));
123        }
124    }
125
126    @Override
127    public double[] project(double y, double x) {
128        final double sinlat = Math.sin(y);
129        final double coslon = Math.cos(x);
130        final double sinlon = Math.sin(x);
131        if (southPole) {
132            final double rho = k0 * tsfn(-y, -sinlat);
133            x = rho * sinlon;
134            y = rho * coslon;
135        } else {
136            final double rho = k0 * tsfn(y, sinlat);
137            x =  rho * sinlon;
138            y = -rho * coslon;
139        }
140        return new double[] {x, y};
141    }
142
143    @Override
144    public double[] invproject(double x, double y) {
145        final double rho = Math.hypot(x, y);
146        if (southPole) {
147            y = -y;
148        }
149        /*
150         * Compute latitude using iterative technique.
151         */
152        final double t = rho/k0;
153        final double halfe = e/2.0;
154        double phi0 = 0;
155        for (int i = MAXIMUM_ITERATIONS;;) {
156            final double esinphi = e * Math.sin(phi0);
157            final double phi = (Math.PI/2) - 2.0*Math.atan(t*Math.pow((1-esinphi)/(1+esinphi), halfe));
158            if (Math.abs(phi-phi0) < ITERATION_TOLERANCE) {
159                x = (Math.abs(rho) < EPSILON) ? 0.0 : Math.atan2(x, -y);
160                y = southPole ? -phi : phi;
161                break;
162            }
163            phi0 = phi;
164            if (--i < 0) {
165                throw new IllegalStateException("no convergence for x="+x+", y="+y);
166            }
167        }
168        return new double[] {y, x};
169    }
170
171    @Override
172    public Bounds getAlgorithmBounds() {
173        final double cut = 60;
174        if (southPole) {
175            return new Bounds(-90, -180, cut, 180, false);
176        } else {
177            return new Bounds(-cut, -180, 90, 180, false);
178        }
179    }
180}