Search in sources :

Example 51 with GPXFile

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

the class MapMarkersHelper method generateGpx.

public String generateGpx(String fileName) {
    final File dir = ctx.getAppPath(IndexConstants.GPX_INDEX_DIR + "/map markers");
    if (!dir.exists()) {
        dir.mkdirs();
    }
    File fout = new File(dir, fileName + ".gpx");
    int ind = 1;
    while (fout.exists()) {
        fout = new File(dir, fileName + "_" + (++ind) + ".gpx");
    }
    GPXFile file = new GPXFile();
    for (MapMarker marker : mapMarkers) {
        WptPt wpt = new WptPt();
        wpt.lat = marker.getLatitude();
        wpt.lon = marker.getLongitude();
        wpt.setColor(ctx.getResources().getColor(MapMarker.getColorId(marker.colorIndex)));
        wpt.name = marker.getOnlyName();
        file.addPoint(wpt);
    }
    GPXUtilities.writeGpxFile(fout, file, ctx);
    return fout.getAbsolutePath();
}
Also used : WptPt(net.osmand.plus.GPXUtilities.WptPt) GPXFile(net.osmand.plus.GPXUtilities.GPXFile) SelectedGpxFile(net.osmand.plus.GpxSelectionHelper.SelectedGpxFile) GPXFile(net.osmand.plus.GPXUtilities.GPXFile) File(java.io.File) LocationPoint(net.osmand.data.LocationPoint) FavouritePoint(net.osmand.data.FavouritePoint)

Example 52 with GPXFile

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

the class FavouritesDbHelper method loadGPXFile.

private boolean loadGPXFile(File file, Map<String, FavouritePoint> points) {
    if (!file.exists()) {
        return false;
    }
    GPXFile res = GPXUtilities.loadGPXFile(context, file);
    if (res.warning != null) {
        return false;
    }
    for (WptPt p : res.getPoints()) {
        int c;
        String name = p.name;
        String categoryName = p.category != null ? p.category : "";
        if (name == null) {
            name = "";
        }
        // old way to store the category, in name.
        if ("".equals(categoryName.trim()) && (c = name.lastIndexOf('_')) != -1) {
            categoryName = name.substring(c + 1);
            name = name.substring(0, c);
        }
        FavouritePoint fp = new FavouritePoint(p.lat, p.lon, name, categoryName);
        fp.setDescription(p.desc);
        if (p.comment != null) {
            fp.setOriginObjectName(p.comment);
        }
        fp.setColor(p.getColor(0));
        fp.setVisible(!p.getExtensionsToRead().containsKey(HIDDEN));
        points.put(getKey(fp), fp);
    }
    return true;
}
Also used : WptPt(net.osmand.plus.GPXUtilities.WptPt) FavouritePoint(net.osmand.data.FavouritePoint) GPXFile(net.osmand.plus.GPXUtilities.GPXFile) FavouritePoint(net.osmand.data.FavouritePoint)

Example 53 with GPXFile

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

the class FavouritesDbHelper method asGpxFile.

private GPXFile asGpxFile(List<FavouritePoint> favoritePoints) {
    GPXFile gpx = new GPXFile();
    for (FavouritePoint p : favoritePoints) {
        WptPt pt = new WptPt();
        pt.lat = p.getLatitude();
        pt.lon = p.getLongitude();
        if (!p.isVisible()) {
            pt.getExtensionsToWrite().put(HIDDEN, "true");
        }
        if (p.getColor() != 0) {
            pt.setColor(p.getColor());
        }
        pt.name = p.getName();
        pt.desc = p.getDescription();
        if (p.getCategory().length() > 0)
            pt.category = p.getCategory();
        if (p.getOriginObjectName().length() > 0) {
            pt.comment = p.getOriginObjectName();
        }
        context.getSelectedGpxHelper().addPoint(pt, gpx);
    }
    return gpx;
}
Also used : WptPt(net.osmand.plus.GPXUtilities.WptPt) FavouritePoint(net.osmand.data.FavouritePoint) GPXFile(net.osmand.plus.GPXUtilities.GPXFile)

Example 54 with GPXFile

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

the class OsmandAidlApi method registerNavigateGpxReceiver.

private void registerNavigateGpxReceiver(final MapActivity mapActivity) {
    navigateGpxReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            boolean force = intent.getBooleanExtra(AIDL_FORCE, false);
            GPXFile gpx = null;
            if (intent.getStringExtra(AIDL_DATA) != null) {
                String gpxStr = intent.getStringExtra(AIDL_DATA);
                if (!Algorithms.isEmpty(gpxStr)) {
                    gpx = GPXUtilities.loadGPXFile(mapActivity, new ByteArrayInputStream(gpxStr.getBytes()));
                }
            } else if (intent.getParcelableExtra(AIDL_URI) != null) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    Uri gpxUri = intent.getParcelableExtra(AIDL_URI);
                    ParcelFileDescriptor gpxParcelDescriptor = null;
                    try {
                        gpxParcelDescriptor = mapActivity.getContentResolver().openFileDescriptor(gpxUri, "r");
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                    if (gpxParcelDescriptor != null) {
                        FileDescriptor fileDescriptor = gpxParcelDescriptor.getFileDescriptor();
                        gpx = GPXUtilities.loadGPXFile(mapActivity, new FileInputStream(fileDescriptor));
                    }
                }
            }
            if (gpx != null) {
                final RoutingHelper routingHelper = app.getRoutingHelper();
                if (routingHelper.isFollowingMode() && !force) {
                    final GPXFile gpxFile = gpx;
                    AlertDialog dlg = mapActivity.getMapActions().stopNavigationActionConfirm();
                    dlg.setOnDismissListener(new DialogInterface.OnDismissListener() {

                        @Override
                        public void onDismiss(DialogInterface dialog) {
                            if (!routingHelper.isFollowingMode()) {
                                startNavigation(mapActivity, gpxFile, null, null, null, null, null);
                            }
                        }
                    });
                } else {
                    startNavigation(mapActivity, gpx, null, null, null, null, null);
                }
            }
        }
    };
    mapActivity.registerReceiver(navigateGpxReceiver, new IntentFilter(AIDL_NAVIGATE_GPX));
}
Also used : Context(android.content.Context) AlertDialog(android.support.v7.app.AlertDialog) IntentFilter(android.content.IntentFilter) DialogInterface(android.content.DialogInterface) FileNotFoundException(java.io.FileNotFoundException) Intent(android.content.Intent) RoutingHelper(net.osmand.plus.routing.RoutingHelper) BroadcastReceiver(android.content.BroadcastReceiver) Uri(android.net.Uri) ParcelFileDescriptor(android.os.ParcelFileDescriptor) FileDescriptor(java.io.FileDescriptor) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ParcelFileDescriptor(android.os.ParcelFileDescriptor) GPXFile(net.osmand.plus.GPXUtilities.GPXFile)

Example 55 with GPXFile

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

the class RouteProvider method createOsmandRouterGPX.

public GPXFile createOsmandRouterGPX(RouteCalculationResult srcRoute, OsmandApplication ctx) {
    TargetPointsHelper helper = ctx.getTargetPointsHelper();
    int currentRoute = srcRoute.currentRoute;
    List<Location> routeNodes = srcRoute.getImmutableAllLocations();
    List<RouteDirectionInfo> directionInfo = srcRoute.getImmutableAllDirections();
    int currentDirectionInfo = srcRoute.currentDirectionInfo;
    GPXFile gpx = new GPXFile();
    gpx.author = OSMAND_ROUTER;
    Track track = new Track();
    gpx.tracks.add(track);
    TrkSegment trkSegment = new TrkSegment();
    track.segments.add(trkSegment);
    int cRoute = currentRoute;
    int cDirInfo = currentDirectionInfo;
    // Save the start point to gpx file's trkpt section unless already contained
    WptPt startpoint = new WptPt();
    TargetPoint sc = helper.getPointToStart();
    int routePointOffsetAdjusted = 0;
    if (sc != null && ((float) sc.getLatitude() != (float) routeNodes.get(cRoute).getLatitude() || (float) sc.getLongitude() != (float) routeNodes.get(cRoute).getLongitude())) {
        startpoint.lat = sc.getLatitude();
        startpoint.lon = sc.getLongitude();
        trkSegment.points.add(startpoint);
        if (directionInfo != null && !directionInfo.isEmpty()) {
            for (RouteDirectionInfo i : directionInfo) {
                i.routePointOffset++;
            }
        }
        routePointOffsetAdjusted = 1;
    }
    for (int i = cRoute; i < routeNodes.size(); i++) {
        Location loc = routeNodes.get(i);
        WptPt pt = new WptPt();
        pt.lat = loc.getLatitude();
        pt.lon = loc.getLongitude();
        if (loc.hasSpeed()) {
            pt.speed = loc.getSpeed();
        }
        if (loc.hasAltitude()) {
            pt.ele = loc.getAltitude();
        }
        if (loc.hasAccuracy()) {
            pt.hdop = loc.getAccuracy();
        }
        trkSegment.points.add(pt);
    }
    Route route = new Route();
    gpx.routes.add(route);
    for (int i = cDirInfo; i < directionInfo.size(); i++) {
        RouteDirectionInfo dirInfo = directionInfo.get(i);
        if (dirInfo.routePointOffset - routePointOffsetAdjusted >= cRoute) {
            if (dirInfo.getTurnType() != null && !dirInfo.getTurnType().isSkipToSpeak() && dirInfo.routePointOffset - routePointOffsetAdjusted < routeNodes.size()) {
                Location loc = routeNodes.get(dirInfo.routePointOffset - routePointOffsetAdjusted);
                WptPt pt = new WptPt();
                pt.lat = loc.getLatitude();
                pt.lon = loc.getLongitude();
                // Collect distances and times for subsequent suppressed turns
                int collectedDistance = 0;
                int collectedTime = 0;
                for (int j = i + 1; j < directionInfo.size(); j++) {
                    if (directionInfo.get(j).getTurnType() != null && directionInfo.get(j).getTurnType().isSkipToSpeak()) {
                        collectedDistance += directionInfo.get(j).getDistance();
                        collectedTime += directionInfo.get(j).getExpectedTime();
                    } else {
                        break;
                    }
                }
                pt.desc = dirInfo.getDescriptionRoute(ctx, collectedDistance + dirInfo.getDistance());
                Map<String, String> extensions = pt.getExtensionsToWrite();
                extensions.put("time", (collectedTime + dirInfo.getExpectedTime()) + "");
                int turnType = dirInfo.getTurnType().getValue();
                if (TurnType.C != turnType) {
                    extensions.put("turn", dirInfo.getTurnType().toXmlString());
                    extensions.put("turn-angle", dirInfo.getTurnType().getTurnAngle() + "");
                }
                extensions.put("offset", (dirInfo.routePointOffset - cRoute) + "");
                // Issue #2894
                if (dirInfo.getRef() != null && !"null".equals(dirInfo.getRef())) {
                    extensions.put("ref", dirInfo.getRef() + "");
                }
                if (dirInfo.getStreetName() != null && !"null".equals(dirInfo.getStreetName())) {
                    extensions.put("street-name", dirInfo.getStreetName() + "");
                }
                if (dirInfo.getDestinationName() != null && !"null".equals(dirInfo.getDestinationName())) {
                    extensions.put("dest", dirInfo.getDestinationName() + "");
                }
                route.points.add(pt);
            }
        }
    }
    List<TargetPoint> ps = helper.getIntermediatePointsWithTarget();
    for (int k = 0; k < ps.size(); k++) {
        WptPt pt = new WptPt();
        pt.lat = ps.get(k).getLatitude();
        pt.lon = ps.get(k).getLongitude();
        if (k < ps.size()) {
            pt.name = ps.get(k).getOnlyName() + "";
            if (k == ps.size() - 1) {
                String target = ctx.getString(R.string.destination_point, "");
                if (pt.name.startsWith(target)) {
                    pt.name = ctx.getString(R.string.destination_point, pt.name);
                }
            } else {
                String prefix = (k + 1) + ". ";
                if (Algorithms.isEmpty(pt.name)) {
                    pt.name = ctx.getString(R.string.target_point, pt.name);
                }
                if (pt.name.startsWith(prefix)) {
                    pt.name = prefix + pt.name;
                }
            }
            pt.desc = pt.name;
        }
        gpx.addPoint(pt);
    }
    return gpx;
}
Also used : WptPt(net.osmand.plus.GPXUtilities.WptPt) TrkSegment(net.osmand.plus.GPXUtilities.TrkSegment) TargetPoint(net.osmand.plus.TargetPointsHelper.TargetPoint) TargetPoint(net.osmand.plus.TargetPointsHelper.TargetPoint) LocationPoint(net.osmand.data.LocationPoint) GPXFile(net.osmand.plus.GPXUtilities.GPXFile) TargetPointsHelper(net.osmand.plus.TargetPointsHelper) Track(net.osmand.plus.GPXUtilities.Track) Route(net.osmand.plus.GPXUtilities.Route) Location(net.osmand.Location)

Aggregations

GPXFile (net.osmand.plus.GPXUtilities.GPXFile)56 File (java.io.File)26 SelectedGpxFile (net.osmand.plus.GpxSelectionHelper.SelectedGpxFile)21 WptPt (net.osmand.plus.GPXUtilities.WptPt)16 OsmandApplication (net.osmand.plus.OsmandApplication)11 View (android.view.View)8 TextView (android.widget.TextView)8 Intent (android.content.Intent)7 ImageView (android.widget.ImageView)7 FavouritePoint (net.osmand.data.FavouritePoint)7 LatLon (net.osmand.data.LatLon)7 DialogInterface (android.content.DialogInterface)6 AlertDialog (android.support.v7.app.AlertDialog)6 ArrayList (java.util.ArrayList)6 MapMarkersHelper (net.osmand.plus.MapMarkersHelper)6 OsmandSettings (net.osmand.plus.OsmandSettings)6 GpxDataItem (net.osmand.plus.GPXDatabase.GpxDataItem)5 Track (net.osmand.plus.GPXUtilities.Track)5 TrkSegment (net.osmand.plus.GPXUtilities.TrkSegment)5 AdapterView (android.widget.AdapterView)4