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