Skip to content

Commit 2b94c0a

Browse files
michalmackarussell
authored andcommitted
Fix point-edge calculations in DistanceCalc2D (shrink factor = 1) (graphhopper#1467)
* overrride calcShrinkFactor() in DistanceCalc2D to have fixed factor of 1 * add tests for overrriding calcShrinkFactor() in DistanceCalc2D * add throw UnsupportedOperationException for methods not applicable in flat 2D space
1 parent 760ac2b commit 2b94c0a

File tree

3 files changed

+87
-3
lines changed

3 files changed

+87
-3
lines changed

api/src/main/java/com/graphhopper/util/DistanceCalcEarth.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
*/
1818
package com.graphhopper.util;
1919

20+
import static java.lang.Math.*;
21+
2022
import com.graphhopper.util.shapes.BBox;
2123
import com.graphhopper.util.shapes.GHPoint;
2224

23-
import static java.lang.Math.*;
24-
2525
/**
2626
* @author Peter Karich
2727
*/
@@ -155,7 +155,7 @@ else if (factor < 0)
155155
return calcNormalizedDist(c_lat, c_lon / shrinkFactor, r_lat_deg, r_lon_deg);
156156
}
157157

158-
private double calcShrinkFactor(double a_lat_deg, double b_lat_deg) {
158+
double calcShrinkFactor(double a_lat_deg, double b_lat_deg) {
159159
return cos(toRadians((a_lat_deg + b_lat_deg) / 2));
160160
}
161161

core/src/main/java/com/graphhopper/util/DistanceCalc2D.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
import static java.lang.Math.sqrt;
2121

22+
import com.graphhopper.util.shapes.BBox;
23+
import com.graphhopper.util.shapes.GHPoint;
24+
2225
/**
2326
* Calculates the distance of two points or one point and an edge in euclidean space.
2427
* <p>
@@ -44,6 +47,10 @@ public double calcNormalizedDist(double dist) {
4447
return dist * dist;
4548
}
4649

50+
double calcShrinkFactor(double a_lat_deg, double b_lat_deg) {
51+
return 1.;
52+
}
53+
4754
/**
4855
* Calculates in normalized meter
4956
*/
@@ -58,4 +65,30 @@ public double calcNormalizedDist(double fromY, double fromX, double toY, double
5865
public String toString() {
5966
return "2D";
6067
}
68+
69+
@Override
70+
public double calcCircumference(double lat) {
71+
throw new UnsupportedOperationException("Not supported for the 2D Euclidean space");
72+
}
73+
74+
@Override
75+
public boolean isDateLineCrossOver(double lon1, double lon2) {
76+
throw new UnsupportedOperationException("Not supported for the 2D Euclidean space");
77+
}
78+
79+
@Override
80+
public BBox createBBox(double lat, double lon, double radiusInMeter) {
81+
throw new UnsupportedOperationException("Not supported for the 2D Euclidean space");
82+
}
83+
84+
@Override
85+
public GHPoint projectCoordinate(double latInDeg, double lonInDeg, double distanceInMeter,
86+
double headingClockwiseFromNorth) {
87+
throw new UnsupportedOperationException("Not supported for the 2D Euclidean space");
88+
}
89+
90+
@Override
91+
public boolean isCrossBoundary(double lon1, double lon2) {
92+
throw new UnsupportedOperationException("Not supported for the 2D Euclidean space");
93+
}
6194
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Licensed to GraphHopper GmbH under one or more contributor
3+
* license agreements. See the NOTICE file distributed with this work for
4+
* additional information regarding copyright ownership.
5+
*
6+
* GraphHopper GmbH licenses this file to you under the Apache License,
7+
* Version 2.0 (the "License"); you may not use this file except in
8+
* compliance with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package com.graphhopper.util;
20+
21+
import org.junit.Assert;
22+
import org.junit.Test;
23+
24+
import com.graphhopper.util.shapes.GHPoint;
25+
26+
public class DistanceCalc2DTest {
27+
28+
@Test
29+
public void testCrossingPointToEdge() {
30+
DistanceCalc2D distanceCalc = new DistanceCalc2D();
31+
GHPoint point = distanceCalc.calcCrossingPointToEdge(0, 10, 0, 0, 10, 10);
32+
Assert.assertEquals(5, point.getLat(), 0);
33+
Assert.assertEquals(5, point.getLon(), 0);
34+
}
35+
36+
@Test
37+
public void testCalcNormalizedEdgeDistance() {
38+
DistanceCalc2D distanceCalc = new DistanceCalc2D();
39+
double distance = distanceCalc.calcNormalizedEdgeDistance(0, 10, 0, 0, 10, 10);
40+
Assert.assertEquals(50, distance, 0);
41+
}
42+
43+
@Test
44+
public void testValidEdgeDistance() {
45+
DistanceCalc2D distanceCalc = new DistanceCalc2D();
46+
boolean validEdgeDistance = distanceCalc.validEdgeDistance(5, 15, 0, 0, 10, 10);
47+
Assert.assertEquals(false, validEdgeDistance);
48+
validEdgeDistance = distanceCalc.validEdgeDistance(15, 5, 0, 0, 10, 10);
49+
Assert.assertEquals(false, validEdgeDistance);
50+
}
51+
}

0 commit comments

Comments
 (0)