--- dao/FuldDaekningWorker/src/main/java/dk/daoas/fulddaekning/osrm/OSRMHelper.java 2015/09/29 09:58:14 2733 +++ dao/FuldDaekningWorker/src/main/java/dk/daoas/fulddaekning/osrm/OSRMHelper.java 2015/12/03 11:30:27 2789 @@ -2,21 +2,18 @@ import java.io.IOException; import java.util.Collection; -import java.util.Iterator; import java.util.logging.Level; import java.util.logging.Logger; +import net.minidev.json.JSONValue; -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.HttpEntity; 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.nio.client.CloseableHttpPipeliningClient; -import org.apache.http.impl.nio.client.HttpAsyncClients; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import com.google.gson.Gson; @@ -24,19 +21,35 @@ import dk.daoas.fulddaekning.Adresse; import dk.daoas.fulddaekning.HttpUtil; +/* +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(); + //static OSRMBinding binding = null; - final String host = "10.30.2.61"; + final String host = "127.0.0.1"; + //final String host = "10.30.2.103"; 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 getNearestViaTable(Adresse a1, Collection haystack) { + public Adresse getNearestTableHttp(Adresse a1, Collection haystack) { Adresse bedsteAdresse = null; @@ -53,16 +66,24 @@ } try { + + String txtResponse = HttpUtil.getContentString(sb.toString(), 500, "UTF-8"); - OSRMDistanceTable table = gson.fromJson(txtResponse, OSRMDistanceTable.class); + //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); + } + int bedsteTid = Integer.MAX_VALUE; - for (int i = 1; i haystack) { + public Adresse getNearestTableHttpExperimental(Adresse a1, Collection haystack) { + + - int bedsteAfstand = Integer.MAX_VALUE; Adresse bedsteAdresse = null; Adresse hayArray[] = new Adresse[ haystack.size() ]; haystack.toArray(hayArray); + StringBuilder sb = new StringBuilder(); + sb.append(base_url); + sb.append("/table?src=").append(a1.latitude).append(",").append(a1.longitude); - try (CloseableHttpPipeliningClient httpclient = HttpAsyncClients.createPipelining(); ) { - httpclient.start(); + for(int i = 0; i" + targetHost.toString() ); - System.out.println("2>" + targetHost.toURI() ); + String txtResponse = HttpUtil.getContentString(sb.toString(), 500, "UTF-8"); - List requests = new ArrayList(); + //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; + } - String loc1 = a1.latitude + "," + a1.longitude; + if ( table.distance_table[0].length != hayArray.length ) { + logger.log(Level.SEVERE, "Wrong number of results in matrix " + table.distance_table[0].length); - //for (int i=0; i> future = httpclient.execute(targetHost, requests, null); - List responses = future.get(); + } catch (Exception e) { + logger.log(Level.SEVERE, "Lookup failed", e); + System.out.println( sb.toString() ); + System.out.println( a1 ); + System.exit(1); + } - HttpResponse respArr[] = new HttpResponse[ responses.size() ]; - responses.toArray( respArr ); + //return gson.fromJson(txtResponse, OSRMResponse.class); - 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) { - return bedsteAdresse; - } + 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 haystack) { + return bedsteAdresse; + } + + public Adresse getNearestViarouteHttp(Adresse a1, Collection haystack) { int bedsteAfstand = Integer.MAX_VALUE; Adresse bedsteAdresse = null; + + try (CloseableHttpClient httpclient = HttpClients.createDefault()) { + + String loc1 = a1.latitude + "," + a1.longitude; - Iterator it = haystack.iterator(); - while (it.hasNext()) { - Adresse a2 = it.next(); - - try { - OSRMResponse res = getRoute(a1, a2); + + for(Adresse a2: haystack) { + String loc2 = a2.latitude + "," + a2.longitude; - if (res.status != 0) { + 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 { +/* + public Adresse getNearestViarouteJni(Adresse a1, Collection haystack) { + + if (binding == null) { + initOsrm(); + } + - String loc1 = a1.latitude + "," + a1.longitude; - String loc2 = a2.latitude + "," + a2.longitude; + 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; + } - String params = "loc=" + loc1 + "&loc=" + loc2 + "&geometry=false&instructions=false&alt=false"; + if (res.totalDistance == 0) { + continue; + } - String url = base_url + "/viaroute?" + params; - String txtResponse = HttpUtil.getContentString(url, 500, "UTF-8"); + if (res.totalDistance < bedsteAfstand) { + bedsteAfstand = res.totalDistance; + bedsteAdresse = a2; + } - return gson.fromJson(txtResponse, OSRMResponse.class); - } + if (bedsteAfstand < 200) { //vejdistance er tæt nok på + return bedsteAdresse; + } + + } + + return bedsteAdresse; + }*/ } + +