21 |
import dk.daoas.fulddaekning.Adresse; |
import dk.daoas.fulddaekning.Adresse; |
22 |
import dk.daoas.fulddaekning.HttpUtil; |
import dk.daoas.fulddaekning.HttpUtil; |
23 |
|
|
|
/* |
|
|
import dk.thoerup.osrmbinding.OSRMBinding; |
|
|
import dk.thoerup.osrmbinding.ViarouteResult; |
|
|
*/ |
|
24 |
|
|
25 |
public class OSRMHelper { |
public class OSRMHelper { |
|
|
|
|
private static final boolean ENABLE_OSRM = false; |
|
26 |
|
|
27 |
|
|
28 |
final static Logger logger = Logger.getLogger( OSRMHelper.class.toString() ); |
final static Logger logger = Logger.getLogger( OSRMHelper.class.toString() ); |
35 |
//final String host = "10.30.2.103"; |
//final String host = "10.30.2.103"; |
36 |
final int port = 5000; |
final int port = 5000; |
37 |
final String base_url = "http://" + host + ":" + port; |
final String base_url = "http://" + host + ":" + port; |
|
/* |
|
|
private static synchronized void initOsrm() { |
|
|
if (binding == null) { |
|
|
binding = new OSRMBinding("/home/openstreetmap/denmark-latest.osrm"); |
|
|
} |
|
|
} |
|
|
*/ |
|
|
|
|
|
public Adresse getNearestTableHttp(Adresse a1, Collection<Adresse> haystack) { |
|
|
|
|
|
Adresse bedsteAdresse = null; |
|
|
|
|
|
Adresse hayArray[] = new Adresse[ haystack.size() ]; |
|
|
haystack.toArray(hayArray); |
|
|
|
|
|
StringBuilder sb = new StringBuilder(); |
|
|
sb.append(base_url); |
|
|
sb.append("/table?loc=").append(a1.latitude).append(",").append(a1.longitude); |
|
|
|
|
|
for(int i = 0; i<hayArray.length; i++) { |
|
|
Adresse a = hayArray[i]; |
|
|
sb.append("&loc=").append( a.latitude ).append(",").append(a.longitude); |
|
|
} |
|
|
|
|
|
try { |
|
|
|
|
|
|
|
|
String txtResponse = HttpUtil.getContentString(sb.toString(), 500, "UTF-8"); |
|
|
OSRMDistanceTable table = gson.fromJson(txtResponse, OSRMDistanceTable.class); |
|
|
//OSRMDistanceTable table = JSONValue.parse(txtResponse, OSRMDistanceTable.class); |
|
|
if (table.status != 0) { |
|
|
logger.info("OSRM failed with message: " + table.status_message); |
|
|
return null; |
|
|
} |
|
|
|
|
|
if ( table.distance_table.length != (hayArray.length + 1) ) { |
|
|
logger.log(Level.SEVERE, "Wrong number of results in matrix " + table.distance_table.length); |
|
|
System.exit(0); |
|
|
} |
|
38 |
|
|
|
int bedsteTid = Integer.MAX_VALUE; |
|
39 |
|
|
40 |
for (int i = 1; i<table.distance_table[0].length; i++) { |
public Adresse getNearestTableHttp(Adresse a1, Collection<Adresse> haystack) { |
|
if (table.distance_table[0][i] < bedsteTid) { |
|
|
bedsteTid = table.distance_table[0][i]; |
|
|
int idx = i-1; |
|
|
bedsteAdresse = hayArray[idx]; |
|
|
} |
|
|
} |
|
|
|
|
|
} catch (Exception e) { |
|
|
logger.log(Level.SEVERE, "Lookup failed", e); |
|
|
System.exit(1); |
|
|
} |
|
|
|
|
|
//return gson.fromJson(txtResponse, OSRMResponse.class); |
|
|
|
|
|
|
|
|
return bedsteAdresse; |
|
|
} |
|
|
|
|
|
public Adresse getNearestTableHttpExperimental(Adresse a1, Collection<Adresse> haystack) { |
|
41 |
|
|
42 |
|
|
43 |
|
|
103 |
|
|
104 |
return bedsteAdresse; |
return bedsteAdresse; |
105 |
} |
} |
106 |
|
|
|
|
|
|
|
|
|
/* |
|
|
public Adresse getNearestTableJni(Adresse a1, Collection<Adresse> haystack) { |
|
|
if (binding == null) { |
|
|
initOsrm(); |
|
|
} |
|
|
|
|
|
Adresse bedsteAdresse = null; |
|
|
|
|
|
Adresse hayArray[] = new Adresse[ haystack.size() ]; |
|
|
haystack.toArray(hayArray); |
|
|
|
|
|
dk.thoerup.osrmbinding.Geopoint points[] = new dk.thoerup.osrmbinding.Geopoint[ hayArray.length + 1 ]; |
|
|
points[0] = new dk.thoerup.osrmbinding.Geopoint( a1.latitude, a1.longitude); |
|
|
|
|
|
|
|
|
for(int i = 0; i<hayArray.length; i++) { |
|
|
Adresse a = hayArray[i]; |
|
|
int idx = i+1; |
|
|
points[idx] = new dk.thoerup.osrmbinding.Geopoint(a.latitude, a.longitude); |
|
|
} |
|
|
|
|
|
try { |
|
|
float distance_table[][] = binding.table( points ); |
|
|
|
|
|
if ( distance_table.length != (hayArray.length + 1) ) { |
|
|
logger.log(Level.SEVERE, "Wrong number of results in matrix " + distance_table.length); |
|
|
System.exit(0); |
|
|
} |
|
|
|
|
|
int bedsteTid = Integer.MAX_VALUE; |
|
|
|
|
|
for (int i = 1; i<distance_table.length; i++) { |
|
|
if (distance_table[0][i] < bedsteTid) { |
|
|
bedsteTid = (int) distance_table[0][i]; |
|
|
int idx = i-1; |
|
|
bedsteAdresse = hayArray[idx]; |
|
|
} |
|
|
} |
|
|
|
|
|
} catch (Exception e) { |
|
|
logger.log(Level.SEVERE, "Lookup failed", e); |
|
|
System.exit(1); |
|
|
} |
|
|
|
|
|
return bedsteAdresse; |
|
|
}*/ |
|
|
|
|
|
|
|
|
public Adresse getNearestMultitargetHttp(Adresse a1, Collection<Adresse> haystack) { |
|
|
|
|
|
Adresse bedsteAdresse = null; |
|
|
|
|
|
Adresse hayArray[] = new Adresse[ haystack.size() ]; |
|
|
haystack.toArray(hayArray); |
|
|
|
|
|
StringBuilder sb = new StringBuilder(); |
|
|
sb.append(base_url); |
|
|
sb.append("/multitarget?loc=").append(a1.latitude).append(",").append(a1.longitude); |
|
|
|
|
|
for(int i = 0; i<hayArray.length; i++) { |
|
|
Adresse a = hayArray[i]; |
|
|
sb.append("&loc=").append( a.latitude ).append(",").append(a.longitude); |
|
|
} |
|
|
|
|
|
try { |
|
|
String txtResponse = HttpUtil.getContentString(sb.toString(), 500, "UTF-8"); |
|
|
OSRMMultiTarget table = gson.fromJson(txtResponse, OSRMMultiTarget.class); |
|
|
|
|
|
|
|
|
double bedsteAfstand = Double.MAX_VALUE; |
|
|
|
|
|
for (int i = 1; i<table.distances.length; i++) { |
|
|
if (table.distances[i].distance < bedsteAfstand) { |
|
|
bedsteAfstand = table.distances[i].distance; |
|
|
bedsteAdresse = hayArray[i]; |
|
|
} |
|
|
} |
|
|
|
|
|
} catch (Exception e) { |
|
|
logger.log(Level.SEVERE, "Lookup failed", e); |
|
|
System.exit(1); |
|
|
} |
|
|
|
|
|
|
|
|
return bedsteAdresse; |
|
|
} |
|
107 |
|
|
108 |
public Adresse getNearestViarouteHttp(Adresse a1, Collection<Adresse> haystack) { |
public Adresse getNearestViarouteHttp(Adresse a1, Collection<Adresse> haystack) { |
109 |
int bedsteAfstand = Integer.MAX_VALUE; |
int bedsteAfstand = Integer.MAX_VALUE; |
170 |
return bedsteAdresse; |
return bedsteAdresse; |
171 |
} |
} |
172 |
|
|
|
/* |
|
|
public Adresse getNearestViarouteJni(Adresse a1, Collection<Adresse> haystack) { |
|
|
|
|
|
if (binding == null) { |
|
|
initOsrm(); |
|
|
} |
|
|
|
|
|
|
|
|
int bedsteAfstand = Integer.MAX_VALUE; |
|
|
Adresse bedsteAdresse = null; |
|
|
|
|
|
|
|
|
for(Adresse a2: haystack) { |
|
|
|
|
|
ViarouteResult res = binding.viaRoute(a1.latitude, a1.longitude, a2.latitude, a2.longitude); |
|
|
|
|
|
|
|
|
if (res.status != 0) { |
|
|
System.out.println("OSRM Returned " + res.status ); |
|
|
continue; |
|
|
} |
|
|
|
|
|
if (res.totalDistance == 0) { |
|
|
continue; |
|
|
} |
|
|
|
|
|
|
|
|
if (res.totalDistance < bedsteAfstand) { |
|
|
bedsteAfstand = res.totalDistance; |
|
|
bedsteAdresse = a2; |
|
|
} |
|
|
|
|
|
if (bedsteAfstand < 200) { //vejdistance er tæt nok på |
|
|
return bedsteAdresse; |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return bedsteAdresse; |
|
|
}*/ |
|
173 |
} |
} |
174 |
|
|
175 |
|
|