Search in sources :

Example 1 with Track

use of net.osmand.plus.GPXUtilities.Track 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)

Example 2 with Track

use of net.osmand.plus.GPXUtilities.Track in project Osmand by osmandapp.

the class SavingTrackHelper method collectDBTracks.

private void collectDBTracks(SQLiteDatabase db, Map<String, GPXFile> dataTracks) {
    Cursor query = db.rawQuery(// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
    "SELECT " + TRACK_COL_LAT + "," + TRACK_COL_LON + "," + TRACK_COL_ALTITUDE + "," + TRACK_COL_SPEED + "," + TRACK_COL_HDOP + "," + TRACK_COL_DATE + " FROM " + TRACK_NAME + " ORDER BY " + TRACK_COL_DATE + " ASC", // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
    null);
    long previousTime = 0;
    long previousInterval = 0;
    TrkSegment segment = null;
    Track track = null;
    if (query.moveToFirst()) {
        do {
            WptPt pt = new WptPt();
            pt.lat = query.getDouble(0);
            pt.lon = query.getDouble(1);
            pt.ele = query.getDouble(2);
            pt.speed = query.getDouble(3);
            pt.hdop = query.getDouble(4);
            long time = query.getLong(5);
            pt.time = time;
            long currentInterval = Math.abs(time - previousTime);
            boolean newInterval = pt.lat == 0 && pt.lon == 0;
            if (track != null && !newInterval && (!ctx.getSettings().AUTO_SPLIT_RECORDING.get() || currentInterval < 6 * 60 * 1000 || currentInterval < 10 * previousInterval)) {
                // 6 minute - same segment
                segment.points.add(pt);
            } else if (track != null && (ctx.getSettings().AUTO_SPLIT_RECORDING.get() && currentInterval < 2 * 60 * 60 * 1000)) {
                // 2 hour - same track
                segment = new TrkSegment();
                if (!newInterval) {
                    segment.points.add(pt);
                }
                track.segments.add(segment);
            } else {
                // check if date the same - new track otherwise new file
                track = new Track();
                segment = new TrkSegment();
                track.segments.add(segment);
                if (!newInterval) {
                    segment.points.add(pt);
                }
                // $NON-NLS-1$
                String date = DateFormat.format("yyyy-MM-dd", time).toString();
                if (dataTracks.containsKey(date)) {
                    GPXFile gpx = dataTracks.get(date);
                    gpx.tracks.add(track);
                } else {
                    GPXFile file = new GPXFile();
                    file.tracks.add(track);
                    dataTracks.put(date, file);
                }
            }
            previousInterval = currentInterval;
            previousTime = time;
        } while (query.moveToNext());
    }
    query.close();
}
Also used : WptPt(net.osmand.plus.GPXUtilities.WptPt) TrkSegment(net.osmand.plus.GPXUtilities.TrkSegment) Cursor(android.database.Cursor) GPXFile(net.osmand.plus.GPXUtilities.GPXFile) Track(net.osmand.plus.GPXUtilities.Track)

Example 3 with Track

use of net.osmand.plus.GPXUtilities.Track in project Osmand by osmandapp.

the class MeasurementToolFragment method saveGpx.

private void saveGpx(final File dir, final String fileName, final boolean showOnMap, final GPXFile gpx, final boolean openTrackActivity, final NewGpxData.ActionType actionType, final SaveType saveType, final boolean close) {
    new AsyncTask<Void, Void, String>() {

        private ProgressDialog progressDialog;

        private File toSave;

        @Override
        protected void onPreExecute() {
            cancelModes();
            MapActivity activity = getMapActivity();
            if (activity != null) {
                progressDialog = new ProgressDialog(activity);
                progressDialog.setMessage(getString(R.string.saving_gpx_tracks));
                progressDialog.show();
            }
        }

        @Override
        protected String doInBackground(Void... voids) {
            MeasurementToolLayer measurementLayer = getMeasurementLayer();
            MapActivity activity = getMapActivity();
            List<WptPt> points = editingCtx.getPoints();
            TrkSegment before = editingCtx.getBeforeTrkSegmentLine();
            TrkSegment after = editingCtx.getAfterTrkSegmentLine();
            if (gpx == null) {
                toSave = new File(dir, fileName);
                GPXFile gpx = new GPXFile();
                if (measurementLayer != null) {
                    if (saveType == SaveType.LINE) {
                        TrkSegment segment = new TrkSegment();
                        if (editingCtx.isInSnapToRoadMode()) {
                            segment.points.addAll(before.points);
                            segment.points.addAll(after.points);
                        } else {
                            segment.points.addAll(points);
                        }
                        Track track = new Track();
                        track.segments.add(segment);
                        gpx.tracks.add(track);
                    } else if (saveType == SaveType.ROUTE_POINT) {
                        if (editingCtx.isInSnapToRoadMode()) {
                            TrkSegment segment = new TrkSegment();
                            segment.points.addAll(before.points);
                            segment.points.addAll(after.points);
                            Track track = new Track();
                            track.segments.add(segment);
                            gpx.tracks.add(track);
                        }
                        Route rt = new Route();
                        gpx.routes.add(rt);
                        rt.points.addAll(points);
                    }
                }
                if (activity != null) {
                    String res = GPXUtilities.writeGpxFile(toSave, gpx, activity.getMyApplication());
                    gpx.path = toSave.getAbsolutePath();
                    if (showOnMap) {
                        activity.getMyApplication().getSelectedGpxHelper().selectGpxFile(gpx, true, false);
                    }
                    return res;
                }
            } else {
                toSave = new File(gpx.path);
                if (measurementLayer != null) {
                    if (actionType != null) {
                        switch(actionType) {
                            case ADD_SEGMENT:
                                if (editingCtx.isInSnapToRoadMode()) {
                                    List<WptPt> snappedPoints = new ArrayList<>();
                                    snappedPoints.addAll(before.points);
                                    snappedPoints.addAll(after.points);
                                    gpx.addTrkSegment(snappedPoints);
                                } else {
                                    gpx.addTrkSegment(points);
                                }
                                break;
                            case ADD_ROUTE_POINTS:
                                gpx.replaceRoutePoints(points);
                                break;
                            case EDIT_SEGMENT:
                                TrkSegment segment = new TrkSegment();
                                segment.points.addAll(points);
                                gpx.replaceSegment(editingCtx.getNewGpxData().getTrkSegment(), segment);
                                break;
                        }
                    } else {
                        gpx.addRoutePoints(points);
                    }
                }
                if (activity != null) {
                    String res = GPXUtilities.writeGpxFile(toSave, gpx, activity.getMyApplication());
                    if (showOnMap) {
                        SelectedGpxFile sf = activity.getMyApplication().getSelectedGpxHelper().selectGpxFile(gpx, true, false);
                        if (sf != null) {
                            if (actionType == NewGpxData.ActionType.ADD_SEGMENT || actionType == NewGpxData.ActionType.EDIT_SEGMENT) {
                                sf.processPoints();
                            }
                        }
                    }
                    return res;
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(String warning) {
            MapActivity activity = getMapActivity();
            if (progressDialog != null && progressDialog.isShowing()) {
                progressDialog.dismiss();
            }
            if (activity != null) {
                activity.refreshMap();
                if (warning == null) {
                    saved = true;
                    if (openTrackActivity) {
                        dismiss(activity);
                    } else {
                        Toast.makeText(activity, MessageFormat.format(getString(R.string.gpx_saved_sucessfully), toSave.getAbsolutePath()), Toast.LENGTH_LONG).show();
                        if (close) {
                            dismiss(activity);
                        }
                    }
                } else {
                    Toast.makeText(activity, warning, Toast.LENGTH_LONG).show();
                }
            }
        }
    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
Also used : TrkSegment(net.osmand.plus.GPXUtilities.TrkSegment) ProgressDialog(android.app.ProgressDialog) SelectedGpxFile(net.osmand.plus.GpxSelectionHelper.SelectedGpxFile) List(java.util.List) ArrayList(java.util.ArrayList) GPXFile(net.osmand.plus.GPXUtilities.GPXFile) SelectedGpxFile(net.osmand.plus.GpxSelectionHelper.SelectedGpxFile) GPXFile(net.osmand.plus.GPXUtilities.GPXFile) File(java.io.File) Track(net.osmand.plus.GPXUtilities.Track) Route(net.osmand.plus.GPXUtilities.Route) MapActivity(net.osmand.plus.activities.MapActivity)

Example 4 with Track

use of net.osmand.plus.GPXUtilities.Track in project Osmand by osmandapp.

the class SavingTrackHelper method addTrackPoint.

private void addTrackPoint(WptPt pt, boolean newSegment, long time) {
    List<TrkSegment> points = currentTrack.getModifiablePointsToDisplay();
    Track track = currentTrack.getModifiableGpxFile().tracks.get(0);
    assert track.segments.size() == points.size();
    if (points.size() == 0 || newSegment) {
        points.add(new TrkSegment());
    }
    boolean segmentAdded = false;
    if (track.segments.size() == 0 || newSegment) {
        track.segments.add(new TrkSegment());
        segmentAdded = true;
    }
    if (pt != null) {
        int ind = points.size() - 1;
        TrkSegment last = points.get(ind);
        last.points.add(pt);
        TrkSegment lt = track.segments.get(track.segments.size() - 1);
        lt.points.add(pt);
    }
    if (segmentAdded) {
        currentTrack.processPoints();
    }
    currentTrack.getModifiableGpxFile().modifiedTime = time;
}
Also used : TrkSegment(net.osmand.plus.GPXUtilities.TrkSegment) Track(net.osmand.plus.GPXUtilities.Track)

Example 5 with Track

use of net.osmand.plus.GPXUtilities.Track in project Osmand by osmandapp.

the class SavingTrackHelper method prepareCurrentTrackForRecording.

private void prepareCurrentTrackForRecording() {
    if (currentTrack.getModifiableGpxFile().tracks.size() == 0) {
        currentTrack.getModifiableGpxFile().tracks.add(new Track());
    }
    while (currentTrack.getPointsToDisplay().size() < currentTrack.getModifiableGpxFile().tracks.size()) {
        TrkSegment trkSegment = new TrkSegment();
        currentTrack.getModifiablePointsToDisplay().add(trkSegment);
    }
}
Also used : TrkSegment(net.osmand.plus.GPXUtilities.TrkSegment) Track(net.osmand.plus.GPXUtilities.Track)

Aggregations

Track (net.osmand.plus.GPXUtilities.Track)9 TrkSegment (net.osmand.plus.GPXUtilities.TrkSegment)8 GPXFile (net.osmand.plus.GPXUtilities.GPXFile)5 WptPt (net.osmand.plus.GPXUtilities.WptPt)5 Location (net.osmand.Location)3 LocationPoint (net.osmand.data.LocationPoint)3 Route (net.osmand.plus.GPXUtilities.Route)3 TargetPoint (net.osmand.plus.TargetPointsHelper.TargetPoint)3 ArrayList (java.util.ArrayList)2 ProgressDialog (android.app.ProgressDialog)1 Cursor (android.database.Cursor)1 Bundle (android.os.Bundle)1 IBRouterService (btools.routingapp.IBRouterService)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 List (java.util.List)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1