Skip to content

Commit 6629569

Browse files
author
Peter
committed
new alternative route feature, merging graphhopper#640
1 parent cdf6fd7 commit 6629569

35 files changed

+2268
-496
lines changed

core/files/changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
0.6
2+
GHResponse now wraps multiple AltResponse; renames GraphHopper.getPaths to calcPaths as 'get' sounds too cheap; a new method RoutingAlgorithm.calcPaths is added; see #596
23
moving lgpl licensed file into own submodule graphhopper-tools-lgpl
34
renaming of Tarjans algorithm class to TarjansSCCAlgorithm
45
more strict naming for Weighting enforced and more strict matching to select Weighting (equals check), #490
Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
/*
2+
* Licensed to GraphHopper and Peter Karich 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 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+
package com.graphhopper;
19+
20+
import com.graphhopper.util.InstructionList;
21+
import com.graphhopper.util.PointList;
22+
import com.graphhopper.util.shapes.BBox;
23+
import java.util.ArrayList;
24+
import java.util.Collections;
25+
import java.util.List;
26+
27+
/**
28+
* This class holds one possibility of a route
29+
* <p/>
30+
* @author Peter Karich
31+
*/
32+
public class AltResponse
33+
{
34+
private List<String> description;
35+
private double distance;
36+
private double ascend;
37+
private double descend;
38+
private double routeWeight;
39+
private long time;
40+
private String debugInfo = "";
41+
private InstructionList instructions;
42+
private PointList list = PointList.EMPTY;
43+
private final List<Throwable> errors = new ArrayList<Throwable>(4);
44+
45+
/**
46+
* @return the description of this route alternative to make it meaningful for the user e.g. it
47+
* displays one or two main roads of the route.
48+
*/
49+
public List<String> getDescription()
50+
{
51+
if (description == null)
52+
return Collections.emptyList();
53+
return description;
54+
}
55+
56+
public AltResponse setDescription( List<String> names )
57+
{
58+
this.description = names;
59+
return this;
60+
}
61+
62+
public AltResponse addDebugInfo( String debugInfo )
63+
{
64+
if (debugInfo == null)
65+
throw new IllegalStateException("Debug information has to be none null");
66+
67+
if (!this.debugInfo.isEmpty())
68+
this.debugInfo += ";";
69+
70+
this.debugInfo += debugInfo;
71+
return this;
72+
}
73+
74+
public String getDebugInfo()
75+
{
76+
return debugInfo;
77+
}
78+
79+
public AltResponse setPoints( PointList points )
80+
{
81+
list = points;
82+
return this;
83+
}
84+
85+
/**
86+
* This method returns all points on the path. Keep in mind that calculating the distance from
87+
* these points might yield different results compared to getDistance as points could have been
88+
* simplified on import or after querying.
89+
*/
90+
public PointList getPoints()
91+
{
92+
check("getPoints");
93+
return list;
94+
}
95+
96+
public AltResponse setDistance( double distance )
97+
{
98+
this.distance = distance;
99+
return this;
100+
}
101+
102+
/**
103+
* This method returns the distance of the path. Always prefer this method over
104+
* getPoints().calcDistance
105+
* <p>
106+
* @return distance in meter
107+
*/
108+
public double getDistance()
109+
{
110+
check("getDistance");
111+
return distance;
112+
}
113+
114+
public AltResponse setAscend( double ascend )
115+
{
116+
if (ascend < 0)
117+
throw new IllegalArgumentException("ascend has to be strictly positive");
118+
119+
this.ascend = ascend;
120+
return this;
121+
}
122+
123+
/**
124+
* This method returns the total elevation change (going upwards) in meter.
125+
* <p>
126+
* @return ascend in meter
127+
*/
128+
public double getAscend()
129+
{
130+
return ascend;
131+
}
132+
133+
public AltResponse setDescend( double descend )
134+
{
135+
if (descend < 0)
136+
throw new IllegalArgumentException("descend has to be strictly positive");
137+
138+
this.descend = descend;
139+
return this;
140+
}
141+
142+
/**
143+
* This method returns the total elevation change (going downwards) in meter.
144+
* <p>
145+
* @return decline in meter
146+
*/
147+
public double getDescend()
148+
{
149+
return descend;
150+
}
151+
152+
public AltResponse setTime( long timeInMillis )
153+
{
154+
this.time = timeInMillis;
155+
return this;
156+
}
157+
158+
/**
159+
* @return time in millis
160+
*/
161+
public long getTime()
162+
{
163+
check("getTimes");
164+
return time;
165+
}
166+
167+
public AltResponse setRouteWeight( double weight )
168+
{
169+
this.routeWeight = weight;
170+
return this;
171+
}
172+
173+
/**
174+
* This method returns a double value which is better than the time for comparison of routes but
175+
* only if you know what you are doing, e.g. only to compare routes gained with the same query
176+
* parameters like vehicle.
177+
*/
178+
public double getRouteWeight()
179+
{
180+
check("getRouteWeight");
181+
return routeWeight;
182+
}
183+
184+
/**
185+
* Calculates the bounding box of this route response
186+
*/
187+
public BBox calcRouteBBox( BBox _fallback )
188+
{
189+
check("calcRouteBBox");
190+
BBox bounds = BBox.createInverse(_fallback.hasElevation());
191+
int len = list.getSize();
192+
if (len == 0)
193+
return _fallback;
194+
195+
for (int i = 0; i < len; i++)
196+
{
197+
double lat = list.getLatitude(i);
198+
double lon = list.getLongitude(i);
199+
if (bounds.hasElevation())
200+
{
201+
double ele = list.getEle(i);
202+
bounds.update(lat, lon, ele);
203+
} else
204+
{
205+
bounds.update(lat, lon);
206+
}
207+
}
208+
return bounds;
209+
}
210+
211+
@Override
212+
public String toString()
213+
{
214+
String str = "nodes:" + list.getSize() + "; " + list.toString();
215+
if (instructions != null && !instructions.isEmpty())
216+
str += ", " + instructions.toString();
217+
218+
if (hasErrors())
219+
str += ", " + errors.toString();
220+
221+
return str;
222+
}
223+
224+
public void setInstructions( InstructionList instructions )
225+
{
226+
this.instructions = instructions;
227+
}
228+
229+
public InstructionList getInstructions()
230+
{
231+
check("getInstructions");
232+
if (instructions == null)
233+
throw new IllegalArgumentException("To access instructions you need to enable creation before routing");
234+
235+
return instructions;
236+
}
237+
238+
private void check( String method )
239+
{
240+
if (hasErrors())
241+
{
242+
throw new RuntimeException("You cannot call " + method + " if response contains errors. Check this with ghResponse.hasErrors(). "
243+
+ "Errors are: " + getErrors());
244+
}
245+
}
246+
247+
/**
248+
* @return true if this alternative response contains one or more errors
249+
*/
250+
public boolean hasErrors()
251+
{
252+
return !errors.isEmpty();
253+
}
254+
255+
public List<Throwable> getErrors()
256+
{
257+
return errors;
258+
}
259+
260+
public AltResponse addError( Throwable error )
261+
{
262+
errors.add(error);
263+
return this;
264+
}
265+
266+
public AltResponse addErrors( List<Throwable> errors )
267+
{
268+
this.errors.addAll(errors);
269+
return this;
270+
}
271+
}

0 commit comments

Comments
 (0)