1 |
package dk.daoas.fulddaekning.osrm; |
package dk.daoas.fulddaekning.osrm; |
2 |
|
|
3 |
import java.io.IOException; |
import java.io.IOException; |
4 |
|
import java.util.ArrayList; |
5 |
import java.util.Collection; |
import java.util.Collection; |
6 |
import java.util.Iterator; |
import java.util.Iterator; |
|
import java.util.logging.Level; |
|
|
import java.util.logging.Logger; |
|
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
7 |
import java.util.List; |
import java.util.List; |
8 |
import java.util.concurrent.Future; |
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; |
import org.apache.http.HttpHost; |
14 |
import org.apache.http.HttpRequest; |
import org.apache.http.HttpRequest; |
15 |
import org.apache.http.HttpResponse; |
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; |
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; |
import org.apache.http.impl.nio.client.CloseableHttpPipeliningClient; |
22 |
import org.apache.http.impl.nio.client.HttpAsyncClients; |
import org.apache.http.impl.nio.client.HttpAsyncClients; |
23 |
import org.apache.http.util.EntityUtils; |
import org.apache.http.util.EntityUtils; |
83 |
|
|
84 |
return bedsteAdresse; |
return bedsteAdresse; |
85 |
} |
} |
86 |
|
|
87 |
|
public Adresse getNearestApacheClient(Adresse a1, Collection<Adresse> haystack) { |
88 |
|
int bedsteAfstand = Integer.MAX_VALUE; |
89 |
|
Adresse bedsteAdresse = null; |
90 |
|
|
91 |
|
try (CloseableHttpClient httpclient = HttpClients.createDefault()) { |
92 |
|
|
93 |
|
String loc1 = a1.latitude + "," + a1.longitude; |
94 |
|
|
95 |
|
|
96 |
|
for(Adresse a2: haystack) { |
97 |
|
String loc2 = a2.latitude + "," + a2.longitude; |
98 |
|
|
99 |
|
String uri = base_url + "/viaroute?loc=" + loc1 + "&loc=" + loc2 + "&geometry=false&instructions=false&alt=false"; |
100 |
|
|
101 |
|
HttpGet httpget = new HttpGet(uri); |
102 |
|
|
103 |
|
ResponseHandler<String> responseHandler = new ResponseHandler<String>() { |
104 |
|
|
105 |
|
@Override |
106 |
|
public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException { |
107 |
|
int status = response.getStatusLine().getStatusCode(); |
108 |
|
|
109 |
|
if (status >= 200 && status < 300) { |
110 |
|
HttpEntity entity = response.getEntity(); |
111 |
|
return entity != null ? EntityUtils.toString(entity) : null; |
112 |
|
} else { |
113 |
|
throw new ClientProtocolException("Unexpected response status: " + status); |
114 |
|
} |
115 |
|
} |
116 |
|
|
117 |
|
}; |
118 |
|
String responseBody = httpclient.execute(httpget, responseHandler); |
119 |
|
|
120 |
|
OSRMResponse res = gson.fromJson(responseBody, OSRMResponse.class); |
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
if (res.status != 0) { |
125 |
|
System.out.println("OSRM Returned " + res.status + "/" + res.status_message ); |
126 |
|
continue; |
127 |
|
} |
128 |
|
|
129 |
|
|
130 |
|
if (res.route_summary.total_distance < bedsteAfstand) { |
131 |
|
bedsteAfstand = res.route_summary.total_distance; |
132 |
|
bedsteAdresse = a2; |
133 |
|
} |
134 |
|
|
135 |
|
} |
136 |
|
|
137 |
|
} catch (Exception e) { |
138 |
|
logger.log(Level.SEVERE, "Lookup failed", e); |
139 |
|
} |
140 |
|
|
141 |
|
return bedsteAdresse; |
142 |
|
} |
143 |
|
|
144 |
|
|
145 |
public Adresse getNearestPipeline(Adresse a1, Collection<Adresse> haystack) { |
public Adresse getNearestPipeline(Adresse a1, Collection<Adresse> haystack) { |
146 |
|
|
193 |
|
|
194 |
} |
} |
195 |
|
|
|
|
|
|
System.out.println(responses); |
|
|
|
|
196 |
} catch(Exception e) { |
} catch(Exception e) { |
197 |
logger.log(Level.SEVERE, "Lookup failed", e); |
logger.log(Level.SEVERE, "Lookup failed", e); |
198 |
|
|