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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2741 - (show annotations) (download)
Tue Oct 6 21:26:55 2015 UTC (8 years, 7 months ago) by torben
File size: 6847 byte(s)
streamline osrm variants - and enhance test parameter
1 package dk.daoas.fulddaekning.osrm;
2
3 import java.io.IOException;
4 import java.util.ArrayList;
5 import java.util.Collection;
6 import java.util.Iterator;
7 import java.util.List;
8 import java.util.concurrent.Future;
9 import java.util.logging.Level;
10 import java.util.logging.Logger;
11
12 import org.apache.http.HttpEntity;
13 import org.apache.http.HttpHost;
14 import org.apache.http.HttpRequest;
15 import org.apache.http.HttpResponse;
16 import org.apache.http.client.ClientProtocolException;
17 import org.apache.http.client.ResponseHandler;
18 import org.apache.http.client.methods.HttpGet;
19 import org.apache.http.impl.client.CloseableHttpClient;
20 import org.apache.http.impl.client.HttpClients;
21 import org.apache.http.impl.nio.client.CloseableHttpPipeliningClient;
22 import org.apache.http.impl.nio.client.HttpAsyncClients;
23 import org.apache.http.util.EntityUtils;
24
25 import com.google.gson.Gson;
26
27 import dk.daoas.fulddaekning.Adresse;
28 import dk.daoas.fulddaekning.HttpUtil;
29
30
31 import dk.thoerup.osrmbinding.OSRMBinding;
32 import dk.thoerup.osrmbinding.ViarouteResult;
33
34 public class OSRMHelper {
35
36 final static Logger logger = Logger.getLogger( OSRMHelper.class.toString() );
37
38 Gson gson = new Gson();
39
40 static OSRMBinding binding = null;
41
42 final String host = "127.0.0.1";
43 final int port = 5000;
44 final String base_url = "http://" + host + ":" + port;
45
46 private static synchronized void initOsrm() {
47 if (binding == null) {
48 binding = new OSRMBinding("/home/openstreetmap/denmark-latest.osrm");
49 }
50 }
51
52 public Adresse getNearestTableHttp(Adresse a1, Collection<Adresse> haystack) {
53
54 Adresse bedsteAdresse = null;
55
56 Adresse hayArray[] = new Adresse[ haystack.size() ];
57 haystack.toArray(hayArray);
58
59 StringBuilder sb = new StringBuilder();
60 sb.append(base_url);
61 sb.append("/table?loc=").append(a1.latitude).append(",").append(a1.longitude);
62
63 for(int i = 0; i<hayArray.length; i++) {
64 Adresse a = hayArray[i];
65 sb.append("&loc=").append( a.latitude ).append(",").append(a.longitude);
66 }
67
68 try {
69 String txtResponse = HttpUtil.getContentString(sb.toString(), 500, "UTF-8");
70 OSRMDistanceTable table = gson.fromJson(txtResponse, OSRMDistanceTable.class);
71 if (table.status != 0) {
72 logger.info("OSRM failed with message: " + table.status_message);
73 return null;
74 }
75
76 int bedsteTid = Integer.MAX_VALUE;
77
78 for (int i = 1; i<table.distance_table.length; i++) {
79 if (table.distance_table[0][i] < bedsteTid) {
80 bedsteTid = table.distance_table[0][i];
81 int idx = i-1;
82 bedsteAdresse = hayArray[idx];
83 }
84 }
85
86 } catch (Exception e) {
87 logger.log(Level.SEVERE, "Lookup failed", e);
88 System.exit(1);
89 }
90
91 //return gson.fromJson(txtResponse, OSRMResponse.class);
92
93
94 return bedsteAdresse;
95 }
96
97 public Adresse getNearestTableJni(Adresse a1, Collection<Adresse> haystack) {
98 if (binding == null) {
99 initOsrm();
100 }
101
102 Adresse bedsteAdresse = null;
103
104 Adresse hayArray[] = new Adresse[ haystack.size() ];
105 haystack.toArray(hayArray);
106
107 dk.thoerup.osrmbinding.Geopoint points[] = new dk.thoerup.osrmbinding.Geopoint[ hayArray.length + 1 ];
108 points[0] = new dk.thoerup.osrmbinding.Geopoint( a1.latitude, a1.longitude);
109
110
111 for(int i = 0; i<hayArray.length; i++) {
112 Adresse a = hayArray[i];
113 int idx = i+1;
114 points[idx] = new dk.thoerup.osrmbinding.Geopoint(a.latitude, a.longitude);
115 }
116
117 try {
118 float distance_table[][] = binding.table( points );
119
120 int bedsteTid = Integer.MAX_VALUE;
121
122 for (int i = 1; i<distance_table.length; i++) {
123 if (distance_table[0][i] < bedsteTid) {
124 bedsteTid = (int) distance_table[0][i];
125 int idx = i-1;
126 bedsteAdresse = hayArray[idx];
127 }
128 }
129
130 } catch (Exception e) {
131 logger.log(Level.SEVERE, "Lookup failed", e);
132 System.exit(1);
133 }
134
135 return bedsteAdresse;
136 }
137
138 public Adresse getNearestViarouteHttp(Adresse a1, Collection<Adresse> haystack) {
139 int bedsteAfstand = Integer.MAX_VALUE;
140 Adresse bedsteAdresse = null;
141
142 try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
143
144 String loc1 = a1.latitude + "," + a1.longitude;
145
146
147 for(Adresse a2: haystack) {
148 String loc2 = a2.latitude + "," + a2.longitude;
149
150 String uri = base_url + "/viaroute?loc=" + loc1 + "&loc=" + loc2 + "&geometry=false&instructions=false&alt=false";
151
152 HttpGet httpget = new HttpGet(uri);
153
154 ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
155
156 @Override
157 public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {
158 int status = response.getStatusLine().getStatusCode();
159
160 if (status >= 200 && status < 300) {
161 HttpEntity entity = response.getEntity();
162 return entity != null ? EntityUtils.toString(entity) : null;
163 } else {
164 throw new ClientProtocolException("Unexpected response status: " + status);
165 }
166 }
167
168 };
169 String responseBody = httpclient.execute(httpget, responseHandler);
170
171 OSRMResponse res = gson.fromJson(responseBody, OSRMResponse.class);
172
173
174
175 if (res.status != 0) {
176 System.out.println("OSRM Returned " + res.status + "/" + res.status_message );
177 continue;
178 }
179
180 if (res.route_summary.total_distance == 0) {
181 continue;
182 }
183
184
185 if (res.route_summary.total_distance < bedsteAfstand) {
186 bedsteAfstand = res.route_summary.total_distance;
187 bedsteAdresse = a2;
188 }
189
190 if (bedsteAfstand < 200) { //vejdistance er tæt nok på
191 return bedsteAdresse;
192 }
193
194 }
195
196 } catch (Exception e) {
197 logger.log(Level.SEVERE, "Lookup failed", e);
198 }
199
200 return bedsteAdresse;
201 }
202
203
204 public Adresse getNearestViarouteJni(Adresse a1, Collection<Adresse> haystack) {
205
206 if (binding == null) {
207 initOsrm();
208 }
209
210
211 int bedsteAfstand = Integer.MAX_VALUE;
212 Adresse bedsteAdresse = null;
213
214
215 for(Adresse a2: haystack) {
216
217 ViarouteResult res = binding.viaRoute(a1.latitude, a1.longitude, a2.latitude, a2.longitude);
218
219
220 if (res.status != 0) {
221 System.out.println("OSRM Returned " + res.status );
222 continue;
223 }
224
225 if (res.totalDistance == 0) {
226 continue;
227 }
228
229
230 if (res.totalDistance < bedsteAfstand) {
231 bedsteAfstand = res.totalDistance;
232 bedsteAdresse = a2;
233 }
234
235 if (bedsteAfstand < 200) { //vejdistance er tæt nok på
236 return bedsteAdresse;
237 }
238
239 }
240
241 return bedsteAdresse;
242 }
243 }
244
245

  ViewVC Help
Powered by ViewVC 1.1.20