1 |
package dk.daoas.fulddaekning.osrm; |
package dk.daoas.fulddaekning.osrm; |
2 |
|
|
3 |
import java.io.IOException; |
import java.io.IOException; |
|
import java.util.ArrayList; |
|
4 |
import java.util.Collection; |
import java.util.Collection; |
|
import java.util.Iterator; |
|
|
import java.util.List; |
|
|
import java.util.concurrent.Future; |
|
5 |
import java.util.logging.Level; |
import java.util.logging.Level; |
6 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
7 |
|
|
8 |
|
import net.minidev.json.JSONValue; |
9 |
|
|
10 |
import org.apache.http.HttpEntity; |
import org.apache.http.HttpEntity; |
|
import org.apache.http.HttpHost; |
|
|
import org.apache.http.HttpRequest; |
|
11 |
import org.apache.http.HttpResponse; |
import org.apache.http.HttpResponse; |
12 |
import org.apache.http.client.ClientProtocolException; |
import org.apache.http.client.ClientProtocolException; |
13 |
import org.apache.http.client.ResponseHandler; |
import org.apache.http.client.ResponseHandler; |
14 |
import org.apache.http.client.methods.HttpGet; |
import org.apache.http.client.methods.HttpGet; |
15 |
import org.apache.http.impl.client.CloseableHttpClient; |
import org.apache.http.impl.client.CloseableHttpClient; |
16 |
import org.apache.http.impl.client.HttpClients; |
import org.apache.http.impl.client.HttpClients; |
|
import org.apache.http.impl.nio.client.CloseableHttpPipeliningClient; |
|
|
import org.apache.http.impl.nio.client.HttpAsyncClients; |
|
17 |
import org.apache.http.util.EntityUtils; |
import org.apache.http.util.EntityUtils; |
18 |
|
|
19 |
import com.google.gson.Gson; |
import com.google.gson.Gson; |
21 |
import dk.daoas.fulddaekning.Adresse; |
import dk.daoas.fulddaekning.Adresse; |
22 |
import dk.daoas.fulddaekning.HttpUtil; |
import dk.daoas.fulddaekning.HttpUtil; |
23 |
|
|
24 |
|
/* |
25 |
import dk.thoerup.osrmbinding.OSRMBinding; |
import dk.thoerup.osrmbinding.OSRMBinding; |
26 |
import dk.thoerup.osrmbinding.ViarouteResult; |
import dk.thoerup.osrmbinding.ViarouteResult; |
27 |
|
*/ |
28 |
|
|
29 |
public class OSRMHelper { |
public class OSRMHelper { |
30 |
|
|
31 |
|
private static final boolean ENABLE_OSRM = false; |
32 |
|
|
33 |
|
|
34 |
final static Logger logger = Logger.getLogger( OSRMHelper.class.toString() ); |
final static Logger logger = Logger.getLogger( OSRMHelper.class.toString() ); |
35 |
|
|
36 |
Gson gson = new Gson(); |
Gson gson = new Gson(); |
37 |
|
|
38 |
static OSRMBinding binding = null; |
//static OSRMBinding binding = null; |
39 |
|
|
40 |
final String host = "127.0.0.1"; |
final String host = "127.0.0.1"; |
41 |
|
//final String host = "10.30.2.103"; |
42 |
final int port = 5000; |
final int port = 5000; |
43 |
final String base_url = "http://" + host + ":" + port; |
final String base_url = "http://" + host + ":" + port; |
44 |
|
/* |
45 |
private static synchronized void initOsrm() { |
private static synchronized void initOsrm() { |
46 |
if (binding == null) { |
if (binding == null) { |
47 |
binding = new OSRMBinding("/home/openstreetmap/denmark-latest.osrm"); |
binding = new OSRMBinding("/home/openstreetmap/denmark-latest.osrm"); |
48 |
} |
} |
49 |
} |
} |
50 |
|
*/ |
51 |
|
|
52 |
public Adresse getNearestTableHttp(Adresse a1, Collection<Adresse> haystack) { |
public Adresse getNearestTableHttp(Adresse a1, Collection<Adresse> haystack) { |
53 |
|
|
66 |
} |
} |
67 |
|
|
68 |
try { |
try { |
69 |
|
|
70 |
|
|
71 |
String txtResponse = HttpUtil.getContentString(sb.toString(), 500, "UTF-8"); |
String txtResponse = HttpUtil.getContentString(sb.toString(), 500, "UTF-8"); |
72 |
OSRMDistanceTable table = gson.fromJson(txtResponse, OSRMDistanceTable.class); |
OSRMDistanceTable table = gson.fromJson(txtResponse, OSRMDistanceTable.class); |
73 |
|
//OSRMDistanceTable table = JSONValue.parse(txtResponse, OSRMDistanceTable.class); |
74 |
if (table.status != 0) { |
if (table.status != 0) { |
75 |
logger.info("OSRM failed with message: " + table.status_message); |
logger.info("OSRM failed with message: " + table.status_message); |
76 |
return null; |
return null; |
77 |
} |
} |
78 |
|
|
79 |
|
if ( table.distance_table.length != (hayArray.length + 1) ) { |
80 |
|
logger.log(Level.SEVERE, "Wrong number of results in matrix " + table.distance_table.length); |
81 |
|
System.exit(0); |
82 |
|
} |
83 |
|
|
84 |
int bedsteTid = Integer.MAX_VALUE; |
int bedsteTid = Integer.MAX_VALUE; |
85 |
|
|
86 |
for (int i = 1; i<table.distance_table.length; i++) { |
for (int i = 1; i<table.distance_table[0].length; i++) { |
87 |
if (table.distance_table[0][i] < bedsteTid) { |
if (table.distance_table[0][i] < bedsteTid) { |
88 |
bedsteTid = table.distance_table[0][i]; |
bedsteTid = table.distance_table[0][i]; |
89 |
int idx = i-1; |
int idx = i-1; |
102 |
return bedsteAdresse; |
return bedsteAdresse; |
103 |
} |
} |
104 |
|
|
105 |
|
public Adresse getNearestTableHttpExperimental(Adresse a1, Collection<Adresse> haystack) { |
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
Adresse bedsteAdresse = null; |
110 |
|
|
111 |
|
Adresse hayArray[] = new Adresse[ haystack.size() ]; |
112 |
|
haystack.toArray(hayArray); |
113 |
|
|
114 |
|
StringBuilder sb = new StringBuilder(); |
115 |
|
sb.append(base_url); |
116 |
|
sb.append("/table?src=").append(a1.latitude).append(",").append(a1.longitude); |
117 |
|
|
118 |
|
for(int i = 0; i<hayArray.length; i++) { |
119 |
|
Adresse a = hayArray[i]; |
120 |
|
sb.append("&dst=").append( a.latitude ).append(",").append(a.longitude); |
121 |
|
} |
122 |
|
|
123 |
|
try { |
124 |
|
|
125 |
|
String txtResponse = HttpUtil.getContentString(sb.toString(), 500, "UTF-8"); |
126 |
|
|
127 |
|
//OSRMDistanceTable table = gson.fromJson(txtResponse, OSRMDistanceTable.class); |
128 |
|
OSRMDistanceTable table = JSONValue.parse(txtResponse, OSRMDistanceTable.class); |
129 |
|
if (table.status != 0) { |
130 |
|
logger.info("OSRM failed with message: " + table.status_message); |
131 |
|
return null; |
132 |
|
} |
133 |
|
|
134 |
|
if ( table.distance_table[0].length != hayArray.length ) { |
135 |
|
logger.log(Level.SEVERE, "Wrong number of results in matrix " + table.distance_table[0].length); |
136 |
|
|
137 |
|
System.out.println("--------------"); |
138 |
|
System.out.println("URL:"); |
139 |
|
System.out.println(sb.toString()); |
140 |
|
System.out.println("--------------"); |
141 |
|
System.out.println("Response:"); |
142 |
|
System.out.println(txtResponse); |
143 |
|
|
144 |
|
|
145 |
|
System.exit(0); |
146 |
|
} |
147 |
|
|
148 |
|
int bedsteTid = Integer.MAX_VALUE; |
149 |
|
|
150 |
|
for (int i = 0; i<table.distance_table[0].length; i++) { |
151 |
|
if (table.distance_table[0][i] < bedsteTid) { |
152 |
|
bedsteTid = table.distance_table[0][i]; |
153 |
|
bedsteAdresse = hayArray[i]; |
154 |
|
} |
155 |
|
} |
156 |
|
|
157 |
|
|
158 |
|
} catch (Exception e) { |
159 |
|
logger.log(Level.SEVERE, "Lookup failed", e); |
160 |
|
System.out.println( sb.toString() ); |
161 |
|
System.out.println( a1 ); |
162 |
|
System.exit(1); |
163 |
|
} |
164 |
|
|
165 |
|
//return gson.fromJson(txtResponse, OSRMResponse.class); |
166 |
|
|
167 |
|
|
168 |
|
|
169 |
|
return bedsteAdresse; |
170 |
|
} |
171 |
|
|
172 |
|
|
173 |
|
|
174 |
|
/* |
175 |
public Adresse getNearestTableJni(Adresse a1, Collection<Adresse> haystack) { |
public Adresse getNearestTableJni(Adresse a1, Collection<Adresse> haystack) { |
176 |
if (binding == null) { |
if (binding == null) { |
177 |
initOsrm(); |
initOsrm(); |
194 |
|
|
195 |
try { |
try { |
196 |
float distance_table[][] = binding.table( points ); |
float distance_table[][] = binding.table( points ); |
197 |
|
|
198 |
|
if ( distance_table.length != (hayArray.length + 1) ) { |
199 |
|
logger.log(Level.SEVERE, "Wrong number of results in matrix " + distance_table.length); |
200 |
|
System.exit(0); |
201 |
|
} |
202 |
|
|
203 |
int bedsteTid = Integer.MAX_VALUE; |
int bedsteTid = Integer.MAX_VALUE; |
204 |
|
|
216 |
} |
} |
217 |
|
|
218 |
return bedsteAdresse; |
return bedsteAdresse; |
219 |
|
}*/ |
220 |
|
|
221 |
|
|
222 |
|
public Adresse getNearestMultitargetHttp(Adresse a1, Collection<Adresse> haystack) { |
223 |
|
|
224 |
|
Adresse bedsteAdresse = null; |
225 |
|
|
226 |
|
Adresse hayArray[] = new Adresse[ haystack.size() ]; |
227 |
|
haystack.toArray(hayArray); |
228 |
|
|
229 |
|
StringBuilder sb = new StringBuilder(); |
230 |
|
sb.append(base_url); |
231 |
|
sb.append("/multitarget?loc=").append(a1.latitude).append(",").append(a1.longitude); |
232 |
|
|
233 |
|
for(int i = 0; i<hayArray.length; i++) { |
234 |
|
Adresse a = hayArray[i]; |
235 |
|
sb.append("&loc=").append( a.latitude ).append(",").append(a.longitude); |
236 |
|
} |
237 |
|
|
238 |
|
try { |
239 |
|
String txtResponse = HttpUtil.getContentString(sb.toString(), 500, "UTF-8"); |
240 |
|
OSRMMultiTarget table = gson.fromJson(txtResponse, OSRMMultiTarget.class); |
241 |
|
|
242 |
|
|
243 |
|
double bedsteAfstand = Double.MAX_VALUE; |
244 |
|
|
245 |
|
for (int i = 1; i<table.distances.length; i++) { |
246 |
|
if (table.distances[i].distance < bedsteAfstand) { |
247 |
|
bedsteAfstand = table.distances[i].distance; |
248 |
|
bedsteAdresse = hayArray[i]; |
249 |
|
} |
250 |
|
} |
251 |
|
|
252 |
|
} catch (Exception e) { |
253 |
|
logger.log(Level.SEVERE, "Lookup failed", e); |
254 |
|
System.exit(1); |
255 |
|
} |
256 |
|
|
257 |
|
|
258 |
|
return bedsteAdresse; |
259 |
} |
} |
260 |
|
|
261 |
public Adresse getNearestViarouteHttp(Adresse a1, Collection<Adresse> haystack) { |
public Adresse getNearestViarouteHttp(Adresse a1, Collection<Adresse> haystack) { |
323 |
return bedsteAdresse; |
return bedsteAdresse; |
324 |
} |
} |
325 |
|
|
326 |
|
/* |
327 |
public Adresse getNearestViarouteJni(Adresse a1, Collection<Adresse> haystack) { |
public Adresse getNearestViarouteJni(Adresse a1, Collection<Adresse> haystack) { |
328 |
|
|
329 |
if (binding == null) { |
if (binding == null) { |
362 |
} |
} |
363 |
|
|
364 |
return bedsteAdresse; |
return bedsteAdresse; |
365 |
} |
}*/ |
366 |
} |
} |
367 |
|
|
368 |
|
|