comparison gnv-artifacts/src/main/java/de/intevation/gnv/utils/DistanceCalculator.java @ 875:5e9efdda6894

merged gnv-artifacts/1.0
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:13:56 +0200
parents 05bf8534a35a
children f953c9a559d8
comparison
equal deleted inserted replaced
722:bb3ffe7d719e 875:5e9efdda6894
1 package de.intevation.gnv.utils;
2
3 import com.vividsolutions.jts.geom.Coordinate;
4 import com.vividsolutions.jts.geom.Point;
5
6 import java.util.List;
7
8 /**
9 * A helper class to calculate distances between points and coordinates.
10 *
11 * @author <a href="mailto:tim.englich@intevation.de">Tim Englich</a>
12 *
13 */
14 public class DistanceCalculator {
15
16 private final static double flattening = 1.0 / 298.257233563;
17
18 private final static double earthRadius = 6378137.0 / 1000.0 ;
19
20 /**
21 * Constructor
22 */
23 public DistanceCalculator() {
24 }
25
26
27 /**
28 * Calculates the distance between two points.
29 *
30 * @param p1 First point.
31 * @param p2 Second point.
32 * @return the distance.
33 */
34 public static double calculateDistance(Point p1, Point p2){
35 return calculateDistance(p1.getCoordinate(), p2.getCoordinate());
36 }
37
38
39 /**
40 * Calculates the distance between two coordinates.
41 *
42 * @param p1 First coordinate.
43 * @param p2 Second coordinate.
44 * @return the distance.
45 */
46 public static double calculateDistance(Coordinate p1, Coordinate p2){
47 double resultValue = 0.0;
48
49 double b1 = p1.y;
50 double b2 = p2.y;
51
52 double l1 = p1.x;
53 double l2 = p2.x;
54
55
56 double F = (b1 + b2) / 2.0;
57 double G = (b1 - b2) / 2.0;
58 double l = (l1 - l2) / 2.0;
59
60 F = (Math.PI / 180.0) * F;
61 G = (Math.PI / 180.0) * G;
62 l = (Math.PI / 180.0) * l;
63
64 double S = ((Math.sin(G) * Math.sin(G)) * ((Math.cos(l) * Math.cos(l))))+
65 ((Math.cos(F) * Math.cos(F)) * ((Math.sin(l) * Math.sin(l))));
66
67 double C = ((Math.cos(G) * Math.cos(G)) * ((Math.cos(l) * Math.cos(l))))+
68 ((Math.sin(F) * Math.sin(F)) * ((Math.sin(l) * Math.sin(l))));
69
70 double w = Math.atan(Math.sqrt((S/C)));
71
72 double D = 2.0 * w * earthRadius;
73
74 double R = Math.sqrt((S*C)) / w;
75
76 double H1 = (3.0 * R - 1.0 ) / (2.0 * C);
77 double H2 = (3.0 * R + 1.0 ) / (2.0 * S);
78
79 resultValue = D * (1 + (flattening * H1 * (Math.sin(F) * Math.sin(F)) *
80 (Math.cos(G) * Math.cos(G))) -
81 (flattening * H2 * (Math.cos(F) * Math.cos(F)) *
82 (Math.sin(G) * Math.sin(G))));
83
84 return resultValue;
85 }
86
87 /**
88 * Calculates the length of a path specified by coordinates in <i>path</i>.
89 *
90 * @param path A list of coordinates.
91 * @return the length of the given path.
92 */
93 public static final double calculateDistance(List<Coordinate> path) {
94 int N = path.size();
95 if (N < 2) {
96 return 0d;
97 }
98 double sum = 0d;
99 for (int i = 1; i < N; ++i) {
100 sum += calculateDistance(path.get(i-1), path.get(i));
101 }
102 return sum;
103 }
104
105 }
106 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org