1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.projection.proj;
|
---|
3 |
|
---|
4 | import static java.lang.Math.PI;
|
---|
5 | import static java.lang.Math.abs;
|
---|
6 | import static java.lang.Math.atan;
|
---|
7 | import static java.lang.Math.cos;
|
---|
8 | import static java.lang.Math.exp;
|
---|
9 | import static java.lang.Math.log;
|
---|
10 | import static java.lang.Math.pow;
|
---|
11 | import static java.lang.Math.sin;
|
---|
12 | import static java.lang.Math.sqrt;
|
---|
13 | import static java.lang.Math.tan;
|
---|
14 | import static java.lang.Math.toRadians;
|
---|
15 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.data.Bounds;
|
---|
18 | import org.openstreetmap.josm.data.projection.CustomProjection.Param;
|
---|
19 | import org.openstreetmap.josm.data.projection.Ellipsoid;
|
---|
20 | import org.openstreetmap.josm.data.projection.ProjectionConfigurationException;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Implementation of the Lambert Conformal Conic projection.
|
---|
24 | *
|
---|
25 | * @author Pieren
|
---|
26 | */
|
---|
27 | public class LambertConformalConic extends AbstractProj {
|
---|
28 |
|
---|
29 | protected Ellipsoid ellps;
|
---|
30 | protected double e;
|
---|
31 |
|
---|
32 | public abstract static class Parameters {
|
---|
33 | public final double latitudeOrigin;
|
---|
34 |
|
---|
35 | public Parameters(double latitudeOrigin) {
|
---|
36 | this.latitudeOrigin = latitudeOrigin;
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | public static class Parameters1SP extends Parameters {
|
---|
41 | public Parameters1SP(double latitudeOrigin) {
|
---|
42 | super(latitudeOrigin);
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | public static class Parameters2SP extends Parameters {
|
---|
47 | public final double standardParallel1;
|
---|
48 | public final double standardParallel2;
|
---|
49 |
|
---|
50 | public Parameters2SP(double latitudeOrigin, double standardParallel1, double standardParallel2) {
|
---|
51 | super(latitudeOrigin);
|
---|
52 | this.standardParallel1 = standardParallel1;
|
---|
53 | this.standardParallel2 = standardParallel2;
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | private Parameters params;
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * projection exponent
|
---|
61 | */
|
---|
62 | protected double n;
|
---|
63 | /**
|
---|
64 | * projection factor
|
---|
65 | */
|
---|
66 | protected double f;
|
---|
67 | /**
|
---|
68 | * radius of the parallel of latitude of the false origin (2SP) or at
|
---|
69 | * natural origin (1SP)
|
---|
70 | */
|
---|
71 | protected double r0;
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * precision in iterative schema
|
---|
75 | */
|
---|
76 | protected static final double epsilon = 1e-12;
|
---|
77 |
|
---|
78 | @Override
|
---|
79 | public void initialize(ProjParameters params) throws ProjectionConfigurationException {
|
---|
80 | ellps = params.ellps;
|
---|
81 | e = ellps.e;
|
---|
82 | if (params.lat0 == null)
|
---|
83 | throw new ProjectionConfigurationException(tr("Parameter ''{0}'' required.", Param.lat_0.key));
|
---|
84 | if (params.lat1 != null && params.lat2 != null) {
|
---|
85 | initialize2SP(params.lat0, params.lat1, params.lat2);
|
---|
86 | } else {
|
---|
87 | initialize1SP(params.lat0);
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Initialize for LCC with 2 standard parallels.
|
---|
93 | *
|
---|
94 | * @param lat_0 latitude of false origin (in degrees)
|
---|
95 | * @param lat_1 latitude of first standard parallel (in degrees)
|
---|
96 | * @param lat_2 latitude of second standard parallel (in degrees)
|
---|
97 | */
|
---|
98 | private void initialize2SP(double lat_0, double lat_1, double lat_2) {
|
---|
99 | this.params = new Parameters2SP(lat_0, lat_1, lat_2);
|
---|
100 |
|
---|
101 | final double m1 = m(toRadians(lat_1));
|
---|
102 | final double m2 = m(toRadians(lat_2));
|
---|
103 |
|
---|
104 | final double t1 = t(toRadians(lat_1));
|
---|
105 | final double t2 = t(toRadians(lat_2));
|
---|
106 | final double tf = t(toRadians(lat_0));
|
---|
107 |
|
---|
108 | n = (log(m1) - log(m2)) / (log(t1) - log(t2));
|
---|
109 | f = m1 / (n * pow(t1, n));
|
---|
110 | r0 = f * pow(tf, n);
|
---|
111 | }
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * Initialize for LCC with 1 standard parallel.
|
---|
115 | *
|
---|
116 | * @param lat_0 latitude of natural origin (in degrees)
|
---|
117 | */
|
---|
118 | private void initialize1SP(double lat_0) {
|
---|
119 | this.params = new Parameters1SP(lat_0);
|
---|
120 | final double lat_0_rad = toRadians(lat_0);
|
---|
121 |
|
---|
122 | final double m0 = m(lat_0_rad);
|
---|
123 | final double t0 = t(lat_0_rad);
|
---|
124 |
|
---|
125 | n = sin(lat_0_rad);
|
---|
126 | f = m0 / (n * pow(t0, n));
|
---|
127 | r0 = f * pow(t0, n);
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * auxiliary function t
|
---|
132 | * @param lat_rad latitude in radians
|
---|
133 | * @return result
|
---|
134 | */
|
---|
135 | protected double t(double lat_rad) {
|
---|
136 | return tan(PI/4 - lat_rad / 2.0)
|
---|
137 | / pow((1.0 - e * sin(lat_rad)) / (1.0 + e * sin(lat_rad)), e/2);
|
---|
138 | }
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * auxiliary function m
|
---|
142 | * @param lat_rad latitude in radians
|
---|
143 | * @return result
|
---|
144 | */
|
---|
145 | protected double m(double lat_rad) {
|
---|
146 | return cos(lat_rad) / (sqrt(1 - e * e * pow(sin(lat_rad), 2)));
|
---|
147 | }
|
---|
148 |
|
---|
149 | @Override
|
---|
150 | public String getName() {
|
---|
151 | return tr("Lambert Conformal Conic");
|
---|
152 | }
|
---|
153 |
|
---|
154 | @Override
|
---|
155 | public String getProj4Id() {
|
---|
156 | return "lcc";
|
---|
157 | }
|
---|
158 |
|
---|
159 | @Override
|
---|
160 | public double[] project(double phi, double lambda) {
|
---|
161 | lambda = normalizeLon(lambda);
|
---|
162 | double sinphi = sin(phi);
|
---|
163 | double l = (0.5*log((1+sinphi)/(1-sinphi))) - e/2*log((1+e*sinphi)/(1-e*sinphi));
|
---|
164 | double r = f*exp(-n*l);
|
---|
165 | double gamma = n*lambda;
|
---|
166 | double x = r*sin(gamma);
|
---|
167 | double y = r0 - r*cos(gamma);
|
---|
168 | return new double[] {x, y};
|
---|
169 | }
|
---|
170 |
|
---|
171 | @Override
|
---|
172 | public double[] invproject(double east, double north) {
|
---|
173 | double r = sqrt(pow(east, 2) + pow(north-r0, 2));
|
---|
174 | double gamma = atan(east / (r0-north));
|
---|
175 | double lambda = gamma/n;
|
---|
176 | double latIso = (-1/n) * log(abs(r/f));
|
---|
177 | double phi = ellps.latitude(latIso, e, epsilon);
|
---|
178 | return new double[] {phi, lambda};
|
---|
179 | }
|
---|
180 |
|
---|
181 | public final Parameters getParameters() {
|
---|
182 | return params;
|
---|
183 | }
|
---|
184 |
|
---|
185 | @Override
|
---|
186 | public Bounds getAlgorithmBounds() {
|
---|
187 | double lat;
|
---|
188 | if (params instanceof Parameters2SP) {
|
---|
189 | Parameters2SP p2p = (Parameters2SP) params;
|
---|
190 | lat = (p2p.standardParallel1 + p2p.standardParallel2) / 2;
|
---|
191 | } else {
|
---|
192 | lat = params.latitudeOrigin;
|
---|
193 | }
|
---|
194 | double minlat = Math.max(lat - 60, -89);
|
---|
195 | double maxlat = Math.min(lat + 60, 89);
|
---|
196 | return new Bounds(minlat, -85, maxlat, 85, false);
|
---|
197 | }
|
---|
198 | }
|
---|