svn-gvsig-desktop / tags / v1_0_RELEASE / libraries / libCq CMS for java.old / src / org / cresques / cts / GeoCalc.java @ 9167
History | View | Annotate | Download (16 KB)
1 |
/*
|
---|---|
2 |
* Cresques Mapping Suite. Graphic Library for constructing mapping applications.
|
3 |
*
|
4 |
* Copyright (C) 2004-5.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
19 |
*
|
20 |
* For more information, contact:
|
21 |
*
|
22 |
* cresques@gmail.com
|
23 |
*/
|
24 |
package org.cresques.cts; |
25 |
|
26 |
import java.awt.geom.Point2D; |
27 |
|
28 |
|
29 |
/**
|
30 |
* Operaciones relacionadas con las proyecciones y sistemas
|
31 |
* de coordenadas.
|
32 |
* @author Luis W. Sevilla (sevilla_lui@gva.es)
|
33 |
*/
|
34 |
public class GeoCalc { |
35 |
IProjection proj; |
36 |
|
37 |
/**
|
38 |
*
|
39 |
* @param proj
|
40 |
*/
|
41 |
public GeoCalc(IProjection proj) {
|
42 |
this.proj = proj;
|
43 |
} |
44 |
|
45 |
/**
|
46 |
* Distancia entre dos puntos en la esfera.
|
47 |
* Los puntos deben estar en coordenadas geogr?ficas
|
48 |
* @param pt1
|
49 |
* @param pt2
|
50 |
* @return distancia en km.
|
51 |
*/
|
52 |
public double distanceGeo(Point2D pt1, Point2D pt2) { |
53 |
double R2 = Math.pow(proj.getDatum().getESemiMajorAxis(), 2); |
54 |
double dLat = Math.toRadians(pt2.getY() - pt1.getY()); |
55 |
double dLong = Math.toRadians(pt2.getX() - pt1.getX()); |
56 |
|
57 |
double alfa = Math.toRadians(pt1.getY()); |
58 |
double alfa2 = Math.toRadians(pt2.getY()); |
59 |
|
60 |
if (Math.abs(alfa2) < Math.abs(alfa)) { |
61 |
alfa = alfa2; |
62 |
} |
63 |
|
64 |
double ds2 = (R2 * dLat * dLat) +
|
65 |
(R2 * Math.cos(alfa) * Math.cos(alfa) * dLong * dLong); |
66 |
|
67 |
return Math.sqrt(ds2); |
68 |
} |
69 |
|
70 |
/**
|
71 |
* Distancia entre dos puntos en el elipsoide.
|
72 |
* Los puntos deben estar en coordenadas geogr?ficas
|
73 |
* ver http://www.codeguru.com/Cpp/Cpp/algorithms/general/article.php/c5115/
|
74 |
* @param lat1
|
75 |
* @param lon1
|
76 |
* @param lat2
|
77 |
* @param lon2
|
78 |
* @return
|
79 |
*/
|
80 |
public double distanceEli(Point2D pt1, Point2D pt2) { |
81 |
double lat1 = Math.toRadians(pt1.getY()); |
82 |
double lon1 = -Math.toRadians(pt1.getX()); |
83 |
double lat2 = Math.toRadians(pt2.getY()); |
84 |
double lon2 = -Math.toRadians(pt2.getX()); |
85 |
|
86 |
double F = (lat1 + lat2) / 2D; |
87 |
double G = (lat1 - lat2) / 2D; |
88 |
double L = (lon1 - lon2) / 2D; |
89 |
|
90 |
double sing = Math.sin(G); |
91 |
double cosl = Math.cos(L); |
92 |
double cosf = Math.cos(F); |
93 |
double sinl = Math.sin(L); |
94 |
double sinf = Math.sin(F); |
95 |
double cosg = Math.cos(G); |
96 |
|
97 |
double flat = 1D / proj.getDatum().getEIFlattening(); |
98 |
|
99 |
double S = (sing * sing * cosl * cosl) + (cosf * cosf * sinl * sinl);
|
100 |
double C = (cosg * cosg * cosl * cosl) + (sinf * sinf * sinl * sinl);
|
101 |
double W = Math.atan2(Math.sqrt(S), Math.sqrt(C)); |
102 |
double R = Math.sqrt((S * C)) / W; |
103 |
double H1 = ((3D * R) - 1D) / (2D * C); |
104 |
double H2 = ((3D * R) + 1D) / (2D * S); |
105 |
double D = 2D * W * proj.getDatum().getESemiMajorAxis(); |
106 |
|
107 |
return (D * ((1D + (flat * H1 * sinf * sinf * cosg * cosg)) - |
108 |
(flat * H2 * cosf * cosf * sing * sing))); |
109 |
} |
110 |
|
111 |
/**
|
112 |
* Algrothims from Geocentric Datum of Australia Technical Manual
|
113 |
*
|
114 |
* http://www.anzlic.org.au/icsm/gdatum/chapter4.html
|
115 |
*
|
116 |
* This page last updated 11 May 1999
|
117 |
*
|
118 |
* Computations on the Ellipsoid
|
119 |
*
|
120 |
* There are a number of formulae that are available
|
121 |
* to calculate accurate geodetic positions,
|
122 |
* azimuths and distances on the ellipsoid.
|
123 |
*
|
124 |
* Vincenty's formulae (Vincenty, 1975) may be used
|
125 |
* for lines ranging from a few cm to nearly 20,000 km,
|
126 |
* with millimetre accuracy.
|
127 |
* The formulae have been extensively tested
|
128 |
* for the Australian region, by comparison with results
|
129 |
* from other formulae (Rainsford, 1955 & Sodano, 1965).
|
130 |
*
|
131 |
* * Inverse problem: azimuth and distance from known
|
132 |
* latitudes and longitudes
|
133 |
* * Direct problem: Latitude and longitude from known
|
134 |
* position, azimuth and distance.
|
135 |
* * Sample data
|
136 |
* * Excel spreadsheet
|
137 |
*
|
138 |
* Vincenty's Inverse formulae
|
139 |
* Given: latitude and longitude of two points
|
140 |
* (phi1, lembda1 and phi2, lembda2),
|
141 |
* Calculate: the ellipsoidal distance (s) and
|
142 |
* forward and reverse azimuths between the points (alpha12, alpha21).
|
143 |
*/
|
144 |
/**
|
145 |
* Devuelve la distancia entre dos puntos usando las formulas
|
146 |
* de vincenty.
|
147 |
* @param pt1
|
148 |
* @param pt2
|
149 |
* @return
|
150 |
*/
|
151 |
public double distanceVincenty(Point2D pt1, Point2D pt2) { |
152 |
return distanceAzimutVincenty(pt1, pt2).dist;
|
153 |
} |
154 |
|
155 |
/**
|
156 |
* Returns the distance between two geographic points on the ellipsoid
|
157 |
* and the forward and reverse azimuths between these points.
|
158 |
* lats, longs and azimuths are in decimal degrees, distance in metres
|
159 |
* Returns ( s, alpha12, alpha21 ) as a tuple
|
160 |
* @param pt1
|
161 |
* @param pt2
|
162 |
* @return
|
163 |
*/
|
164 |
public GeoData distanceAzimutVincenty(Point2D pt1, Point2D pt2) { |
165 |
GeoData gd = new GeoData(0, 0); |
166 |
double f = 1D / proj.getDatum().getEIFlattening(); |
167 |
double a = proj.getDatum().getESemiMajorAxis();
|
168 |
double phi1 = pt1.getY();
|
169 |
double lembda1 = pt1.getX();
|
170 |
double phi2 = pt2.getY();
|
171 |
double lembda2 = pt2.getX();
|
172 |
|
173 |
if ((Math.abs(phi2 - phi1) < 1e-8) && |
174 |
(Math.abs(lembda2 - lembda1) < 1e-8)) { |
175 |
return gd;
|
176 |
} |
177 |
|
178 |
double piD4 = Math.atan(1.0); |
179 |
double two_pi = piD4 * 8.0; |
180 |
|
181 |
phi1 = (phi1 * piD4) / 45.0;
|
182 |
lembda1 = (lembda1 * piD4) / 45.0; // unfortunately lambda is a key word! |
183 |
phi2 = (phi2 * piD4) / 45.0;
|
184 |
lembda2 = (lembda2 * piD4) / 45.0;
|
185 |
|
186 |
double b = a * (1.0 - f); |
187 |
|
188 |
double TanU1 = (1 - f) * Math.tan(phi1); |
189 |
double TanU2 = (1 - f) * Math.tan(phi2); |
190 |
|
191 |
double U1 = Math.atan(TanU1); |
192 |
double U2 = Math.atan(TanU2); |
193 |
|
194 |
double lembda = lembda2 - lembda1;
|
195 |
double last_lembda = -4000000.0; // an impossibe value |
196 |
double omega = lembda;
|
197 |
|
198 |
// Iterate the following equations,
|
199 |
// until there is no significant change in lembda
|
200 |
double Sin_sigma = 0; |
201 |
|
202 |
// Iterate the following equations,
|
203 |
// until there is no significant change in lembda
|
204 |
double Cos_sigma = 0; |
205 |
double Cos2sigma_m = 0; |
206 |
double alpha = 0; |
207 |
double sigma = 0; |
208 |
double sqr_sin_sigma = 0; |
209 |
|
210 |
while ((last_lembda < -3000000.0) || |
211 |
((lembda != 0) &&
|
212 |
(Math.abs((last_lembda - lembda) / lembda) > 1.0e-9))) { |
213 |
sqr_sin_sigma = Math.pow(Math.cos(U2) * Math.sin(lembda), 2) + |
214 |
Math.pow(((Math.cos(U1) * Math.sin(U2)) - |
215 |
(Math.sin(U1) * Math.cos(U2) * Math.cos(lembda))), |
216 |
2);
|
217 |
|
218 |
Sin_sigma = Math.sqrt(sqr_sin_sigma);
|
219 |
|
220 |
Cos_sigma = (Math.sin(U1) * Math.sin(U2)) + |
221 |
(Math.cos(U1) * Math.cos(U2) * Math.cos(lembda)); |
222 |
|
223 |
sigma = Math.atan2(Sin_sigma, Cos_sigma);
|
224 |
|
225 |
double Sin_alpha = (Math.cos(U1) * Math.cos(U2) * Math.sin(lembda)) / Math.sin(sigma); |
226 |
alpha = Math.asin(Sin_alpha);
|
227 |
|
228 |
Cos2sigma_m = Math.cos(sigma) -
|
229 |
((2 * Math.sin(U1) * Math.sin(U2)) / Math.pow(Math.cos(alpha), |
230 |
2));
|
231 |
|
232 |
double C = (f / 16) * Math.pow(Math.cos(alpha), 2) * (4 + |
233 |
(f * (4 - (3 * Math.pow(Math.cos(alpha), 2))))); |
234 |
|
235 |
last_lembda = lembda; |
236 |
|
237 |
lembda = omega + |
238 |
((1 - C) * f * Math.sin(alpha) * (sigma + |
239 |
(C * Math.sin(sigma) * (Cos2sigma_m +
|
240 |
(C * Math.cos(sigma) * (-1 + |
241 |
(2 * Math.pow(Cos2sigma_m, 2)))))))); |
242 |
} |
243 |
|
244 |
double u2 = (Math.pow(Math.cos(alpha), 2) * ((a * a) - (b * b))) / (b * b); |
245 |
|
246 |
double A = 1 + |
247 |
((u2 / 16384) * (4096 + |
248 |
(u2 * (-768 + (u2 * (320 - (175 * u2))))))); |
249 |
|
250 |
double B = (u2 / 1024) * (256 + |
251 |
(u2 * (-128 + (u2 * (74 - (47 * u2)))))); |
252 |
|
253 |
double delta_sigma = B * Sin_sigma * (Cos2sigma_m +
|
254 |
((B / 4) * ((Cos_sigma * (-1 + |
255 |
(2 * Math.pow(Cos2sigma_m, 2)))) - |
256 |
((B / 6) * Cos2sigma_m * (-3 + |
257 |
(4 * sqr_sin_sigma)) * (-3 + |
258 |
(4 * Math.pow(Cos2sigma_m, 2))))))); |
259 |
|
260 |
double s = b * A * (sigma - delta_sigma);
|
261 |
|
262 |
double alpha12 = Math.atan2((Math.cos(U2) * Math.sin(lembda)), |
263 |
((Math.cos(U1) * Math.sin(U2)) - |
264 |
(Math.sin(U1) * Math.cos(U2) * Math.cos(lembda)))); |
265 |
|
266 |
double alpha21 = Math.atan2((Math.cos(U1) * Math.sin(lembda)), |
267 |
((-Math.sin(U1) * Math.cos(U2)) + |
268 |
(Math.cos(U1) * Math.sin(U2) * Math.cos(lembda)))); |
269 |
|
270 |
if (alpha12 < 0.0) { |
271 |
alpha12 = alpha12 + two_pi; |
272 |
} |
273 |
|
274 |
if (alpha12 > two_pi) {
|
275 |
alpha12 = alpha12 - two_pi; |
276 |
} |
277 |
|
278 |
alpha21 = alpha21 + (two_pi / 2.0);
|
279 |
|
280 |
if (alpha21 < 0.0) { |
281 |
alpha21 = alpha21 + two_pi; |
282 |
} |
283 |
|
284 |
if (alpha21 > two_pi) {
|
285 |
alpha21 = alpha21 - two_pi; |
286 |
} |
287 |
|
288 |
alpha12 = (alpha12 * 45.0) / piD4;
|
289 |
alpha21 = (alpha21 * 45.0) / piD4;
|
290 |
|
291 |
return new GeoData(0, 0, s, alpha12, alpha21); |
292 |
} |
293 |
|
294 |
/**
|
295 |
* Vincenty's Direct formulae
|
296 |
* Given: latitude and longitude of a point (phi1, lembda1) and
|
297 |
* the geodetic azimuth (alpha12)
|
298 |
* and ellipsoidal distance in metres (s) to a second point,
|
299 |
*
|
300 |
* Calculate: the latitude and longitude of the second point (phi2, lembda2)
|
301 |
* and the reverse azimuth (alpha21).
|
302 |
*/
|
303 |
/**
|
304 |
* Returns the lat and long of projected point and reverse azimuth
|
305 |
* given a reference point and a distance and azimuth to project.
|
306 |
* lats, longs and azimuths are passed in decimal degrees.
|
307 |
* Returns ( phi2, lambda2, alpha21 ) as a tuple
|
308 |
* @param pt
|
309 |
* @param azimut
|
310 |
* @param dist
|
311 |
* @return
|
312 |
*/
|
313 |
public GeoData getPointVincenty(Point2D pt, double azimut, double dist) { |
314 |
GeoData ret = new GeoData(0, 0); |
315 |
double f = 1D / proj.getDatum().getEIFlattening(); |
316 |
double a = proj.getDatum().getESemiMajorAxis();
|
317 |
double phi1 = pt.getY();
|
318 |
double lembda1 = pt.getX();
|
319 |
double alpha12 = azimut;
|
320 |
double s = dist;
|
321 |
|
322 |
double piD4 = Math.atan(1.0); |
323 |
double two_pi = piD4 * 8.0; |
324 |
|
325 |
phi1 = (phi1 * piD4) / 45.0;
|
326 |
lembda1 = (lembda1 * piD4) / 45.0;
|
327 |
alpha12 = (alpha12 * piD4) / 45.0;
|
328 |
|
329 |
if (alpha12 < 0.0) { |
330 |
alpha12 = alpha12 + two_pi; |
331 |
} |
332 |
|
333 |
if (alpha12 > two_pi) {
|
334 |
alpha12 = alpha12 - two_pi; |
335 |
} |
336 |
|
337 |
double b = a * (1.0 - f); |
338 |
|
339 |
double TanU1 = (1 - f) * Math.tan(phi1); |
340 |
double U1 = Math.atan(TanU1); |
341 |
double sigma1 = Math.atan2(TanU1, Math.cos(alpha12)); |
342 |
double Sinalpha = Math.cos(U1) * Math.sin(alpha12); |
343 |
double cosalpha_sq = 1.0 - (Sinalpha * Sinalpha); |
344 |
|
345 |
double u2 = (cosalpha_sq * ((a * a) - (b * b))) / (b * b);
|
346 |
double A = 1.0 + |
347 |
((u2 / 16384) * (4096 + |
348 |
(u2 * (-768 + (u2 * (320 - (175 * u2))))))); |
349 |
double B = (u2 / 1024) * (256 + |
350 |
(u2 * (-128 + (u2 * (74 - (47 * u2)))))); |
351 |
|
352 |
// Starting with the approximation
|
353 |
double sigma = (s / (b * A));
|
354 |
|
355 |
double last_sigma = (2.0 * sigma) + 2.0; // something impossible |
356 |
|
357 |
// Iterate the following three equations
|
358 |
// until there is no significant change in sigma
|
359 |
// two_sigma_m , delta_sigma
|
360 |
double two_sigma_m = 0; |
361 |
|
362 |
while (Math.abs((last_sigma - sigma) / sigma) > 1.0e-9) { |
363 |
two_sigma_m = (2 * sigma1) + sigma;
|
364 |
|
365 |
double delta_sigma = B * Math.sin(sigma) * (Math.cos(two_sigma_m) + |
366 |
((B / 4) * (Math.cos(sigma) * ((-1 + |
367 |
(2 * Math.pow(Math.cos(two_sigma_m), 2))) - |
368 |
((B / 6) * Math.cos(two_sigma_m) * (-3 + |
369 |
(4 * Math.pow(Math.sin(sigma), 2))) * (-3 + |
370 |
(4 * Math.pow(Math.cos(two_sigma_m), 2)))))))); |
371 |
|
372 |
last_sigma = sigma; |
373 |
sigma = (s / (b * A)) + delta_sigma; |
374 |
} |
375 |
|
376 |
double phi2 = Math.atan2(((Math.sin(U1) * Math.cos(sigma)) + |
377 |
(Math.cos(U1) * Math.sin(sigma) * Math.cos(alpha12))), |
378 |
((1 - f) * Math.sqrt(Math.pow(Sinalpha, 2) + |
379 |
Math.pow((Math.sin(U1) * Math.sin(sigma)) - |
380 |
(Math.cos(U1) * Math.cos(sigma) * Math.cos(alpha12)), |
381 |
2))));
|
382 |
|
383 |
double lembda = Math.atan2((Math.sin(sigma) * Math.sin(alpha12)), |
384 |
((Math.cos(U1) * Math.cos(sigma)) - |
385 |
(Math.sin(U1) * Math.sin(sigma) * Math.cos(alpha12)))); |
386 |
|
387 |
double C = (f / 16) * cosalpha_sq * (4 + (f * (4 - (3 * cosalpha_sq)))); |
388 |
|
389 |
double omega = lembda -
|
390 |
((1 - C) * f * Sinalpha * (sigma +
|
391 |
(C * Math.sin(sigma) * (Math.cos(two_sigma_m) + |
392 |
(C * Math.cos(sigma) * (-1 + |
393 |
(2 * Math.pow(Math.cos(two_sigma_m), 2)))))))); |
394 |
|
395 |
double lembda2 = lembda1 + omega;
|
396 |
|
397 |
double alpha21 = Math.atan2(Sinalpha, |
398 |
((-Math.sin(U1) * Math.sin(sigma)) + |
399 |
(Math.cos(U1) * Math.cos(sigma) * Math.cos(alpha12)))); |
400 |
|
401 |
alpha21 = alpha21 + (two_pi / 2.0);
|
402 |
|
403 |
if (alpha21 < 0.0) { |
404 |
alpha21 = alpha21 + two_pi; |
405 |
} |
406 |
|
407 |
if (alpha21 > two_pi) {
|
408 |
alpha21 = alpha21 - two_pi; |
409 |
} |
410 |
|
411 |
phi2 = (phi2 * 45.0) / piD4;
|
412 |
lembda2 = (lembda2 * 45.0) / piD4;
|
413 |
alpha21 = (alpha21 * 45.0) / piD4;
|
414 |
|
415 |
ret.pt = new Point2D.Double(lembda2, phi2); |
416 |
ret.azimut = alpha21; |
417 |
|
418 |
return ret;
|
419 |
} |
420 |
|
421 |
/**
|
422 |
* Superficie de un triangulo (esf?rico). Los puntos deben de estar
|
423 |
* en coordenadas geogr?ficas.
|
424 |
* @param pt1
|
425 |
* @param pt2
|
426 |
* @param pt3
|
427 |
* @return
|
428 |
*/
|
429 |
public double surfaceSphere(Point2D pt1, Point2D pt2, Point2D pt3) { |
430 |
double sup = -1; |
431 |
double A = distanceGeo(pt1, pt2);
|
432 |
double B = distanceGeo(pt2, pt3);
|
433 |
double C = distanceGeo(pt3, pt1);
|
434 |
sup = (((A + B + C) - Math.toRadians(180D)) * Math.PI * proj.getDatum() |
435 |
.getESemiMajorAxis()) / Math.toRadians(180D); |
436 |
|
437 |
return sup;
|
438 |
} |
439 |
|
440 |
/*
|
441 |
* F?rmulas de Vincenty's.
|
442 |
* (pasadas de http://wegener.mechanik.tu-darmstadt.de/GMT-Help/Archiv/att-8710/Geodetic_py
|
443 |
* http://www.icsm.gov.au/icsm/gda/gdatm/index.html
|
444 |
*/
|
445 |
class GeoData { |
446 |
Point2D pt;
|
447 |
double azimut;
|
448 |
double revAzimut;
|
449 |
double dist;
|
450 |
|
451 |
public GeoData(double x, double y) { |
452 |
pt = new Point2D.Double(x, y); |
453 |
azimut = revAzimut = dist = 0;
|
454 |
} |
455 |
|
456 |
public GeoData(double x, double y, double dist, double azi, double rAzi) { |
457 |
pt = new Point2D.Double(x, y); |
458 |
azimut = azi; |
459 |
revAzimut = rAzi; |
460 |
this.dist = dist;
|
461 |
} |
462 |
} |
463 |
} |