use of btools.routingapp.IBRouterService in project Osmand by osmandapp.
the class RouteProvider method findBROUTERRoute.
protected RouteCalculationResult findBROUTERRoute(RouteCalculationParams params) throws MalformedURLException, IOException, ParserConfigurationException, FactoryConfigurationError, SAXException {
int numpoints = 2 + (params.intermediates != null ? params.intermediates.size() : 0);
double[] lats = new double[numpoints];
double[] lons = new double[numpoints];
int index = 0;
String mode;
lats[index] = params.start.getLatitude();
lons[index] = params.start.getLongitude();
index++;
if (params.intermediates != null && params.intermediates.size() > 0) {
for (LatLon il : params.intermediates) {
lats[index] = il.getLatitude();
lons[index] = il.getLongitude();
index++;
}
}
lats[index] = params.end.getLatitude();
lons[index] = params.end.getLongitude();
if (ApplicationMode.PEDESTRIAN == params.mode) {
// $NON-NLS-1$
mode = "foot";
} else if (ApplicationMode.BICYCLE == params.mode) {
// $NON-NLS-1$
mode = "bicycle";
} else {
// $NON-NLS-1$
mode = "motorcar";
}
Bundle bpars = new Bundle();
bpars.putDoubleArray("lats", lats);
bpars.putDoubleArray("lons", lons);
bpars.putString("fast", params.fast ? "1" : "0");
bpars.putString("v", mode);
bpars.putString("trackFormat", "gpx");
OsmandApplication ctx = (OsmandApplication) params.ctx;
List<Location> res = new ArrayList<Location>();
IBRouterService brouterService = ctx.getBRouterService();
if (brouterService == null) {
return new RouteCalculationResult("BRouter service is not available");
}
try {
String gpxMessage = brouterService.getTrackFromParams(bpars);
if (gpxMessage == null)
gpxMessage = "no result from brouter";
if (!gpxMessage.startsWith("<")) {
return new RouteCalculationResult(gpxMessage);
}
GPXFile gpxFile = GPXUtilities.loadGPXFile(ctx, new ByteArrayInputStream(gpxMessage.getBytes("UTF-8")));
for (Track track : gpxFile.tracks) {
for (TrkSegment ts : track.segments) {
for (WptPt p : ts.points) {
// $NON-NLS-1$
Location l = new Location("router");
l.setLatitude(p.lat);
l.setLongitude(p.lon);
if (p.ele != Double.NaN) {
l.setAltitude(p.ele);
}
res.add(l);
}
}
}
} catch (Exception e) {
// $NON-NLS-1$
return new RouteCalculationResult("Exception calling BRouter: " + e);
}
return new RouteCalculationResult(res, null, params, null, true);
}
Aggregations