Skip to content

Commit 63ea993

Browse files
RSSP-1975: Add unit tests for GhCustomSpeeds (#44)
* RSSP-1975: Add unit tests for GhCustomSpeeds * Rm import
1 parent 5ff2ef3 commit 63ea993

File tree

1 file changed

+153
-19
lines changed

1 file changed

+153
-19
lines changed

core/src/test/java/com/graphhopper/GraphHopperCustomSpeedsTest.java

Lines changed: 153 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,22 @@ public void setup() {
5555
Helper.removeDir(new File(GH_LOCATION_CUSTOM_SPEEDS));
5656
}
5757

58-
class CustomWaySpeedProvider implements WaySpeedsProvider {
58+
@Test
59+
public void testDynamicSpeedProvider() {
5960

60-
@Override
61-
public Optional<SpeedKmByHour> speedForWay(long osmWayId) {
62-
return Optional.of(new SpeedKmByHour(10.10));
63-
}
61+
class CustomWaySpeedProvider implements WaySpeedsProvider {
62+
63+
@Override
64+
public Optional<SpeedKmByHour> speedForWay(long osmWayId) {
65+
return Optional.of(new SpeedKmByHour(10.10));
66+
}
6467

65-
@Override
66-
public Optional<SpeedKmByHour> speedForRoadClass(RoadClass roadClass) {
67-
return Optional.of(new SpeedKmByHour(10.1));
68+
@Override
69+
public Optional<SpeedKmByHour> speedForRoadClass(RoadClass roadClass) {
70+
return Optional.of(new SpeedKmByHour(10.1));
71+
}
6872
}
69-
}
7073

71-
@Test
72-
public void testDynamicSpeedProvider() {
7374
final String bikeProfile = "bike_profile";
7475
final String carProfile = "car_profile";
7576
List<Profile> profiles = asList(
@@ -101,36 +102,169 @@ public void testDynamicSpeedProvider() {
101102
hopper.importOrLoad();
102103
hopperCustomSpeeds.importOrLoad();
103104

104-
GHResponse rsp = hopper.route(new GHRequest(43.73005, 7.415707, 43.741522, 7.42826)
105-
.setProfile(carProfile));
105+
GHRequest request = new GHRequest(43.73005, 7.415707, 43.741522, 7.42826);
106+
107+
GHResponse rsp = hopper.route(request.setProfile(carProfile));
106108
ResponsePath res = rsp.getBest();
107109
assertFalse(rsp.hasErrors(), rsp.getErrors().toString());
108110
assertEquals(205, res.getTime() / 1000f, 1);
109111
assertEquals(2837, res.getDistance(), 1);
110112

111-
GHResponse rspSpeeds = hopperCustomSpeeds.route(new GHRequest(43.73005, 7.415707, 43.741522, 7.42826)
112-
.setProfile(carProfile));
113+
GHResponse rspSpeeds = hopperCustomSpeeds.route(request.setProfile(carProfile));
113114
ResponsePath resSpeeds = rspSpeeds.getBest();
114115
assertFalse(rspSpeeds.hasErrors(), rspSpeeds.getErrors().toString());
115116
assertEquals(893, resSpeeds.getTime() / 1000f, 1);
116117
assertEquals(2481, resSpeeds.getDistance(), 1);
117118

118119

119-
GHResponse rspBike = hopper.route(new GHRequest(43.73005, 7.415707, 43.741522, 7.42826)
120-
.setProfile(bikeProfile));
120+
GHResponse rspBike = hopper.route(request.setProfile(bikeProfile));
121121
ResponsePath resBike = rspBike.getBest();
122122
assertFalse(rspBike.hasErrors(), rspBike.getErrors().toString());
123123
assertEquals(536, resBike.getTime() / 1000f, 1);
124124
assertEquals(2521, resBike.getDistance(), 1);
125125

126-
GHResponse rspBikeSpeeds = hopperCustomSpeeds.route(new GHRequest(43.73005, 7.415707, 43.741522, 7.42826)
127-
.setProfile(bikeProfile));
126+
GHResponse rspBikeSpeeds = hopperCustomSpeeds.route(request.setProfile(bikeProfile));
128127
ResponsePath resBikeSpeeds = rspBikeSpeeds.getBest();
129128
assertFalse(rspBikeSpeeds.hasErrors(), rspBikeSpeeds.getErrors().toString());
130129
assertEquals(834, resBikeSpeeds.getTime() / 1000f, 1);
131130
assertEquals(2318, resBikeSpeeds.getDistance(), 1);
132131

133132
}
134133

134+
@Test
135+
public void testDynamicSpeedProviderDiscardCustomSpeedIfHigherThanMaxSpeed() {
136+
137+
class SpeedsTooHighSpeedProvider implements WaySpeedsProvider {
138+
139+
@Override
140+
public Optional<SpeedKmByHour> speedForWay(long osmWayId) {
141+
return Optional.of(new SpeedKmByHour(100));
142+
}
143+
144+
@Override
145+
public Optional<SpeedKmByHour> speedForRoadClass(RoadClass roadClass) {
146+
return Optional.of(new SpeedKmByHour(80.1));
147+
}
148+
}
149+
150+
final String carProfile = "car_profile";
151+
152+
List<Profile> profiles = asList(new Profile(carProfile).setVehicle("car"));
153+
154+
GraphHopper hopper = new GraphHopper().
155+
setGraphHopperLocation(GH_LOCATION).
156+
setOSMFile(MONACO).
157+
setProfiles(profiles).
158+
setStoreOnFlush(true);
159+
hopper.getCHPreparationHandler().setCHProfiles(
160+
new CHProfile(carProfile)
161+
);
162+
163+
GraphHopper hopperCustomSpeeds = new GraphHopperCustomSpeeds(new SpeedsTooHighSpeedProvider()).
164+
setGraphHopperLocation(GH_LOCATION_CUSTOM_SPEEDS).
165+
setOSMFile(MONACO).
166+
setProfiles(profiles).
167+
setStoreOnFlush(true)
168+
.setEncodedValuesString("roundabout, road_class, road_class_link, road_environment, max_speed, road_access, ferry_speed, bike_network, get_off_bike, smoothness, osm_way_id");
169+
hopperCustomSpeeds.getCHPreparationHandler().setCHProfiles(
170+
new CHProfile(carProfile)
171+
);
172+
173+
hopper.importOrLoad();
174+
hopperCustomSpeeds.importOrLoad();
175+
176+
// requests uses osm way 166009792 (secondary road, that has max speed = 50km)
177+
GHRequest request = new GHRequest(43.73887, 7.42074, 43.73992, 7.42386);
178+
179+
GHResponse rsp = hopper.route(request.setProfile(carProfile));
180+
ResponsePath res = rsp.getBest();
181+
assertFalse(rsp.hasErrors(), rsp.getErrors().toString());
182+
assertEquals(21, res.getTime() / 1000f, 1); // 47kmh = 13.2m/s
183+
assertEquals(277, res.getDistance(), 1);
184+
185+
GHResponse rspSpeeds = hopperCustomSpeeds.route(request
186+
.setProfile(carProfile));
187+
ResponsePath resSpeeds = rspSpeeds.getBest();
188+
assertFalse(rspSpeeds.hasErrors(), rspSpeeds.getErrors().toString());
189+
assertEquals(21, resSpeeds.getTime() / 1000f, 1); // 47kmh = 13.2m/s
190+
assertEquals(277, resSpeeds.getDistance(), 1);
191+
192+
// the result with and without custom speeds should be the same when the custom speed is > than the max_speed
193+
assertEquals(res.getTime() / 1000f, resSpeeds.getTime() / 1000f, 1);
194+
195+
196+
}
197+
198+
@Test
199+
public void testDynamicSpeedProviderDiscardCustomSpeedIfHigherThanMaxEncoderSpeed() {
200+
201+
// For bike, the max encoder speed is 30kmh
202+
// For car, the max encoder speed is 254 kmh
203+
// We are trying to set a custom speed for bike > 30kmh, that is the limit for the bike
204+
// encoder, so we expect it to be discarded, and we expect that the default speed
205+
// for those roads will be used
206+
207+
class SpeedsTooHighSpeedProvider implements WaySpeedsProvider {
208+
209+
@Override
210+
public Optional<SpeedKmByHour> speedForWay(long osmWayId) {
211+
return Optional.of(new SpeedKmByHour(100));
212+
}
213+
214+
@Override
215+
public Optional<SpeedKmByHour> speedForRoadClass(RoadClass roadClass) {
216+
return Optional.of(new SpeedKmByHour(80.1));
217+
}
218+
}
219+
220+
final String bikeProfile = "bike_profile";
221+
222+
List<Profile> profiles = asList(
223+
new Profile(bikeProfile).setVehicle("bike")
224+
);
225+
226+
GraphHopper hopper = new GraphHopper().
227+
setGraphHopperLocation(GH_LOCATION).
228+
setOSMFile(MONACO).
229+
setProfiles(profiles).
230+
setStoreOnFlush(true);
231+
hopper.getCHPreparationHandler().setCHProfiles(
232+
new CHProfile(bikeProfile)
233+
);
234+
235+
GraphHopper hopperCustomSpeeds = new GraphHopperCustomSpeeds(new SpeedsTooHighSpeedProvider()).
236+
setGraphHopperLocation(GH_LOCATION_CUSTOM_SPEEDS).
237+
setOSMFile(MONACO).
238+
setProfiles(profiles).
239+
setStoreOnFlush(true)
240+
.setEncodedValuesString("roundabout, road_class, road_class_link, road_environment, max_speed, road_access, ferry_speed, bike_network, get_off_bike, smoothness, osm_way_id");
241+
hopperCustomSpeeds.getCHPreparationHandler().setCHProfiles(
242+
new CHProfile(bikeProfile)
243+
);
244+
245+
hopper.importOrLoad();
246+
hopperCustomSpeeds.importOrLoad();
247+
248+
GHRequest request = new GHRequest(43.73887, 7.42074, 43.73992, 7.42386);
249+
250+
GHResponse rspBike = hopper.route(request
251+
.setProfile(bikeProfile));
252+
ResponsePath resBike = rspBike.getBest();
253+
assertFalse(rspBike.hasErrors(), rspBike.getErrors().toString());
254+
assertEquals(55, resBike.getTime() / 1000f, 1); // 18kmh = 5 m/s
255+
assertEquals(277, resBike.getDistance(), 1);
256+
257+
GHResponse rspBikeSpeeds = hopperCustomSpeeds.route(request
258+
.setProfile(bikeProfile));
259+
ResponsePath resBikeSpeeds = rspBikeSpeeds.getBest();
260+
assertFalse(rspBikeSpeeds.hasErrors(), rspBikeSpeeds.getErrors().toString());
261+
assertEquals(55, resBikeSpeeds.getTime() / 1000f, 1); // 17kmh = 4.7 m/s
262+
assertEquals(277, resBikeSpeeds.getDistance(), 1);
263+
264+
// the result with and without custom speeds should be the same when the custom speed is > than the encoder limit
265+
assertEquals(resBike.getTime() / 1000f, resBikeSpeeds.getTime() / 1000f, 1);
266+
267+
}
268+
135269
}
136270

0 commit comments

Comments
 (0)