1 |
package dk.daoas.fulddaekning.osrm;
|
2 |
|
3 |
import java.io.IOException;
|
4 |
import java.util.Collection;
|
5 |
import java.util.logging.Level;
|
6 |
import java.util.logging.Logger;
|
7 |
|
8 |
import net.minidev.json.JSONValue;
|
9 |
|
10 |
import org.apache.http.HttpEntity;
|
11 |
import org.apache.http.HttpResponse;
|
12 |
import org.apache.http.client.ClientProtocolException;
|
13 |
import org.apache.http.client.ResponseHandler;
|
14 |
import org.apache.http.client.methods.HttpGet;
|
15 |
import org.apache.http.impl.client.CloseableHttpClient;
|
16 |
import org.apache.http.impl.client.HttpClients;
|
17 |
import org.apache.http.util.EntityUtils;
|
18 |
|
19 |
import com.google.gson.Gson;
|
20 |
|
21 |
import dk.daoas.fulddaekning.Adresse;
|
22 |
import dk.daoas.fulddaekning.HttpUtil;
|
23 |
|
24 |
|
25 |
public class OSRMHelper {
|
26 |
|
27 |
|
28 |
final static Logger logger = Logger.getLogger( OSRMHelper.class.toString() );
|
29 |
|
30 |
Gson gson = new Gson();
|
31 |
|
32 |
//static OSRMBinding binding = null;
|
33 |
|
34 |
final String host = "127.0.0.1";
|
35 |
//final String host = "10.30.2.103";
|
36 |
final int port = 5000;
|
37 |
final String base_url = "http://" + host + ":" + port;
|
38 |
|
39 |
|
40 |
public Adresse getNearestTableHttp(Adresse a1, Collection<Adresse> haystack) {
|
41 |
|
42 |
|
43 |
|
44 |
Adresse bedsteAdresse = null;
|
45 |
|
46 |
Adresse hayArray[] = new Adresse[ haystack.size() ];
|
47 |
haystack.toArray(hayArray);
|
48 |
|
49 |
StringBuilder sb = new StringBuilder();
|
50 |
sb.append(base_url);
|
51 |
sb.append("/table?src=").append(a1.latitude).append(",").append(a1.longitude);
|
52 |
|
53 |
for(int i = 0; i<hayArray.length; i++) {
|
54 |
Adresse a = hayArray[i];
|
55 |
sb.append("&dst=").append( a.latitude ).append(",").append(a.longitude);
|
56 |
}
|
57 |
|
58 |
try {
|
59 |
|
60 |
String txtResponse = HttpUtil.getContentString(sb.toString(), 500, "UTF-8");
|
61 |
|
62 |
//OSRMDistanceTable table = gson.fromJson(txtResponse, OSRMDistanceTable.class);
|
63 |
OSRMDistanceTable table = JSONValue.parse(txtResponse, OSRMDistanceTable.class);
|
64 |
if (table.status != 0) {
|
65 |
logger.info("OSRM failed with message: " + table.status_message);
|
66 |
return null;
|
67 |
}
|
68 |
|
69 |
if ( table.distance_table[0].length != hayArray.length ) {
|
70 |
logger.log(Level.SEVERE, "Wrong number of results in matrix " + table.distance_table[0].length);
|
71 |
|
72 |
System.out.println("--------------");
|
73 |
System.out.println("URL:");
|
74 |
System.out.println(sb.toString());
|
75 |
System.out.println("--------------");
|
76 |
System.out.println("Response:");
|
77 |
System.out.println(txtResponse);
|
78 |
|
79 |
|
80 |
System.exit(0);
|
81 |
}
|
82 |
|
83 |
int bedsteTid = Integer.MAX_VALUE;
|
84 |
|
85 |
for (int i = 0; i<table.distance_table[0].length; i++) {
|
86 |
if (table.distance_table[0][i] < bedsteTid) {
|
87 |
bedsteTid = table.distance_table[0][i];
|
88 |
bedsteAdresse = hayArray[i];
|
89 |
}
|
90 |
}
|
91 |
|
92 |
|
93 |
} catch (Exception e) {
|
94 |
logger.log(Level.SEVERE, "Lookup failed", e);
|
95 |
System.out.println( sb.toString() );
|
96 |
System.out.println( a1 );
|
97 |
System.exit(1);
|
98 |
}
|
99 |
|
100 |
//return gson.fromJson(txtResponse, OSRMResponse.class);
|
101 |
|
102 |
|
103 |
|
104 |
return bedsteAdresse;
|
105 |
}
|
106 |
|
107 |
|
108 |
public Adresse getNearestViarouteHttp(Adresse a1, Collection<Adresse> haystack) {
|
109 |
int bedsteAfstand = Integer.MAX_VALUE;
|
110 |
Adresse bedsteAdresse = null;
|
111 |
|
112 |
try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
|
113 |
|
114 |
String loc1 = a1.latitude + "," + a1.longitude;
|
115 |
|
116 |
|
117 |
for(Adresse a2: haystack) {
|
118 |
String loc2 = a2.latitude + "," + a2.longitude;
|
119 |
|
120 |
String uri = base_url + "/viaroute?loc=" + loc1 + "&loc=" + loc2 + "&geometry=false&instructions=false&alt=false";
|
121 |
|
122 |
HttpGet httpget = new HttpGet(uri);
|
123 |
|
124 |
ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
|
125 |
|
126 |
@Override
|
127 |
public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {
|
128 |
int status = response.getStatusLine().getStatusCode();
|
129 |
|
130 |
if (status >= 200 && status < 300) {
|
131 |
HttpEntity entity = response.getEntity();
|
132 |
return entity != null ? EntityUtils.toString(entity) : null;
|
133 |
} else {
|
134 |
throw new ClientProtocolException("Unexpected response status: " + status);
|
135 |
}
|
136 |
}
|
137 |
|
138 |
};
|
139 |
String responseBody = httpclient.execute(httpget, responseHandler);
|
140 |
|
141 |
OSRMResponse res = gson.fromJson(responseBody, OSRMResponse.class);
|
142 |
|
143 |
|
144 |
|
145 |
if (res.status != 0) {
|
146 |
System.out.println("OSRM Returned " + res.status + "/" + res.status_message );
|
147 |
continue;
|
148 |
}
|
149 |
|
150 |
if (res.route_summary.total_distance == 0) {
|
151 |
continue;
|
152 |
}
|
153 |
|
154 |
|
155 |
if (res.route_summary.total_distance < bedsteAfstand) {
|
156 |
bedsteAfstand = res.route_summary.total_distance;
|
157 |
bedsteAdresse = a2;
|
158 |
}
|
159 |
|
160 |
if (bedsteAfstand < 200) { //vejdistance er tæt nok på
|
161 |
return bedsteAdresse;
|
162 |
}
|
163 |
|
164 |
}
|
165 |
|
166 |
} catch (Exception e) {
|
167 |
logger.log(Level.SEVERE, "Lookup failed", e);
|
168 |
}
|
169 |
|
170 |
return bedsteAdresse;
|
171 |
}
|
172 |
|
173 |
}
|
174 |
|
175 |
|