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