/[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

dao/FuldDaekningWorker/src/dk/daoas/fulddaekning/osrm/OSRMHelper.java revision 2708 by torben, Sun Sep 27 18:20:41 2015 UTC dao/FuldDaekningWorker/src/main/java/dk/daoas/fulddaekning/osrm/OSRMHelper.java revision 2733 by torben, Tue Sep 29 09:58:14 2015 UTC
# Line 3  package dk.daoas.fulddaekning.osrm; Line 3  package dk.daoas.fulddaekning.osrm;
3  import java.io.IOException;  import java.io.IOException;
4  import java.util.Collection;  import java.util.Collection;
5  import java.util.Iterator;  import java.util.Iterator;
6    import java.util.logging.Level;
7    import java.util.logging.Logger;
8    
9    
10    import java.util.ArrayList;
11    import java.util.List;
12    import java.util.concurrent.Future;
13    
14    import org.apache.http.HttpHost;
15    import org.apache.http.HttpRequest;
16    import org.apache.http.HttpResponse;
17    import org.apache.http.client.methods.HttpGet;
18    import org.apache.http.impl.nio.client.CloseableHttpPipeliningClient;
19    import org.apache.http.impl.nio.client.HttpAsyncClients;
20    import org.apache.http.util.EntityUtils;
21    
22  import com.google.gson.Gson;  import com.google.gson.Gson;
23    
24  import dk.daoas.fulddaekning.Adresse;  import dk.daoas.fulddaekning.Adresse;
25  import dk.daoas.fulddaekning.HttpUtil;  import dk.daoas.fulddaekning.HttpUtil;
26    
27    
28  public class OSRMHelper {  public class OSRMHelper {
29            
30            final static Logger logger = Logger.getLogger( OSRMHelper.class.toString() );
31    
32          Gson gson = new Gson();          Gson gson = new Gson();
33            
34          final String base_url = "http://10.30.2.61:5000";  
35            final String host = "10.30.2.61";
36            final int port = 5000;
37            final String base_url = "http://" + host + ":" + port;
38    
39          public Adresse getNearestViaTable(Adresse a1, Collection<Adresse> haystack) {          public Adresse getNearestViaTable(Adresse a1, Collection<Adresse> haystack) {
40                    
41                  Adresse bedsteAdresse = null;                  Adresse bedsteAdresse = null;
42                    
43                  Adresse hayArray[] = new Adresse[ haystack.size() ];                  Adresse hayArray[] = new Adresse[ haystack.size() ];
44                  haystack.toArray(hayArray);                  haystack.toArray(hayArray);
45                    
46                  StringBuilder sb = new StringBuilder();                  StringBuilder sb = new StringBuilder();
47                  sb.append(base_url);                  sb.append(base_url);
48                  sb.append("/table?loc=").append(a1.latitude).append(",").append(a1.longitude);                  sb.append("/table?loc=").append(a1.latitude).append(",").append(a1.longitude);
49                    
50                  for(int i = 0; i<hayArray.length; i++) {                  for(int i = 0; i<hayArray.length; i++) {
51                          Adresse a = hayArray[i];                          Adresse a = hayArray[i];
52                          sb.append("&loc=").append( a.latitude ).append(",").append(a.longitude);                          sb.append("&loc=").append( a.latitude ).append(",").append(a.longitude);
53                  }                  }
54                    
55                  try {                  try {
56                          String txtResponse = HttpUtil.getContentString(sb.toString(), 500, "UTF-8");                          String txtResponse = HttpUtil.getContentString(sb.toString(), 500, "UTF-8");
57                          OSRMDistanceTable table = gson.fromJson(txtResponse, OSRMDistanceTable.class);                          OSRMDistanceTable table = gson.fromJson(txtResponse, OSRMDistanceTable.class);
58                            if (table.status != 0) {
59                                    logger.info("OSRM failed with message: " + table.status_message);
60                                    return null;
61                            }
62    
63                          int bedsteTid = Integer.MAX_VALUE;                          int bedsteTid = Integer.MAX_VALUE;
64                            
65                          for (int i = 1; i<table.distance_table.length; i++) {                          for (int i = 1; i<table.distance_table.length; i++) {
66                                  if (table.distance_table[0][i] < bedsteTid) {                                  if (table.distance_table[0][i] < bedsteTid) {
67                                          bedsteTid = table.distance_table[0][i];                                          bedsteTid = table.distance_table[0][i];
# Line 45  public class OSRMHelper { Line 71  public class OSRMHelper {
71                          }                          }
72    
73                  } catch (Exception e) {                  } catch (Exception e) {
74                          System.out.println( e.getMessage() );                          logger.log(Level.SEVERE, "Lookup failed", e);
75                          System.exit(1);                                          System.exit(1);                
76                  }                  }
77                    
78                  //return gson.fromJson(txtResponse, OSRMResponse.class);                  //return gson.fromJson(txtResponse, OSRMResponse.class);
79                    
80                    
81                    return bedsteAdresse;
82            }
83    
84            public Adresse getNearestPipeline(Adresse a1, Collection<Adresse> haystack) {
85    
86                    int bedsteAfstand = Integer.MAX_VALUE;
87                    Adresse bedsteAdresse = null;
88    
89                    Adresse hayArray[] = new Adresse[ haystack.size() ];
90                    haystack.toArray(hayArray);
91    
92    
93                    try (CloseableHttpPipeliningClient httpclient = HttpAsyncClients.createPipelining(); ) {
94                            httpclient.start();
95    
96                            HttpHost targetHost = new HttpHost(host, port);
97    
98                            System.out.println("1>" + targetHost.toString() );
99                            System.out.println("2>" + targetHost.toURI() );
100    
101                            List<HttpRequest> requests = new ArrayList<HttpRequest>();
102    
103                            String loc1 = a1.latitude + "," + a1.longitude;
104    
105                            //for (int i=0; i<hayArray.length; i++) {
106                            for (int i=0; i<2; i++) {
107                                    Adresse a2 = hayArray[i];
108                                    String loc2 = a2.latitude + "," + a2.longitude;
109    
110                                    String uri = "/viaroute?loc=" + loc1 + "&loc=" + loc2 + "&geometry=false&instructions=false&alt=false";
111                                    System.out.println( uri );
112    
113                                    requests.add( new HttpGet(uri) );
114    
115                            }
116    
117    
118                            Future<List<HttpResponse>> future = httpclient.execute(targetHost, requests, null);
119                            List<HttpResponse> responses = future.get();
120    
121                            HttpResponse respArr[] = new HttpResponse[ responses.size() ];
122                            responses.toArray( respArr );
123    
124                            for (int i=0; i<respArr.length; i++) {
125                                    String jsonBody = EntityUtils.toString( respArr[i].getEntity() );
126                                    OSRMResponse res = gson.fromJson(jsonBody, OSRMResponse.class);
127    
128                                    if (res.route_summary.total_distance < bedsteAfstand) {
129                                            bedsteAfstand = res.route_summary.total_distance;
130                                            bedsteAdresse = hayArray[i];
131                                    }
132    
133                            }
134    
135    
136                            System.out.println(responses);
137    
138                    } catch(Exception e) {
139                            logger.log(Level.SEVERE, "Lookup failed", e);
140    
141                    }
142    
143    
144                  return bedsteAdresse;                  return bedsteAdresse;
145          }          }
146            
147    
148          /*          /*
149           * Denne virker men table opslaget er 10 gange hurtigere           * Denne virker men table opslaget er 10 gange hurtigere
150           *           *
151           */           */
152          @Deprecated          @Deprecated
153          public Adresse getNearest(Adresse a1, Collection<Adresse> haystack) {          public Adresse getNearest(Adresse a1, Collection<Adresse> haystack) {
154                  int bedsteAfstand = 9999999;                  int bedsteAfstand = Integer.MAX_VALUE;
155                  Adresse bedsteAdresse = null;                  Adresse bedsteAdresse = null;
156                    
157                  Iterator<Adresse> it = haystack.iterator();                  Iterator<Adresse> it = haystack.iterator();
158                  while (it.hasNext()) {                  while (it.hasNext()) {
159                          Adresse a2 = it.next();                          Adresse a2 = it.next();
160                            
161                          try {                          try {
162                                  OSRMResponse res = getRoute(a1, a2);                                  OSRMResponse res = getRoute(a1, a2);
163                                    
164                                                                    if (res.status != 0) {
165                                            System.out.println("OSRM Returned " + res.status + "/" + res.status_message );
166                                            continue;
167                                    }
168    
169    
170                                  if (res.route_summary.total_distance < bedsteAfstand) {                                  if (res.route_summary.total_distance < bedsteAfstand) {
171                                          bedsteAfstand = res.route_summary.total_distance;                                          bedsteAfstand = res.route_summary.total_distance;
172                                          bedsteAdresse = a2;                                          bedsteAdresse = a2;
# Line 80  public class OSRMHelper { Line 175  public class OSRMHelper {
175                                  System.out.println( e.getMessage() );                                  System.out.println( e.getMessage() );
176                                  System.exit(1);                                  System.exit(1);
177                          }                          }
178            
179                  }                  }
180                    
181                  return bedsteAdresse;                  return bedsteAdresse;
182          }          }
183            
184          public OSRMResponse getRoute(Adresse a1, Adresse a2) throws IOException {          public OSRMResponse getRoute(Adresse a1, Adresse a2) throws IOException {
185                    
186                  String loc1 = a1.latitude + "," + a1.longitude;                  String loc1 = a1.latitude + "," + a1.longitude;
187                  String loc2 = a2.latitude + "," + a2.longitude;                  String loc2 = a2.latitude + "," + a2.longitude;
188                    
189                  String params = "loc=" + loc1 + "&loc=" + loc2 + "&geometry=false&instructions=false&alt=false";                  String params = "loc=" + loc1 + "&loc=" + loc2 + "&geometry=false&instructions=false&alt=false";
190                    
191                  String url = base_url + "/viaroute?" + params;                  String url = base_url + "/viaroute?" + params;
192                    
193                  String txtResponse = HttpUtil.getContentString(url, 500, "UTF-8");                  String txtResponse = HttpUtil.getContentString(url, 500, "UTF-8");
194                    
195                  return gson.fromJson(txtResponse, OSRMResponse.class);                  return gson.fromJson(txtResponse, OSRMResponse.class);
196          }          }
197  }  }

Legend:
Removed from v.2708  
changed lines
  Added in v.2733

  ViewVC Help
Powered by ViewVC 1.1.20