Skip to content

Commit 0e31e3e

Browse files
committed
The OSRMClient is modified to return both tables (durations and distances)
1 parent fcd6d29 commit 0e31e3e

File tree

2 files changed

+52
-80
lines changed

2 files changed

+52
-80
lines changed

src/main/java/com/osrm/client/Main.java

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,48 @@
11
package com.osrm.client;
2+
import com.osrm.client.exception.EmptyUrlException;
3+
import com.osrm.client.request.CostMatricesRequest;
4+
import com.osrm.client.request.GeoLocation;
5+
26
import java.util.List;
37
import java.util.ArrayList;
48

59
public class Main {
610
public static void main(String[] args) throws EmptyUrlException {
711

812
try {
9-
OSRMClient client = new OSRMClient("http://localhost:8008");
13+
OSRMClient client = new OSRMClient("http://0.0.0.0:8080");
1014
List<GeoLocation> locations = new ArrayList<>();
1115

12-
GeoLocation geo1 = new GeoLocation(-33.416943, -70.60952);
13-
GeoLocation geo2 = new GeoLocation(-33.416943, -70.60952);
14-
GeoLocation geo3 = new GeoLocation(-33.4445755, -70.6404943);
15-
GeoLocation geo4 = new GeoLocation(-33.4457167, -70.61926449999999);
16+
GeoLocation geo1 = new GeoLocation(42.5434488, 1.4949332);
17+
GeoLocation geo2 = new GeoLocation(42.5434488, 1.395);
1618

1719
locations.add(geo1);
1820
locations.add(geo2);
19-
locations.add(geo3);
20-
locations.add(geo4);
2121

2222
// low fmv
2323
double speedRate = 2;
24-
String country = "CL";
24+
String country = "AD";
2525

2626
// token
27-
String token = "None";
27+
String token = "Token b926b46ddb8f5efab66693961369e0116712adde";
2828

2929
String profile = "car";
3030

31-
OSRMDistanceResponse response = client.getDistanceMatrix(locations, speedRate, country, token, profile,
32-
null);
31+
String options = "{\"metrics\": \"time,distance\"}";
32+
33+
CostMatricesRequest request = CostMatricesRequest.builder()
34+
.speedRate(speedRate)
35+
.country(country)
36+
.locations(locations)
37+
.token(token)
38+
.profile(profile)
39+
.metrics("time,distance")
40+
//.metrics("time")
41+
.build();
42+
43+
CostMatrices matrices = client.getCostMatrices(request);
3344

34-
System.out.println(response);
45+
System.out.println(matrices);
3546
}catch (Exception e){
3647
e.printStackTrace();
3748
System.out.println("End with errors.");
Lines changed: 29 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
package com.osrm.client;
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
4-
import java.net.URLEncoder;
5-
import java.nio.charset.StandardCharsets;
4+
65
import java.util.ArrayList;
76
import java.util.List;
8-
import java.util.Map;
97
import java.util.concurrent.TimeUnit;
108

11-
import com.osrm.client.exception.DistanceMatrixResponseException;
129
import com.osrm.client.exception.EmptyUrlException;
13-
import com.osrm.client.exception.OptimizationDistanceMatrixException;
10+
import com.osrm.client.exception.OSRMClientException;
11+
import com.osrm.client.request.CostMatricesRequest;
12+
import com.osrm.client.request.GeoLocation;
1413
import okhttp3.MediaType;
1514
import okhttp3.OkHttpClient;
1615
import okhttp3.OkHttpClient.Builder;
@@ -19,8 +18,9 @@
1918
import okhttp3.Response;
2019

2120

22-
public class OSRMClient {
21+
public class OSRMClient implements CostService {
2322
private final String uri;
23+
private static final String ENDPOINT_V2_TABLE = "/v2/table/";
2424

2525
public OSRMClient(String uri) throws EmptyUrlException {
2626
if (uri != null || !uri.isEmpty()) {
@@ -30,10 +30,8 @@ public OSRMClient(String uri) throws EmptyUrlException {
3030
}
3131
}
3232

33-
34-
public OSRMCostMatrixResponse getDistanceMatrix(List<GeoLocation> locations, double speedRate, String country,
35-
String token, String profile,
36-
String options) throws OptimizationDistanceMatrixException {
33+
@Override
34+
public CostMatrices getCostMatrices(CostMatricesRequest request) {
3735
Builder requestBuilder = new Builder();
3836

3937
requestBuilder.readTimeout(900000, TimeUnit.MILLISECONDS);
@@ -45,76 +43,39 @@ public OSRMCostMatrixResponse getDistanceMatrix(List<GeoLocation> locations, dou
4543

4644
List<String> locationsCollection = new ArrayList<>();
4745

48-
49-
for (GeoLocation geoloc : locations) {
50-
locationsCollection.add(geoloc.getLatLongString());
46+
for (GeoLocation geolocation : request.getLocations()) {
47+
locationsCollection.add(geolocation.getLatLongString());
5148
}
5249

5350
String paramsString = String.join("&loc=", locationsCollection);
5451

55-
paramsString += "&speedRate=" + speedRate;
56-
paramsString += "&country=" + country;
57-
paramsString += encodeJsonToUrlParams(options);
52+
paramsString = addParamString(paramsString, "speedRate", Double.toString(request.getSpeedRate()));
53+
paramsString = addParamString(paramsString, "country", request.getCountry());
54+
paramsString = addParamString(paramsString, "start_time", request.getStartTime());
55+
paramsString = addParamString(paramsString, "vehicleSubType", request.getVehicleSubType());
56+
paramsString = addParamString(paramsString, "metrics", request.getMetrics());
57+
//TODO:423 pending implement vehicular restriction
5858

5959
RequestBody body = RequestBody.create(mediaType, "loc=" + paramsString);
6060

61-
Request request = new Request.Builder()
62-
.url(this.uri + "/table/" + profile)
63-
.post(body)
64-
.addHeader("Content-Type", "application/x-www-form-urlencoded")
65-
.addHeader("Authorization", token)
66-
.build();
61+
Request osrmRequest = new Request.Builder()
62+
.url(this.uri + ENDPOINT_V2_TABLE + request.getProfile())
63+
.post(body)
64+
.addHeader("Content-Type", "application/x-www-form-urlencoded")
65+
.addHeader("Authorization", request.getToken())
66+
.build();
6767

68-
Response response;
6968
try {
70-
response = client.newCall(request).execute();
71-
if (response.isSuccessful()) {
72-
return OSRMCostMatrixResponse.fromJSON(response.body().string());
73-
}
74-
} catch (Exception e) {
75-
System.out.print(e.getMessage());
76-
throw new OptimizationDistanceMatrixException("Error while connecting to OSRM Server");
77-
}
78-
79-
UnsuccessfulResponse unsuccessfulResponse = this.getUnsuccessfulResponse(response);
80-
if (unsuccessfulResponse != null && unsuccessfulResponse.getMessage() != null) {
81-
throw new DistanceMatrixResponseException("OSRM Error: " + unsuccessfulResponse.getMessage());
82-
}
69+
Response response = client.newCall(osrmRequest).execute();
8370

84-
throw new DistanceMatrixResponseException("OSRM Error: " + response);
85-
}
86-
87-
public static String encodeJsonToUrlParams(String options) {
88-
ObjectMapper objectMapper = new ObjectMapper();
89-
StringBuilder urlParams = new StringBuilder();
90-
try {
91-
Map<String, Object> map = objectMapper.readValue(options, Map.class);
92-
93-
for (Map.Entry<String, Object> entry : map.entrySet()) {
94-
String key = URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8.toString());
95-
String value = URLEncoder.encode(String.valueOf(entry.getValue()), StandardCharsets.UTF_8.toString());
96-
urlParams.append("&").append(key).append("=").append(value);
97-
}
98-
}catch (Exception e) {
99-
System.out.print("Error getUnsuccessfulResponse.fromJSON: " + e.getMessage());
71+
ObjectMapper mapper = new ObjectMapper();
72+
return mapper.readValue(response.body().string(), CostMatrices.class);
73+
} catch (Exception e) {
74+
throw new OSRMClientException("Error while connecting to OSRM Server");
10075
}
101-
return urlParams.toString();
10276
}
10377

104-
private UnsuccessfulResponse getUnsuccessfulResponse(Response response){
105-
try {
106-
if (response.body() != null) {
107-
String bodyResponse = response.body().string();
108-
UnsuccessfulResponse unsuccessfulResponse = UnsuccessfulResponse.fromJSON(bodyResponse);
109-
if(unsuccessfulResponse.getMessage() == null){
110-
return new UnsuccessfulResponse(bodyResponse,String.valueOf(response.code()),"");
111-
}
112-
}
113-
} catch (Exception e) {
114-
System.out.print("Error getUnsuccessfulResponse.fromJSON: " + e.getMessage());
115-
}
116-
return null;
78+
private String addParamString(String paramString, String key, String value) {
79+
return (value != null) && (!value.equals("")) ? paramString.concat("&" + key + "=" + value) : paramString;
11780
}
118-
119-
12081
}

0 commit comments

Comments
 (0)