1 |
package dk.daoas.fulddaekning.osrm;
|
2 |
|
3 |
import java.io.IOException;
|
4 |
import java.util.Collection;
|
5 |
import java.util.Iterator;
|
6 |
import java.util.logging.Level;
|
7 |
import java.util.logging.Logger;
|
8 |
|
9 |
|
10 |
import java.util.ArrayList;
|
11 |
import java.util.List;
|
12 |
import java.util.concurrent.Future;
|
13 |
|
14 |
import org.apache.http.HttpHost;
|
15 |
import org.apache.http.HttpRequest;
|
16 |
import org.apache.http.HttpResponse;
|
17 |
import org.apache.http.client.methods.HttpGet;
|
18 |
import org.apache.http.impl.nio.client.CloseableHttpPipeliningClient;
|
19 |
import org.apache.http.impl.nio.client.HttpAsyncClients;
|
20 |
import org.apache.http.util.EntityUtils;
|
21 |
|
22 |
import com.google.gson.Gson;
|
23 |
|
24 |
import dk.daoas.fulddaekning.Adresse;
|
25 |
import dk.daoas.fulddaekning.HttpUtil;
|
26 |
import dk.daoas.fulddaekning.LookupWorker;
|
27 |
|
28 |
public class OSRMHelper {
|
29 |
|
30 |
final static Logger logger = Logger.getLogger( OSRMHelper.class.toString() );
|
31 |
|
32 |
Gson gson = new Gson();
|
33 |
|
34 |
|
35 |
final String host = "10.30.2.61";
|
36 |
final int port = 5000;
|
37 |
final String base_url = "http://" + host + ":" + port;
|
38 |
|
39 |
public Adresse getNearestViaTable(Adresse a1, Collection<Adresse> haystack) {
|
40 |
|
41 |
Adresse bedsteAdresse = null;
|
42 |
|
43 |
Adresse hayArray[] = new Adresse[ haystack.size() ];
|
44 |
haystack.toArray(hayArray);
|
45 |
|
46 |
StringBuilder sb = new StringBuilder();
|
47 |
sb.append(base_url);
|
48 |
sb.append("/table?loc=").append(a1.latitude).append(",").append(a1.longitude);
|
49 |
|
50 |
for(int i = 0; i<hayArray.length; i++) {
|
51 |
Adresse a = hayArray[i];
|
52 |
sb.append("&loc=").append( a.latitude ).append(",").append(a.longitude);
|
53 |
}
|
54 |
|
55 |
try {
|
56 |
String txtResponse = HttpUtil.getContentString(sb.toString(), 500, "UTF-8");
|
57 |
OSRMDistanceTable table = gson.fromJson(txtResponse, OSRMDistanceTable.class);
|
58 |
if (table.status != 0) {
|
59 |
logger.info("OSRM failed with message: " + table.status_message);
|
60 |
return null;
|
61 |
}
|
62 |
|
63 |
int bedsteTid = Integer.MAX_VALUE;
|
64 |
|
65 |
for (int i = 1; i<table.distance_table.length; i++) {
|
66 |
if (table.distance_table[0][i] < bedsteTid) {
|
67 |
bedsteTid = table.distance_table[0][i];
|
68 |
int idx = i-1;
|
69 |
bedsteAdresse = hayArray[idx];
|
70 |
}
|
71 |
}
|
72 |
|
73 |
} catch (Exception e) {
|
74 |
logger.log(Level.SEVERE, "Lookup failed", e);
|
75 |
System.exit(1);
|
76 |
}
|
77 |
|
78 |
//return gson.fromJson(txtResponse, OSRMResponse.class);
|
79 |
|
80 |
|
81 |
return bedsteAdresse;
|
82 |
}
|
83 |
|
84 |
public Adresse getNearestPipeline(Adresse a1, Collection<Adresse> haystack) {
|
85 |
|
86 |
int bedsteAfstand = Integer.MAX_VALUE;
|
87 |
Adresse bedsteAdresse = null;
|
88 |
|
89 |
Adresse hayArray[] = new Adresse[ haystack.size() ];
|
90 |
haystack.toArray(hayArray);
|
91 |
|
92 |
|
93 |
try (CloseableHttpPipeliningClient httpclient = HttpAsyncClients.createPipelining(); ) {
|
94 |
httpclient.start();
|
95 |
|
96 |
HttpHost targetHost = new HttpHost(host, port);
|
97 |
|
98 |
System.out.println("1>" + targetHost.toString() );
|
99 |
System.out.println("2>" + targetHost.toURI() );
|
100 |
|
101 |
List<HttpRequest> requests = new ArrayList<HttpRequest>();
|
102 |
|
103 |
String loc1 = a1.latitude + "," + a1.longitude;
|
104 |
|
105 |
//for (int i=0; i<hayArray.length; i++) {
|
106 |
for (int i=0; i<2; i++) {
|
107 |
Adresse a2 = hayArray[i];
|
108 |
String loc2 = a2.latitude + "," + a2.longitude;
|
109 |
|
110 |
String uri = "/viaroute?loc=" + loc1 + "&loc=" + loc2 + "&geometry=false&instructions=false&alt=false";
|
111 |
System.out.println( uri );
|
112 |
|
113 |
requests.add( new HttpGet(uri) );
|
114 |
|
115 |
}
|
116 |
|
117 |
|
118 |
Future<List<HttpResponse>> future = httpclient.execute(targetHost, requests, null);
|
119 |
List<HttpResponse> responses = future.get();
|
120 |
|
121 |
HttpResponse respArr[] = new HttpResponse[ responses.size() ];
|
122 |
responses.toArray( respArr );
|
123 |
|
124 |
for (int i=0; i<respArr.length; i++) {
|
125 |
String jsonBody = EntityUtils.toString( respArr[i].getEntity() );
|
126 |
OSRMResponse res = gson.fromJson(jsonBody, OSRMResponse.class);
|
127 |
|
128 |
if (res.route_summary.total_distance < bedsteAfstand) {
|
129 |
bedsteAfstand = res.route_summary.total_distance;
|
130 |
bedsteAdresse = hayArray[i];
|
131 |
}
|
132 |
|
133 |
}
|
134 |
|
135 |
|
136 |
System.out.println(responses);
|
137 |
|
138 |
} catch(Exception e) {
|
139 |
logger.log(Level.SEVERE, "Lookup failed", e);
|
140 |
|
141 |
}
|
142 |
|
143 |
|
144 |
return bedsteAdresse;
|
145 |
}
|
146 |
|
147 |
|
148 |
/*
|
149 |
* Denne virker men table opslaget er 10 gange hurtigere
|
150 |
*
|
151 |
*/
|
152 |
@Deprecated
|
153 |
public Adresse getNearest(Adresse a1, Collection<Adresse> haystack) {
|
154 |
int bedsteAfstand = Integer.MAX_VALUE;
|
155 |
Adresse bedsteAdresse = null;
|
156 |
|
157 |
Iterator<Adresse> it = haystack.iterator();
|
158 |
while (it.hasNext()) {
|
159 |
Adresse a2 = it.next();
|
160 |
|
161 |
try {
|
162 |
OSRMResponse res = getRoute(a1, a2);
|
163 |
|
164 |
if (res.status != 0) {
|
165 |
System.out.println("OSRM Returned " + res.status + "/" + res.status_message );
|
166 |
continue;
|
167 |
}
|
168 |
|
169 |
|
170 |
if (res.route_summary.total_distance < bedsteAfstand) {
|
171 |
bedsteAfstand = res.route_summary.total_distance;
|
172 |
bedsteAdresse = a2;
|
173 |
}
|
174 |
} catch (IOException e) {
|
175 |
System.out.println( e.getMessage() );
|
176 |
System.exit(1);
|
177 |
}
|
178 |
|
179 |
}
|
180 |
|
181 |
return bedsteAdresse;
|
182 |
}
|
183 |
|
184 |
public OSRMResponse getRoute(Adresse a1, Adresse a2) throws IOException {
|
185 |
|
186 |
String loc1 = a1.latitude + "," + a1.longitude;
|
187 |
String loc2 = a2.latitude + "," + a2.longitude;
|
188 |
|
189 |
String params = "loc=" + loc1 + "&loc=" + loc2 + "&geometry=false&instructions=false&alt=false";
|
190 |
|
191 |
String url = base_url + "/viaroute?" + params;
|
192 |
|
193 |
String txtResponse = HttpUtil.getContentString(url, 500, "UTF-8");
|
194 |
|
195 |
return gson.fromJson(txtResponse, OSRMResponse.class);
|
196 |
}
|
197 |
}
|