1 |
torben |
2708 |
package dk.daoas.fulddaekning.osrm;
|
2 |
|
|
|
3 |
|
|
import java.io.IOException;
|
4 |
torben |
2735 |
import java.util.ArrayList;
|
5 |
torben |
2708 |
import java.util.Collection;
|
6 |
|
|
import java.util.Iterator;
|
7 |
torben |
2735 |
import java.util.List;
|
8 |
|
|
import java.util.concurrent.Future;
|
9 |
torben |
2720 |
import java.util.logging.Level;
|
10 |
|
|
import java.util.logging.Logger;
|
11 |
torben |
2708 |
|
12 |
torben |
2735 |
import org.apache.http.HttpEntity;
|
13 |
torben |
2730 |
import org.apache.http.HttpHost;
|
14 |
|
|
import org.apache.http.HttpRequest;
|
15 |
|
|
import org.apache.http.HttpResponse;
|
16 |
torben |
2735 |
import org.apache.http.client.ClientProtocolException;
|
17 |
|
|
import org.apache.http.client.ResponseHandler;
|
18 |
torben |
2730 |
import org.apache.http.client.methods.HttpGet;
|
19 |
torben |
2735 |
import org.apache.http.impl.client.CloseableHttpClient;
|
20 |
|
|
import org.apache.http.impl.client.HttpClients;
|
21 |
torben |
2730 |
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 |
torben |
2708 |
import com.google.gson.Gson;
|
26 |
|
|
|
27 |
|
|
import dk.daoas.fulddaekning.Adresse;
|
28 |
|
|
import dk.daoas.fulddaekning.HttpUtil;
|
29 |
|
|
|
30 |
torben |
2733 |
|
31 |
torben |
2741 |
import dk.thoerup.osrmbinding.OSRMBinding;
|
32 |
|
|
import dk.thoerup.osrmbinding.ViarouteResult;
|
33 |
|
|
|
34 |
torben |
2708 |
public class OSRMHelper {
|
35 |
torben |
2733 |
|
36 |
torben |
2720 |
final static Logger logger = Logger.getLogger( OSRMHelper.class.toString() );
|
37 |
torben |
2733 |
|
38 |
torben |
2708 |
Gson gson = new Gson();
|
39 |
|
|
|
40 |
torben |
2741 |
static OSRMBinding binding = null;
|
41 |
torben |
2733 |
|
42 |
torben |
2741 |
final String host = "127.0.0.1";
|
43 |
torben |
2730 |
final int port = 5000;
|
44 |
|
|
final String base_url = "http://" + host + ":" + port;
|
45 |
|
|
|
46 |
torben |
2741 |
private static synchronized void initOsrm() {
|
47 |
|
|
if (binding == null) {
|
48 |
|
|
binding = new OSRMBinding("/home/openstreetmap/denmark-latest.osrm");
|
49 |
|
|
}
|
50 |
|
|
}
|
51 |
torben |
2733 |
|
52 |
torben |
2741 |
public Adresse getNearestTableHttp(Adresse a1, Collection<Adresse> haystack) {
|
53 |
|
|
|
54 |
torben |
2708 |
Adresse bedsteAdresse = null;
|
55 |
torben |
2733 |
|
56 |
torben |
2708 |
Adresse hayArray[] = new Adresse[ haystack.size() ];
|
57 |
|
|
haystack.toArray(hayArray);
|
58 |
torben |
2733 |
|
59 |
torben |
2708 |
StringBuilder sb = new StringBuilder();
|
60 |
|
|
sb.append(base_url);
|
61 |
|
|
sb.append("/table?loc=").append(a1.latitude).append(",").append(a1.longitude);
|
62 |
torben |
2733 |
|
63 |
torben |
2708 |
for(int i = 0; i<hayArray.length; i++) {
|
64 |
|
|
Adresse a = hayArray[i];
|
65 |
|
|
sb.append("&loc=").append( a.latitude ).append(",").append(a.longitude);
|
66 |
|
|
}
|
67 |
torben |
2733 |
|
68 |
torben |
2708 |
try {
|
69 |
|
|
String txtResponse = HttpUtil.getContentString(sb.toString(), 500, "UTF-8");
|
70 |
|
|
OSRMDistanceTable table = gson.fromJson(txtResponse, OSRMDistanceTable.class);
|
71 |
torben |
2723 |
if (table.status != 0) {
|
72 |
|
|
logger.info("OSRM failed with message: " + table.status_message);
|
73 |
|
|
return null;
|
74 |
|
|
}
|
75 |
torben |
2733 |
|
76 |
torben |
2708 |
int bedsteTid = Integer.MAX_VALUE;
|
77 |
torben |
2733 |
|
78 |
torben |
2708 |
for (int i = 1; i<table.distance_table.length; i++) {
|
79 |
|
|
if (table.distance_table[0][i] < bedsteTid) {
|
80 |
|
|
bedsteTid = table.distance_table[0][i];
|
81 |
|
|
int idx = i-1;
|
82 |
|
|
bedsteAdresse = hayArray[idx];
|
83 |
|
|
}
|
84 |
|
|
}
|
85 |
|
|
|
86 |
|
|
} catch (Exception e) {
|
87 |
torben |
2720 |
logger.log(Level.SEVERE, "Lookup failed", e);
|
88 |
torben |
2708 |
System.exit(1);
|
89 |
|
|
}
|
90 |
torben |
2733 |
|
91 |
torben |
2708 |
//return gson.fromJson(txtResponse, OSRMResponse.class);
|
92 |
torben |
2733 |
|
93 |
|
|
|
94 |
torben |
2708 |
return bedsteAdresse;
|
95 |
|
|
}
|
96 |
torben |
2741 |
|
97 |
|
|
public Adresse getNearestTableJni(Adresse a1, Collection<Adresse> haystack) {
|
98 |
|
|
if (binding == null) {
|
99 |
|
|
initOsrm();
|
100 |
|
|
}
|
101 |
|
|
|
102 |
|
|
Adresse bedsteAdresse = null;
|
103 |
|
|
|
104 |
|
|
Adresse hayArray[] = new Adresse[ haystack.size() ];
|
105 |
|
|
haystack.toArray(hayArray);
|
106 |
|
|
|
107 |
|
|
dk.thoerup.osrmbinding.Geopoint points[] = new dk.thoerup.osrmbinding.Geopoint[ hayArray.length + 1 ];
|
108 |
|
|
points[0] = new dk.thoerup.osrmbinding.Geopoint( a1.latitude, a1.longitude);
|
109 |
|
|
|
110 |
|
|
|
111 |
|
|
for(int i = 0; i<hayArray.length; i++) {
|
112 |
|
|
Adresse a = hayArray[i];
|
113 |
|
|
int idx = i+1;
|
114 |
|
|
points[idx] = new dk.thoerup.osrmbinding.Geopoint(a.latitude, a.longitude);
|
115 |
|
|
}
|
116 |
|
|
|
117 |
|
|
try {
|
118 |
|
|
float distance_table[][] = binding.table( points );
|
119 |
|
|
|
120 |
|
|
int bedsteTid = Integer.MAX_VALUE;
|
121 |
|
|
|
122 |
|
|
for (int i = 1; i<distance_table.length; i++) {
|
123 |
|
|
if (distance_table[0][i] < bedsteTid) {
|
124 |
|
|
bedsteTid = (int) distance_table[0][i];
|
125 |
|
|
int idx = i-1;
|
126 |
|
|
bedsteAdresse = hayArray[idx];
|
127 |
|
|
}
|
128 |
|
|
}
|
129 |
|
|
|
130 |
|
|
} catch (Exception e) {
|
131 |
|
|
logger.log(Level.SEVERE, "Lookup failed", e);
|
132 |
|
|
System.exit(1);
|
133 |
|
|
}
|
134 |
|
|
|
135 |
|
|
return bedsteAdresse;
|
136 |
|
|
}
|
137 |
torben |
2735 |
|
138 |
torben |
2741 |
public Adresse getNearestViarouteHttp(Adresse a1, Collection<Adresse> haystack) {
|
139 |
torben |
2735 |
int bedsteAfstand = Integer.MAX_VALUE;
|
140 |
|
|
Adresse bedsteAdresse = null;
|
141 |
|
|
|
142 |
|
|
try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
|
143 |
|
|
|
144 |
|
|
String loc1 = a1.latitude + "," + a1.longitude;
|
145 |
torben |
2730 |
|
146 |
torben |
2735 |
|
147 |
|
|
for(Adresse a2: haystack) {
|
148 |
|
|
String loc2 = a2.latitude + "," + a2.longitude;
|
149 |
|
|
|
150 |
|
|
String uri = base_url + "/viaroute?loc=" + loc1 + "&loc=" + loc2 + "&geometry=false&instructions=false&alt=false";
|
151 |
|
|
|
152 |
|
|
HttpGet httpget = new HttpGet(uri);
|
153 |
|
|
|
154 |
|
|
ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
|
155 |
|
|
|
156 |
|
|
@Override
|
157 |
|
|
public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {
|
158 |
|
|
int status = response.getStatusLine().getStatusCode();
|
159 |
|
|
|
160 |
|
|
if (status >= 200 && status < 300) {
|
161 |
|
|
HttpEntity entity = response.getEntity();
|
162 |
|
|
return entity != null ? EntityUtils.toString(entity) : null;
|
163 |
|
|
} else {
|
164 |
|
|
throw new ClientProtocolException("Unexpected response status: " + status);
|
165 |
|
|
}
|
166 |
|
|
}
|
167 |
|
|
|
168 |
|
|
};
|
169 |
|
|
String responseBody = httpclient.execute(httpget, responseHandler);
|
170 |
|
|
|
171 |
|
|
OSRMResponse res = gson.fromJson(responseBody, OSRMResponse.class);
|
172 |
|
|
|
173 |
|
|
|
174 |
|
|
|
175 |
|
|
if (res.status != 0) {
|
176 |
|
|
System.out.println("OSRM Returned " + res.status + "/" + res.status_message );
|
177 |
|
|
continue;
|
178 |
|
|
}
|
179 |
|
|
|
180 |
torben |
2741 |
if (res.route_summary.total_distance == 0) {
|
181 |
|
|
continue;
|
182 |
|
|
}
|
183 |
torben |
2735 |
|
184 |
torben |
2741 |
|
185 |
torben |
2735 |
if (res.route_summary.total_distance < bedsteAfstand) {
|
186 |
|
|
bedsteAfstand = res.route_summary.total_distance;
|
187 |
|
|
bedsteAdresse = a2;
|
188 |
|
|
}
|
189 |
torben |
2741 |
|
190 |
|
|
if (bedsteAfstand < 200) { //vejdistance er tæt nok på
|
191 |
|
|
return bedsteAdresse;
|
192 |
|
|
}
|
193 |
torben |
2735 |
|
194 |
|
|
}
|
195 |
|
|
|
196 |
|
|
} catch (Exception e) {
|
197 |
|
|
logger.log(Level.SEVERE, "Lookup failed", e);
|
198 |
|
|
}
|
199 |
|
|
|
200 |
|
|
return bedsteAdresse;
|
201 |
|
|
}
|
202 |
|
|
|
203 |
torben |
2741 |
|
204 |
|
|
public Adresse getNearestViarouteJni(Adresse a1, Collection<Adresse> haystack) {
|
205 |
torben |
2735 |
|
206 |
torben |
2741 |
if (binding == null) {
|
207 |
|
|
initOsrm();
|
208 |
|
|
}
|
209 |
torben |
2733 |
|
210 |
torben |
2741 |
|
211 |
torben |
2730 |
int bedsteAfstand = Integer.MAX_VALUE;
|
212 |
|
|
Adresse bedsteAdresse = null;
|
213 |
torben |
2741 |
|
214 |
|
|
|
215 |
|
|
for(Adresse a2: haystack) {
|
216 |
torben |
2730 |
|
217 |
torben |
2741 |
ViarouteResult res = binding.viaRoute(a1.latitude, a1.longitude, a2.latitude, a2.longitude);
|
218 |
torben |
2730 |
|
219 |
torben |
2741 |
|
220 |
|
|
if (res.status != 0) {
|
221 |
|
|
System.out.println("OSRM Returned " + res.status );
|
222 |
|
|
continue;
|
223 |
torben |
2733 |
}
|
224 |
torben |
2730 |
|
225 |
torben |
2741 |
if (res.totalDistance == 0) {
|
226 |
torben |
2731 |
continue;
|
227 |
|
|
}
|
228 |
torben |
2733 |
|
229 |
|
|
|
230 |
torben |
2741 |
if (res.totalDistance < bedsteAfstand) {
|
231 |
|
|
bedsteAfstand = res.totalDistance;
|
232 |
torben |
2708 |
bedsteAdresse = a2;
|
233 |
|
|
}
|
234 |
torben |
2733 |
|
235 |
torben |
2741 |
if (bedsteAfstand < 200) { //vejdistance er tæt nok på
|
236 |
|
|
return bedsteAdresse;
|
237 |
|
|
}
|
238 |
|
|
|
239 |
|
|
}
|
240 |
torben |
2733 |
|
241 |
torben |
2708 |
return bedsteAdresse;
|
242 |
|
|
}
|
243 |
torben |
2741 |
}
|
244 |
torben |
2733 |
|
245 |
|
|
|