1 |
package dk.daoas.fulddaekning.osrm;
|
2 |
|
3 |
import java.io.IOException;
|
4 |
import java.util.Collection;
|
5 |
import java.util.Iterator;
|
6 |
|
7 |
import com.google.gson.Gson;
|
8 |
|
9 |
import dk.daoas.fulddaekning.Adresse;
|
10 |
import dk.daoas.fulddaekning.HttpUtil;
|
11 |
|
12 |
public class OSRMHelper {
|
13 |
|
14 |
Gson gson = new Gson();
|
15 |
|
16 |
final String base_url = "http://10.30.2.61:5000";
|
17 |
|
18 |
public Adresse getNearestViaTable(Adresse a1, Collection<Adresse> haystack) {
|
19 |
|
20 |
Adresse bedsteAdresse = null;
|
21 |
|
22 |
Adresse hayArray[] = new Adresse[ haystack.size() ];
|
23 |
haystack.toArray(hayArray);
|
24 |
|
25 |
StringBuilder sb = new StringBuilder();
|
26 |
sb.append(base_url);
|
27 |
sb.append("/table?loc=").append(a1.latitude).append(",").append(a1.longitude);
|
28 |
|
29 |
for(int i = 0; i<hayArray.length; i++) {
|
30 |
Adresse a = hayArray[i];
|
31 |
sb.append("&loc=").append( a.latitude ).append(",").append(a.longitude);
|
32 |
}
|
33 |
|
34 |
try {
|
35 |
String txtResponse = HttpUtil.getContentString(sb.toString(), 500, "UTF-8");
|
36 |
OSRMDistanceTable table = gson.fromJson(txtResponse, OSRMDistanceTable.class);
|
37 |
int bedsteTid = Integer.MAX_VALUE;
|
38 |
|
39 |
for (int i = 1; i<table.distance_table.length; i++) {
|
40 |
if (table.distance_table[0][i] < bedsteTid) {
|
41 |
bedsteTid = table.distance_table[0][i];
|
42 |
int idx = i-1;
|
43 |
bedsteAdresse = hayArray[idx];
|
44 |
}
|
45 |
}
|
46 |
|
47 |
} catch (Exception e) {
|
48 |
System.out.println( e.getMessage() );
|
49 |
System.exit(1);
|
50 |
}
|
51 |
|
52 |
//return gson.fromJson(txtResponse, OSRMResponse.class);
|
53 |
|
54 |
|
55 |
return bedsteAdresse;
|
56 |
}
|
57 |
|
58 |
/*
|
59 |
* Denne virker men table opslaget er 10 gange hurtigere
|
60 |
*
|
61 |
*/
|
62 |
@Deprecated
|
63 |
public Adresse getNearest(Adresse a1, Collection<Adresse> haystack) {
|
64 |
int bedsteAfstand = 9999999;
|
65 |
Adresse bedsteAdresse = null;
|
66 |
|
67 |
Iterator<Adresse> it = haystack.iterator();
|
68 |
while (it.hasNext()) {
|
69 |
Adresse a2 = it.next();
|
70 |
|
71 |
try {
|
72 |
OSRMResponse res = getRoute(a1, a2);
|
73 |
|
74 |
|
75 |
if (res.route_summary.total_distance < bedsteAfstand) {
|
76 |
bedsteAfstand = res.route_summary.total_distance;
|
77 |
bedsteAdresse = a2;
|
78 |
}
|
79 |
} catch (IOException e) {
|
80 |
System.out.println( e.getMessage() );
|
81 |
System.exit(1);
|
82 |
}
|
83 |
|
84 |
}
|
85 |
|
86 |
return bedsteAdresse;
|
87 |
}
|
88 |
|
89 |
public OSRMResponse getRoute(Adresse a1, Adresse a2) throws IOException {
|
90 |
|
91 |
String loc1 = a1.latitude + "," + a1.longitude;
|
92 |
String loc2 = a2.latitude + "," + a2.longitude;
|
93 |
|
94 |
String params = "loc=" + loc1 + "&loc=" + loc2 + "&geometry=false&instructions=false&alt=false";
|
95 |
|
96 |
String url = base_url + "/viaroute?" + params;
|
97 |
|
98 |
String txtResponse = HttpUtil.getContentString(url, 500, "UTF-8");
|
99 |
|
100 |
return gson.fromJson(txtResponse, OSRMResponse.class);
|
101 |
}
|
102 |
}
|