/[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 2725 by torben, Tue Sep 29 07:48:48 2015 UTC revision 2765 by torben, Tue Nov 3 10:27:01 2015 UTC
# Line 2  package dk.daoas.fulddaekning.osrm; Line 2  package dk.daoas.fulddaekning.osrm;
2    
3  import java.io.IOException;  import java.io.IOException;
4  import java.util.Collection;  import java.util.Collection;
 import java.util.Iterator;  
5  import java.util.logging.Level;  import java.util.logging.Level;
6  import java.util.logging.Logger;  import java.util.logging.Logger;
7    
8    import net.minidev.json.JSONValue;
9    
10    import org.apache.http.HttpEntity;
11    import org.apache.http.HttpResponse;
12    import org.apache.http.client.ClientProtocolException;
13    import org.apache.http.client.ResponseHandler;
14    import org.apache.http.client.methods.HttpGet;
15    import org.apache.http.impl.client.CloseableHttpClient;
16    import org.apache.http.impl.client.HttpClients;
17    import org.apache.http.util.EntityUtils;
18    
19  import com.google.gson.Gson;  import com.google.gson.Gson;
20    
21  import dk.daoas.fulddaekning.Adresse;  import dk.daoas.fulddaekning.Adresse;
22  import dk.daoas.fulddaekning.HttpUtil;  import dk.daoas.fulddaekning.HttpUtil;
23  import dk.daoas.fulddaekning.LookupWorker;  
24    /*
25    import dk.thoerup.osrmbinding.OSRMBinding;
26    import dk.thoerup.osrmbinding.ViarouteResult;
27    */
28    
29  public class OSRMHelper {  public class OSRMHelper {
30                    
31            private static final boolean ENABLE_OSRM = false;
32    
33    
34          final static Logger logger = Logger.getLogger( OSRMHelper.class.toString() );          final static Logger logger = Logger.getLogger( OSRMHelper.class.toString() );
35            
36          Gson gson = new Gson();          Gson gson = new Gson();
           
         final String base_url = "http://10.30.2.61:5000";  
37    
38          public Adresse getNearestViaTable(Adresse a1, Collection<Adresse> haystack) {          //static OSRMBinding binding = null;
39                    
40            final String host = "127.0.0.1";
41            //final String host = "10.30.2.103";
42            final int port = 5000;
43            final String base_url = "http://" + host + ":" + port;
44    /*
45            private static synchronized void initOsrm() {
46                    if (binding == null) {
47                            binding = new OSRMBinding("/home/openstreetmap/denmark-latest.osrm");
48                    }
49            }
50    */
51    
52            public Adresse getNearestTableHttp(Adresse a1, Collection<Adresse> haystack) {
53    
54                  Adresse bedsteAdresse = null;                  Adresse bedsteAdresse = null;
55                    
56                  Adresse hayArray[] = new Adresse[ haystack.size() ];                  Adresse hayArray[] = new Adresse[ haystack.size() ];
57                  haystack.toArray(hayArray);                  haystack.toArray(hayArray);
58                    
59                  StringBuilder sb = new StringBuilder();                  StringBuilder sb = new StringBuilder();
60                  sb.append(base_url);                  sb.append(base_url);
61                  sb.append("/table?loc=").append(a1.latitude).append(",").append(a1.longitude);                  sb.append("/table?loc=").append(a1.latitude).append(",").append(a1.longitude);
62                    
63                  for(int i = 0; i<hayArray.length; i++) {                  for(int i = 0; i<hayArray.length; i++) {
64                          Adresse a = hayArray[i];                          Adresse a = hayArray[i];
65                          sb.append("&loc=").append( a.latitude ).append(",").append(a.longitude);                          sb.append("&loc=").append( a.latitude ).append(",").append(a.longitude);
66                  }                  }
67                    
68                  try {                  try {
69                          String txtResponse = HttpUtil.getContentString(sb.toString(), 500, "UTF-8");                          String txtResponse = HttpUtil.getContentString(sb.toString(), 500, "UTF-8");
70                          OSRMDistanceTable table = gson.fromJson(txtResponse, OSRMDistanceTable.class);                          //OSRMDistanceTable table = gson.fromJson(txtResponse, OSRMDistanceTable.class);
71                            OSRMDistanceTable table = JSONValue.parse(txtResponse, OSRMDistanceTable.class);
72                          if (table.status != 0) {                          if (table.status != 0) {
73                                  logger.info("OSRM failed with message: " + table.status_message);                                  logger.info("OSRM failed with message: " + table.status_message);
74                                  return null;                                  return null;
75                          }                          }
76                            
77                            if ( table.distance_table.length != (hayArray.length + 1) ) {
78                                    logger.log(Level.SEVERE, "Wrong number of results in matrix " + table.distance_table.length);
79                                    System.exit(0);
80                            }
81    
82                          int bedsteTid = Integer.MAX_VALUE;                          int bedsteTid = Integer.MAX_VALUE;
83                            
84                          for (int i = 1; i<table.distance_table.length; i++) {                          for (int i = 1; i<table.distance_table.length; i++) {
85                                  if (table.distance_table[0][i] < bedsteTid) {                                  if (table.distance_table[0][i] < bedsteTid) {
86                                          bedsteTid = table.distance_table[0][i];                                          bedsteTid = table.distance_table[0][i];
# Line 58  public class OSRMHelper { Line 93  public class OSRMHelper {
93                          logger.log(Level.SEVERE, "Lookup failed", e);                          logger.log(Level.SEVERE, "Lookup failed", e);
94                          System.exit(1);                                          System.exit(1);                
95                  }                  }
96                    
97                  //return gson.fromJson(txtResponse, OSRMResponse.class);                  //return gson.fromJson(txtResponse, OSRMResponse.class);
98                    
99                    
100                    return bedsteAdresse;
101            }
102            
103            
104            
105    /*
106            public Adresse getNearestTableJni(Adresse a1, Collection<Adresse> haystack) {
107                    if (binding == null) {
108                            initOsrm();
109                    }
110    
111                    Adresse bedsteAdresse = null;
112    
113                    Adresse hayArray[] = new Adresse[ haystack.size() ];
114                    haystack.toArray(hayArray);
115    
116                    dk.thoerup.osrmbinding.Geopoint points[] = new dk.thoerup.osrmbinding.Geopoint[ hayArray.length + 1 ];
117                    points[0] = new dk.thoerup.osrmbinding.Geopoint( a1.latitude, a1.longitude);
118    
119    
120                    for(int i = 0; i<hayArray.length; i++) {
121                            Adresse a = hayArray[i];
122                            int idx = i+1;
123                            points[idx] = new dk.thoerup.osrmbinding.Geopoint(a.latitude, a.longitude);
124                    }
125    
126                    try {
127                            float distance_table[][] = binding.table( points );
128                            
129                            if ( distance_table.length != (hayArray.length + 1) ) {
130                                    logger.log(Level.SEVERE, "Wrong number of results in matrix " + distance_table.length);
131                                    System.exit(0);
132                            }
133    
134                            int bedsteTid = Integer.MAX_VALUE;
135    
136                            for (int i = 1; i<distance_table.length; i++) {
137                                    if (distance_table[0][i] < bedsteTid) {
138                                            bedsteTid = (int) distance_table[0][i];
139                                            int idx = i-1;
140                                            bedsteAdresse = hayArray[idx];
141                                    }
142                            }
143    
144                    } catch (Exception e) {
145                            logger.log(Level.SEVERE, "Lookup failed", e);
146                            System.exit(1);                
147                    }
148    
149                    return bedsteAdresse;
150            }*/
151            
152    
153            public Adresse getNearestMultitargetHttp(Adresse a1, Collection<Adresse> haystack) {
154    
155                    Adresse bedsteAdresse = null;
156    
157                    Adresse hayArray[] = new Adresse[ haystack.size() ];
158                    haystack.toArray(hayArray);
159    
160                    StringBuilder sb = new StringBuilder();
161                    sb.append(base_url);
162                    sb.append("/multitarget?loc=").append(a1.latitude).append(",").append(a1.longitude);
163    
164                    for(int i = 0; i<hayArray.length; i++) {
165                            Adresse a = hayArray[i];
166                            sb.append("&loc=").append( a.latitude ).append(",").append(a.longitude);
167                    }
168    
169                    try {
170                            String txtResponse = HttpUtil.getContentString(sb.toString(), 500, "UTF-8");
171                            OSRMMultiTarget table = gson.fromJson(txtResponse, OSRMMultiTarget.class);
172    
173    
174                            double bedsteAfstand = Double.MAX_VALUE;
175    
176                            for (int i = 1; i<table.distances.length; i++) {
177                                    if (table.distances[i].distance < bedsteAfstand) {
178                                            bedsteAfstand = table.distances[i].distance;
179                                            bedsteAdresse = hayArray[i];
180                                    }
181                            }
182    
183                    } catch (Exception e) {
184                            logger.log(Level.SEVERE, "Lookup failed", e);
185                            System.exit(1);                
186                    }
187    
188    
189                  return bedsteAdresse;                  return bedsteAdresse;
190          }          }
191                    
192          /*          public Adresse getNearestViarouteHttp(Adresse a1, Collection<Adresse> haystack) {
          * Denne virker men table opslaget er 10 gange hurtigere  
          *  
          */  
         @Deprecated  
         public Adresse getNearest(Adresse a1, Collection<Adresse> haystack) {  
193                  int bedsteAfstand = Integer.MAX_VALUE;                  int bedsteAfstand = Integer.MAX_VALUE;
194                  Adresse bedsteAdresse = null;                  Adresse bedsteAdresse = null;
195                                    
196                  Iterator<Adresse> it = haystack.iterator();                  try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
197                  while (it.hasNext()) {                          
198                          Adresse a2 = it.next();                          String loc1 = a1.latitude + "," + a1.longitude;
199    
200                                                    
201                          try {                          for(Adresse a2: haystack) {
202                                  OSRMResponse res = getRoute(a1, a2);                                  String loc2 = a2.latitude + "," + a2.longitude;
203    
204                                    String uri = base_url + "/viaroute?loc=" + loc1 + "&loc=" + loc2 + "&geometry=false&instructions=false&alt=false";
205                                                                    
206                                    HttpGet httpget = new HttpGet(uri);
207                                                                    
208                                    ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
209    
210                            @Override
211                            public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {
212                                int status = response.getStatusLine().getStatusCode();
213                                
214                                if (status >= 200 && status < 300) {
215                                    HttpEntity entity = response.getEntity();
216                                    return entity != null ? EntityUtils.toString(entity) : null;
217                                } else {
218                                    throw new ClientProtocolException("Unexpected response status: " + status);
219                                }
220                            }
221    
222                        };
223                        String responseBody = httpclient.execute(httpget, responseHandler);
224                        
225                                    OSRMResponse res = gson.fromJson(responseBody, OSRMResponse.class);
226    
227                                    
228                        
229                        if (res.status != 0) {
230                                            System.out.println("OSRM Returned " + res.status + "/" + res.status_message );
231                                            continue;
232                                    }
233    
234                                    if (res.route_summary.total_distance == 0) {
235                                            continue;
236                                    }
237    
238    
239                                  if (res.route_summary.total_distance < bedsteAfstand) {                                  if (res.route_summary.total_distance < bedsteAfstand) {
240                                          bedsteAfstand = res.route_summary.total_distance;                                          bedsteAfstand = res.route_summary.total_distance;
241                                          bedsteAdresse = a2;                                          bedsteAdresse = a2;
242                                  }                                  }
243                          } catch (IOException e) {  
244                                  System.out.println( e.getMessage() );                                  if (bedsteAfstand < 200) { //vejdistance er tæt nok på
245                                  System.exit(1);                                          return bedsteAdresse;
246                                    }
247                        
248                          }                          }
249                                    
250                    } catch (Exception e) {
251                            logger.log(Level.SEVERE, "Lookup failed", e);
252                  }                  }
253                    
254                  return bedsteAdresse;                  return bedsteAdresse;
255          }          }
256            
257          public OSRMResponse getRoute(Adresse a1, Adresse a2) throws IOException {  /*      
258                            public Adresse getNearestViarouteJni(Adresse a1, Collection<Adresse> haystack) {
259                  String loc1 = a1.latitude + "," + a1.longitude;  
260                  String loc2 = a2.latitude + "," + a2.longitude;                  if (binding == null) {
261                                            initOsrm();
262                  String params = "loc=" + loc1 + "&loc=" + loc2 + "&geometry=false&instructions=false&alt=false";                  }
263                    
264                  String url = base_url + "/viaroute?" + params;  
265                                    int bedsteAfstand = Integer.MAX_VALUE;
266                  String txtResponse = HttpUtil.getContentString(url, 500, "UTF-8");                  Adresse bedsteAdresse = null;
267                                    
268                  return gson.fromJson(txtResponse, OSRMResponse.class);                          
269          }                          for(Adresse a2: haystack) {
270    
271                        ViarouteResult res = binding.viaRoute(a1.latitude, a1.longitude, a2.latitude, a2.longitude);
272    
273                        
274                        if (res.status != 0) {
275                                            System.out.println("OSRM Returned " + res.status  );
276                                            continue;
277                                    }
278    
279                                    if (res.totalDistance == 0) {
280                                            continue;
281                                    }
282    
283    
284                                    if (res.totalDistance < bedsteAfstand) {
285                                            bedsteAfstand = res.totalDistance;
286                                            bedsteAdresse = a2;
287                                    }
288    
289                                    if (bedsteAfstand < 200) { //vejdistance er tæt nok på
290                                            return bedsteAdresse;
291                                    }
292                        
293                            }                      
294    
295                    return bedsteAdresse;
296            }*/
297  }  }
298    
299    

Legend:
Removed from v.2725  
changed lines
  Added in v.2765

  ViewVC Help
Powered by ViewVC 1.1.20