27 |
import dk.daoas.fulddaekning.Adresse; |
import dk.daoas.fulddaekning.Adresse; |
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 |
|
|
35 |
public class OSRMHelper { |
public class OSRMHelper { |
36 |
|
|
37 |
|
private static final boolean ENABLE_OSRM = false; |
38 |
|
|
39 |
|
|
40 |
final static Logger logger = Logger.getLogger( OSRMHelper.class.toString() ); |
final static Logger logger = Logger.getLogger( OSRMHelper.class.toString() ); |
41 |
|
|
42 |
Gson gson = new Gson(); |
Gson gson = new Gson(); |
43 |
|
|
44 |
|
//static OSRMBinding binding = null; |
45 |
|
|
46 |
final String host = "10.30.2.61"; |
final String host = "127.0.0.1"; |
47 |
final int port = 5000; |
final int port = 5000; |
48 |
final String base_url = "http://" + host + ":" + port; |
final String base_url = "http://" + host + ":" + port; |
49 |
|
/* |
50 |
|
private static synchronized void initOsrm() { |
51 |
|
if (binding == null) { |
52 |
|
binding = new OSRMBinding("/home/openstreetmap/denmark-latest.osrm"); |
53 |
|
} |
54 |
|
} |
55 |
|
*/ |
56 |
|
|
57 |
public Adresse getNearestViaTable(Adresse a1, Collection<Adresse> haystack) { |
public Adresse getNearestTableHttp(Adresse a1, Collection<Adresse> haystack) { |
58 |
|
|
59 |
Adresse bedsteAdresse = null; |
Adresse bedsteAdresse = null; |
60 |
|
|
98 |
|
|
99 |
return bedsteAdresse; |
return bedsteAdresse; |
100 |
} |
} |
101 |
|
/* |
102 |
|
public Adresse getNearestTableJni(Adresse a1, Collection<Adresse> haystack) { |
103 |
|
if (binding == null) { |
104 |
|
initOsrm(); |
105 |
|
} |
106 |
|
|
107 |
|
Adresse bedsteAdresse = null; |
108 |
|
|
109 |
|
Adresse hayArray[] = new Adresse[ haystack.size() ]; |
110 |
|
haystack.toArray(hayArray); |
111 |
|
|
112 |
|
dk.thoerup.osrmbinding.Geopoint points[] = new dk.thoerup.osrmbinding.Geopoint[ hayArray.length + 1 ]; |
113 |
|
points[0] = new dk.thoerup.osrmbinding.Geopoint( a1.latitude, a1.longitude); |
114 |
|
|
115 |
|
|
116 |
|
for(int i = 0; i<hayArray.length; i++) { |
117 |
|
Adresse a = hayArray[i]; |
118 |
|
int idx = i+1; |
119 |
|
points[idx] = new dk.thoerup.osrmbinding.Geopoint(a.latitude, a.longitude); |
120 |
|
} |
121 |
|
|
122 |
|
try { |
123 |
|
float distance_table[][] = binding.table( points ); |
124 |
|
|
125 |
|
int bedsteTid = Integer.MAX_VALUE; |
126 |
|
|
127 |
|
for (int i = 1; i<distance_table.length; i++) { |
128 |
|
if (distance_table[0][i] < bedsteTid) { |
129 |
|
bedsteTid = (int) distance_table[0][i]; |
130 |
|
int idx = i-1; |
131 |
|
bedsteAdresse = hayArray[idx]; |
132 |
|
} |
133 |
|
} |
134 |
|
|
135 |
|
} catch (Exception e) { |
136 |
|
logger.log(Level.SEVERE, "Lookup failed", e); |
137 |
|
System.exit(1); |
138 |
|
} |
139 |
|
|
140 |
|
return bedsteAdresse; |
141 |
|
}*/ |
142 |
|
|
143 |
public Adresse getNearestApacheClient(Adresse a1, Collection<Adresse> haystack) { |
public Adresse getNearestViarouteHttp(Adresse a1, Collection<Adresse> haystack) { |
144 |
int bedsteAfstand = Integer.MAX_VALUE; |
int bedsteAfstand = Integer.MAX_VALUE; |
145 |
Adresse bedsteAdresse = null; |
Adresse bedsteAdresse = null; |
146 |
|
|
182 |
continue; |
continue; |
183 |
} |
} |
184 |
|
|
185 |
|
if (res.route_summary.total_distance == 0) { |
186 |
|
continue; |
187 |
|
} |
188 |
|
|
189 |
|
|
190 |
if (res.route_summary.total_distance < bedsteAfstand) { |
if (res.route_summary.total_distance < bedsteAfstand) { |
191 |
bedsteAfstand = res.route_summary.total_distance; |
bedsteAfstand = res.route_summary.total_distance; |
192 |
bedsteAdresse = a2; |
bedsteAdresse = a2; |
193 |
} |
} |
194 |
|
|
195 |
|
if (bedsteAfstand < 200) { //vejdistance er tæt nok på |
196 |
|
return bedsteAdresse; |
197 |
|
} |
198 |
|
|
199 |
} |
} |
200 |
|
|
205 |
return bedsteAdresse; |
return bedsteAdresse; |
206 |
} |
} |
207 |
|
|
208 |
|
/* |
209 |
|
public Adresse getNearestViarouteJni(Adresse a1, Collection<Adresse> haystack) { |
210 |
|
|
211 |
public Adresse getNearestPipeline(Adresse a1, Collection<Adresse> haystack) { |
if (binding == null) { |
212 |
|
initOsrm(); |
213 |
int bedsteAfstand = Integer.MAX_VALUE; |
} |
|
Adresse bedsteAdresse = null; |
|
|
|
|
|
Adresse hayArray[] = new Adresse[ haystack.size() ]; |
|
|
haystack.toArray(hayArray); |
|
|
|
|
|
|
|
|
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) ); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
Future<List<HttpResponse>> future = httpclient.execute(targetHost, requests, null); |
|
|
List<HttpResponse> responses = future.get(); |
|
|
|
|
|
HttpResponse respArr[] = new HttpResponse[ responses.size() ]; |
|
|
responses.toArray( respArr ); |
|
|
|
|
|
for (int i=0; i<respArr.length; i++) { |
|
|
String jsonBody = EntityUtils.toString( respArr[i].getEntity() ); |
|
|
OSRMResponse res = gson.fromJson(jsonBody, OSRMResponse.class); |
|
|
|
|
|
if (res.route_summary.total_distance < bedsteAfstand) { |
|
|
bedsteAfstand = res.route_summary.total_distance; |
|
|
bedsteAdresse = hayArray[i]; |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} catch(Exception e) { |
|
|
logger.log(Level.SEVERE, "Lookup failed", e); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return bedsteAdresse; |
|
|
} |
|
214 |
|
|
215 |
|
|
|
/* |
|
|
* Denne virker men table opslaget er 10 gange hurtigere |
|
|
* |
|
|
*/ |
|
|
@Deprecated |
|
|
public Adresse getNearest(Adresse a1, Collection<Adresse> haystack) { |
|
216 |
int bedsteAfstand = Integer.MAX_VALUE; |
int bedsteAfstand = Integer.MAX_VALUE; |
217 |
Adresse bedsteAdresse = null; |
Adresse bedsteAdresse = null; |
218 |
|
|
219 |
|
|
220 |
|
for(Adresse a2: haystack) { |
221 |
|
|
222 |
Iterator<Adresse> it = haystack.iterator(); |
ViarouteResult res = binding.viaRoute(a1.latitude, a1.longitude, a2.latitude, a2.longitude); |
|
while (it.hasNext()) { |
|
|
Adresse a2 = it.next(); |
|
223 |
|
|
224 |
try { |
|
225 |
OSRMResponse res = getRoute(a1, a2); |
if (res.status != 0) { |
226 |
|
System.out.println("OSRM Returned " + res.status ); |
227 |
|
continue; |
228 |
|
} |
229 |
|
|
230 |
if (res.status != 0) { |
if (res.totalDistance == 0) { |
|
System.out.println("OSRM Returned " + res.status + "/" + res.status_message ); |
|
231 |
continue; |
continue; |
232 |
} |
} |
233 |
|
|
234 |
|
|
235 |
if (res.route_summary.total_distance < bedsteAfstand) { |
if (res.totalDistance < bedsteAfstand) { |
236 |
bedsteAfstand = res.route_summary.total_distance; |
bedsteAfstand = res.totalDistance; |
237 |
bedsteAdresse = a2; |
bedsteAdresse = a2; |
238 |
} |
} |
|
} catch (IOException e) { |
|
|
System.out.println( e.getMessage() ); |
|
|
System.exit(1); |
|
|
} |
|
239 |
|
|
240 |
} |
if (bedsteAfstand < 200) { //vejdistance er tæt nok på |
241 |
|
return bedsteAdresse; |
242 |
|
} |
243 |
|
|
244 |
|
} |
245 |
|
|
246 |
return bedsteAdresse; |
return bedsteAdresse; |
247 |
} |
}*/ |
248 |
|
} |
|
public OSRMResponse getRoute(Adresse a1, Adresse a2) throws IOException { |
|
|
|
|
|
String loc1 = a1.latitude + "," + a1.longitude; |
|
|
String loc2 = a2.latitude + "," + a2.longitude; |
|
|
|
|
|
String params = "loc=" + loc1 + "&loc=" + loc2 + "&geometry=false&instructions=false&alt=false"; |
|
|
|
|
|
String url = base_url + "/viaroute?" + params; |
|
249 |
|
|
|
String txtResponse = HttpUtil.getContentString(url, 500, "UTF-8"); |
|
250 |
|
|
|
return gson.fromJson(txtResponse, OSRMResponse.class); |
|
|
} |
|
|
} |
|