/[projects]/dao/FuldDaekningWorker/src/main/java/dk/daoas/fulddaekning/osrm/OSRMHelper.java
ViewVC logotype

Contents of /dao/FuldDaekningWorker/src/main/java/dk/daoas/fulddaekning/osrm/OSRMHelper.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2749 - (show annotations) (download)
Thu Oct 8 14:27:54 2015 UTC (8 years, 7 months ago) by torben
File size: 8074 byte(s)
Add code for multitarget
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 import dk.thoerup.osrmbinding.OSRMBinding;
32 import dk.thoerup.osrmbinding.ViarouteResult;
33 */
34
35 public class OSRMHelper {
36
37 private static final boolean ENABLE_OSRM = false;
38
39
40 final static Logger logger = Logger.getLogger( OSRMHelper.class.toString() );
41
42 Gson gson = new Gson();
43
44 //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;
61
62 Adresse hayArray[] = new Adresse[ haystack.size() ];
63 haystack.toArray(hayArray);
64
65 StringBuilder sb = new StringBuilder();
66 sb.append(base_url);
67 sb.append("/table?loc=").append(a1.latitude).append(",").append(a1.longitude);
68
69 for(int i = 0; i<hayArray.length; i++) {
70 Adresse a = hayArray[i];
71 sb.append("&loc=").append( a.latitude ).append(",").append(a.longitude);
72 }
73
74 try {
75 String txtResponse = HttpUtil.getContentString(sb.toString(), 500, "UTF-8");
76 OSRMDistanceTable table = gson.fromJson(txtResponse, OSRMDistanceTable.class);
77 if (table.status != 0) {
78 logger.info("OSRM failed with message: " + table.status_message);
79 return null;
80 }
81
82 int bedsteTid = Integer.MAX_VALUE;
83
84 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 logger.log(Level.SEVERE, "Lookup failed", e);
94 System.exit(1);
95 }
96
97 //return gson.fromJson(txtResponse, OSRMResponse.class);
98
99
100 return bedsteAdresse;
101 }
102
103
104
105 /*
106 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
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;
189 Adresse bedsteAdresse = null;
190
191 try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
192
193 String loc1 = a1.latitude + "," + a1.longitude;
194
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) {
235 bedsteAfstand = res.route_summary.total_distance;
236 bedsteAdresse = a2;
237 }
238
239 if (bedsteAfstand < 200) { //vejdistance er tæt nok på
240 return bedsteAdresse;
241 }
242
243 }
244
245 } catch (Exception e) {
246 logger.log(Level.SEVERE, "Lookup failed", e);
247 }
248
249 return bedsteAdresse;
250 }
251
252 /*
253 public Adresse getNearestViarouteJni(Adresse a1, Collection<Adresse> haystack) {
254
255 if (binding == null) {
256 initOsrm();
257 }
258
259
260 int bedsteAfstand = Integer.MAX_VALUE;
261 Adresse bedsteAdresse = null;
262
263
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

  ViewVC Help
Powered by ViewVC 1.1.20