Search in sources :

Example 1 with IBRouterService

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);
}
Also used : WptPt(net.osmand.plus.GPXUtilities.WptPt) OsmandApplication(net.osmand.plus.OsmandApplication) Bundle(android.os.Bundle) ArrayList(java.util.ArrayList) TrkSegment(net.osmand.plus.GPXUtilities.TrkSegment) TargetPoint(net.osmand.plus.TargetPointsHelper.TargetPoint) LocationPoint(net.osmand.data.LocationPoint) JSONException(org.json.JSONException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) LatLon(net.osmand.data.LatLon) ByteArrayInputStream(java.io.ByteArrayInputStream) GPXFile(net.osmand.plus.GPXUtilities.GPXFile) IBRouterService(btools.routingapp.IBRouterService) Track(net.osmand.plus.GPXUtilities.Track) Location(net.osmand.Location)

Aggregations

Bundle (android.os.Bundle)1 IBRouterService (btools.routingapp.IBRouterService)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 ArrayList (java.util.ArrayList)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Location (net.osmand.Location)1 LatLon (net.osmand.data.LatLon)1 LocationPoint (net.osmand.data.LocationPoint)1 GPXFile (net.osmand.plus.GPXUtilities.GPXFile)1 Track (net.osmand.plus.GPXUtilities.Track)1 TrkSegment (net.osmand.plus.GPXUtilities.TrkSegment)1 WptPt (net.osmand.plus.GPXUtilities.WptPt)1 OsmandApplication (net.osmand.plus.OsmandApplication)1 TargetPoint (net.osmand.plus.TargetPointsHelper.TargetPoint)1 JSONException (org.json.JSONException)1 SAXException (org.xml.sax.SAXException)1