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

Last change on this file since 9139 was 9124, checked in by bastiK, 8 years ago

guess resonable projection bounds, when they haven't been specified (see #12186)

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