1 |
package dk.daoas.fulddaekning.osrm;
|
2 |
|
3 |
import java.io.IOException;
|
4 |
import java.util.ArrayList;
|
5 |
import java.util.Collection;
|
6 |
import java.util.Iterator;
|
7 |
import java.util.List;
|
8 |
import java.util.concurrent.Future;
|
9 |
import java.util.logging.Level;
|
10 |
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;
|
26 |
|
27 |
import dk.daoas.fulddaekning.Adresse;
|
28 |
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 {
|
36 |
|
37 |
private static final boolean ENABLE_OSRM = false;
|
38 |
|
39 |
|
40 |
final static Logger logger = Logger.getLogger( OSRMHelper.class.toString() );
|
41 |
|
42 |
Gson gson = new Gson();
|
43 |
|
44 |
//static OSRMBinding binding = null;
|
45 |
|
46 |
final String host = "127.0.0.1";
|
47 |
final int port = 5000;
|
48 |
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 getNearestTableHttp(Adresse a1, Collection<Adresse> haystack) {
|
58 |
|
59 |
Adresse bedsteAdresse = null;
|
60 |
|
61 |
Adresse hayArray[] = new Adresse[ haystack.size() ];
|
62 |
haystack.toArray(hayArray);
|
63 |
|
64 |
StringBuilder sb = new StringBuilder();
|
65 |
sb.append(base_url);
|
66 |
sb.append("/table?loc=").append(a1.latitude).append(",").append(a1.longitude);
|
67 |
|
68 |
for(int i = 0; i<hayArray.length; i++) {
|
69 |
Adresse a = hayArray[i];
|
70 |
sb.append("&loc=").append( a.latitude ).append(",").append(a.longitude);
|
71 |
}
|
72 |
|
73 |
try {
|
74 |
String txtResponse = HttpUtil.getContentString(sb.toString(), 500, "UTF-8");
|
75 |
OSRMDistanceTable table = gson.fromJson(txtResponse, OSRMDistanceTable.class);
|
76 |
if (table.status != 0) {
|
77 |
logger.info("OSRM failed with message: " + table.status_message);
|
78 |
return null;
|
79 |
}
|
80 |
|
81 |
int bedsteTid = Integer.MAX_VALUE;
|
82 |
|
83 |
for (int i = 1; i<table.distance_table.length; i++) {
|
84 |
if (table.distance_table[0][i] < bedsteTid) {
|
85 |
bedsteTid = table.distance_table[0][i];
|
86 |
int idx = i-1;
|
87 |
bedsteAdresse = hayArray[idx];
|
88 |
}
|
89 |
}
|
90 |
|
91 |
} catch (Exception e) {
|
92 |
logger.log(Level.SEVERE, "Lookup failed", e);
|
93 |
System.exit(1);
|
94 |
}
|
95 |
|
96 |
//return gson.fromJson(txtResponse, OSRMResponse.class);
|
97 |
|
98 |
|
99 |
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 getNearestViarouteHttp(Adresse a1, Collection<Adresse> haystack) {
|
144 |
int bedsteAfstand = Integer.MAX_VALUE;
|
145 |
Adresse bedsteAdresse = null;
|
146 |
|
147 |
try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
|
148 |
|
149 |
String loc1 = a1.latitude + "," + a1.longitude;
|
150 |
|
151 |
|
152 |
for(Adresse a2: haystack) {
|
153 |
String loc2 = a2.latitude + "," + a2.longitude;
|
154 |
|
155 |
String uri = base_url + "/viaroute?loc=" + loc1 + "&loc=" + loc2 + "&geometry=false&instructions=false&alt=false";
|
156 |
|
157 |
HttpGet httpget = new HttpGet(uri);
|
158 |
|
159 |
ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
|
160 |
|
161 |
@Override
|
162 |
public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {
|
163 |
int status = response.getStatusLine().getStatusCode();
|
164 |
|
165 |
if (status >= 200 && status < 300) {
|
166 |
HttpEntity entity = response.getEntity();
|
167 |
return entity != null ? EntityUtils.toString(entity) : null;
|
168 |
} else {
|
169 |
throw new ClientProtocolException("Unexpected response status: " + status);
|
170 |
}
|
171 |
}
|
172 |
|
173 |
};
|
174 |
String responseBody = httpclient.execute(httpget, responseHandler);
|
175 |
|
176 |
OSRMResponse res = gson.fromJson(responseBody, OSRMResponse.class);
|
177 |
|
178 |
|
179 |
|
180 |
if (res.status != 0) {
|
181 |
System.out.println("OSRM Returned " + res.status + "/" + res.status_message );
|
182 |
continue;
|
183 |
}
|
184 |
|
185 |
if (res.route_summary.total_distance == 0) {
|
186 |
continue;
|
187 |
}
|
188 |
|
189 |
|
190 |
if (res.route_summary.total_distance < bedsteAfstand) {
|
191 |
bedsteAfstand = res.route_summary.total_distance;
|
192 |
bedsteAdresse = a2;
|
193 |
}
|
194 |
|
195 |
if (bedsteAfstand < 200) { //vejdistance er tæt nok på
|
196 |
return bedsteAdresse;
|
197 |
}
|
198 |
|
199 |
}
|
200 |
|
201 |
} catch (Exception e) {
|
202 |
logger.log(Level.SEVERE, "Lookup failed", e);
|
203 |
}
|
204 |
|
205 |
return bedsteAdresse;
|
206 |
}
|
207 |
|
208 |
/*
|
209 |
public Adresse getNearestViarouteJni(Adresse a1, Collection<Adresse> haystack) {
|
210 |
|
211 |
if (binding == null) {
|
212 |
initOsrm();
|
213 |
}
|
214 |
|
215 |
|
216 |
int bedsteAfstand = Integer.MAX_VALUE;
|
217 |
Adresse bedsteAdresse = null;
|
218 |
|
219 |
|
220 |
for(Adresse a2: haystack) {
|
221 |
|
222 |
ViarouteResult res = binding.viaRoute(a1.latitude, a1.longitude, a2.latitude, a2.longitude);
|
223 |
|
224 |
|
225 |
if (res.status != 0) {
|
226 |
System.out.println("OSRM Returned " + res.status );
|
227 |
continue;
|
228 |
}
|
229 |
|
230 |
if (res.totalDistance == 0) {
|
231 |
continue;
|
232 |
}
|
233 |
|
234 |
|
235 |
if (res.totalDistance < bedsteAfstand) {
|
236 |
bedsteAfstand = res.totalDistance;
|
237 |
bedsteAdresse = a2;
|
238 |
}
|
239 |
|
240 |
if (bedsteAfstand < 200) { //vejdistance er tæt nok på
|
241 |
return bedsteAdresse;
|
242 |
}
|
243 |
|
244 |
}
|
245 |
|
246 |
return bedsteAdresse;
|
247 |
}*/
|
248 |
}
|
249 |
|
250 |
|