package dk.daoas.fulddaekning.osrm; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.concurrent.Future; import java.util.logging.Level; import java.util.logging.Logger; import org.apache.http.HttpEntity; import org.apache.http.HttpHost; import org.apache.http.HttpRequest; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; 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; import dk.thoerup.osrmbinding.OSRMBinding; import dk.thoerup.osrmbinding.ViarouteResult; public class OSRMHelper { final static Logger logger = Logger.getLogger( OSRMHelper.class.toString() ); Gson gson = new Gson(); static OSRMBinding binding = null; final String host = "127.0.0.1"; final int port = 5000; 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 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) { 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 haystack) { int bedsteAfstand = Integer.MAX_VALUE; Adresse bedsteAdresse = null; try (CloseableHttpClient httpclient = HttpClients.createDefault()) { String loc1 = a1.latitude + "," + a1.longitude; for(Adresse a2: haystack) { String loc2 = a2.latitude + "," + a2.longitude; String uri = base_url + "/viaroute?loc=" + loc1 + "&loc=" + loc2 + "&geometry=false&instructions=false&alt=false"; HttpGet httpget = new HttpGet(uri); ResponseHandler responseHandler = new ResponseHandler() { @Override public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException { int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } else { throw new ClientProtocolException("Unexpected response status: " + status); } } }; String responseBody = httpclient.execute(httpget, responseHandler); OSRMResponse res = gson.fromJson(responseBody, OSRMResponse.class); if (res.status != 0) { System.out.println("OSRM Returned " + res.status + "/" + res.status_message ); continue; } if (res.route_summary.total_distance == 0) { continue; } if (res.route_summary.total_distance < bedsteAfstand) { bedsteAfstand = res.route_summary.total_distance; bedsteAdresse = a2; } if (bedsteAfstand < 200) { //vejdistance er tæt nok på return bedsteAdresse; } } } catch (Exception e) { logger.log(Level.SEVERE, "Lookup failed", e); } return bedsteAdresse; } public Adresse getNearestViarouteJni(Adresse a1, Collection 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; } }