1 |
package dk.daoas.fulddaekning.osrm; |
package dk.daoas.fulddaekning.osrm; |
2 |
|
|
3 |
import java.io.IOException; |
import java.io.IOException; |
4 |
|
import java.util.ArrayList; |
5 |
import java.util.Collection; |
import java.util.Collection; |
6 |
import java.util.Iterator; |
import java.util.Iterator; |
7 |
|
import java.util.List; |
8 |
|
import java.util.concurrent.Future; |
9 |
import java.util.logging.Level; |
import java.util.logging.Level; |
10 |
import java.util.logging.Logger; |
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; |
import com.google.gson.Gson; |
26 |
|
|
27 |
import dk.daoas.fulddaekning.Adresse; |
import dk.daoas.fulddaekning.Adresse; |
28 |
import dk.daoas.fulddaekning.HttpUtil; |
import dk.daoas.fulddaekning.HttpUtil; |
29 |
import dk.daoas.fulddaekning.LookupWorker; |
|
30 |
|
/* |
31 |
|
import dk.thoerup.osrmbinding.OSRMBinding; |
32 |
|
import dk.thoerup.osrmbinding.ViarouteResult; |
33 |
|
*/ |
34 |
|
|
35 |
public class OSRMHelper { |
public class OSRMHelper { |
36 |
|
|
37 |
|
private static final boolean ENABLE_OSRM = false; |
38 |
|
|
39 |
|
|
40 |
final static Logger logger = Logger.getLogger( OSRMHelper.class.toString() ); |
final static Logger logger = Logger.getLogger( OSRMHelper.class.toString() ); |
41 |
|
|
42 |
Gson gson = new Gson(); |
Gson gson = new Gson(); |
|
|
|
|
final String base_url = "http://10.30.2.61:5000"; |
|
43 |
|
|
44 |
public Adresse getNearestViaTable(Adresse a1, Collection<Adresse> haystack) { |
//static OSRMBinding binding = null; |
45 |
|
|
46 |
|
//final String host = "127.0.0.1"; |
47 |
|
final String host = "10.30.2.103"; |
48 |
|
final int port = 5000; |
49 |
|
final String base_url = "http://" + host + ":" + port; |
50 |
|
/* |
51 |
|
private static synchronized void initOsrm() { |
52 |
|
if (binding == null) { |
53 |
|
binding = new OSRMBinding("/home/openstreetmap/denmark-latest.osrm"); |
54 |
|
} |
55 |
|
} |
56 |
|
*/ |
57 |
|
|
58 |
|
public Adresse getNearestTableHttp(Adresse a1, Collection<Adresse> haystack) { |
59 |
|
|
60 |
Adresse bedsteAdresse = null; |
Adresse bedsteAdresse = null; |
61 |
|
|
62 |
Adresse hayArray[] = new Adresse[ haystack.size() ]; |
Adresse hayArray[] = new Adresse[ haystack.size() ]; |
63 |
haystack.toArray(hayArray); |
haystack.toArray(hayArray); |
64 |
|
|
65 |
StringBuilder sb = new StringBuilder(); |
StringBuilder sb = new StringBuilder(); |
66 |
sb.append(base_url); |
sb.append(base_url); |
67 |
sb.append("/table?loc=").append(a1.latitude).append(",").append(a1.longitude); |
sb.append("/table?loc=").append(a1.latitude).append(",").append(a1.longitude); |
68 |
|
|
69 |
for(int i = 0; i<hayArray.length; i++) { |
for(int i = 0; i<hayArray.length; i++) { |
70 |
Adresse a = hayArray[i]; |
Adresse a = hayArray[i]; |
71 |
sb.append("&loc=").append( a.latitude ).append(",").append(a.longitude); |
sb.append("&loc=").append( a.latitude ).append(",").append(a.longitude); |
72 |
} |
} |
73 |
|
|
74 |
try { |
try { |
75 |
String txtResponse = HttpUtil.getContentString(sb.toString(), 500, "UTF-8"); |
String txtResponse = HttpUtil.getContentString(sb.toString(), 500, "UTF-8"); |
76 |
OSRMDistanceTable table = gson.fromJson(txtResponse, OSRMDistanceTable.class); |
OSRMDistanceTable table = gson.fromJson(txtResponse, OSRMDistanceTable.class); |
78 |
logger.info("OSRM failed with message: " + table.status_message); |
logger.info("OSRM failed with message: " + table.status_message); |
79 |
return null; |
return null; |
80 |
} |
} |
81 |
|
|
82 |
int bedsteTid = Integer.MAX_VALUE; |
int bedsteTid = Integer.MAX_VALUE; |
83 |
|
|
84 |
for (int i = 1; i<table.distance_table.length; i++) { |
for (int i = 1; i<table.distance_table.length; i++) { |
85 |
if (table.distance_table[0][i] < bedsteTid) { |
if (table.distance_table[0][i] < bedsteTid) { |
86 |
bedsteTid = table.distance_table[0][i]; |
bedsteTid = table.distance_table[0][i]; |
93 |
logger.log(Level.SEVERE, "Lookup failed", e); |
logger.log(Level.SEVERE, "Lookup failed", e); |
94 |
System.exit(1); |
System.exit(1); |
95 |
} |
} |
96 |
|
|
97 |
//return gson.fromJson(txtResponse, OSRMResponse.class); |
//return gson.fromJson(txtResponse, OSRMResponse.class); |
98 |
|
|
99 |
|
|
100 |
return bedsteAdresse; |
return bedsteAdresse; |
101 |
} |
} |
102 |
|
|
103 |
/* |
|
104 |
* Denne virker men table opslaget er 10 gange hurtigere |
|
105 |
* |
/* |
106 |
*/ |
public Adresse getNearestTableJni(Adresse a1, Collection<Adresse> haystack) { |
107 |
@Deprecated |
if (binding == null) { |
108 |
public Adresse getNearest(Adresse a1, Collection<Adresse> haystack) { |
initOsrm(); |
109 |
|
} |
110 |
|
|
111 |
|
Adresse bedsteAdresse = null; |
112 |
|
|
113 |
|
Adresse hayArray[] = new Adresse[ haystack.size() ]; |
114 |
|
haystack.toArray(hayArray); |
115 |
|
|
116 |
|
dk.thoerup.osrmbinding.Geopoint points[] = new dk.thoerup.osrmbinding.Geopoint[ hayArray.length + 1 ]; |
117 |
|
points[0] = new dk.thoerup.osrmbinding.Geopoint( a1.latitude, a1.longitude); |
118 |
|
|
119 |
|
|
120 |
|
for(int i = 0; i<hayArray.length; i++) { |
121 |
|
Adresse a = hayArray[i]; |
122 |
|
int idx = i+1; |
123 |
|
points[idx] = new dk.thoerup.osrmbinding.Geopoint(a.latitude, a.longitude); |
124 |
|
} |
125 |
|
|
126 |
|
try { |
127 |
|
float distance_table[][] = binding.table( points ); |
128 |
|
|
129 |
|
int bedsteTid = Integer.MAX_VALUE; |
130 |
|
|
131 |
|
for (int i = 1; i<distance_table.length; i++) { |
132 |
|
if (distance_table[0][i] < bedsteTid) { |
133 |
|
bedsteTid = (int) distance_table[0][i]; |
134 |
|
int idx = i-1; |
135 |
|
bedsteAdresse = hayArray[idx]; |
136 |
|
} |
137 |
|
} |
138 |
|
|
139 |
|
} catch (Exception e) { |
140 |
|
logger.log(Level.SEVERE, "Lookup failed", e); |
141 |
|
System.exit(1); |
142 |
|
} |
143 |
|
|
144 |
|
return bedsteAdresse; |
145 |
|
}*/ |
146 |
|
|
147 |
|
|
148 |
|
public Adresse getNearestMultitargetHttp(Adresse a1, Collection<Adresse> haystack) { |
149 |
|
|
150 |
|
Adresse bedsteAdresse = null; |
151 |
|
|
152 |
|
Adresse hayArray[] = new Adresse[ haystack.size() ]; |
153 |
|
haystack.toArray(hayArray); |
154 |
|
|
155 |
|
StringBuilder sb = new StringBuilder(); |
156 |
|
sb.append(base_url); |
157 |
|
sb.append("/multitarget?loc=").append(a1.latitude).append(",").append(a1.longitude); |
158 |
|
|
159 |
|
for(int i = 0; i<hayArray.length; i++) { |
160 |
|
Adresse a = hayArray[i]; |
161 |
|
sb.append("&loc=").append( a.latitude ).append(",").append(a.longitude); |
162 |
|
} |
163 |
|
|
164 |
|
try { |
165 |
|
String txtResponse = HttpUtil.getContentString(sb.toString(), 500, "UTF-8"); |
166 |
|
OSRMMultiTarget table = gson.fromJson(txtResponse, OSRMMultiTarget.class); |
167 |
|
|
168 |
|
|
169 |
|
double bedsteAfstand = Double.MAX_VALUE; |
170 |
|
|
171 |
|
for (int i = 1; i<table.distances.length; i++) { |
172 |
|
if (table.distances[i].distance < bedsteAfstand) { |
173 |
|
bedsteAfstand = table.distances[i].distance; |
174 |
|
bedsteAdresse = hayArray[i]; |
175 |
|
} |
176 |
|
} |
177 |
|
|
178 |
|
} catch (Exception e) { |
179 |
|
logger.log(Level.SEVERE, "Lookup failed", e); |
180 |
|
System.exit(1); |
181 |
|
} |
182 |
|
|
183 |
|
|
184 |
|
return bedsteAdresse; |
185 |
|
} |
186 |
|
|
187 |
|
public Adresse getNearestViarouteHttp(Adresse a1, Collection<Adresse> haystack) { |
188 |
int bedsteAfstand = Integer.MAX_VALUE; |
int bedsteAfstand = Integer.MAX_VALUE; |
189 |
Adresse bedsteAdresse = null; |
Adresse bedsteAdresse = null; |
190 |
|
|
191 |
Iterator<Adresse> it = haystack.iterator(); |
try (CloseableHttpClient httpclient = HttpClients.createDefault()) { |
|
while (it.hasNext()) { |
|
|
Adresse a2 = it.next(); |
|
192 |
|
|
193 |
try { |
String loc1 = a1.latitude + "," + a1.longitude; |
194 |
OSRMResponse res = getRoute(a1, a2); |
|
195 |
|
|
196 |
|
for(Adresse a2: haystack) { |
197 |
|
String loc2 = a2.latitude + "," + a2.longitude; |
198 |
|
|
199 |
|
String uri = base_url + "/viaroute?loc=" + loc1 + "&loc=" + loc2 + "&geometry=false&instructions=false&alt=false"; |
200 |
|
|
201 |
|
HttpGet httpget = new HttpGet(uri); |
202 |
|
|
203 |
|
ResponseHandler<String> responseHandler = new ResponseHandler<String>() { |
204 |
|
|
205 |
|
@Override |
206 |
|
public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException { |
207 |
|
int status = response.getStatusLine().getStatusCode(); |
208 |
|
|
209 |
|
if (status >= 200 && status < 300) { |
210 |
|
HttpEntity entity = response.getEntity(); |
211 |
|
return entity != null ? EntityUtils.toString(entity) : null; |
212 |
|
} else { |
213 |
|
throw new ClientProtocolException("Unexpected response status: " + status); |
214 |
|
} |
215 |
|
} |
216 |
|
|
217 |
|
}; |
218 |
|
String responseBody = httpclient.execute(httpget, responseHandler); |
219 |
|
|
220 |
|
OSRMResponse res = gson.fromJson(responseBody, OSRMResponse.class); |
221 |
|
|
222 |
|
|
223 |
|
|
224 |
|
if (res.status != 0) { |
225 |
|
System.out.println("OSRM Returned " + res.status + "/" + res.status_message ); |
226 |
|
continue; |
227 |
|
} |
228 |
|
|
229 |
|
if (res.route_summary.total_distance == 0) { |
230 |
|
continue; |
231 |
|
} |
232 |
|
|
233 |
|
|
234 |
if (res.route_summary.total_distance < bedsteAfstand) { |
if (res.route_summary.total_distance < bedsteAfstand) { |
235 |
bedsteAfstand = res.route_summary.total_distance; |
bedsteAfstand = res.route_summary.total_distance; |
236 |
bedsteAdresse = a2; |
bedsteAdresse = a2; |
237 |
} |
} |
238 |
} catch (IOException e) { |
|
239 |
System.out.println( e.getMessage() ); |
if (bedsteAfstand < 200) { //vejdistance er tæt nok på |
240 |
System.exit(1); |
return bedsteAdresse; |
241 |
|
} |
242 |
|
|
243 |
} |
} |
244 |
|
|
245 |
|
} catch (Exception e) { |
246 |
|
logger.log(Level.SEVERE, "Lookup failed", e); |
247 |
} |
} |
248 |
|
|
249 |
return bedsteAdresse; |
return bedsteAdresse; |
250 |
} |
} |
251 |
|
|
252 |
public OSRMResponse getRoute(Adresse a1, Adresse a2) throws IOException { |
/* |
253 |
|
public Adresse getNearestViarouteJni(Adresse a1, Collection<Adresse> haystack) { |
254 |
String loc1 = a1.latitude + "," + a1.longitude; |
|
255 |
String loc2 = a2.latitude + "," + a2.longitude; |
if (binding == null) { |
256 |
|
initOsrm(); |
257 |
String params = "loc=" + loc1 + "&loc=" + loc2 + "&geometry=false&instructions=false&alt=false"; |
} |
258 |
|
|
259 |
String url = base_url + "/viaroute?" + params; |
|
260 |
|
int bedsteAfstand = Integer.MAX_VALUE; |
261 |
String txtResponse = HttpUtil.getContentString(url, 500, "UTF-8"); |
Adresse bedsteAdresse = null; |
262 |
|
|
263 |
return gson.fromJson(txtResponse, OSRMResponse.class); |
|
264 |
} |
for(Adresse a2: haystack) { |
265 |
|
|
266 |
|
ViarouteResult res = binding.viaRoute(a1.latitude, a1.longitude, a2.latitude, a2.longitude); |
267 |
|
|
268 |
|
|
269 |
|
if (res.status != 0) { |
270 |
|
System.out.println("OSRM Returned " + res.status ); |
271 |
|
continue; |
272 |
|
} |
273 |
|
|
274 |
|
if (res.totalDistance == 0) { |
275 |
|
continue; |
276 |
|
} |
277 |
|
|
278 |
|
|
279 |
|
if (res.totalDistance < bedsteAfstand) { |
280 |
|
bedsteAfstand = res.totalDistance; |
281 |
|
bedsteAdresse = a2; |
282 |
|
} |
283 |
|
|
284 |
|
if (bedsteAfstand < 200) { //vejdistance er tæt nok på |
285 |
|
return bedsteAdresse; |
286 |
|
} |
287 |
|
|
288 |
|
} |
289 |
|
|
290 |
|
return bedsteAdresse; |
291 |
|
}*/ |
292 |
} |
} |
293 |
|
|
294 |
|
|