annotate gnv-artifacts/src/main/java/de/intevation/gnv/utils/DistanceCalculator.java @ 362:1ab23cd66870

Added result set handling. Needs some testing. gnv-artifacts/trunk@436 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 16 Dec 2009 01:32:19 +0000
parents da1499a464b9
children 9a828e5a2390
rev   line source
185
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
1 /**
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
2 *
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
3 */
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
4 package de.intevation.gnv.utils;
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
5
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
6 import com.vividsolutions.jts.geom.Point;
362
1ab23cd66870 Added result set handling. Needs some testing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 296
diff changeset
7 import com.vividsolutions.jts.geom.Coordinate;
1ab23cd66870 Added result set handling. Needs some testing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 296
diff changeset
8
1ab23cd66870 Added result set handling. Needs some testing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 296
diff changeset
9 import java.util.List;
185
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
10
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
11 /**
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
12 * @author Tim Englich <tim.englich@intevation.de>
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
13 *
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
14 */
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
15 public class DistanceCalculator {
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
16
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
17 private final static double flattening = 1.0 / 298.257233563;
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
18
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
19 private final static double earthRadius = 6378137.0 / 1000.0 ;
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
20
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
21 /**
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
22 * Constructor
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
23 */
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
24 public DistanceCalculator() {
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
25 }
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
26
296
da1499a464b9 Declared a method as static to use it without instantiating an object of its class.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 185
diff changeset
27 public static double calculateDistance(Point p1, Point p2){
362
1ab23cd66870 Added result set handling. Needs some testing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 296
diff changeset
28 return calculateDistance(p1.getCoordinate(), p2.getCoordinate());
1ab23cd66870 Added result set handling. Needs some testing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 296
diff changeset
29 }
1ab23cd66870 Added result set handling. Needs some testing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 296
diff changeset
30
1ab23cd66870 Added result set handling. Needs some testing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 296
diff changeset
31 public static double calculateDistance(Coordinate p1, Coordinate p2){
185
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
32 double resultValue = 0.0;
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
33
362
1ab23cd66870 Added result set handling. Needs some testing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 296
diff changeset
34 double b1 = p1.y;
1ab23cd66870 Added result set handling. Needs some testing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 296
diff changeset
35 double b2 = p2.y;
185
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
36
362
1ab23cd66870 Added result set handling. Needs some testing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 296
diff changeset
37 double l1 = p1.x;
1ab23cd66870 Added result set handling. Needs some testing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 296
diff changeset
38 double l2 = p2.x;
185
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
39
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
40
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
41 double F = (b1 + b2) / 2.0;
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
42 double G = (b1 - b2) / 2.0;
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
43 double l = (l1 - l2) / 2.0;
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
44
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
45 F = (Math.PI / 180.0) * F;
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
46 G = (Math.PI / 180.0) * G;
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
47 l = (Math.PI / 180.0) * l;
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
48
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
49 double S = ((Math.sin(G) * Math.sin(G)) * ((Math.cos(l) * Math.cos(l))))+
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
50 ((Math.cos(F) * Math.cos(F)) * ((Math.sin(l) * Math.sin(l))));
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
51
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
52 double C = ((Math.cos(G) * Math.cos(G)) * ((Math.cos(l) * Math.cos(l))))+
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
53 ((Math.sin(F) * Math.sin(F)) * ((Math.sin(l) * Math.sin(l))));
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
54
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
55 double w = Math.atan(Math.sqrt((S/C)));
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
56
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
57 double D = 2.0 * w * earthRadius;
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
58
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
59 double R = Math.sqrt((S*C)) / w;
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
60
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
61 double H1 = (3.0 * R - 1.0 ) / (2.0 * C);
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
62 double H2 = (3.0 * R + 1.0 ) / (2.0 * S);
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
63
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
64 resultValue = D * (1 + (flattening * H1 * (Math.sin(F) * Math.sin(F)) *
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
65 (Math.cos(G) * Math.cos(G))) -
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
66 (flattening * H2 * (Math.cos(F) * Math.cos(F)) *
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
67 (Math.sin(G) * Math.sin(G))));
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
68
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
69 return resultValue;
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
70 }
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
71
362
1ab23cd66870 Added result set handling. Needs some testing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 296
diff changeset
72 public static final double calculateDistance(List<Coordinate> path) {
1ab23cd66870 Added result set handling. Needs some testing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 296
diff changeset
73 int N = path.size();
1ab23cd66870 Added result set handling. Needs some testing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 296
diff changeset
74 if (N < 2) {
1ab23cd66870 Added result set handling. Needs some testing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 296
diff changeset
75 return 0d;
1ab23cd66870 Added result set handling. Needs some testing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 296
diff changeset
76 }
1ab23cd66870 Added result set handling. Needs some testing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 296
diff changeset
77 double sum = 0d;
1ab23cd66870 Added result set handling. Needs some testing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 296
diff changeset
78 for (int i = 1; i < N; ++i) {
1ab23cd66870 Added result set handling. Needs some testing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 296
diff changeset
79 sum += calculateDistance(path.get(i-1), path.get(i));
1ab23cd66870 Added result set handling. Needs some testing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 296
diff changeset
80 }
1ab23cd66870 Added result set handling. Needs some testing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 296
diff changeset
81 return sum;
1ab23cd66870 Added result set handling. Needs some testing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 296
diff changeset
82 }
1ab23cd66870 Added result set handling. Needs some testing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 296
diff changeset
83
185
5fc8f41669a6 Added Calculation of the Distance in the unit km for Geodetic-Coordinates according to the Specification GT_0030.004
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
84 }

http://dive4elements.wald.intevation.org