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.projection.Ellipsoid;
|
---|
18 | import org.openstreetmap.josm.data.projection.ProjectionConfigurationException;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Implementation of the Lambert Conformal Conic projection.
|
---|
22 | *
|
---|
23 | * @author Pieren
|
---|
24 | */
|
---|
25 | public class LambertConformalConic implements Proj {
|
---|
26 |
|
---|
27 | protected Ellipsoid ellps;
|
---|
28 | protected double e;
|
---|
29 |
|
---|
30 | public abstract static class Parameters {
|
---|
31 | public final double latitudeOrigin;
|
---|
32 | public Parameters(double latitudeOrigin) {
|
---|
33 | this.latitudeOrigin = latitudeOrigin;
|
---|
34 | }
|
---|
35 | }
|
---|
36 |
|
---|
37 | public static class Parameters1SP extends Parameters {
|
---|
38 | public Parameters1SP(double latitudeOrigin) {
|
---|
39 | super(latitudeOrigin);
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 | public static class Parameters2SP extends Parameters {
|
---|
44 | public final double standardParallel1;
|
---|
45 | public final double standardParallel2;
|
---|
46 | public Parameters2SP(double latitudeOrigin, double standardParallel1, double standardParallel2) {
|
---|
47 | super(latitudeOrigin);
|
---|
48 | this.standardParallel1 = standardParallel1;
|
---|
49 | this.standardParallel2 = standardParallel2;
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | private Parameters params;
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * projection exponent
|
---|
57 | */
|
---|
58 | protected double n;
|
---|
59 | /**
|
---|
60 | * projection factor
|
---|
61 | */
|
---|
62 | protected double F;
|
---|
63 | /**
|
---|
64 | * radius of the parallel of latitude of the false origin (2SP) or at
|
---|
65 | * natural origin (1SP)
|
---|
66 | */
|
---|
67 | protected double r0;
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * precision in iterative schema
|
---|
71 | */
|
---|
72 | protected static final double epsilon = 1e-12;
|
---|
73 |
|
---|
74 | @Override
|
---|
75 | public void initialize(ProjParameters params) throws ProjectionConfigurationException {
|
---|
76 | ellps = params.ellps;
|
---|
77 | e = ellps.e;
|
---|
78 | if (params.lat_0 == null)
|
---|
79 | throw new ProjectionConfigurationException(tr("Parameter ''{0}'' required.", "lat_0"));
|
---|
80 | if (params.lat_1 != null && params.lat_2 != null) {
|
---|
81 | initialize2SP(params.lat_0, params.lat_1, params.lat_2);
|
---|
82 | } else {
|
---|
83 | initialize1SP(params.lat_0);
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Initialize for LCC with 2 standard parallels.
|
---|
89 | *
|
---|
90 | * @param lat_0 latitude of false origin (in degrees)
|
---|
91 | * @param lat_1 latitude of first standard parallel (in degrees)
|
---|
92 | * @param lat_2 latitude of second standard parallel (in degrees)
|
---|
93 | */
|
---|
94 | private void initialize2SP(double lat_0, double lat_1, double lat_2) {
|
---|
95 | this.params = new Parameters2SP(lat_0, lat_1, lat_2);
|
---|
96 |
|
---|
97 | final double m1 = m(toRadians(lat_1));
|
---|
98 | final double m2 = m(toRadians(lat_2));
|
---|
99 |
|
---|
100 | final double t1 = t(toRadians(lat_1));
|
---|
101 | final double t2 = t(toRadians(lat_2));
|
---|
102 | final double tf = t(toRadians(lat_0));
|
---|
103 |
|
---|
104 | n = (log(m1) - log(m2)) / (log(t1) - log(t2));
|
---|
105 | F = m1 / (n * pow(t1, n));
|
---|
106 | r0 = F * pow(tf, n);
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Initialize for LCC with 1 standard parallel.
|
---|
111 | *
|
---|
112 | * @param lat_0 latitude of natural origin (in degrees)
|
---|
113 | */
|
---|
114 | private void initialize1SP(double lat_0) {
|
---|
115 | this.params = new Parameters1SP(lat_0);
|
---|
116 | final double lat_0_rad = toRadians(lat_0);
|
---|
117 |
|
---|
118 | final double m0 = m(lat_0_rad);
|
---|
119 | final double t0 = t(lat_0_rad);
|
---|
120 |
|
---|
121 | n = sin(lat_0_rad);
|
---|
122 | F = m0 / (n * pow(t0, n));
|
---|
123 | r0 = F * pow(t0, n);
|
---|
124 | }
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * auxiliary function t
|
---|
128 | */
|
---|
129 | protected double t(double lat_rad) {
|
---|
130 | return tan(PI/4 - lat_rad / 2.0)
|
---|
131 | / pow(( (1.0 - e * sin(lat_rad)) / (1.0 + e * sin(lat_rad))) , e/2);
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * auxiliary function m
|
---|
136 | */
|
---|
137 | protected double m(double lat_rad) {
|
---|
138 | return cos(lat_rad) / (sqrt(1 - e * e * pow(sin(lat_rad), 2)));
|
---|
139 | }
|
---|
140 |
|
---|
141 | @Override
|
---|
142 | public String getName() {
|
---|
143 | return tr("Lambert Conformal Conic");
|
---|
144 | }
|
---|
145 |
|
---|
146 | @Override
|
---|
147 | public String getProj4Id() {
|
---|
148 | return "lcc";
|
---|
149 | }
|
---|
150 |
|
---|
151 | @Override
|
---|
152 | public double[] project(double phi, double lambda) {
|
---|
153 | double sinphi = sin(phi);
|
---|
154 | double L = (0.5*log((1+sinphi)/(1-sinphi))) - e/2*log((1+e*sinphi)/(1-e*sinphi));
|
---|
155 | double r = F*exp(-n*L);
|
---|
156 | double gamma = n*lambda;
|
---|
157 | double X = r*sin(gamma);
|
---|
158 | double Y = r0 - r*cos(gamma);
|
---|
159 | return new double[] { X, Y };
|
---|
160 | }
|
---|
161 |
|
---|
162 | @Override
|
---|
163 | public double[] invproject(double east, double north) {
|
---|
164 | double r = sqrt(pow(east,2) + pow(north-r0, 2));
|
---|
165 | double gamma = atan(east / (r0-north));
|
---|
166 | double lambda = gamma/n;
|
---|
167 | double latIso = (-1/n) * log(abs(r/F));
|
---|
168 | double phi = ellps.latitude(latIso, e, epsilon);
|
---|
169 | return new double[] { phi, lambda };
|
---|
170 | }
|
---|
171 |
|
---|
172 | public final Parameters getParameters() {
|
---|
173 | return params;
|
---|
174 | }
|
---|
175 | }
|
---|