/[projects]/dao/FuldDaekningWorker/src/main/java/dk/daoas/fulddaekning/osrm/OSRMHelper.java
ViewVC logotype

Diff of /dao/FuldDaekningWorker/src/main/java/dk/daoas/fulddaekning/osrm/OSRMHelper.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2740 by torben, Tue Sep 29 18:01:33 2015 UTC revision 2741 by torben, Tue Oct 6 21:26:55 2015 UTC
# Line 28  import dk.daoas.fulddaekning.Adresse; Line 28  import dk.daoas.fulddaekning.Adresse;
28  import dk.daoas.fulddaekning.HttpUtil;  import dk.daoas.fulddaekning.HttpUtil;
29    
30    
31    import dk.thoerup.osrmbinding.OSRMBinding;
32    import dk.thoerup.osrmbinding.ViarouteResult;
33    
34  public class OSRMHelper {  public class OSRMHelper {
35    
36          final static Logger logger = Logger.getLogger( OSRMHelper.class.toString() );          final static Logger logger = Logger.getLogger( OSRMHelper.class.toString() );
37    
38          Gson gson = new Gson();          Gson gson = new Gson();
39    
40            static OSRMBinding binding = null;
41    
42          final String host = "10.30.2.61";          final String host = "127.0.0.1";
43          final int port = 5000;          final int port = 5000;
44          final String base_url = "http://" + host + ":" + port;          final String base_url = "http://" + host + ":" + port;
45    
46          public Adresse getNearestViaTable(Adresse a1, Collection<Adresse> haystack) {          private static synchronized void initOsrm() {
47                    if (binding == null) {
48                            binding = new OSRMBinding("/home/openstreetmap/denmark-latest.osrm");
49                    }
50            }
51    
52            public Adresse getNearestTableHttp(Adresse a1, Collection<Adresse> haystack) {
53    
54                  Adresse bedsteAdresse = null;                  Adresse bedsteAdresse = null;
55    
# Line 83  public class OSRMHelper { Line 93  public class OSRMHelper {
93    
94                  return bedsteAdresse;                  return bedsteAdresse;
95          }          }
96    
97            public Adresse getNearestTableJni(Adresse a1, Collection<Adresse> haystack) {
98                    if (binding == null) {
99                            initOsrm();
100                    }
101    
102                    Adresse bedsteAdresse = null;
103    
104                    Adresse hayArray[] = new Adresse[ haystack.size() ];
105                    haystack.toArray(hayArray);
106    
107                    dk.thoerup.osrmbinding.Geopoint points[] = new dk.thoerup.osrmbinding.Geopoint[ hayArray.length + 1 ];
108                    points[0] = new dk.thoerup.osrmbinding.Geopoint( a1.latitude, a1.longitude);
109    
110    
111                    for(int i = 0; i<hayArray.length; i++) {
112                            Adresse a = hayArray[i];
113                            int idx = i+1;
114                            points[idx] = new dk.thoerup.osrmbinding.Geopoint(a.latitude, a.longitude);
115                    }
116    
117                    try {
118                            float distance_table[][] = binding.table( points );
119    
120                            int bedsteTid = Integer.MAX_VALUE;
121    
122                            for (int i = 1; i<distance_table.length; i++) {
123                                    if (distance_table[0][i] < bedsteTid) {
124                                            bedsteTid = (int) distance_table[0][i];
125                                            int idx = i-1;
126                                            bedsteAdresse = hayArray[idx];
127                                    }
128                            }
129    
130                    } catch (Exception e) {
131                            logger.log(Level.SEVERE, "Lookup failed", e);
132                            System.exit(1);                
133                    }
134    
135                    return bedsteAdresse;
136            }
137                    
138          public Adresse getNearestApacheClient(Adresse a1, Collection<Adresse> haystack) {          public Adresse getNearestViarouteHttp(Adresse a1, Collection<Adresse> haystack) {
139                  int bedsteAfstand = Integer.MAX_VALUE;                  int bedsteAfstand = Integer.MAX_VALUE;
140                  Adresse bedsteAdresse = null;                  Adresse bedsteAdresse = null;
141                                    
# Line 126  public class OSRMHelper { Line 177  public class OSRMHelper {
177                                          continue;                                          continue;
178                                  }                                  }
179    
180                                    if (res.route_summary.total_distance == 0) {
181                                            continue;
182                                    }
183    
184    
185                                  if (res.route_summary.total_distance < bedsteAfstand) {                                  if (res.route_summary.total_distance < bedsteAfstand) {
186                                          bedsteAfstand = res.route_summary.total_distance;                                          bedsteAfstand = res.route_summary.total_distance;
187                                          bedsteAdresse = a2;                                          bedsteAdresse = a2;
188                                  }                                  }
189    
190                                    if (bedsteAfstand < 200) { //vejdistance er tæt nok på
191                                            return bedsteAdresse;
192                                    }
193                                            
194                          }                          }
195                                                    
# Line 141  public class OSRMHelper { Line 200  public class OSRMHelper {
200                  return bedsteAdresse;                  return bedsteAdresse;
201          }          }
202    
203            
204            public Adresse getNearestViarouteJni(Adresse a1, Collection<Adresse> haystack) {
205    
206          public Adresse getNearestPipeline(Adresse a1, Collection<Adresse> haystack) {                  if (binding == null) {
207                            initOsrm();
208                  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<HttpRequest> requests = new ArrayList<HttpRequest>();  
   
                         String loc1 = a1.latitude + "," + a1.longitude;  
   
                         //for (int i=0; i<hayArray.length; i++) {  
                         for (int i=0; i<2; i++) {  
                                 Adresse a2 = hayArray[i];  
                                 String loc2 = a2.latitude + "," + a2.longitude;  
   
                                 String uri = "/viaroute?loc=" + loc1 + "&loc=" + loc2 + "&geometry=false&instructions=false&alt=false";  
                                 System.out.println( uri );  
   
                                 requests.add( new HttpGet(uri) );  
   
                         }  
   
   
                         Future<List<HttpResponse>> future = httpclient.execute(targetHost, requests, null);  
                         List<HttpResponse> responses = future.get();  
   
                         HttpResponse respArr[] = new HttpResponse[ responses.size() ];  
                         responses.toArray( respArr );  
   
                         for (int i=0; i<respArr.length; i++) {  
                                 String jsonBody = EntityUtils.toString( respArr[i].getEntity() );  
                                 OSRMResponse res = gson.fromJson(jsonBody, OSRMResponse.class);  
   
                                 if (res.route_summary.total_distance < bedsteAfstand) {  
                                         bedsteAfstand = res.route_summary.total_distance;  
                                         bedsteAdresse = hayArray[i];  
                                 }  
   
                         }  
   
                 } catch(Exception e) {  
                         logger.log(Level.SEVERE, "Lookup failed", e);  
   
                 }  
   
   
                 return bedsteAdresse;  
         }  
209    
210    
         /*  
          * Denne virker men table opslaget er 10 gange hurtigere  
          *  
          */  
         @Deprecated  
         public Adresse getNearest(Adresse a1, Collection<Adresse> haystack) {  
211                  int bedsteAfstand = Integer.MAX_VALUE;                  int bedsteAfstand = Integer.MAX_VALUE;
212                  Adresse bedsteAdresse = null;                  Adresse bedsteAdresse = null;
213                    
214                            
215                            for(Adresse a2: haystack) {
216    
217                  Iterator<Adresse> it = haystack.iterator();                      ViarouteResult res = binding.viaRoute(a1.latitude, a1.longitude, a2.latitude, a2.longitude);
                 while (it.hasNext()) {  
                         Adresse a2 = it.next();  
218    
219                          try {                      
220                                  OSRMResponse res = getRoute(a1, a2);                      if (res.status != 0) {
221                                            System.out.println("OSRM Returned " + res.status  );
222                                            continue;
223                                    }
224    
225                                  if (res.status != 0) {                                  if (res.totalDistance == 0) {
                                         System.out.println("OSRM Returned " + res.status + "/" + res.status_message );  
226                                          continue;                                          continue;
227                                  }                                  }
228    
229    
230                                  if (res.route_summary.total_distance < bedsteAfstand) {                                  if (res.totalDistance < bedsteAfstand) {
231                                          bedsteAfstand = res.route_summary.total_distance;                                          bedsteAfstand = res.totalDistance;
232                                          bedsteAdresse = a2;                                          bedsteAdresse = a2;
233                                  }                                  }
                         } catch (IOException e) {  
                                 System.out.println( e.getMessage() );  
                                 System.exit(1);  
                         }  
234    
235                  }                                  if (bedsteAfstand < 200) { //vejdistance er tæt nok på
236                                            return bedsteAdresse;
237                                    }
238                        
239                            }                      
240    
241                  return bedsteAdresse;                  return bedsteAdresse;
242          }          }
243    }
244    
         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");  
245    
                 return gson.fromJson(txtResponse, OSRMResponse.class);  
         }  
 }  

Legend:
Removed from v.2740  
changed lines
  Added in v.2741

  ViewVC Help
Powered by ViewVC 1.1.20