source: josm/trunk/src/org/openstreetmap/josm/data/projection/Lambert.java@ 2304

Last change on this file since 2304 was 2304, checked in by stoecker, 15 years ago

apply #3737 - patch by Pieren - new Lambert zones for France, still needs some rework

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