1 | //License: GPL. For details, see LICENSE file.
|
---|
2 | //Thanks to Johan Montagnat and its geoconv java converter application
|
---|
3 | //(http://www.i3s.unice.fr/~johan/gps/ , published under GPL license)
|
---|
4 | //from which some code and constants have been reused here.
|
---|
5 | package org.openstreetmap.josm.data.projection;
|
---|
6 |
|
---|
7 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
8 |
|
---|
9 | import javax.swing.JOptionPane;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.Main;
|
---|
12 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
13 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
14 | import org.openstreetmap.josm.data.Bounds;
|
---|
15 | import org.openstreetmap.josm.gui.OptionPaneUtil;
|
---|
16 |
|
---|
17 | public class Lambert implements Projection {
|
---|
18 | /**
|
---|
19 | * Lambert I, II, III, and IV projection exponents
|
---|
20 | */
|
---|
21 | public static final double n[] = { 0.7604059656, 0.7289686274, 0.6959127966, 0.6712679322 };
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Lambert I, II, III, and IV projection constants
|
---|
25 | */
|
---|
26 | public static final double c[] = { 11603796.98, 11745793.39, 11947992.52, 12136281.99 };
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Lambert I, II, III, and IV false east
|
---|
30 | */
|
---|
31 | public static final double Xs[] = { 600000.0, 600000.0, 600000.0, 234.358 };
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Lambert I, II, III, and IV false north
|
---|
35 | */
|
---|
36 | public static final double Ys[] = { 5657616.674, 6199695.768, 6791905.085, 7239161.542 };
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Lambert I, II, III, and IV longitudinal offset to Greenwich meridian
|
---|
40 | */
|
---|
41 | public static final double lg0 = 0.04079234433198; // 2deg20'14.025"
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * precision in iterative schema
|
---|
45 | */
|
---|
46 |
|
---|
47 | public static final double epsilon = 1e-11;
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * France is divided in 4 Lambert projection zones (1,2,3 + 4th for Corsica)
|
---|
51 | */
|
---|
52 | public static final double cMaxLatZone1 = Math.toRadians(57 * 0.9);
|
---|
53 |
|
---|
54 | public static final double zoneLimits[] = { Math.toRadians(53.5 * 0.9), // between Zone 1 and Zone 2 (in grad *0.9)
|
---|
55 | Math.toRadians(50.5 * 0.9), // between Zone 2 and Zone 3
|
---|
56 | Math.toRadians(47.51963 * 0.9), // between Zone 3 and Zone 4
|
---|
57 | Math.toRadians(46.17821 * 0.9) };// lowest latitude of Zone 4
|
---|
58 |
|
---|
59 | public static final double cMinLonZones = Math.toRadians(-4.9074074074074059 * 0.9);
|
---|
60 |
|
---|
61 | public static final double cMaxLonZones = Math.toRadians(10.2 * 0.9);
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Because josm cannot work correctly if two zones are displayed, we allow some overlapping
|
---|
65 | */
|
---|
66 | public static final double cMaxOverlappingZones = Math.toRadians(1.5 * 0.9);
|
---|
67 |
|
---|
68 | public static int layoutZone = -1;
|
---|
69 |
|
---|
70 | private static int currentZone = 0;
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * @param p WGS84 lat/lon (ellipsoid GRS80) (in degree)
|
---|
74 | * @return eastnorth projection in Lambert Zone (ellipsoid Clark)
|
---|
75 | */
|
---|
76 | public EastNorth latlon2eastNorth(LatLon p) {
|
---|
77 | // translate ellipsoid GRS80 (WGS83) => Clark
|
---|
78 | LatLon geo = GRS802Clark(p);
|
---|
79 | double lt = geo.lat(); // in radian
|
---|
80 | double lg = geo.lon();
|
---|
81 |
|
---|
82 | // check if longitude and latitude are inside the French Lambert zones
|
---|
83 | currentZone = 0;
|
---|
84 | boolean outOfLambertZones = false;
|
---|
85 | if (lt >= zoneLimits[3] && lt <= cMaxLatZone1 && lg >= cMinLonZones && lg <= cMaxLonZones) {
|
---|
86 | // zone I
|
---|
87 | if (lt > zoneLimits[0]) {
|
---|
88 | currentZone = 0;
|
---|
89 | } else if (lt > zoneLimits[1]) {
|
---|
90 | currentZone = 1;
|
---|
91 | } else if (lt > zoneLimits[2]) {
|
---|
92 | currentZone = 2;
|
---|
93 | } else if (lt > zoneLimits[3])
|
---|
94 | // Note: zone IV is dedicated to Corsica island and extends from 47.8 to
|
---|
95 | // 45.9 degrees of latitude. There is an overlap with zone III that can be
|
---|
96 | // solved only with longitude (covers Corsica if lon > 7.2 degree)
|
---|
97 | if (lg < Math.toRadians(8 * 0.9)) {
|
---|
98 | currentZone = 2;
|
---|
99 | } else {
|
---|
100 | currentZone = 3;
|
---|
101 | }
|
---|
102 | } else {
|
---|
103 | outOfLambertZones = true; // possible when MAX_LAT is used
|
---|
104 | }
|
---|
105 | if (!outOfLambertZones) {
|
---|
106 | if (layoutZone == -1) {
|
---|
107 | layoutZone = currentZone;
|
---|
108 | } else if (layoutZone != currentZone) {
|
---|
109 | if (farawayFromLambertZoneFrance(lt,lg)) {
|
---|
110 | OptionPaneUtil.showMessageDialog(Main.parent,
|
---|
111 | tr("IMPORTANT : data positioned far away from\n"
|
---|
112 | + "the current Lambert zone limits.\n"
|
---|
113 | + "Do not upload any data after this message.\n"
|
---|
114 | + "Undo your last action, save your work\n"
|
---|
115 | + "and start a new layer on the new zone."),
|
---|
116 | tr("Warning"),
|
---|
117 | JOptionPane.WARNING_MESSAGE);
|
---|
118 | layoutZone = -1;
|
---|
119 | } else {
|
---|
120 | System.out.println("temporarily extend Lambert zone " + layoutZone + " projection at lat,lon:"
|
---|
121 | + lt + "," + lg);
|
---|
122 | }
|
---|
123 | }
|
---|
124 | }
|
---|
125 | if (layoutZone == -1)
|
---|
126 | return ConicProjection(lt, lg, Xs[currentZone], Ys[currentZone], c[currentZone], n[currentZone]);
|
---|
127 | return ConicProjection(lt, lg, Xs[layoutZone], Ys[layoutZone], c[layoutZone], n[layoutZone]);
|
---|
128 | }
|
---|
129 |
|
---|
130 | public LatLon eastNorth2latlon(EastNorth p) {
|
---|
131 | LatLon geo;
|
---|
132 | if (layoutZone == -1) {
|
---|
133 | // possible until the Lambert zone is determined by latlon2eastNorth() with a valid LatLon
|
---|
134 | geo = Geographic(p, Xs[currentZone], Ys[currentZone], c[currentZone], n[currentZone]);
|
---|
135 | } else {
|
---|
136 | geo = Geographic(p, Xs[layoutZone], Ys[layoutZone], c[layoutZone], n[layoutZone]);
|
---|
137 | }
|
---|
138 | // translate ellipsoid Clark => GRS80 (WGS83)
|
---|
139 | LatLon wgs = Clark2GRS80(geo);
|
---|
140 | return new LatLon(Math.toDegrees(wgs.lat()), Math.toDegrees(wgs.lon()));
|
---|
141 | }
|
---|
142 |
|
---|
143 | @Override public String toString() {
|
---|
144 | return tr("Lambert Zone (France)");
|
---|
145 | }
|
---|
146 |
|
---|
147 | public String toCode() {
|
---|
148 | return "EPSG:"+(27571+currentZone);
|
---|
149 | }
|
---|
150 |
|
---|
151 | public String getCacheDirectoryName() {
|
---|
152 | return "lambert";
|
---|
153 | }
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * Initializes from geographic coordinates. Note that reference ellipsoid
|
---|
157 | * used by Lambert is the Clark ellipsoid.
|
---|
158 | *
|
---|
159 | * @param lat latitude in grad
|
---|
160 | * @param lon longitude in grad
|
---|
161 | * @param Xs false east (coordinate system origin) in meters
|
---|
162 | * @param Ys false north (coordinate system origin) in meters
|
---|
163 | * @param c projection constant
|
---|
164 | * @param n projection exponent
|
---|
165 | * @return EastNorth projected coordinates in meter
|
---|
166 | */
|
---|
167 | private EastNorth ConicProjection(double lat, double lon, double Xs, double Ys, double c, double n) {
|
---|
168 | double eslt = Ellipsoid.clarke.e * Math.sin(lat);
|
---|
169 | double l = Math.log(Math.tan(Math.PI / 4.0 + (lat / 2.0))
|
---|
170 | * Math.pow((1.0 - eslt) / (1.0 + eslt), Ellipsoid.clarke.e / 2.0));
|
---|
171 | double east = Xs + c * Math.exp(-n * l) * Math.sin(n * (lon - lg0));
|
---|
172 | double north = Ys - c * Math.exp(-n * l) * Math.cos(n * (lon - lg0));
|
---|
173 | return new EastNorth(east, north);
|
---|
174 | }
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * Initializes from projected coordinates (conic projection). Note that
|
---|
178 | * reference ellipsoid used by Lambert is Clark
|
---|
179 | *
|
---|
180 | * @param eastNorth projected coordinates pair in meters
|
---|
181 | * @param Xs false east (coordinate system origin) in meters
|
---|
182 | * @param Ys false north (coordinate system origin) in meters
|
---|
183 | * @param c projection constant
|
---|
184 | * @param n projection exponent
|
---|
185 | * @return LatLon in radian
|
---|
186 | */
|
---|
187 | private LatLon Geographic(EastNorth eastNorth, double Xs, double Ys, double c, double n) {
|
---|
188 | double dx = eastNorth.east() - Xs;
|
---|
189 | double dy = Ys - eastNorth.north();
|
---|
190 | double R = Math.sqrt(dx * dx + dy * dy);
|
---|
191 | double gamma = Math.atan(dx / dy);
|
---|
192 | double l = -1.0 / n * Math.log(Math.abs(R / c));
|
---|
193 | l = Math.exp(l);
|
---|
194 | double lon = lg0 + gamma / n;
|
---|
195 | double lat = 2.0 * Math.atan(l) - Math.PI / 2.0;
|
---|
196 | double delta = 1.0;
|
---|
197 | while (delta > epsilon) {
|
---|
198 | double eslt = Ellipsoid.clarke.e * Math.sin(lat);
|
---|
199 | double nlt = 2.0 * Math.atan(Math.pow((1.0 + eslt) / (1.0 - eslt), Ellipsoid.clarke.e / 2.0) * l) - Math.PI
|
---|
200 | / 2.0;
|
---|
201 | delta = Math.abs(nlt - lat);
|
---|
202 | lat = nlt;
|
---|
203 | }
|
---|
204 | return new LatLon(lat, lon); // in radian
|
---|
205 | }
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Translate latitude/longitude in WGS84, (ellipsoid GRS80) to Lambert
|
---|
209 | * geographic, (ellipsoid Clark)
|
---|
210 | */
|
---|
211 | private LatLon GRS802Clark(LatLon wgs) {
|
---|
212 | double lat = Math.toRadians(wgs.lat()); // degree to radian
|
---|
213 | double lon = Math.toRadians(wgs.lon());
|
---|
214 | // WGS84 geographic => WGS84 cartesian
|
---|
215 | double N = Ellipsoid.GRS80.a / (Math.sqrt(1.0 - Ellipsoid.GRS80.e2 * Math.sin(lat) * Math.sin(lat)));
|
---|
216 | double X = (N/* +height */) * Math.cos(lat) * Math.cos(lon);
|
---|
217 | double Y = (N/* +height */) * Math.cos(lat) * Math.sin(lon);
|
---|
218 | double Z = (N * (1.0 - Ellipsoid.GRS80.e2)/* + height */) * Math.sin(lat);
|
---|
219 | // WGS84 => Lambert ellipsoide similarity
|
---|
220 | X += 168.0;
|
---|
221 | Y += 60.0;
|
---|
222 | Z += -320.0;
|
---|
223 | // Lambert cartesian => Lambert geographic
|
---|
224 | return Geographic(X, Y, Z, Ellipsoid.clarke);
|
---|
225 | }
|
---|
226 |
|
---|
227 | private LatLon Clark2GRS80(LatLon lambert) {
|
---|
228 | double lat = lambert.lat(); // in radian
|
---|
229 | double lon = lambert.lon();
|
---|
230 | // Lambert geographic => Lambert cartesian
|
---|
231 | double N = Ellipsoid.clarke.a / (Math.sqrt(1.0 - Ellipsoid.clarke.e2 * Math.sin(lat) * Math.sin(lat)));
|
---|
232 | double X = (N/* +height */) * Math.cos(lat) * Math.cos(lon);
|
---|
233 | double Y = (N/* +height */) * Math.cos(lat) * Math.sin(lon);
|
---|
234 | double Z = (N * (1.0 - Ellipsoid.clarke.e2)/* + height */) * Math.sin(lat);
|
---|
235 | // Lambert => WGS84 ellipsoide similarity
|
---|
236 | X += -168.0;
|
---|
237 | Y += -60.0;
|
---|
238 | Z += 320.0;
|
---|
239 | // WGS84 cartesian => WGS84 geographic
|
---|
240 | return Geographic(X, Y, Z, Ellipsoid.GRS80);
|
---|
241 | }
|
---|
242 |
|
---|
243 | /**
|
---|
244 | * initializes from cartesian coordinates
|
---|
245 | *
|
---|
246 | * @param X
|
---|
247 | * 1st coordinate in meters
|
---|
248 | * @param Y
|
---|
249 | * 2nd coordinate in meters
|
---|
250 | * @param Z
|
---|
251 | * 3rd coordinate in meters
|
---|
252 | * @param ell
|
---|
253 | * reference ellipsoid
|
---|
254 | */
|
---|
255 | private LatLon Geographic(double X, double Y, double Z, Ellipsoid ell) {
|
---|
256 | double norm = Math.sqrt(X * X + Y * Y);
|
---|
257 | double lg = 2.0 * Math.atan(Y / (X + norm));
|
---|
258 | double lt = Math.atan(Z / (norm * (1.0 - (ell.a * ell.e2 / Math.sqrt(X * X + Y * Y + Z * Z)))));
|
---|
259 | double delta = 1.0;
|
---|
260 | while (delta > epsilon) {
|
---|
261 | double s2 = Math.sin(lt);
|
---|
262 | s2 *= s2;
|
---|
263 | double l = Math.atan((Z / norm)
|
---|
264 | / (1.0 - (ell.a * ell.e2 * Math.cos(lt) / (norm * Math.sqrt(1.0 - ell.e2 * s2)))));
|
---|
265 | delta = Math.abs(l - lt);
|
---|
266 | lt = l;
|
---|
267 | }
|
---|
268 | double s2 = Math.sin(lt);
|
---|
269 | s2 *= s2;
|
---|
270 | // h = norm / Math.cos(lt) - ell.a / Math.sqrt(1.0 - ell.e2 * s2);
|
---|
271 | return new LatLon(lt, lg);
|
---|
272 | }
|
---|
273 |
|
---|
274 | private boolean farawayFromLambertZoneFrance(double lat, double lon) {
|
---|
275 | if (lat < (zoneLimits[3] - cMaxOverlappingZones) || (lat > (cMaxLatZone1 + cMaxOverlappingZones))
|
---|
276 | || (lon < (cMinLonZones - cMaxOverlappingZones)) || (lon > (cMaxLonZones + cMaxOverlappingZones)))
|
---|
277 | return true;
|
---|
278 | return false;
|
---|
279 | }
|
---|
280 |
|
---|
281 | public Bounds getWorldBoundsLatLon()
|
---|
282 | {
|
---|
283 | // These are not the Lambert Zone boundaries but we keep these values until coordinates outside the
|
---|
284 | // projection boundaries are handled correctly.
|
---|
285 | return new Bounds(
|
---|
286 | new LatLon(-85.05112877980659, -180.0),
|
---|
287 | new LatLon(85.05112877980659, 180.0));
|
---|
288 | /*return new Bounds(
|
---|
289 | new LatLon(45.0, -4.9074074074074059),
|
---|
290 | new LatLon(57.0, 10.2));*/
|
---|
291 | }
|
---|
292 | }
|
---|