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