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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2720 - (hide annotations) (download)
Mon Sep 28 14:59:11 2015 UTC (8 years, 8 months ago) by torben
Original Path: dao/FuldDaekningWorker/src/dk/daoas/fulddaekning/osrm/OSRMHelper.java
File size: 2904 byte(s)
Log all exceptions regarding osrm fails
1 torben 2708 package dk.daoas.fulddaekning.osrm;
2    
3     import java.io.IOException;
4     import java.util.Collection;
5     import java.util.Iterator;
6 torben 2720 import java.util.logging.Level;
7     import java.util.logging.Logger;
8 torben 2708
9     import com.google.gson.Gson;
10    
11     import dk.daoas.fulddaekning.Adresse;
12     import dk.daoas.fulddaekning.HttpUtil;
13 torben 2720 import dk.daoas.fulddaekning.LookupWorker;
14 torben 2708
15     public class OSRMHelper {
16    
17 torben 2720 final static Logger logger = Logger.getLogger( OSRMHelper.class.toString() );
18    
19 torben 2708 Gson gson = new Gson();
20    
21     final String base_url = "http://10.30.2.61:5000";
22    
23     public Adresse getNearestViaTable(Adresse a1, Collection<Adresse> haystack) {
24    
25     Adresse bedsteAdresse = null;
26    
27     Adresse hayArray[] = new Adresse[ haystack.size() ];
28     haystack.toArray(hayArray);
29    
30     StringBuilder sb = new StringBuilder();
31     sb.append(base_url);
32     sb.append("/table?loc=").append(a1.latitude).append(",").append(a1.longitude);
33    
34     for(int i = 0; i<hayArray.length; i++) {
35     Adresse a = hayArray[i];
36     sb.append("&loc=").append( a.latitude ).append(",").append(a.longitude);
37     }
38    
39     try {
40     String txtResponse = HttpUtil.getContentString(sb.toString(), 500, "UTF-8");
41     OSRMDistanceTable table = gson.fromJson(txtResponse, OSRMDistanceTable.class);
42     int bedsteTid = Integer.MAX_VALUE;
43    
44     for (int i = 1; i<table.distance_table.length; i++) {
45     if (table.distance_table[0][i] < bedsteTid) {
46     bedsteTid = table.distance_table[0][i];
47     int idx = i-1;
48     bedsteAdresse = hayArray[idx];
49     }
50     }
51    
52     } catch (Exception e) {
53 torben 2720 logger.log(Level.SEVERE, "Lookup failed", e);
54 torben 2708 System.exit(1);
55     }
56    
57     //return gson.fromJson(txtResponse, OSRMResponse.class);
58    
59    
60     return bedsteAdresse;
61     }
62    
63     /*
64     * Denne virker men table opslaget er 10 gange hurtigere
65     *
66     */
67     @Deprecated
68     public Adresse getNearest(Adresse a1, Collection<Adresse> haystack) {
69     int bedsteAfstand = 9999999;
70     Adresse bedsteAdresse = null;
71    
72     Iterator<Adresse> it = haystack.iterator();
73     while (it.hasNext()) {
74     Adresse a2 = it.next();
75    
76     try {
77     OSRMResponse res = getRoute(a1, a2);
78    
79    
80     if (res.route_summary.total_distance < bedsteAfstand) {
81     bedsteAfstand = res.route_summary.total_distance;
82     bedsteAdresse = a2;
83     }
84     } catch (IOException e) {
85     System.out.println( e.getMessage() );
86     System.exit(1);
87     }
88    
89     }
90    
91     return bedsteAdresse;
92     }
93    
94     public OSRMResponse getRoute(Adresse a1, Adresse a2) throws IOException {
95    
96     String loc1 = a1.latitude + "," + a1.longitude;
97     String loc2 = a2.latitude + "," + a2.longitude;
98    
99     String params = "loc=" + loc1 + "&loc=" + loc2 + "&geometry=false&instructions=false&alt=false";
100    
101     String url = base_url + "/viaroute?" + params;
102    
103     String txtResponse = HttpUtil.getContentString(url, 500, "UTF-8");
104    
105     return gson.fromJson(txtResponse, OSRMResponse.class);
106     }
107     }

  ViewVC Help
Powered by ViewVC 1.1.20