--- dao/FuldDaekningWorker/src/dk/daoas/fulddaekning/osrm/OSRMHelper.java 2015/09/28 14:59:11 2720 +++ dao/FuldDaekningWorker/src/main/java/dk/daoas/fulddaekning/osrm/OSRMHelper.java 2015/10/07 19:32:00 2744 @@ -1,46 +1,85 @@ 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.daoas.fulddaekning.LookupWorker; + +/* +import dk.thoerup.osrmbinding.OSRMBinding; +import dk.thoerup.osrmbinding.ViarouteResult; +*/ public class OSRMHelper { + private static final boolean ENABLE_OSRM = false; + + final static Logger logger = Logger.getLogger( OSRMHelper.class.toString() ); - + Gson gson = new Gson(); - - final String base_url = "http://10.30.2.61:5000"; - public Adresse getNearestViaTable(Adresse a1, Collection haystack) { - + //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 = 9999999; + public Adresse getNearestViarouteHttp(Adresse a1, Collection haystack) { + int bedsteAfstand = Integer.MAX_VALUE; Adresse bedsteAdresse = null; - Iterator it = haystack.iterator(); - while (it.hasNext()) { - Adresse a2 = it.next(); + try (CloseableHttpClient httpclient = HttpClients.createDefault()) { + + String loc1 = a1.latitude + "," + a1.longitude; + - try { - OSRMResponse res = getRoute(a1, a2); + 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; } - } catch (IOException e) { - System.out.println( e.getMessage() ); - System.exit(1); + + if (bedsteAfstand < 200) { //vejdistance er tæt nok på + return bedsteAdresse; + } + } - + + } catch (Exception e) { + logger.log(Level.SEVERE, "Lookup failed", e); } - + 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"); + +/* + public Adresse getNearestViarouteJni(Adresse a1, Collection haystack) { + + if (binding == null) { + initOsrm(); + } + + + int bedsteAfstand = Integer.MAX_VALUE; + Adresse bedsteAdresse = null; - return gson.fromJson(txtResponse, OSRMResponse.class); - } + + 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; + }*/ } + +