1 |
package dk.daoas.fulddaekning.osrm; |
package dk.daoas.fulddaekning.osrm; |
2 |
|
|
3 |
import java.io.IOException; |
import java.io.IOException; |
4 |
|
import java.util.ArrayList; |
5 |
import java.util.Collection; |
import java.util.Collection; |
6 |
import java.util.Iterator; |
import java.util.Iterator; |
|
import java.util.logging.Level; |
|
|
import java.util.logging.Logger; |
|
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
7 |
import java.util.List; |
import java.util.List; |
8 |
import java.util.concurrent.Future; |
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; |
import org.apache.http.HttpHost; |
14 |
import org.apache.http.HttpRequest; |
import org.apache.http.HttpRequest; |
15 |
import org.apache.http.HttpResponse; |
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; |
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; |
import org.apache.http.impl.nio.client.CloseableHttpPipeliningClient; |
22 |
import org.apache.http.impl.nio.client.HttpAsyncClients; |
import org.apache.http.impl.nio.client.HttpAsyncClients; |
23 |
import org.apache.http.util.EntityUtils; |
import org.apache.http.util.EntityUtils; |
28 |
import dk.daoas.fulddaekning.HttpUtil; |
import dk.daoas.fulddaekning.HttpUtil; |
29 |
|
|
30 |
|
|
31 |
|
import dk.thoerup.osrmbinding.OSRMBinding; |
32 |
|
import dk.thoerup.osrmbinding.ViarouteResult; |
33 |
|
|
34 |
public class OSRMHelper { |
public class OSRMHelper { |
35 |
|
|
36 |
final static Logger logger = Logger.getLogger( OSRMHelper.class.toString() ); |
final static Logger logger = Logger.getLogger( OSRMHelper.class.toString() ); |
37 |
|
|
38 |
Gson gson = new Gson(); |
Gson gson = new Gson(); |
39 |
|
|
40 |
|
static OSRMBinding binding = null; |
41 |
|
|
42 |
final String host = "10.30.2.61"; |
final String host = "127.0.0.1"; |
43 |
final int port = 5000; |
final int port = 5000; |
44 |
final String base_url = "http://" + host + ":" + port; |
final String base_url = "http://" + host + ":" + port; |
45 |
|
|
46 |
public Adresse getNearestViaTable(Adresse a1, Collection<Adresse> haystack) { |
private static synchronized void initOsrm() { |
47 |
|
if (binding == null) { |
48 |
|
binding = new OSRMBinding("/home/openstreetmap/denmark-latest.osrm"); |
49 |
|
} |
50 |
|
} |
51 |
|
|
52 |
|
public Adresse getNearestTableHttp(Adresse a1, Collection<Adresse> haystack) { |
53 |
|
|
54 |
Adresse bedsteAdresse = null; |
Adresse bedsteAdresse = null; |
55 |
|
|
94 |
return bedsteAdresse; |
return bedsteAdresse; |
95 |
} |
} |
96 |
|
|
97 |
public Adresse getNearestPipeline(Adresse a1, Collection<Adresse> haystack) { |
public Adresse getNearestTableJni(Adresse a1, Collection<Adresse> haystack) { |
98 |
|
if (binding == null) { |
99 |
|
initOsrm(); |
100 |
|
} |
101 |
|
|
|
int bedsteAfstand = Integer.MAX_VALUE; |
|
102 |
Adresse bedsteAdresse = null; |
Adresse bedsteAdresse = null; |
103 |
|
|
104 |
Adresse hayArray[] = new Adresse[ haystack.size() ]; |
Adresse hayArray[] = new Adresse[ haystack.size() ]; |
105 |
haystack.toArray(hayArray); |
haystack.toArray(hayArray); |
106 |
|
|
107 |
|
dk.thoerup.osrmbinding.Geopoint points[] = new dk.thoerup.osrmbinding.Geopoint[ hayArray.length + 1 ]; |
108 |
|
points[0] = new dk.thoerup.osrmbinding.Geopoint( a1.latitude, a1.longitude); |
109 |
|
|
|
try (CloseableHttpPipeliningClient httpclient = HttpAsyncClients.createPipelining(); ) { |
|
|
httpclient.start(); |
|
|
|
|
|
HttpHost targetHost = new HttpHost(host, port); |
|
|
|
|
|
System.out.println("1>" + targetHost.toString() ); |
|
|
System.out.println("2>" + targetHost.toURI() ); |
|
|
|
|
|
List<HttpRequest> requests = new ArrayList<HttpRequest>(); |
|
|
|
|
|
String loc1 = a1.latitude + "," + a1.longitude; |
|
|
|
|
|
//for (int i=0; i<hayArray.length; i++) { |
|
|
for (int i=0; i<2; i++) { |
|
|
Adresse a2 = hayArray[i]; |
|
|
String loc2 = a2.latitude + "," + a2.longitude; |
|
|
|
|
|
String uri = "/viaroute?loc=" + loc1 + "&loc=" + loc2 + "&geometry=false&instructions=false&alt=false"; |
|
|
System.out.println( uri ); |
|
|
|
|
|
requests.add( new HttpGet(uri) ); |
|
110 |
|
|
111 |
} |
for(int i = 0; i<hayArray.length; i++) { |
112 |
|
Adresse a = hayArray[i]; |
113 |
|
int idx = i+1; |
114 |
Future<List<HttpResponse>> future = httpclient.execute(targetHost, requests, null); |
points[idx] = new dk.thoerup.osrmbinding.Geopoint(a.latitude, a.longitude); |
115 |
List<HttpResponse> responses = future.get(); |
} |
116 |
|
|
117 |
HttpResponse respArr[] = new HttpResponse[ responses.size() ]; |
try { |
118 |
responses.toArray( respArr ); |
float distance_table[][] = binding.table( points ); |
119 |
|
|
120 |
for (int i=0; i<respArr.length; i++) { |
int bedsteTid = Integer.MAX_VALUE; |
|
String jsonBody = EntityUtils.toString( respArr[i].getEntity() ); |
|
|
OSRMResponse res = gson.fromJson(jsonBody, OSRMResponse.class); |
|
121 |
|
|
122 |
if (res.route_summary.total_distance < bedsteAfstand) { |
for (int i = 1; i<distance_table.length; i++) { |
123 |
bedsteAfstand = res.route_summary.total_distance; |
if (distance_table[0][i] < bedsteTid) { |
124 |
bedsteAdresse = hayArray[i]; |
bedsteTid = (int) distance_table[0][i]; |
125 |
|
int idx = i-1; |
126 |
|
bedsteAdresse = hayArray[idx]; |
127 |
} |
} |
|
|
|
128 |
} |
} |
129 |
|
|
130 |
|
} catch (Exception e) { |
|
System.out.println(responses); |
|
|
|
|
|
} catch(Exception e) { |
|
131 |
logger.log(Level.SEVERE, "Lookup failed", e); |
logger.log(Level.SEVERE, "Lookup failed", e); |
132 |
|
System.exit(1); |
133 |
} |
} |
|
|
|
134 |
|
|
135 |
return bedsteAdresse; |
return bedsteAdresse; |
136 |
} |
} |
137 |
|
|
138 |
|
public Adresse getNearestViarouteHttp(Adresse a1, Collection<Adresse> haystack) { |
|
/* |
|
|
* Denne virker men table opslaget er 10 gange hurtigere |
|
|
* |
|
|
*/ |
|
|
@Deprecated |
|
|
public Adresse getNearest(Adresse a1, Collection<Adresse> haystack) { |
|
139 |
int bedsteAfstand = Integer.MAX_VALUE; |
int bedsteAfstand = Integer.MAX_VALUE; |
140 |
Adresse bedsteAdresse = null; |
Adresse bedsteAdresse = null; |
141 |
|
|
142 |
|
try (CloseableHttpClient httpclient = HttpClients.createDefault()) { |
143 |
|
|
144 |
|
String loc1 = a1.latitude + "," + a1.longitude; |
145 |
|
|
146 |
Iterator<Adresse> it = haystack.iterator(); |
|
147 |
while (it.hasNext()) { |
for(Adresse a2: haystack) { |
148 |
Adresse a2 = it.next(); |
String loc2 = a2.latitude + "," + a2.longitude; |
|
|
|
|
try { |
|
|
OSRMResponse res = getRoute(a1, a2); |
|
149 |
|
|
150 |
if (res.status != 0) { |
String uri = base_url + "/viaroute?loc=" + loc1 + "&loc=" + loc2 + "&geometry=false&instructions=false&alt=false"; |
151 |
|
|
152 |
|
HttpGet httpget = new HttpGet(uri); |
153 |
|
|
154 |
|
ResponseHandler<String> responseHandler = new ResponseHandler<String>() { |
155 |
|
|
156 |
|
@Override |
157 |
|
public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException { |
158 |
|
int status = response.getStatusLine().getStatusCode(); |
159 |
|
|
160 |
|
if (status >= 200 && status < 300) { |
161 |
|
HttpEntity entity = response.getEntity(); |
162 |
|
return entity != null ? EntityUtils.toString(entity) : null; |
163 |
|
} else { |
164 |
|
throw new ClientProtocolException("Unexpected response status: " + status); |
165 |
|
} |
166 |
|
} |
167 |
|
|
168 |
|
}; |
169 |
|
String responseBody = httpclient.execute(httpget, responseHandler); |
170 |
|
|
171 |
|
OSRMResponse res = gson.fromJson(responseBody, OSRMResponse.class); |
172 |
|
|
173 |
|
|
174 |
|
|
175 |
|
if (res.status != 0) { |
176 |
System.out.println("OSRM Returned " + res.status + "/" + res.status_message ); |
System.out.println("OSRM Returned " + res.status + "/" + res.status_message ); |
177 |
continue; |
continue; |
178 |
} |
} |
179 |
|
|
180 |
|
if (res.route_summary.total_distance == 0) { |
181 |
|
continue; |
182 |
|
} |
183 |
|
|
184 |
|
|
185 |
if (res.route_summary.total_distance < bedsteAfstand) { |
if (res.route_summary.total_distance < bedsteAfstand) { |
186 |
bedsteAfstand = res.route_summary.total_distance; |
bedsteAfstand = res.route_summary.total_distance; |
187 |
bedsteAdresse = a2; |
bedsteAdresse = a2; |
188 |
} |
} |
|
} catch (IOException e) { |
|
|
System.out.println( e.getMessage() ); |
|
|
System.exit(1); |
|
|
} |
|
189 |
|
|
190 |
|
if (bedsteAfstand < 200) { //vejdistance er tæt nok på |
191 |
|
return bedsteAdresse; |
192 |
|
} |
193 |
|
|
194 |
|
} |
195 |
|
|
196 |
|
} catch (Exception e) { |
197 |
|
logger.log(Level.SEVERE, "Lookup failed", e); |
198 |
} |
} |
199 |
|
|
200 |
return bedsteAdresse; |
return bedsteAdresse; |
201 |
} |
} |
202 |
|
|
203 |
public OSRMResponse getRoute(Adresse a1, Adresse a2) throws IOException { |
|
204 |
|
public Adresse getNearestViarouteJni(Adresse a1, Collection<Adresse> haystack) { |
205 |
|
|
206 |
|
if (binding == null) { |
207 |
|
initOsrm(); |
208 |
|
} |
209 |
|
|
210 |
|
|
211 |
|
int bedsteAfstand = Integer.MAX_VALUE; |
212 |
|
Adresse bedsteAdresse = null; |
213 |
|
|
214 |
|
|
215 |
|
for(Adresse a2: haystack) { |
216 |
|
|
217 |
|
ViarouteResult res = binding.viaRoute(a1.latitude, a1.longitude, a2.latitude, a2.longitude); |
218 |
|
|
219 |
|
|
220 |
|
if (res.status != 0) { |
221 |
|
System.out.println("OSRM Returned " + res.status ); |
222 |
|
continue; |
223 |
|
} |
224 |
|
|
225 |
String loc1 = a1.latitude + "," + a1.longitude; |
if (res.totalDistance == 0) { |
226 |
String loc2 = a2.latitude + "," + a2.longitude; |
continue; |
227 |
|
} |
228 |
|
|
|
String params = "loc=" + loc1 + "&loc=" + loc2 + "&geometry=false&instructions=false&alt=false"; |
|
229 |
|
|
230 |
String url = base_url + "/viaroute?" + params; |
if (res.totalDistance < bedsteAfstand) { |
231 |
|
bedsteAfstand = res.totalDistance; |
232 |
|
bedsteAdresse = a2; |
233 |
|
} |
234 |
|
|
235 |
String txtResponse = HttpUtil.getContentString(url, 500, "UTF-8"); |
if (bedsteAfstand < 200) { //vejdistance er tæt nok på |
236 |
|
return bedsteAdresse; |
237 |
|
} |
238 |
|
|
239 |
|
} |
240 |
|
|
241 |
return gson.fromJson(txtResponse, OSRMResponse.class); |
return bedsteAdresse; |
242 |
} |
} |
243 |
} |
} |
244 |
|
|
245 |
|
|