Skip to content

Commit ca0518d

Browse files
author
Peter
committed
Merge pull request graphhopper#130 from lmar/extended_instructions
Extended instructions
2 parents a837e22 + 0075975 commit ca0518d

File tree

10 files changed

+403
-271
lines changed

10 files changed

+403
-271
lines changed

core/src/main/java/com/graphhopper/GHResponse.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
*/
1818
package com.graphhopper;
1919

20+
import com.graphhopper.util.Instruction;
2021
import com.graphhopper.util.PointList;
21-
import com.graphhopper.util.InstructionList;
2222
import com.graphhopper.util.shapes.BBox;
23+
2324
import java.text.SimpleDateFormat;
2425
import java.util.ArrayList;
2526
import java.util.Date;
@@ -37,7 +38,7 @@ public class GHResponse
3738
private long time;
3839
private String debugInfo = "";
3940
private List<Throwable> errors = new ArrayList<Throwable>(4);
40-
private InstructionList instructions = new InstructionList(0);
41+
private List<Instruction> instructions = new ArrayList<Instruction>(0);
4142
private boolean found;
4243

4344
public GHResponse()
@@ -156,12 +157,12 @@ public String toString()
156157
return "found:" + isFound() + ", nodes:" + list.getSize() + ": " + list.toString();
157158
}
158159

159-
public void setInstructions( InstructionList instructions )
160+
public void setInstructions( List<Instruction> instructions )
160161
{
161162
this.instructions = instructions;
162163
}
163164

164-
public InstructionList getInstructions()
165+
public List<Instruction> getInstructions()
165166
{
166167
return instructions;
167168
}

core/src/main/java/com/graphhopper/routing/Path.java

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
import gnu.trove.list.TIntList;
2525
import gnu.trove.list.array.TIntArrayList;
2626

27+
import java.util.ArrayList;
28+
import java.util.List;
29+
2730
/**
2831
* Stores the nodes for the found path of an algorithm. It additionally needs the edgeIds to make
2932
* edge determination faster and less complex as there could be several edges (u,v) especially for
@@ -47,7 +50,7 @@ public class Path
4750
protected int endNode = -1;
4851
private TIntList edgeIds;
4952
private PointList cachedPoints;
50-
private InstructionList cachedWays;
53+
private List<Instruction> cachedWays;
5154
private double weight;
5255

5356
public Path( Graph graph, FlagEncoder encoder )
@@ -300,12 +303,12 @@ public void next( EdgeIteratorState eb, int i )
300303
/**
301304
* @return the cached list of ways for this path
302305
*/
303-
public InstructionList calcInstructions()
306+
public List<Instruction> calcInstructions()
304307
{
305308
if (cachedWays != null)
306309
return cachedWays;
307310

308-
cachedWays = new InstructionList(edgeIds.size() / 4);
311+
cachedWays = new ArrayList<Instruction>(edgeIds.size() / 4);
309312
if (edgeIds.isEmpty())
310313
return cachedWays;
311314

@@ -337,6 +340,7 @@ public InstructionList calcInstructions()
337340
double prevLon = graph.getLongitude(tmpNode);
338341
double prevOrientation;
339342
double prevDist;
343+
long prevTime;
340344

341345
@Override
342346
public void next( EdgeIteratorState edgeBase, int index )
@@ -366,7 +370,8 @@ public void next( EdgeIteratorState edgeBase, int index )
366370
{
367371
name = edgeBase.getName();
368372
prevDist = calcDistance(edgeBase);
369-
cachedWays.add(InstructionList.CONTINUE_ON_STREET, name, prevDist);
373+
prevTime = calcTime(prevDist, edgeBase.getFlags());
374+
cachedWays.add(new Instruction(Instruction.CONTINUE_ON_STREET, name, prevDist, prevTime, prevLat, prevLon));
370375
} else
371376
{
372377
double tmpOrientation;
@@ -388,41 +393,46 @@ public void next( EdgeIteratorState edgeBase, int index )
388393
String tmpName = edgeBase.getName();
389394
if (!name.equals(tmpName))
390395
{
391-
cachedWays.updateLastDistance(prevDist);
396+
InstructionUtil.updateLastDistanceAndTime(cachedWays, prevDist, prevTime);
392397
prevDist = calcDistance(edgeBase);
398+
prevTime = calcTime(prevDist, edgeBase.getFlags());
393399
name = tmpName;
394400
double delta = Math.abs(tmpOrientation - prevOrientation);
395401
if (delta < 0.2)
396402
{
397403
// 0.2 ~= 11°
398-
cachedWays.add(InstructionList.CONTINUE_ON_STREET, name, prevDist);
404+
cachedWays.add(new Instruction(Instruction.CONTINUE_ON_STREET, name, prevDist, prevTime, prevLat, prevLon));
399405

400406
} else if (delta < 0.8)
401407
{
402408
// 0.8 ~= 40°
403409
if (tmpOrientation > prevOrientation)
404-
cachedWays.add(InstructionList.TURN_SLIGHT_LEFT, name, prevDist);
410+
cachedWays.add(new Instruction(Instruction.TURN_SLIGHT_LEFT, name, prevDist, prevTime, prevLat, prevLon));
405411
else
406-
cachedWays.add(InstructionList.TURN_SLIGHT_RIGHT, name, prevDist);
412+
cachedWays.add(new Instruction(Instruction.TURN_SLIGHT_RIGHT, name, prevDist, prevTime, prevLat, prevLon));
407413

408414
} else if (delta < 1.8)
409415
{
410416
// 1.8 ~= 103°
411417
if (tmpOrientation > prevOrientation)
412-
cachedWays.add(InstructionList.TURN_LEFT, name, prevDist);
418+
cachedWays.add(new Instruction(Instruction.TURN_LEFT, name, prevDist, prevTime, prevLat, prevLon));
413419
else
414-
cachedWays.add(InstructionList.TURN_RIGHT, name, prevDist);
420+
cachedWays.add(new Instruction(Instruction.TURN_RIGHT, name, prevDist, prevTime, prevLat, prevLon));
415421

416422
} else
417423
{
418424
if (tmpOrientation > prevOrientation)
419-
cachedWays.add(InstructionList.TURN_SHARP_LEFT, name, prevDist);
425+
cachedWays.add(new Instruction(Instruction.TURN_SHARP_LEFT, name, prevDist, prevTime, prevLat, prevLon));
420426
else
421-
cachedWays.add(InstructionList.TURN_SHARP_RIGHT, name, prevDist);
427+
cachedWays.add(new Instruction(Instruction.TURN_SHARP_RIGHT, name, prevDist, prevTime, prevLat, prevLon));
422428

423429
}
424430
} else
425-
prevDist += calcDistance(edgeBase);
431+
{
432+
double tmpDist = calcDistance(edgeBase);
433+
prevDist += tmpDist;
434+
prevTime += calcTime(tmpDist, edgeBase.getFlags());
435+
}
426436
}
427437

428438
prevLat = baseLat;
@@ -434,7 +444,7 @@ public void next( EdgeIteratorState edgeBase, int index )
434444

435445
boolean lastEdgeIter = index == edgeIds.size() - 1;
436446
if (lastEdgeIter)
437-
cachedWays.updateLastDistance(prevDist);
447+
InstructionUtil.updateLastDistanceAndTime(cachedWays, prevDist, prevTime);
438448
}
439449
});
440450

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.graphhopper.util;
2+
3+
public class Instruction
4+
{
5+
public static final int TURN_SHARP_LEFT = -3;
6+
public static final int TURN_LEFT = -2;
7+
public static final int TURN_SLIGHT_LEFT = -1;
8+
public static final int CONTINUE_ON_STREET = 0;
9+
public static final int TURN_SLIGHT_RIGHT = 1;
10+
public static final int TURN_RIGHT = 2;
11+
public static final int TURN_SHARP_RIGHT = 3;
12+
private int indication;
13+
private String name;
14+
private double distance;
15+
private long time;
16+
private double lat;
17+
private double lon;
18+
19+
public Instruction(int indication, String name, double distance, long time, double lat, double lon)
20+
{
21+
this.indication = indication;
22+
this.name = name;
23+
this.distance = distance;
24+
this.time = time;
25+
this.lat = lat;
26+
this.lon = lon;
27+
}
28+
29+
public int getIndication()
30+
{
31+
return indication;
32+
}
33+
34+
public String getName()
35+
{
36+
return name;
37+
}
38+
39+
public double getDistance()
40+
{
41+
return distance;
42+
}
43+
44+
public long getTime()
45+
{
46+
return time;
47+
}
48+
49+
public void setDistance(double distance)
50+
{
51+
this.distance = distance;
52+
}
53+
54+
public void setTime(long time)
55+
{
56+
this.time = time;
57+
}
58+
59+
public double getLat()
60+
{
61+
return lat;
62+
}
63+
64+
public double getLon()
65+
{
66+
return lon;
67+
}
68+
69+
@Override
70+
public String toString()
71+
{
72+
StringBuilder sb = new StringBuilder();
73+
sb.append('(');
74+
sb.append(indication);
75+
sb.append(',');
76+
sb.append(name);
77+
sb.append(',');
78+
sb.append(distance);
79+
sb.append(',');
80+
sb.append(time);
81+
sb.append(')');
82+
83+
return sb.toString();
84+
}
85+
}

0 commit comments

Comments
 (0)