package dk.daoas.fulddaekning.osrm; import java.io.IOException; import java.util.Collection; import java.util.Iterator; import java.util.logging.Level; import java.util.logging.Logger; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Future; import org.apache.http.HttpHost; import org.apache.http.HttpRequest; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.nio.client.CloseableHttpPipeliningClient; import org.apache.http.impl.nio.client.HttpAsyncClients; import org.apache.http.util.EntityUtils; import com.google.gson.Gson; import dk.daoas.fulddaekning.Adresse; import dk.daoas.fulddaekning.HttpUtil; public class OSRMHelper { final static Logger logger = Logger.getLogger( OSRMHelper.class.toString() ); Gson gson = new Gson(); final String host = "10.30.2.61"; final int port = 5000; final String base_url = "http://" + host + ":" + port; public Adresse getNearestViaTable(Adresse a1, Collection 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 haystack) { int bedsteAfstand = Integer.MAX_VALUE; Adresse bedsteAdresse = null; Adresse hayArray[] = new Adresse[ haystack.size() ]; haystack.toArray(hayArray); try (CloseableHttpPipeliningClient httpclient = HttpAsyncClients.createPipelining(); ) { httpclient.start(); HttpHost targetHost = new HttpHost(host, port); System.out.println("1>" + targetHost.toString() ); System.out.println("2>" + targetHost.toURI() ); List requests = new ArrayList(); String loc1 = a1.latitude + "," + a1.longitude; //for (int i=0; i> future = httpclient.execute(targetHost, requests, null); List responses = future.get(); HttpResponse respArr[] = new HttpResponse[ responses.size() ]; responses.toArray( respArr ); for (int i=0; i haystack) { int bedsteAfstand = Integer.MAX_VALUE; Adresse bedsteAdresse = null; Iterator it = haystack.iterator(); while (it.hasNext()) { Adresse a2 = it.next(); try { OSRMResponse res = getRoute(a1, a2); if (res.status != 0) { System.out.println("OSRM Returned " + res.status + "/" + res.status_message ); continue; } if (res.route_summary.total_distance < bedsteAfstand) { bedsteAfstand = res.route_summary.total_distance; bedsteAdresse = a2; } } catch (IOException e) { System.out.println( e.getMessage() ); System.exit(1); } } return bedsteAdresse; } public OSRMResponse getRoute(Adresse a1, Adresse a2) throws IOException { String loc1 = a1.latitude + "," + a1.longitude; String loc2 = a2.latitude + "," + a2.longitude; String params = "loc=" + loc1 + "&loc=" + loc2 + "&geometry=false&instructions=false&alt=false"; String url = base_url + "/viaroute?" + params; String txtResponse = HttpUtil.getContentString(url, 500, "UTF-8"); return gson.fromJson(txtResponse, OSRMResponse.class); } }