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; |
7 |
|
import java.util.List; |
8 |
|
import java.util.concurrent.Future; |
9 |
import java.util.logging.Level; |
import java.util.logging.Level; |
10 |
import java.util.logging.Logger; |
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; |
import com.google.gson.Gson; |
26 |
|
|
27 |
import dk.daoas.fulddaekning.Adresse; |
import dk.daoas.fulddaekning.Adresse; |
28 |
import dk.daoas.fulddaekning.HttpUtil; |
import dk.daoas.fulddaekning.HttpUtil; |
29 |
import dk.daoas.fulddaekning.LookupWorker; |
|
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(); |
|
|
|
|
final String base_url = "http://10.30.2.61:5000"; |
|
39 |
|
|
40 |
public Adresse getNearestViaTable(Adresse a1, Collection<Adresse> haystack) { |
static OSRMBinding binding = null; |
41 |
|
|
42 |
|
final String host = "127.0.0.1"; |
43 |
|
final int port = 5000; |
44 |
|
final String base_url = "http://" + host + ":" + port; |
45 |
|
|
46 |
|
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 |
|
|
56 |
Adresse hayArray[] = new Adresse[ haystack.size() ]; |
Adresse hayArray[] = new Adresse[ haystack.size() ]; |
57 |
haystack.toArray(hayArray); |
haystack.toArray(hayArray); |
58 |
|
|
59 |
StringBuilder sb = new StringBuilder(); |
StringBuilder sb = new StringBuilder(); |
60 |
sb.append(base_url); |
sb.append(base_url); |
61 |
sb.append("/table?loc=").append(a1.latitude).append(",").append(a1.longitude); |
sb.append("/table?loc=").append(a1.latitude).append(",").append(a1.longitude); |
62 |
|
|
63 |
for(int i = 0; i<hayArray.length; i++) { |
for(int i = 0; i<hayArray.length; i++) { |
64 |
Adresse a = hayArray[i]; |
Adresse a = hayArray[i]; |
65 |
sb.append("&loc=").append( a.latitude ).append(",").append(a.longitude); |
sb.append("&loc=").append( a.latitude ).append(",").append(a.longitude); |
66 |
} |
} |
67 |
|
|
68 |
try { |
try { |
69 |
String txtResponse = HttpUtil.getContentString(sb.toString(), 500, "UTF-8"); |
String txtResponse = HttpUtil.getContentString(sb.toString(), 500, "UTF-8"); |
70 |
OSRMDistanceTable table = gson.fromJson(txtResponse, OSRMDistanceTable.class); |
OSRMDistanceTable table = gson.fromJson(txtResponse, OSRMDistanceTable.class); |
72 |
logger.info("OSRM failed with message: " + table.status_message); |
logger.info("OSRM failed with message: " + table.status_message); |
73 |
return null; |
return null; |
74 |
} |
} |
75 |
|
|
76 |
int bedsteTid = Integer.MAX_VALUE; |
int bedsteTid = Integer.MAX_VALUE; |
77 |
|
|
78 |
for (int i = 1; i<table.distance_table.length; i++) { |
for (int i = 1; i<table.distance_table.length; i++) { |
79 |
if (table.distance_table[0][i] < bedsteTid) { |
if (table.distance_table[0][i] < bedsteTid) { |
80 |
bedsteTid = table.distance_table[0][i]; |
bedsteTid = table.distance_table[0][i]; |
87 |
logger.log(Level.SEVERE, "Lookup failed", e); |
logger.log(Level.SEVERE, "Lookup failed", e); |
88 |
System.exit(1); |
System.exit(1); |
89 |
} |
} |
90 |
|
|
91 |
//return gson.fromJson(txtResponse, OSRMResponse.class); |
//return gson.fromJson(txtResponse, OSRMResponse.class); |
92 |
|
|
93 |
|
|
94 |
|
return bedsteAdresse; |
95 |
|
} |
96 |
|
|
97 |
|
public Adresse getNearestTableJni(Adresse a1, Collection<Adresse> haystack) { |
98 |
|
if (binding == null) { |
99 |
|
initOsrm(); |
100 |
|
} |
101 |
|
|
102 |
|
Adresse bedsteAdresse = null; |
103 |
|
|
104 |
|
Adresse hayArray[] = new Adresse[ haystack.size() ]; |
105 |
|
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 |
|
|
110 |
|
|
111 |
|
for(int i = 0; i<hayArray.length; i++) { |
112 |
|
Adresse a = hayArray[i]; |
113 |
|
int idx = i+1; |
114 |
|
points[idx] = new dk.thoerup.osrmbinding.Geopoint(a.latitude, a.longitude); |
115 |
|
} |
116 |
|
|
117 |
|
try { |
118 |
|
float distance_table[][] = binding.table( points ); |
119 |
|
|
120 |
|
int bedsteTid = Integer.MAX_VALUE; |
121 |
|
|
122 |
|
for (int i = 1; i<distance_table.length; i++) { |
123 |
|
if (distance_table[0][i] < bedsteTid) { |
124 |
|
bedsteTid = (int) distance_table[0][i]; |
125 |
|
int idx = i-1; |
126 |
|
bedsteAdresse = hayArray[idx]; |
127 |
|
} |
128 |
|
} |
129 |
|
|
130 |
|
} catch (Exception e) { |
131 |
|
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) { |
139 |
* Denne virker men table opslaget er 10 gange hurtigere |
int bedsteAfstand = Integer.MAX_VALUE; |
|
* |
|
|
*/ |
|
|
@Deprecated |
|
|
public Adresse getNearest(Adresse a1, Collection<Adresse> haystack) { |
|
|
int bedsteAfstand = 9999999; |
|
140 |
Adresse bedsteAdresse = null; |
Adresse bedsteAdresse = null; |
141 |
|
|
142 |
Iterator<Adresse> it = haystack.iterator(); |
try (CloseableHttpClient httpclient = HttpClients.createDefault()) { |
143 |
while (it.hasNext()) { |
|
144 |
Adresse a2 = it.next(); |
String loc1 = a1.latitude + "," + a1.longitude; |
145 |
|
|
146 |
|
|
147 |
try { |
for(Adresse a2: haystack) { |
148 |
OSRMResponse res = getRoute(a1, a2); |
String loc2 = a2.latitude + "," + a2.longitude; |
149 |
|
|
150 |
|
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 ); |
177 |
|
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 |
} |
} |
189 |
} catch (IOException e) { |
|
190 |
System.out.println( e.getMessage() ); |
if (bedsteAfstand < 200) { //vejdistance er tæt nok på |
191 |
System.exit(1); |
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 |
|
|
204 |
public OSRMResponse getRoute(Adresse a1, Adresse a2) throws IOException { |
public Adresse getNearestViarouteJni(Adresse a1, Collection<Adresse> haystack) { |
205 |
|
|
206 |
String loc1 = a1.latitude + "," + a1.longitude; |
if (binding == null) { |
207 |
String loc2 = a2.latitude + "," + a2.longitude; |
initOsrm(); |
208 |
|
} |
209 |
String params = "loc=" + loc1 + "&loc=" + loc2 + "&geometry=false&instructions=false&alt=false"; |
|
210 |
|
|
211 |
String url = base_url + "/viaroute?" + params; |
int bedsteAfstand = Integer.MAX_VALUE; |
212 |
|
Adresse bedsteAdresse = null; |
|
String txtResponse = HttpUtil.getContentString(url, 500, "UTF-8"); |
|
213 |
|
|
214 |
return gson.fromJson(txtResponse, OSRMResponse.class); |
|
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 |
|
if (res.totalDistance == 0) { |
226 |
|
continue; |
227 |
|
} |
228 |
|
|
229 |
|
|
230 |
|
if (res.totalDistance < bedsteAfstand) { |
231 |
|
bedsteAfstand = res.totalDistance; |
232 |
|
bedsteAdresse = a2; |
233 |
|
} |
234 |
|
|
235 |
|
if (bedsteAfstand < 200) { //vejdistance er tæt nok på |
236 |
|
return bedsteAdresse; |
237 |
|
} |
238 |
|
|
239 |
|
} |
240 |
|
|
241 |
|
return bedsteAdresse; |
242 |
} |
} |
243 |
} |
} |
244 |
|
|
245 |
|
|