annotate gnv-artifacts/src/main/java/de/intevation/gnv/utils/DistanceCalculator.java @ 1141:f030acb8d999

dummy merge for repo head
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:15:18 +0200
parents f953c9a559d8
children
rev   line source
1115
f953c9a559d8 Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 836
diff changeset
1 /*
f953c9a559d8 Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 836
diff changeset
2 * Copyright (c) 2010 by Intevation GmbH
f953c9a559d8 Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 836
diff changeset
3 *
f953c9a559d8 Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 836
diff changeset
4 * This program is free software under the LGPL (>=v2.1)
f953c9a559d8 Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 836
diff changeset
5 * Read the file LGPL.txt coming with the software for details
f953c9a559d8 Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 836
diff changeset
6 * or visit http://www.gnu.org/licenses/ if it does not exist.
f953c9a559d8 Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 836
diff changeset
7 */
f953c9a559d8 Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 836
diff changeset
8
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
9 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
10
779
b1f5f2a8840f Ordered imports. Removed needless imports. Removed empty headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 778
diff changeset
11 import com.vividsolutions.jts.geom.Coordinate;
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
12 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
13
1ab23cd66870 Added result set handling. Needs some testing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 296
diff changeset
14 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
15
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 /**
806
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
17 * A helper class to calculate distances between points and coordinates.
807
a645bd23c1c8 Added more javadoc. Removed trailing whitespace.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 806
diff changeset
18 *
780
c4156275c1e1 Bring @author javadoc tags in form '@author <a href="john.doe@example.com">John Doe</a>'
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 779
diff changeset
19 * @author <a href="mailto:tim.englich@intevation.de">Tim Englich</a>
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
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 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
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 private final static double flattening = 1.0 / 298.257233563;
778
9a828e5a2390 Removed trailing whitespace
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 362
diff changeset
25
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
26 private final static double earthRadius = 6378137.0 / 1000.0 ;
778
9a828e5a2390 Removed trailing whitespace
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 362
diff changeset
27
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
28 /**
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
29 * 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
30 */
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
31 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
32 }
778
9a828e5a2390 Removed trailing whitespace
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 362
diff changeset
33
806
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
34
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
35 /**
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
36 * Calculates the distance between two points.
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
37 *
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
38 * @param p1 First point.
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
39 * @param p2 Second point.
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
40 * @return the distance.
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
41 */
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
42 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
43 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
44 }
1ab23cd66870 Added result set handling. Needs some testing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 296
diff changeset
45
806
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
46
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
47 /**
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
48 * Calculates the distance between two coordinates.
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
49 *
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
50 * @param p1 First coordinate.
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
51 * @param p2 Second coordinate.
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
52 * @return the distance.
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
53 */
362
1ab23cd66870 Added result set handling. Needs some testing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 296
diff changeset
54 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
55 double resultValue = 0.0;
778
9a828e5a2390 Removed trailing whitespace
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 362
diff changeset
56
362
1ab23cd66870 Added result set handling. Needs some testing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 296
diff changeset
57 double b1 = p1.y;
1ab23cd66870 Added result set handling. Needs some testing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 296
diff changeset
58 double b2 = p2.y;
778
9a828e5a2390 Removed trailing whitespace
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 362
diff changeset
59
362
1ab23cd66870 Added result set handling. Needs some testing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 296
diff changeset
60 double l1 = p1.x;
1ab23cd66870 Added result set handling. Needs some testing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 296
diff changeset
61 double l2 = p2.x;
778
9a828e5a2390 Removed trailing whitespace
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 362
diff changeset
62
9a828e5a2390 Removed trailing whitespace
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 362
diff changeset
63
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
64 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
65 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
66 double l = (l1 - l2) / 2.0;
778
9a828e5a2390 Removed trailing whitespace
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 362
diff changeset
67
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
68 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
69 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
70 l = (Math.PI / 180.0) * l;
778
9a828e5a2390 Removed trailing whitespace
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 362
diff changeset
71
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
72 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
73 ((Math.cos(F) * Math.cos(F)) * ((Math.sin(l) * Math.sin(l))));
778
9a828e5a2390 Removed trailing whitespace
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 362
diff changeset
74
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
75 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
76 ((Math.sin(F) * Math.sin(F)) * ((Math.sin(l) * Math.sin(l))));
778
9a828e5a2390 Removed trailing whitespace
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 362
diff changeset
77
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
78 double w = Math.atan(Math.sqrt((S/C)));
778
9a828e5a2390 Removed trailing whitespace
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 362
diff changeset
79
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
80 double D = 2.0 * w * earthRadius;
778
9a828e5a2390 Removed trailing whitespace
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 362
diff changeset
81
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
82 double R = Math.sqrt((S*C)) / w;
778
9a828e5a2390 Removed trailing whitespace
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 362
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 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
85 double H2 = (3.0 * R + 1.0 ) / (2.0 * S);
778
9a828e5a2390 Removed trailing whitespace
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 362
diff changeset
86
9a828e5a2390 Removed trailing whitespace
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 362
diff changeset
87 resultValue = D * (1 + (flattening * H1 * (Math.sin(F) * Math.sin(F)) *
9a828e5a2390 Removed trailing whitespace
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 362
diff changeset
88 (Math.cos(G) * Math.cos(G))) -
9a828e5a2390 Removed trailing whitespace
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 362
diff changeset
89 (flattening * H2 * (Math.cos(F) * Math.cos(F)) *
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
90 (Math.sin(G) * Math.sin(G))));
778
9a828e5a2390 Removed trailing whitespace
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 362
diff changeset
91
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
92 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
93 }
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
94
806
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
95 /**
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
96 * Calculates the length of a path specified by coordinates in <i>path</i>.
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
97 *
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
98 * @param path A list of coordinates.
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
99 * @return the length of the given path.
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
100 */
362
1ab23cd66870 Added result set handling. Needs some testing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 296
diff changeset
101 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
102 int N = path.size();
1ab23cd66870 Added result set handling. Needs some testing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 296
diff changeset
103 if (N < 2) {
1ab23cd66870 Added result set handling. Needs some testing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 296
diff changeset
104 return 0d;
1ab23cd66870 Added result set handling. Needs some testing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 296
diff changeset
105 }
1ab23cd66870 Added result set handling. Needs some testing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 296
diff changeset
106 double sum = 0d;
1ab23cd66870 Added result set handling. Needs some testing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 296
diff changeset
107 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
108 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
109 }
1ab23cd66870 Added result set handling. Needs some testing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 296
diff changeset
110 return sum;
1ab23cd66870 Added result set handling. Needs some testing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 296
diff changeset
111 }
1ab23cd66870 Added result set handling. Needs some testing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 296
diff changeset
112
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
113 }
836
05bf8534a35a Using unix line endings only.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 807
diff changeset
114 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org