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;
008import org.openstreetmap.josm.tools.CheckParameterUtil;
009
010/**
011 * Transverse Mercator Projection (EPSG code 9807). This
012 * is a cylindrical projection, in which the cylinder has been rotated 90°.
013 * Instead of being tangent to the equator (or to an other standard latitude),
014 * it is tangent to a central meridian. Deformation are more important as we
015 * are going futher from the central meridian. The Transverse Mercator
016 * projection is appropriate for region wich have a greater extent north-south
017 * than east-west.
018 * <p>
019 *
020 * The elliptical equations used here are series approximations, and their accuracy
021 * decreases as points move farther from the central meridian of the projection.
022 * The forward equations here are accurate to a less than a mm &plusmn;10 degrees from
023 * the central meridian, a few mm &plusmn;15 degrees from the
024 * central meridian and a few cm &plusmn;20 degrees from the central meridian.
025 * The spherical equations are not approximations and should always give the
026 * correct values.
027 * <p>
028 *
029 * There are a number of versions of the transverse mercator projection
030 * including the Universal (UTM) and Modified (MTM) Transverses Mercator
031 * projections. In these cases the earth is divided into zones. For the UTM
032 * the zones are 6 degrees wide, numbered from 1 to 60 proceeding east from
033 * 180 degrees longitude, and between lats 84 degrees North and 80
034 * degrees South. The central meridian is taken as the center of the zone
035 * and the latitude of origin is the equator. A scale factor of 0.9996 and
036 * false easting of 500000m is used for all zones and a false northing of 10000000m
037 * is used for zones in the southern hemisphere.
038 * <p>
039 *
040 * NOTE: formulas used below are not those of Snyder, but rather those
041 *       from the {@code proj4} package of the USGS survey, which
042 *       have been reproduced verbatim. USGS work is acknowledged here.
043 * <p>
044 *
045 * This class has been derived from the implementation of the Geotools project;
046 * git 8cbf52d, org.geotools.referencing.operation.projection.TransverseMercator
047 * at the time of migration.
048 * <p>
049 *
050 * <b>References:</b>
051 * <ul>
052 *   <li> Proj-4.4.6 available at <A HREF="http://www.remotesensing.org/proj">www.remotesensing.org/proj</A><br>
053 *        Relevent files are: {@code PJ_tmerc.c}, {@code pj_mlfn.c}, {@code pj_fwd.c} and {@code pj_inv.c}.</li>
054 *   <li> John P. Snyder (Map Projections - A Working Manual,
055 *        U.S. Geological Survey Professional Paper 1395, 1987).</li>
056 *   <li> "Coordinate Conversions and Transformations including Formulas",
057 *        EPSG Guidence Note Number 7, Version 19.</li>
058 * </ul>
059 *
060 * @author André Gosselin
061 * @author Martin Desruisseaux (PMO, IRD)
062 * @author Rueben Schulz
063 *
064 * @see <A HREF="http://mathworld.wolfram.com/MercatorProjection.html">Transverse Mercator projection on MathWorld</A>
065 * @see <A HREF="http://www.remotesensing.org/geotiff/proj_list/transverse_mercator.html">"Transverse_Mercator" on RemoteSensing.org</A>
066 */
067public class TransverseMercator extends AbstractProj {
068
069    /**
070     * Contants used for the forward and inverse transform for the eliptical
071     * case of the Transverse Mercator.
072     */
073    private static final double FC1 = 1.00000000000000000000000,  // 1/1
074                                FC2 = 0.50000000000000000000000,  // 1/2
075                                FC3 = 0.16666666666666666666666,  // 1/6
076                                FC4 = 0.08333333333333333333333,  // 1/12
077                                FC5 = 0.05000000000000000000000,  // 1/20
078                                FC6 = 0.03333333333333333333333,  // 1/30
079                                FC7 = 0.02380952380952380952380,  // 1/42
080                                FC8 = 0.01785714285714285714285;  // 1/56
081
082    /**
083     * Maximum difference allowed when comparing real numbers.
084     */
085    private static final double EPSILON = 1E-6;
086
087    /**
088     * A derived quantity of excentricity, computed by <code>e'² = (a²-b²)/b² = es/(1-es)</code>
089     * where <var>a</var> is the semi-major axis length and <var>b</var> is the semi-minor axis
090     * length.
091     */
092    private double eb2;
093
094    /**
095     * Latitude of origin in <u>radians</u>. Default value is 0, the equator.
096     * This is called '<var>phi0</var>' in Snyder.
097     * <p>
098     * <strong>Consider this field as final</strong>. It is not final only
099     * because some classes need to modify it at construction time.
100     */
101    protected double latitudeOfOrigin;
102
103    /**
104     * Meridian distance at the {@code latitudeOfOrigin}.
105     * Used for calculations for the ellipsoid.
106     */
107    private double ml0;
108
109    @Override
110    public String getName() {
111        return tr("Transverse Mercator");
112    }
113
114    @Override
115    public String getProj4Id() {
116        return "tmerc";
117    }
118
119    @Override
120    public void initialize(ProjParameters params) throws ProjectionConfigurationException {
121        super.initialize(params);
122        CheckParameterUtil.ensureParameterNotNull(params, "params");
123        CheckParameterUtil.ensureParameterNotNull(params.ellps, "params.ellps");
124        eb2 = params.ellps.eb2;
125        latitudeOfOrigin = params.lat0 == null ? 0 : Math.toRadians(params.lat0);
126        ml0 = mlfn(latitudeOfOrigin, Math.sin(latitudeOfOrigin), Math.cos(latitudeOfOrigin));
127    }
128
129    @Override
130    public double[] project(double y, double x) {
131        double sinphi = Math.sin(y);
132        double cosphi = Math.cos(y);
133
134        double t = (Math.abs(cosphi) > EPSILON) ? sinphi/cosphi : 0;
135        t *= t;
136        double al = cosphi*x;
137        double als = al*al;
138        al /= Math.sqrt(1.0 - e2 * sinphi*sinphi);
139        double n = eb2 * cosphi*cosphi;
140
141        /* NOTE: meridinal distance at latitudeOfOrigin is always 0 */
142        y = mlfn(y, sinphi, cosphi) - ml0 +
143            sinphi * al * x *
144            FC2 * (1.0 +
145            FC4 * als * (5.0 - t + n*(9.0 + 4.0*n) +
146            FC6 * als * (61.0 + t * (t - 58.0) + n*(270.0 - 330.0*t) +
147            FC8 * als * (1385.0 + t * (t*(543.0 - t) - 3111.0)))));
148
149        x = al*(FC1 + FC3 * als*(1.0 - t + n +
150            FC5 * als * (5.0 + t*(t - 18.0) + n*(14.0 - 58.0*t) +
151            FC7 * als * (61.0+ t*(t*(179.0 - t) - 479.0)))));
152
153        return new double[] {x, y};
154    }
155
156    @Override
157    public double[] invproject(double x, double y) {
158        double phi = invMlfn(ml0 + y);
159
160        if (Math.abs(phi) >= Math.PI/2) {
161            y = y < 0.0 ? -(Math.PI/2) : (Math.PI/2);
162            x = 0.0;
163        } else {
164            double sinphi = Math.sin(phi);
165            double cosphi = Math.cos(phi);
166            double t = (Math.abs(cosphi) > EPSILON) ? sinphi/cosphi : 0.0;
167            double n = eb2 * cosphi*cosphi;
168            double con = 1.0 - e2 * sinphi*sinphi;
169            double d = x * Math.sqrt(con);
170            con *= t;
171            t *= t;
172            double ds = d*d;
173
174            y = phi - (con*ds / (1.0 - e2)) *
175                FC2 * (1.0 - ds *
176                FC4 * (5.0 + t*(3.0 - 9.0*n) + n*(1.0 - 4*n) - ds *
177                FC6 * (61.0 + t*(90.0 - 252.0*n + 45.0*t) + 46.0*n - ds *
178                FC8 * (1385.0 + t*(3633.0 + t*(4095.0 + 1574.0*t))))));
179
180            x = d*(FC1 - ds * FC3 * (1.0 + 2.0*t + n -
181                ds*FC5*(5.0 + t*(28.0 + 24* t + 8.0*n) + 6.0*n -
182                ds*FC7*(61.0 + t*(662.0 + t*(1320.0 + 720.0*t))))))/cosphi;
183        }
184        return new double[] {y, x};
185    }
186
187    @Override
188    public Bounds getAlgorithmBounds() {
189        return new Bounds(-89, -7, 89, 7, false);
190    }
191}