/[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 2765 - (show annotations) (download)
Tue Nov 3 10:27:01 2015 UTC (8 years, 6 months ago) by torben
File size: 8260 byte(s)
reducer dao max distance til 5 km - pt kører Bil ruter på max 3.2 km
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 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 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 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
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 }*/
151
152
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 public Adresse getNearestViarouteHttp(Adresse a1, Collection<Adresse> haystack) {
193 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
200
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 if (res.route_summary.total_distance == 0) {
235 continue;
236 }
237
238
239 if (res.route_summary.total_distance < bedsteAfstand) {
240 bedsteAfstand = res.route_summary.total_distance;
241 bedsteAdresse = a2;
242 }
243
244 if (bedsteAfstand < 200) { //vejdistance er tæt nok på
245 return bedsteAdresse;
246 }
247
248 }
249
250 } catch (Exception e) {
251 logger.log(Level.SEVERE, "Lookup failed", e);
252 }
253
254 return bedsteAdresse;
255 }
256
257 /*
258 public Adresse getNearestViarouteJni(Adresse a1, Collection<Adresse> haystack) {
259
260 if (binding == null) {
261 initOsrm();
262 }
263
264
265 int bedsteAfstand = Integer.MAX_VALUE;
266 Adresse bedsteAdresse = null;
267
268
269 for(Adresse a2: haystack) {
270
271 ViarouteResult res = binding.viaRoute(a1.latitude, a1.longitude, a2.latitude, a2.longitude);
272
273
274 if (res.status != 0) {
275 System.out.println("OSRM Returned " + res.status );
276 continue;
277 }
278
279 if (res.totalDistance == 0) {
280 continue;
281 }
282
283
284 if (res.totalDistance < bedsteAfstand) {
285 bedsteAfstand = res.totalDistance;
286 bedsteAdresse = a2;
287 }
288
289 if (bedsteAfstand < 200) { //vejdistance er tæt nok på
290 return bedsteAdresse;
291 }
292
293 }
294
295 return bedsteAdresse;
296 }*/
297 }
298
299

  ViewVC Help
Powered by ViewVC 1.1.20