source: josm/trunk/src/org/openstreetmap/josm/data/projection/proj/SwissObliqueMercator.java@ 8509

Last change on this file since 8509 was 8509, checked in by Don-vip, 9 years ago

fix many checkstyle violations

  • Property svn:eol-style set to native
File size: 3.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.projection.proj;
3
4import static java.lang.Math.PI;
5import static java.lang.Math.abs;
6import static java.lang.Math.asin;
7import static java.lang.Math.atan;
8import static java.lang.Math.atan2;
9import static java.lang.Math.cos;
10import static java.lang.Math.exp;
11import static java.lang.Math.log;
12import static java.lang.Math.pow;
13import static java.lang.Math.sin;
14import static java.lang.Math.sqrt;
15import static java.lang.Math.tan;
16import static java.lang.Math.toRadians;
17import static org.openstreetmap.josm.tools.I18n.tr;
18
19import org.openstreetmap.josm.data.projection.Ellipsoid;
20import org.openstreetmap.josm.data.projection.ProjectionConfigurationException;
21
22/**
23 * Projection for the SwissGrid CH1903 / L03, see <a href="https://en.wikipedia.org/wiki/Swiss_coordinate_system">Wikipedia article</a>.<br>
24 *
25 * Calculations were originally based on
26 * <a href="http://www.swisstopo.admin.ch/internet/swisstopo/en/home/topics/survey/sys/refsys/switzerland.parsysrelated1.37696.downloadList.12749.DownloadFile.tmp/ch1903wgs84en.pdf">
27 * simple formula</a>.<br>
28 *
29 * August 2010 update to
30 * <a href="http://www.swisstopo.admin.ch/internet/swisstopo/en/home/topics/survey/sys/refsys/switzerland.parsysrelated1.37696.downloadList.97912.DownloadFile.tmp/swissprojectionen.pdf">
31 * this formula (rigorous formulas)</a>.
32 */
33public class SwissObliqueMercator implements Proj {
34
35 private Ellipsoid ellps;
36 private double kR;
37 private double alpha;
38 private double b0;
39 private double k;
40
41 private static final double EPSILON = 1e-11;
42
43 @Override
44 public void initialize(ProjParameters params) throws ProjectionConfigurationException {
45 if (params.lat0 == null)
46 throw new ProjectionConfigurationException(tr("Parameter ''{0}'' required.", "lat_0"));
47 ellps = params.ellps;
48 initialize(params.lat0);
49 }
50
51 private void initialize(double lat_0) {
52 double phi0 = toRadians(lat_0);
53 kR = sqrt(1 - ellps.e2) / (1 - (ellps.e2 * pow(sin(phi0), 2)));
54 alpha = sqrt(1 + (ellps.eb2 * pow(cos(phi0), 4)));
55 b0 = asin(sin(phi0) / alpha);
56 k = log(tan(PI / 4 + b0 / 2)) - alpha
57 * log(tan(PI / 4 + phi0 / 2)) + alpha * ellps.e / 2
58 * log((1 + ellps.e * sin(phi0)) / (1 - ellps.e * sin(phi0)));
59 }
60
61 @Override
62 public String getName() {
63 return tr("Swiss Oblique Mercator");
64 }
65
66 @Override
67 public String getProj4Id() {
68 return "somerc";
69 }
70
71 @Override
72 public double[] project(double phi, double lambda) {
73
74 double S = alpha * log(tan(PI / 4 + phi / 2)) - alpha * ellps.e / 2
75 * log((1 + ellps.e * sin(phi)) / (1 - ellps.e * sin(phi))) + k;
76 double b = 2 * (atan(exp(S)) - PI / 4);
77 double l = alpha * lambda;
78
79 double lb = atan2(sin(l), sin(b0) * tan(b) + cos(b0) * cos(l));
80 double bb = asin(cos(b0) * sin(b) - sin(b0) * cos(b) * cos(l));
81
82 double y = kR * lb;
83 double x = kR / 2 * log((1 + sin(bb)) / (1 - sin(bb)));
84
85 return new double[] {y, x};
86 }
87
88 @Override
89 public double[] invproject(double y, double x) {
90 double lb = y / kR;
91 double bb = 2 * (atan(exp(x / kR)) - PI / 4);
92
93 double b = asin(cos(b0) * sin(bb) + sin(b0) * cos(bb) * cos(lb));
94 double l = atan2(sin(lb), cos(b0) * cos(lb) - sin(b0) * tan(bb));
95
96 double lambda = l / alpha;
97 double phi = b;
98 double s = 0;
99
100 double prevPhi = -1000;
101 int iteration = 0;
102 // iteration to finds S and phi
103 while (abs(phi - prevPhi) > EPSILON) {
104 if (++iteration > 30)
105 throw new RuntimeException("Two many iterations");
106 prevPhi = phi;
107 s = 1 / alpha * (log(tan(PI / 4 + b / 2)) - k) + ellps.e
108 * log(tan(PI / 4 + asin(ellps.e * sin(phi)) / 2));
109 phi = 2 * atan(exp(s)) - PI / 2;
110 }
111 return new double[] {phi, lambda};
112 }
113}
Note: See TracBrowser for help on using the repository browser.