1 |
package dk.daoas.fulddaekning.osrm;
|
2 |
|
3 |
import java.io.IOException;
|
4 |
import java.util.ArrayList;
|
5 |
import java.util.Collection;
|
6 |
import java.util.Iterator;
|
7 |
import java.util.List;
|
8 |
import java.util.concurrent.Future;
|
9 |
import java.util.logging.Level;
|
10 |
import java.util.logging.Logger;
|
11 |
|
12 |
import org.apache.http.HttpEntity;
|
13 |
import org.apache.http.HttpHost;
|
14 |
import org.apache.http.HttpRequest;
|
15 |
import org.apache.http.HttpResponse;
|
16 |
import org.apache.http.client.ClientProtocolException;
|
17 |
import org.apache.http.client.ResponseHandler;
|
18 |
import org.apache.http.client.methods.HttpGet;
|
19 |
import org.apache.http.impl.client.CloseableHttpClient;
|
20 |
import org.apache.http.impl.client.HttpClients;
|
21 |
import org.apache.http.impl.nio.client.CloseableHttpPipeliningClient;
|
22 |
import org.apache.http.impl.nio.client.HttpAsyncClients;
|
23 |
import org.apache.http.util.EntityUtils;
|
24 |
|
25 |
import com.google.gson.Gson;
|
26 |
|
27 |
import dk.daoas.fulddaekning.Adresse;
|
28 |
import dk.daoas.fulddaekning.HttpUtil;
|
29 |
|
30 |
|
31 |
public class OSRMHelper {
|
32 |
|
33 |
final static Logger logger = Logger.getLogger( OSRMHelper.class.toString() );
|
34 |
|
35 |
Gson gson = new Gson();
|
36 |
|
37 |
|
38 |
final String host = "10.30.2.61";
|
39 |
final int port = 5000;
|
40 |
final String base_url = "http://" + host + ":" + port;
|
41 |
|
42 |
public Adresse getNearestViaTable(Adresse a1, Collection<Adresse> haystack) {
|
43 |
|
44 |
Adresse bedsteAdresse = null;
|
45 |
|
46 |
Adresse hayArray[] = new Adresse[ haystack.size() ];
|
47 |
haystack.toArray(hayArray);
|
48 |
|
49 |
StringBuilder sb = new StringBuilder();
|
50 |
sb.append(base_url);
|
51 |
sb.append("/table?loc=").append(a1.latitude).append(",").append(a1.longitude);
|
52 |
|
53 |
for(int i = 0; i<hayArray.length; i++) {
|
54 |
Adresse a = hayArray[i];
|
55 |
sb.append("&loc=").append( a.latitude ).append(",").append(a.longitude);
|
56 |
}
|
57 |
|
58 |
try {
|
59 |
String txtResponse = HttpUtil.getContentString(sb.toString(), 500, "UTF-8");
|
60 |
OSRMDistanceTable table = gson.fromJson(txtResponse, OSRMDistanceTable.class);
|
61 |
if (table.status != 0) {
|
62 |
logger.info("OSRM failed with message: " + table.status_message);
|
63 |
return null;
|
64 |
}
|
65 |
|
66 |
int bedsteTid = Integer.MAX_VALUE;
|
67 |
|
68 |
for (int i = 1; i<table.distance_table.length; i++) {
|
69 |
if (table.distance_table[0][i] < bedsteTid) {
|
70 |
bedsteTid = table.distance_table[0][i];
|
71 |
int idx = i-1;
|
72 |
bedsteAdresse = hayArray[idx];
|
73 |
}
|
74 |
}
|
75 |
|
76 |
} catch (Exception e) {
|
77 |
logger.log(Level.SEVERE, "Lookup failed", e);
|
78 |
System.exit(1);
|
79 |
}
|
80 |
|
81 |
//return gson.fromJson(txtResponse, OSRMResponse.class);
|
82 |
|
83 |
|
84 |
return bedsteAdresse;
|
85 |
}
|
86 |
|
87 |
public Adresse getNearestApacheClient(Adresse a1, Collection<Adresse> haystack) {
|
88 |
int bedsteAfstand = Integer.MAX_VALUE;
|
89 |
Adresse bedsteAdresse = null;
|
90 |
|
91 |
try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
|
92 |
|
93 |
String loc1 = a1.latitude + "," + a1.longitude;
|
94 |
|
95 |
|
96 |
for(Adresse a2: haystack) {
|
97 |
String loc2 = a2.latitude + "," + a2.longitude;
|
98 |
|
99 |
String uri = base_url + "/viaroute?loc=" + loc1 + "&loc=" + loc2 + "&geometry=false&instructions=false&alt=false";
|
100 |
|
101 |
HttpGet httpget = new HttpGet(uri);
|
102 |
|
103 |
ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
|
104 |
|
105 |
@Override
|
106 |
public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {
|
107 |
int status = response.getStatusLine().getStatusCode();
|
108 |
|
109 |
if (status >= 200 && status < 300) {
|
110 |
HttpEntity entity = response.getEntity();
|
111 |
return entity != null ? EntityUtils.toString(entity) : null;
|
112 |
} else {
|
113 |
throw new ClientProtocolException("Unexpected response status: " + status);
|
114 |
}
|
115 |
}
|
116 |
|
117 |
};
|
118 |
String responseBody = httpclient.execute(httpget, responseHandler);
|
119 |
|
120 |
OSRMResponse res = gson.fromJson(responseBody, OSRMResponse.class);
|
121 |
|
122 |
|
123 |
|
124 |
if (res.status != 0) {
|
125 |
System.out.println("OSRM Returned " + res.status + "/" + res.status_message );
|
126 |
continue;
|
127 |
}
|
128 |
|
129 |
|
130 |
if (res.route_summary.total_distance < bedsteAfstand) {
|
131 |
bedsteAfstand = res.route_summary.total_distance;
|
132 |
bedsteAdresse = a2;
|
133 |
}
|
134 |
|
135 |
}
|
136 |
|
137 |
} catch (Exception e) {
|
138 |
logger.log(Level.SEVERE, "Lookup failed", e);
|
139 |
}
|
140 |
|
141 |
return bedsteAdresse;
|
142 |
}
|
143 |
|
144 |
|
145 |
public Adresse getNearestPipeline(Adresse a1, Collection<Adresse> haystack) {
|
146 |
|
147 |
int bedsteAfstand = Integer.MAX_VALUE;
|
148 |
Adresse bedsteAdresse = null;
|
149 |
|
150 |
Adresse hayArray[] = new Adresse[ haystack.size() ];
|
151 |
haystack.toArray(hayArray);
|
152 |
|
153 |
|
154 |
try (CloseableHttpPipeliningClient httpclient = HttpAsyncClients.createPipelining(); ) {
|
155 |
httpclient.start();
|
156 |
|
157 |
HttpHost targetHost = new HttpHost(host, port);
|
158 |
|
159 |
System.out.println("1>" + targetHost.toString() );
|
160 |
System.out.println("2>" + targetHost.toURI() );
|
161 |
|
162 |
List<HttpRequest> requests = new ArrayList<HttpRequest>();
|
163 |
|
164 |
String loc1 = a1.latitude + "," + a1.longitude;
|
165 |
|
166 |
//for (int i=0; i<hayArray.length; i++) {
|
167 |
for (int i=0; i<2; i++) {
|
168 |
Adresse a2 = hayArray[i];
|
169 |
String loc2 = a2.latitude + "," + a2.longitude;
|
170 |
|
171 |
String uri = "/viaroute?loc=" + loc1 + "&loc=" + loc2 + "&geometry=false&instructions=false&alt=false";
|
172 |
System.out.println( uri );
|
173 |
|
174 |
requests.add( new HttpGet(uri) );
|
175 |
|
176 |
}
|
177 |
|
178 |
|
179 |
Future<List<HttpResponse>> future = httpclient.execute(targetHost, requests, null);
|
180 |
List<HttpResponse> responses = future.get();
|
181 |
|
182 |
HttpResponse respArr[] = new HttpResponse[ responses.size() ];
|
183 |
responses.toArray( respArr );
|
184 |
|
185 |
for (int i=0; i<respArr.length; i++) {
|
186 |
String jsonBody = EntityUtils.toString( respArr[i].getEntity() );
|
187 |
OSRMResponse res = gson.fromJson(jsonBody, OSRMResponse.class);
|
188 |
|
189 |
if (res.route_summary.total_distance < bedsteAfstand) {
|
190 |
bedsteAfstand = res.route_summary.total_distance;
|
191 |
bedsteAdresse = hayArray[i];
|
192 |
}
|
193 |
|
194 |
}
|
195 |
|
196 |
} catch(Exception e) {
|
197 |
logger.log(Level.SEVERE, "Lookup failed", e);
|
198 |
|
199 |
}
|
200 |
|
201 |
|
202 |
return bedsteAdresse;
|
203 |
}
|
204 |
|
205 |
|
206 |
/*
|
207 |
* Denne virker men table opslaget er 10 gange hurtigere
|
208 |
*
|
209 |
*/
|
210 |
@Deprecated
|
211 |
public Adresse getNearest(Adresse a1, Collection<Adresse> haystack) {
|
212 |
int bedsteAfstand = Integer.MAX_VALUE;
|
213 |
Adresse bedsteAdresse = null;
|
214 |
|
215 |
Iterator<Adresse> it = haystack.iterator();
|
216 |
while (it.hasNext()) {
|
217 |
Adresse a2 = it.next();
|
218 |
|
219 |
try {
|
220 |
OSRMResponse res = getRoute(a1, a2);
|
221 |
|
222 |
if (res.status != 0) {
|
223 |
System.out.println("OSRM Returned " + res.status + "/" + res.status_message );
|
224 |
continue;
|
225 |
}
|
226 |
|
227 |
|
228 |
if (res.route_summary.total_distance < bedsteAfstand) {
|
229 |
bedsteAfstand = res.route_summary.total_distance;
|
230 |
bedsteAdresse = a2;
|
231 |
}
|
232 |
} catch (IOException e) {
|
233 |
System.out.println( e.getMessage() );
|
234 |
System.exit(1);
|
235 |
}
|
236 |
|
237 |
}
|
238 |
|
239 |
return bedsteAdresse;
|
240 |
}
|
241 |
|
242 |
public OSRMResponse getRoute(Adresse a1, Adresse a2) throws IOException {
|
243 |
|
244 |
String loc1 = a1.latitude + "," + a1.longitude;
|
245 |
String loc2 = a2.latitude + "," + a2.longitude;
|
246 |
|
247 |
String params = "loc=" + loc1 + "&loc=" + loc2 + "&geometry=false&instructions=false&alt=false";
|
248 |
|
249 |
String url = base_url + "/viaroute?" + params;
|
250 |
|
251 |
String txtResponse = HttpUtil.getContentString(url, 500, "UTF-8");
|
252 |
|
253 |
return gson.fromJson(txtResponse, OSRMResponse.class);
|
254 |
}
|
255 |
}
|