Skip to content

Commit e0955f5

Browse files
author
Peter
committed
fixing GraphHopperWeb bug graphhopper#359 and improving this interface, minor refactoring necessary
1 parent f8287d4 commit e0955f5

File tree

9 files changed

+150
-36
lines changed

9 files changed

+150
-36
lines changed

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

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

20-
import com.graphhopper.storage.NodeAccess;
21-
2220
/**
2321
* @author Peter Karich
2422
*/
2523
public class FinishInstruction extends Instruction
2624
{
27-
private int count = -1;
28-
2925
public FinishInstruction( final double lat, final double lon, final double ele )
3026
{
3127
super(FINISH, "", InstructionAnnotation.EMPTY, new PointList(2, true)
@@ -36,20 +32,18 @@ public FinishInstruction( final double lat, final double lon, final double ele )
3632
});
3733
}
3834

39-
public FinishInstruction( NodeAccess nodeAccess, int node )
35+
public FinishInstruction( PointAccess pointAccess, int node )
4036
{
41-
this(nodeAccess.getLatitude(node), nodeAccess.getLongitude(node),
42-
nodeAccess.is3D() ? nodeAccess.getElevation(node) : 0);
37+
this(pointAccess.getLatitude(node), pointAccess.getLongitude(node),
38+
pointAccess.is3D() ? pointAccess.getElevation(node) : 0);
4339
}
4440

45-
void setVia( int i )
41+
@Override
42+
public String getTurnDescription( Translation tr )
4643
{
47-
sign = REACHED_VIA;
48-
count = i;
49-
}
44+
if (rawName)
45+
return getName();
5046

51-
public int getViaPosition()
52-
{
53-
return count;
47+
return tr.tr("finish");
5448
}
5549
}

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
package com.graphhopper.util;
1919

2020
import java.util.Collections;
21-
import java.util.HashMap;
2221
import java.util.List;
2322
import java.util.Map;
2423

@@ -38,6 +37,7 @@ public class Instruction
3837
public static final int REACHED_VIA = 5;
3938
public static final int USE_ROUNDABOUT = 6;
4039

40+
protected boolean rawName;
4141
protected int sign;
4242
protected String name;
4343
protected double distance;
@@ -49,14 +49,23 @@ public class Instruction
4949
* The points, distances and times have exactly the same count. The last point of this
5050
* instruction is not duplicated here and should be in the next one.
5151
*/
52-
public Instruction( int sign, String name, InstructionAnnotation ia, PointList pl)
52+
public Instruction( int sign, String name, InstructionAnnotation ia, PointList pl )
5353
{
5454
this.sign = sign;
5555
this.name = name;
5656
this.points = pl;
5757
this.annotation = ia;
5858
}
5959

60+
/**
61+
* This method does not perform translation or combination with the sign - it just uses the
62+
* provided name as instruction.
63+
*/
64+
public void setUseRawName()
65+
{
66+
rawName = true;
67+
}
68+
6069
public InstructionAnnotation getAnnotation()
6170
{
6271
return annotation;
@@ -75,17 +84,17 @@ public String getName()
7584
return name;
7685
}
7786

78-
public void setName(String name)
87+
public void setName( String name )
7988
{
8089
this.name = name;
8190
}
8291

83-
public Map<String,Object> getExtraInfoJSON()
92+
public Map<String, Object> getExtraInfoJSON()
8493
{
8594
return Collections.<String, Object>emptyMap();
8695
}
8796

88-
public void setExtraInfo(String key, Object value)
97+
public void setExtraInfo( String key, Object value )
8998
{
9099
throw new IllegalArgumentException("Key" + key + " is not a valid option");
91100
}
@@ -244,16 +253,13 @@ void checkOne()
244253

245254
public String getTurnDescription( Translation tr )
246255
{
256+
if (rawName)
257+
return getName();
258+
247259
String str;
248260
String streetName = getName();
249261
int indi = getSign();
250-
if (indi == Instruction.FINISH)
251-
{
252-
str = tr.tr("finish");
253-
} else if (indi == Instruction.REACHED_VIA)
254-
{
255-
str = tr.tr("stopover", ((FinishInstruction) this).getViaPosition());
256-
} else if (indi == Instruction.CONTINUE_ON_STREET)
262+
if (indi == Instruction.CONTINUE_ON_STREET)
257263
{
258264
str = Helper.isEmpty(streetName) ? tr.tr("continue") : tr.tr("continue_onto", streetName);
259265
} else

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ public InstructionList( int cap, Translation tr )
4646
this.tr = tr;
4747
}
4848

49+
public void replaceLast( Instruction instr )
50+
{
51+
if (instructions.isEmpty())
52+
throw new IllegalStateException("Cannot replace last instruction as list is empty");
53+
54+
instructions.set(instructions.size() - 1, instr);
55+
}
56+
4957
public void add( Instruction instr )
5058
{
5159
instructions.add(instr);

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ public void doWork( GHResponse rsp, List<Path> paths, Translation tr )
8181
// if not yet reached finish replace with 'reached via'
8282
if (pathIndex + 1 < paths.size())
8383
{
84-
FinishInstruction fi = (FinishInstruction) fullInstructions.get(fullInstructions.size() - 1);
85-
fi.setVia(pathIndex + 1);
84+
ViaInstruction newInstr = new ViaInstruction(fullInstructions.get(fullInstructions.size() - 1));
85+
newInstr.setViaCount(pathIndex + 1);
86+
fullInstructions.replaceLast(newInstr);
8687
}
8788
}
8889

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
public class RoundaboutInstruction extends Instruction
1010
{
1111
private int exitNumber = 0;
12-
private int clockwise = 0; // 0 undetermined, 1 clockwise, -1 counterclockwise, 2 inconsistent
12+
// 0 undetermined, 1 clockwise, -1 counterclockwise, 2 inconsistent
13+
private int clockwise = 0;
1314
private boolean exited = false;
1415
private double radian = Double.NaN;
1516

@@ -107,6 +108,9 @@ public Map<String, Object> getExtraInfoJSON()
107108
@Override
108109
public String getTurnDescription( Translation tr )
109110
{
111+
if (rawName)
112+
return getName();
113+
110114
String str;
111115
String streetName = getName();
112116
int indi = getSign();
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2015 Peter Karich.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.graphhopper.util;
17+
18+
/**
19+
*
20+
* @author Peter Karich
21+
*/
22+
public class ViaInstruction extends Instruction
23+
{
24+
private int viaPosition = -1;
25+
26+
public ViaInstruction( String name, InstructionAnnotation ia, PointList pl )
27+
{
28+
super(REACHED_VIA, name, ia, pl);
29+
}
30+
31+
public ViaInstruction( Instruction instr )
32+
{
33+
this(instr.getName(), instr.getAnnotation(), instr.getPoints());
34+
setDistance(instr.getDistance());
35+
setTime(instr.getTime());
36+
}
37+
38+
public void setViaCount( int count )
39+
{
40+
this.viaPosition = count;
41+
}
42+
43+
public int getViaCount()
44+
{
45+
return viaPosition;
46+
}
47+
48+
@Override
49+
public String getTurnDescription( Translation tr )
50+
{
51+
if (rawName)
52+
return getName();
53+
54+
return tr.tr("stopover", viaPosition);
55+
}
56+
}

web/src/main/java/com/graphhopper/http/GraphHopperWeb.java

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222
import com.graphhopper.GraphHopperAPI;
2323
import com.graphhopper.util.*;
2424
import com.graphhopper.util.shapes.GHPoint;
25-
import java.util.Arrays;
2625
import org.json.JSONArray;
2726
import org.json.JSONObject;
2827
import org.slf4j.Logger;
2928
import org.slf4j.LoggerFactory;
3029

3130
/**
32-
* Main wrapper of the offline API for a simple and efficient usage.
31+
* This class is nearly identical in usage as the offline API but connects to a remove routing
32+
* service like self-hosted or the GraphHopper Directions API.
3333
* <p/>
3434
* @author Peter Karich
3535
*/
@@ -117,6 +117,7 @@ public GHResponse route( GHRequest request )
117117
+ places
118118
+ "&type=json"
119119
+ "&points_encoded=" + pointsEncoded
120+
+ "&instructions=" + instructions
120121
+ "&way_point_max_distance=" + request.getHints().getDouble("wayPointMaxDistance", 1)
121122
+ "&algo=" + request.getAlgorithm()
122123
+ "&locale=" + request.getLocale().toString()
@@ -195,6 +196,7 @@ public GHResponse route( GHRequest request )
195196
JSONArray instrArr = firstPath.getJSONArray("instructions");
196197

197198
InstructionList il = new InstructionList(trMap.getWithFallBack(request.getLocale()));
199+
int viaCount = 1;
198200
for (int instrIndex = 0; instrIndex < instrArr.length(); instrIndex++)
199201
{
200202
JSONObject jsonObj = instrArr.getJSONObject(instrIndex);
@@ -211,9 +213,35 @@ public GHResponse route( GHRequest request )
211213
instPL.add(pointList, j);
212214
}
213215

214-
// TODO way and payment type
215-
Instruction instr = new Instruction(sign, text, InstructionAnnotation.EMPTY, instPL).
216-
setDistance(instDist).setTime(instTime);
216+
InstructionAnnotation ia = InstructionAnnotation.EMPTY;
217+
if (jsonObj.has("annotation_importance") && jsonObj.has("annotation_text"))
218+
{
219+
ia = new InstructionAnnotation(jsonObj.getInt("annotation_importance"), jsonObj.getString("annotation_text"));
220+
}
221+
222+
Instruction instr;
223+
if (sign == Instruction.USE_ROUNDABOUT || sign == Instruction.LEAVE_ROUNDABOUT)
224+
{
225+
instr = new RoundaboutInstruction(sign, text, ia, instPL);
226+
} else if (sign == Instruction.REACHED_VIA)
227+
{
228+
ViaInstruction tmpInstr = new ViaInstruction(text, ia, instPL);
229+
tmpInstr.setViaCount(viaCount);
230+
viaCount++;
231+
instr = tmpInstr;
232+
} else if (sign == Instruction.FINISH)
233+
{
234+
instr = new FinishInstruction(instPL, 0);
235+
} else
236+
{
237+
instr = new Instruction(sign, text, ia, instPL);
238+
}
239+
240+
// The translation is done from the routing service so just use the provided string
241+
// instead of creating a combination with sign and name etc
242+
instr.setUseRawName();
243+
244+
instr.setDistance(instDist).setTime(instTime);
217245
il.add(instr);
218246
}
219247
res.setInstructions(il);

web/src/test/java/com/graphhopper/http/GraphHopperServletIT.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
import com.graphhopper.GraphHopperAPI;
2323
import com.graphhopper.util.CmdArgs;
2424
import com.graphhopper.util.Helper;
25+
import com.graphhopper.util.shapes.GHPoint;
2526
import java.io.File;
27+
import java.util.List;
28+
import java.util.Map;
2629
import org.json.JSONObject;
2730
import org.junit.AfterClass;
2831
import org.junit.Before;
@@ -91,6 +94,19 @@ public void testGraphHopperWeb() throws Exception
9194
assertTrue(rsp.getErrors().toString(), rsp.getErrors().isEmpty());
9295
assertTrue("distance wasn't correct:" + rsp.getDistance(), rsp.getDistance() > 9000);
9396
assertTrue("distance wasn't correct:" + rsp.getDistance(), rsp.getDistance() < 9500);
97+
98+
rsp = hopper.route(new GHRequest().
99+
addPoint(new GHPoint(42.554851, 1.536198)).
100+
addPoint(new GHPoint(42.531896, 1.553278)).
101+
addPoint(new GHPoint(42.510071, 1.548128)));
102+
assertTrue(rsp.getErrors().toString(), rsp.getErrors().isEmpty());
103+
assertTrue("distance wasn't correct:" + rsp.getDistance(), rsp.getDistance() > 20000);
104+
assertTrue("distance wasn't correct:" + rsp.getDistance(), rsp.getDistance() < 21000);
105+
106+
List<Map<String, Object>> instructions = rsp.getInstructions().createJson();
107+
assertEquals(23, instructions.size());
108+
assertEquals("Continue onto la Callisa", instructions.get(0).get("text"));
109+
assertEquals("At roundabout, take exit 2", instructions.get(3).get("text"));
94110
}
95111

96112
@Test
@@ -108,7 +124,7 @@ public void testGraphHopperWebRealExceptions()
108124

109125
ex = rsp.getErrors().get(0);
110126
assertTrue("Wrong Exception found: " + ex.getClass().getName()
111-
+ ", IllegalStateException expected.", ex instanceof IllegalStateException);
127+
+ ", IllegalStateException expected.", ex instanceof IllegalStateException);
112128

113129
// IllegalArgumentException (Wrong Points)
114130
rsp = hopper.route(new GHRequest(0.0, 0.0, 0.0, 0.0));

web/src/test/java/com/graphhopper/http/GraphHopperWebTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
*/
3232
public class GraphHopperWebTest
3333
{
34-
34+
// see also GraphHopperServletIT.testGraphHopperWeb for real routes against local jetty service
35+
3536
@Test
3637
public void testReadUnencoded() throws Exception
3738
{

0 commit comments

Comments
 (0)