/[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 2741 by torben, Tue Oct 6 21:26:55 2015 UTC revision 2755 by torben, Tue Oct 20 10:36:06 2015 UTC
# Line 1  Line 1 
1  package dk.daoas.fulddaekning.osrm;  package dk.daoas.fulddaekning.osrm;
2    
3  import java.io.IOException;  import java.io.IOException;
 import java.util.ArrayList;  
4  import java.util.Collection;  import java.util.Collection;
 import java.util.Iterator;  
 import java.util.List;  
 import java.util.concurrent.Future;  
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;  import org.apache.http.HttpEntity;
 import org.apache.http.HttpHost;  
 import org.apache.http.HttpRequest;  
11  import org.apache.http.HttpResponse;  import org.apache.http.HttpResponse;
12  import org.apache.http.client.ClientProtocolException;  import org.apache.http.client.ClientProtocolException;
13  import org.apache.http.client.ResponseHandler;  import org.apache.http.client.ResponseHandler;
14  import org.apache.http.client.methods.HttpGet;  import org.apache.http.client.methods.HttpGet;
15  import org.apache.http.impl.client.CloseableHttpClient;  import org.apache.http.impl.client.CloseableHttpClient;
16  import org.apache.http.impl.client.HttpClients;  import org.apache.http.impl.client.HttpClients;
 import org.apache.http.impl.nio.client.CloseableHttpPipeliningClient;  
 import org.apache.http.impl.nio.client.HttpAsyncClients;  
17  import org.apache.http.util.EntityUtils;  import org.apache.http.util.EntityUtils;
18    
19  import com.google.gson.Gson;  import com.google.gson.Gson;
# Line 27  import com.google.gson.Gson; Line 21  import com.google.gson.Gson;
21  import dk.daoas.fulddaekning.Adresse;  import dk.daoas.fulddaekning.Adresse;
22  import dk.daoas.fulddaekning.HttpUtil;  import dk.daoas.fulddaekning.HttpUtil;
23    
24    /*
25  import dk.thoerup.osrmbinding.OSRMBinding;  import dk.thoerup.osrmbinding.OSRMBinding;
26  import dk.thoerup.osrmbinding.ViarouteResult;  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();
37    
38          static OSRMBinding binding = null;          //static OSRMBinding binding = null;
39    
40          final String host = "127.0.0.1";          //final String host = "127.0.0.1";
41            final String host = "10.30.2.103";
42          final int port = 5000;          final int port = 5000;
43          final String base_url = "http://" + host + ":" + port;          final String base_url = "http://" + host + ":" + port;
44    /*
45          private static synchronized void initOsrm() {          private static synchronized void initOsrm() {
46                  if (binding == null) {                  if (binding == null) {
47                          binding = new OSRMBinding("/home/openstreetmap/denmark-latest.osrm");                          binding = new OSRMBinding("/home/openstreetmap/denmark-latest.osrm");
48                  }                  }
49          }          }
50    */
51    
52          public Adresse getNearestTableHttp(Adresse a1, Collection<Adresse> haystack) {          public Adresse getNearestTableHttp(Adresse a1, Collection<Adresse> haystack) {
53    
# Line 67  public class OSRMHelper { Line 67  public class OSRMHelper {
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++) {
# Line 93  public class OSRMHelper { Line 99  public class OSRMHelper {
99    
100                  return bedsteAdresse;                  return bedsteAdresse;
101          }          }
102            
103            
104            
105    /*
106          public Adresse getNearestTableJni(Adresse a1, Collection<Adresse> haystack) {          public Adresse getNearestTableJni(Adresse a1, Collection<Adresse> haystack) {
107                  if (binding == null) {                  if (binding == null) {
108                          initOsrm();                          initOsrm();
# Line 116  public class OSRMHelper { Line 125  public class OSRMHelper {
125    
126                  try {                  try {
127                          float distance_table[][] = binding.table( points );                          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;                          int bedsteTid = Integer.MAX_VALUE;
135    
# Line 133  public class OSRMHelper { Line 147  public class OSRMHelper {
147                  }                  }
148    
149                  return bedsteAdresse;                  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;
190          }          }
191                    
192          public Adresse getNearestViarouteHttp(Adresse a1, Collection<Adresse> haystack) {          public Adresse getNearestViarouteHttp(Adresse a1, Collection<Adresse> haystack) {
# Line 200  public class OSRMHelper { Line 254  public class OSRMHelper {
254                  return bedsteAdresse;                  return bedsteAdresse;
255          }          }
256    
257            /*      
258          public Adresse getNearestViarouteJni(Adresse a1, Collection<Adresse> haystack) {          public Adresse getNearestViarouteJni(Adresse a1, Collection<Adresse> haystack) {
259    
260                  if (binding == null) {                  if (binding == null) {
# Line 239  public class OSRMHelper { Line 293  public class OSRMHelper {
293                          }                                                }                      
294    
295                  return bedsteAdresse;                  return bedsteAdresse;
296          }          }*/
297  }  }
298    
299    

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

  ViewVC Help
Powered by ViewVC 1.1.20