Mercurial > dive4elements > gnv-client
comparison gnv-artifacts/src/main/java/de/intevation/gnv/utils/DistanceCalculator.java @ 540:80630520e25a
merged gnv-artifacts/0.4
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:13:49 +0200 |
parents | 1ab23cd66870 |
children | 9a828e5a2390 |
comparison
equal
deleted
inserted
replaced
415:9f4a0b990d27 | 540:80630520e25a |
---|---|
1 /** | |
2 * | |
3 */ | |
4 package de.intevation.gnv.utils; | |
5 | |
6 import com.vividsolutions.jts.geom.Point; | |
7 import com.vividsolutions.jts.geom.Coordinate; | |
8 | |
9 import java.util.List; | |
10 | |
11 /** | |
12 * @author Tim Englich <tim.englich@intevation.de> | |
13 * | |
14 */ | |
15 public class DistanceCalculator { | |
16 | |
17 private final static double flattening = 1.0 / 298.257233563; | |
18 | |
19 private final static double earthRadius = 6378137.0 / 1000.0 ; | |
20 | |
21 /** | |
22 * Constructor | |
23 */ | |
24 public DistanceCalculator() { | |
25 } | |
26 | |
27 public static double calculateDistance(Point p1, Point p2){ | |
28 return calculateDistance(p1.getCoordinate(), p2.getCoordinate()); | |
29 } | |
30 | |
31 public static double calculateDistance(Coordinate p1, Coordinate p2){ | |
32 double resultValue = 0.0; | |
33 | |
34 double b1 = p1.y; | |
35 double b2 = p2.y; | |
36 | |
37 double l1 = p1.x; | |
38 double l2 = p2.x; | |
39 | |
40 | |
41 double F = (b1 + b2) / 2.0; | |
42 double G = (b1 - b2) / 2.0; | |
43 double l = (l1 - l2) / 2.0; | |
44 | |
45 F = (Math.PI / 180.0) * F; | |
46 G = (Math.PI / 180.0) * G; | |
47 l = (Math.PI / 180.0) * l; | |
48 | |
49 double S = ((Math.sin(G) * Math.sin(G)) * ((Math.cos(l) * Math.cos(l))))+ | |
50 ((Math.cos(F) * Math.cos(F)) * ((Math.sin(l) * Math.sin(l)))); | |
51 | |
52 double C = ((Math.cos(G) * Math.cos(G)) * ((Math.cos(l) * Math.cos(l))))+ | |
53 ((Math.sin(F) * Math.sin(F)) * ((Math.sin(l) * Math.sin(l)))); | |
54 | |
55 double w = Math.atan(Math.sqrt((S/C))); | |
56 | |
57 double D = 2.0 * w * earthRadius; | |
58 | |
59 double R = Math.sqrt((S*C)) / w; | |
60 | |
61 double H1 = (3.0 * R - 1.0 ) / (2.0 * C); | |
62 double H2 = (3.0 * R + 1.0 ) / (2.0 * S); | |
63 | |
64 resultValue = D * (1 + (flattening * H1 * (Math.sin(F) * Math.sin(F)) * | |
65 (Math.cos(G) * Math.cos(G))) - | |
66 (flattening * H2 * (Math.cos(F) * Math.cos(F)) * | |
67 (Math.sin(G) * Math.sin(G)))); | |
68 | |
69 return resultValue; | |
70 } | |
71 | |
72 public static final double calculateDistance(List<Coordinate> path) { | |
73 int N = path.size(); | |
74 if (N < 2) { | |
75 return 0d; | |
76 } | |
77 double sum = 0d; | |
78 for (int i = 1; i < N; ++i) { | |
79 sum += calculateDistance(path.get(i-1), path.get(i)); | |
80 } | |
81 return sum; | |
82 } | |
83 | |
84 } |