/[projects]/dao/DaoAdresseVedligehold/src/main/java/dk/daoas/adressevedligehold/afstandandenrute/OSRMHelper.java
ViewVC logotype

Annotation of /dao/DaoAdresseVedligehold/src/main/java/dk/daoas/adressevedligehold/afstandandenrute/OSRMHelper.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2887 - (hide annotations) (download)
Sat Jan 30 19:27:16 2016 UTC (8 years, 4 months ago) by torben
File size: 2513 byte(s)
replace system.exit with exceptions
1 torben 2878 package dk.daoas.adressevedligehold.afstandandenrute;
2    
3     import java.util.Collection;
4     import java.util.logging.Level;
5     import java.util.logging.Logger;
6    
7     import com.google.gson.Gson;
8    
9     import dk.daoas.adressevedligehold.beans.Address;
10     import dk.daoas.adressevedligehold.util.HttpUtil;
11    
12    
13     public class OSRMHelper {
14    
15    
16     final static Logger logger = Logger.getLogger( OSRMHelper.class.toString() );
17    
18     Gson gson = new Gson();
19    
20     //static OSRMBinding binding = null;
21    
22     final String host = "127.0.0.1";
23     //final String host = "10.30.2.103";
24     final int port = 5000;
25     final String base_url = "http://" + host + ":" + port;
26    
27    
28 torben 2885 public Address getNearestTableHttp(Address a1, Collection<Address> haystack) throws Exception {
29 torben 2878
30    
31    
32     Address bedsteAddress = null;
33    
34     Address hayArray[] = new Address[ haystack.size() ];
35     haystack.toArray(hayArray);
36    
37     StringBuilder sb = new StringBuilder();
38     sb.append(base_url);
39     sb.append("/table?src=").append(a1.latitude).append(",").append(a1.longitude);
40    
41     for(int i = 0; i<hayArray.length; i++) {
42     Address a = hayArray[i];
43     sb.append("&dst=").append( a.latitude ).append(",").append(a.longitude);
44     }
45    
46     try {
47    
48     String txtResponse = HttpUtil.getContentString(sb.toString(), 500, "UTF-8");
49    
50     OSRMDistanceTable table = gson.fromJson(txtResponse, OSRMDistanceTable.class);
51    
52     if (table.status != 200) {
53     logger.info("OSRM failed with message: " + table.status + " / "+ table.status_message);
54     return null;
55     }
56    
57     if ( table.distance_table[0].length != hayArray.length ) {
58     logger.log(Level.SEVERE, "Wrong number of results in matrix " + table.distance_table[0].length);
59    
60     System.out.println("--------------");
61     System.out.println("URL:");
62     System.out.println(sb.toString());
63     System.out.println("--------------");
64     System.out.println("Response:");
65     System.out.println(txtResponse);
66    
67    
68 torben 2887 throw new Exception("Wrong number of results in matrix");
69 torben 2878 }
70    
71     int bedsteTid = Integer.MAX_VALUE;
72    
73     for (int i = 0; i<table.distance_table[0].length; i++) {
74     if (table.distance_table[0][i] < bedsteTid) {
75     bedsteTid = table.distance_table[0][i];
76     bedsteAddress = hayArray[i];
77     }
78     }
79    
80    
81     } catch (Exception e) {
82     logger.log(Level.SEVERE, "Lookup failed", e);
83     System.out.println( sb.toString() );
84     System.out.println( a1 );
85 torben 2885 throw e; //Re-throw
86 torben 2878 }
87    
88     //return gson.fromJson(txtResponse, OSRMResponse.class);
89    
90    
91    
92     return bedsteAddress;
93     }
94    
95     }
96    
97    

  ViewVC Help
Powered by ViewVC 1.1.20