--- dao/FuldDaekningWorker/src/main/java/dk/daoas/fulddaekning/osrm/OSRMHelper.java 2015/10/02 14:24:36 2740 +++ dao/FuldDaekningWorker/src/main/java/dk/daoas/fulddaekning/osrm/OSRMHelper.java 2015/10/06 21:26:55 2741 @@ -28,18 +28,28 @@ 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 = "10.30.2.61"; + final String host = "127.0.0.1"; final int port = 5000; final String base_url = "http://" + host + ":" + port; - public Adresse getNearestViaTable(Adresse a1, Collection haystack) { + 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; @@ -83,8 +93,49 @@ return bedsteAdresse; } + + public Adresse getNearestTableJni(Adresse a1, Collection 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) { + public Adresse getNearestViarouteHttp(Adresse a1, Collection haystack) { int bedsteAfstand = Integer.MAX_VALUE; Adresse bedsteAdresse = null; @@ -126,11 +177,19 @@ 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; + } } @@ -141,115 +200,46 @@ return bedsteAdresse; } + + public Adresse getNearestViarouteJni(Adresse a1, Collection haystack) { - public Adresse getNearestPipeline(Adresse a1, Collection 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; + + + for(Adresse a2: haystack) { - Iterator it = haystack.iterator(); - while (it.hasNext()) { - Adresse a2 = it.next(); + ViarouteResult res = binding.viaRoute(a1.latitude, a1.longitude, a2.latitude, a2.longitude); - try { - OSRMResponse res = getRoute(a1, a2); + + if (res.status != 0) { + System.out.println("OSRM Returned " + res.status ); + continue; + } - if (res.status != 0) { - System.out.println("OSRM Returned " + res.status + "/" + res.status_message ); + if (res.totalDistance == 0) { continue; } - if (res.route_summary.total_distance < bedsteAfstand) { - bedsteAfstand = res.route_summary.total_distance; + if (res.totalDistance < bedsteAfstand) { + bedsteAfstand = res.totalDistance; bedsteAdresse = a2; } - } catch (IOException e) { - System.out.println( e.getMessage() ); - System.exit(1); - } - } + if (bedsteAfstand < 200) { //vejdistance er tæt nok på + return bedsteAdresse; + } + + } 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); - } -}