Skip to content

Commit a423bd7

Browse files
author
Peter
committed
fixed GraphHopperWeb client for custom parameters, fixes graphhopper#658
1 parent 3d08f67 commit a423bd7

File tree

2 files changed

+64
-6
lines changed

2 files changed

+64
-6
lines changed

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

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@
2424
import com.graphhopper.util.*;
2525
import com.graphhopper.util.shapes.GHPoint;
2626
import java.util.ArrayList;
27+
import java.util.HashSet;
2728

2829
import java.util.List;
30+
import java.util.Map.Entry;
31+
import java.util.Set;
2932

3033
import org.json.JSONArray;
3134
import org.json.JSONObject;
@@ -46,9 +49,28 @@ public class GraphHopperWeb implements GraphHopperAPI
4649
private boolean instructions = true;
4750
private boolean calcPoints = true;
4851
private boolean elevation = false;
52+
private final Set<String> ignoreSet;
4953

5054
public GraphHopperWeb()
5155
{
56+
// some parameters are supported directly via Java API so ignore them when writing the getHints map
57+
ignoreSet = new HashSet<String>();
58+
ignoreSet.add("calc_points");
59+
ignoreSet.add("calcpoints");
60+
ignoreSet.add("instructions");
61+
ignoreSet.add("elevation");
62+
ignoreSet.add("key");
63+
64+
// some parameters are in the request:
65+
ignoreSet.add("algorithm");
66+
ignoreSet.add("locale");
67+
ignoreSet.add("point");
68+
ignoreSet.add("vehicle");
69+
70+
// some are special and need to be avoided
71+
ignoreSet.add("points_encoded");
72+
ignoreSet.add("pointsencoded");
73+
ignoreSet.add("type");
5274
}
5375

5476
public void setDownloader( Downloader downloader )
@@ -109,7 +131,6 @@ public GHResponse route( GHRequest request )
109131
+ "Use calcPoints=false and instructions=false to disable point and instruction calculation");
110132

111133
boolean tmpElevation = request.getHints().getBool("elevation", elevation);
112-
String tmpKey = request.getHints().get("key", key);
113134

114135
String url = routeServiceUrl
115136
+ "?"
@@ -125,12 +146,20 @@ public GHResponse route( GHRequest request )
125146
if (!request.getVehicle().isEmpty())
126147
url += "&vehicle=" + request.getVehicle();
127148

128-
if (!tmpKey.isEmpty())
129-
url += "&key=" + tmpKey;
130-
int altMax = request.getHints().getInt("alternative_route.max_num", 0);
131-
if (altMax != 0)
149+
if (!key.isEmpty())
150+
url += "&key=" + key;
151+
152+
for (Entry<String, String> entry : request.getHints().toMap().entrySet())
132153
{
133-
url += "&alternative_route.max_num=" + altMax;
154+
String urlKey = entry.getKey();
155+
String urlValue = entry.getValue();
156+
157+
// use lower case conversion for check only!
158+
if (ignoreSet.contains(urlKey.toLowerCase()))
159+
continue;
160+
161+
if (urlValue != null && !urlValue.isEmpty())
162+
url += "&" + WebHelper.encodeURL(urlKey) + "=" + WebHelper.encodeURL(urlValue);
134163
}
135164

136165
String str = downloader.downloadAsString(url, true);

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.graphhopper.GHRequest;
2222
import com.graphhopper.GHResponse;
2323
import com.graphhopper.util.Downloader;
24+
import com.graphhopper.util.Helper;
2425

2526
import java.io.IOException;
2627
import java.io.InputStream;
@@ -58,4 +59,32 @@ public InputStream fetch( HttpURLConnection conn, boolean readErrorStreamNoExcep
5859
assertEquals("(0,Geradeaus auf A 100,1268.519329705091,65237)", arsp.getInstructions().get(0).toString());
5960
assertEquals(11, arsp.getInstructions().get(0).getPoints().size());
6061
}
62+
63+
@Test
64+
public void testCreateURL() throws Exception
65+
{
66+
Downloader downloader = new Downloader("GraphHopper Test")
67+
{
68+
@Override
69+
public String downloadAsString( String url, boolean readErrorStreamNoException ) throws IOException
70+
{
71+
assertFalse(url.contains("xy"));
72+
assertFalse(url.contains("algo1"));
73+
assertTrue(url.contains("alternative_route.max_paths=4"));
74+
75+
assertEquals("https://graphhopper.com/api/1/route?point=52.0,13.0&point=52.0,14.0&&type=json&instructions=true&points_encoded=true&calc_points=true&algorithm=&locale=en_US&elevation=false&key=blup", url);
76+
return Helper.isToString(getClass().getResourceAsStream("test_encoded.json"));
77+
}
78+
};
79+
GraphHopperWeb instance = new GraphHopperWeb();
80+
instance.setKey("blup");
81+
instance.setDownloader(downloader);
82+
GHRequest req = new GHRequest(52, 13, 52, 14);
83+
84+
// should be ignored, use GraphHopperWeb or GHRequest directly instead
85+
req.getHints().put("key", "xy");
86+
req.getHints().put("algorithm", "algo1");
87+
req.getHints().put("alternative_route.max_paths", "4");
88+
instance.route(req);
89+
}
6190
}

0 commit comments

Comments
 (0)