source: josm/trunk/src/org/openstreetmap/josm/data/projection/proj/TransverseMercator.java@ 12445

Last change on this file since 12445 was 12013, checked in by bastiK, 7 years ago

see #11889 - backport improved version of Math.toDegrees and Math.toRadians from Java 9

  • Property svn:eol-style set to native
File size: 8.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.projection.proj;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import org.openstreetmap.josm.data.Bounds;
7import org.openstreetmap.josm.data.projection.ProjectionConfigurationException;
8import org.openstreetmap.josm.tools.CheckParameterUtil;
9import org.openstreetmap.josm.tools.Utils;
10
11/**
12 * Transverse Mercator Projection (EPSG code 9807). This
13 * is a cylindrical projection, in which the cylinder has been rotated 90°.
14 * Instead of being tangent to the equator (or to an other standard latitude),
15 * it is tangent to a central meridian. Deformation are more important as we
16 * are going futher from the central meridian. The Transverse Mercator
17 * projection is appropriate for region wich have a greater extent north-south
18 * than east-west.
19 * <p>
20 *
21 * The elliptical equations used here are series approximations, and their accuracy
22 * decreases as points move farther from the central meridian of the projection.
23 * The forward equations here are accurate to a less than a mm &plusmn;10 degrees from
24 * the central meridian, a few mm &plusmn;15 degrees from the
25 * central meridian and a few cm &plusmn;20 degrees from the central meridian.
26 * The spherical equations are not approximations and should always give the
27 * correct values.
28 * <p>
29 *
30 * There are a number of versions of the transverse mercator projection
31 * including the Universal (UTM) and Modified (MTM) Transverses Mercator
32 * projections. In these cases the earth is divided into zones. For the UTM
33 * the zones are 6 degrees wide, numbered from 1 to 60 proceeding east from
34 * 180 degrees longitude, and between lats 84 degrees North and 80
35 * degrees South. The central meridian is taken as the center of the zone
36 * and the latitude of origin is the equator. A scale factor of 0.9996 and
37 * false easting of 500000m is used for all zones and a false northing of 10000000m
38 * is used for zones in the southern hemisphere.
39 * <p>
40 *
41 * NOTE: formulas used below are not those of Snyder, but rather those
42 * from the {@code proj4} package of the USGS survey, which
43 * have been reproduced verbatim. USGS work is acknowledged here.
44 * <p>
45 *
46 * This class has been derived from the implementation of the Geotools project;
47 * git 8cbf52d, org.geotools.referencing.operation.projection.TransverseMercator
48 * at the time of migration.
49 * <p>
50 * The non-standard parameter <code>gamma</code> has been added as a method
51 * to rotate the projected coordinates by a certain angle (clockwise, see
52 * {@link ObliqueMercator}).
53 * <p>
54 * <b>References:</b>
55 * <ul>
56 * <li> Proj-4.4.6 available at <A HREF="http://www.remotesensing.org/proj">www.remotesensing.org/proj</A><br>
57 * Relevent files are: {@code PJ_tmerc.c}, {@code pj_mlfn.c}, {@code pj_fwd.c} and {@code pj_inv.c}.</li>
58 * <li> John P. Snyder (Map Projections - A Working Manual,
59 * U.S. Geological Survey Professional Paper 1395, 1987).</li>
60 * <li> "Coordinate Conversions and Transformations including Formulas",
61 * EPSG Guidence Note Number 7, Version 19.</li>
62 * </ul>
63 *
64 * @author André Gosselin
65 * @author Martin Desruisseaux (PMO, IRD)
66 * @author Rueben Schulz
67 *
68 * @see <A HREF="http://mathworld.wolfram.com/MercatorProjection.html">Transverse Mercator projection on MathWorld</A>
69 * @see <A HREF="http://www.remotesensing.org/geotiff/proj_list/transverse_mercator.html">"Transverse_Mercator" on RemoteSensing.org</A>
70 */
71public class TransverseMercator extends AbstractProj {
72
73 /**
74 * Contants used for the forward and inverse transform for the eliptical
75 * case of the Transverse Mercator.
76 */
77 private static final double FC1 = 1.00000000000000000000000, // 1/1
78 FC2 = 0.50000000000000000000000, // 1/2
79 FC3 = 0.16666666666666666666666, // 1/6
80 FC4 = 0.08333333333333333333333, // 1/12
81 FC5 = 0.05000000000000000000000, // 1/20
82 FC6 = 0.03333333333333333333333, // 1/30
83 FC7 = 0.02380952380952380952380, // 1/42
84 FC8 = 0.01785714285714285714285; // 1/56
85
86 /**
87 * Maximum difference allowed when comparing real numbers.
88 */
89 private static final double EPSILON = 1E-6;
90
91 /**
92 * A derived quantity of excentricity, computed by <code>e'² = (a²-b²)/b² = es/(1-es)</code>
93 * where <var>a</var> is the semi-major axis length and <var>b</var> is the semi-minor axis
94 * length.
95 */
96 private double eb2;
97
98 /**
99 * Latitude of origin in <u>radians</u>. Default value is 0, the equator.
100 * This is called '<var>phi0</var>' in Snyder.
101 * <p>
102 * <strong>Consider this field as final</strong>. It is not final only
103 * because some classes need to modify it at construction time.
104 */
105 protected double latitudeOfOrigin;
106
107 /**
108 * Meridian distance at the {@code latitudeOfOrigin}.
109 * Used for calculations for the ellipsoid.
110 */
111 private double ml0;
112
113 /**
114 * The rectified bearing of the central line, in radians.
115 */
116 protected double rectifiedGridAngle;
117
118 /**
119 * Sine and Cosine values for the coordinate system rotation angle
120 */
121 private double sinrot, cosrot;
122
123 @Override
124 public String getName() {
125 return tr("Transverse Mercator");
126 }
127
128 @Override
129 public String getProj4Id() {
130 return "tmerc";
131 }
132
133 @Override
134 public void initialize(ProjParameters params) throws ProjectionConfigurationException {
135 super.initialize(params);
136 CheckParameterUtil.ensureParameterNotNull(params, "params");
137 CheckParameterUtil.ensureParameterNotNull(params.ellps, "params.ellps");
138 eb2 = params.ellps.eb2;
139 latitudeOfOrigin = params.lat0 == null ? 0 : Utils.toRadians(params.lat0);
140 ml0 = mlfn(latitudeOfOrigin, Math.sin(latitudeOfOrigin), Math.cos(latitudeOfOrigin));
141
142 if (params.gamma != null) {
143 rectifiedGridAngle = Utils.toRadians(params.gamma);
144 } else {
145 rectifiedGridAngle = 0.0;
146 }
147 sinrot = Math.sin(rectifiedGridAngle);
148 cosrot = Math.cos(rectifiedGridAngle);
149
150 }
151
152 @Override
153 public double[] project(double y, double x) {
154 double sinphi = Math.sin(y);
155 double cosphi = Math.cos(y);
156 double u, v;
157
158 double t = (Math.abs(cosphi) > EPSILON) ? sinphi/cosphi : 0;
159 t *= t;
160 double al = cosphi*x;
161 double als = al*al;
162 al /= Math.sqrt(1.0 - e2 * sinphi*sinphi);
163 double n = eb2 * cosphi*cosphi;
164
165 /* NOTE: meridinal distance at latitudeOfOrigin is always 0 */
166 y = mlfn(y, sinphi, cosphi) - ml0 +
167 sinphi * al * x *
168 FC2 * (1.0 +
169 FC4 * als * (5.0 - t + n*(9.0 + 4.0*n) +
170 FC6 * als * (61.0 + t * (t - 58.0) + n*(270.0 - 330.0*t) +
171 FC8 * als * (1385.0 + t * (t*(543.0 - t) - 3111.0)))));
172
173 x = al*(FC1 + FC3 * als*(1.0 - t + n +
174 FC5 * als * (5.0 + t*(t - 18.0) + n*(14.0 - 58.0*t) +
175 FC7 * als * (61.0+ t*(t*(179.0 - t) - 479.0)))));
176
177 u = y;
178 v = x;
179 x = v * cosrot + u * sinrot;
180 y = u * cosrot - v * sinrot;
181
182 return new double[] {x, y};
183 }
184
185 @Override
186 public double[] invproject(double x, double y) {
187 double v = x * cosrot - y * sinrot;
188 double u = y * cosrot + x * sinrot;
189 x = v;
190 y = u;
191
192 double phi = invMlfn(ml0 + y);
193
194 if (Math.abs(phi) >= Math.PI/2) {
195 y = y < 0.0 ? -(Math.PI/2) : (Math.PI/2);
196 x = 0.0;
197 } else {
198 double sinphi = Math.sin(phi);
199 double cosphi = Math.cos(phi);
200 double t = (Math.abs(cosphi) > EPSILON) ? sinphi/cosphi : 0.0;
201 double n = eb2 * cosphi*cosphi;
202 double con = 1.0 - e2 * sinphi*sinphi;
203 double d = x * Math.sqrt(con);
204 con *= t;
205 t *= t;
206 double ds = d*d;
207
208 y = phi - (con*ds / (1.0 - e2)) *
209 FC2 * (1.0 - ds *
210 FC4 * (5.0 + t*(3.0 - 9.0*n) + n*(1.0 - 4*n) - ds *
211 FC6 * (61.0 + t*(90.0 - 252.0*n + 45.0*t) + 46.0*n - ds *
212 FC8 * (1385.0 + t*(3633.0 + t*(4095.0 + 1574.0*t))))));
213
214 x = d*(FC1 - ds * FC3 * (1.0 + 2.0*t + n -
215 ds*FC5*(5.0 + t*(28.0 + 24* t + 8.0*n) + 6.0*n -
216 ds*FC7*(61.0 + t*(662.0 + t*(1320.0 + 720.0*t))))))/cosphi;
217 }
218 return new double[] {y, x};
219 }
220
221 @Override
222 public Bounds getAlgorithmBounds() {
223 return new Bounds(-89, -7, 89, 7, false);
224 }
225}
Note: See TracBrowser for help on using the repository browser.