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