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