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