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