Search in sources :

Example 1 with GPXFile

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

the class NotesFragment method generateGPXForRecordings.

private File generateGPXForRecordings(Set<Recording> selected) {
    File tmpFile = new File(getActivity().getCacheDir(), "share/noteLocations.gpx");
    tmpFile.getParentFile().mkdirs();
    GPXFile file = new GPXFile();
    for (Recording r : getRecordingsForGpx(selected)) {
        if (r != SHARE_LOCATION_FILE) {
            String desc = r.getDescriptionName(r.getFileName());
            if (desc == null) {
                desc = r.getFileName();
            }
            WptPt wpt = new WptPt();
            wpt.lat = r.getLatitude();
            wpt.lon = r.getLongitude();
            wpt.name = desc;
            wpt.link = r.getFileName();
            wpt.time = r.getFile().lastModified();
            wpt.category = r.getSearchHistoryType();
            wpt.desc = r.getTypeWithDuration(getContext());
            getMyApplication().getSelectedGpxHelper().addPoint(wpt, file);
        }
    }
    GPXUtilities.writeGpxFile(tmpFile, file, getMyApplication());
    return tmpFile;
}
Also used : WptPt(net.osmand.plus.GPXUtilities.WptPt) Recording(net.osmand.plus.audionotes.AudioVideoNotesPlugin.Recording) GPXFile(net.osmand.plus.GPXUtilities.GPXFile) GPXFile(net.osmand.plus.GPXUtilities.GPXFile) File(java.io.File)

Example 2 with GPXFile

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

the class FailSafeFuntions method restoreRoutingMode.

public static void restoreRoutingMode(final MapActivity ma) {
    final OsmandApplication app = ma.getMyApplication();
    final OsmandSettings settings = app.getSettings();
    final Handler uiHandler = new Handler();
    final String gpxPath = settings.FOLLOW_THE_GPX_ROUTE.get();
    final TargetPointsHelper targetPoints = app.getTargetPointsHelper();
    final TargetPoint pointToNavigate = targetPoints.getPointToNavigate();
    if (pointToNavigate == null && gpxPath == null) {
        notRestoreRoutingMode(ma, app);
    } else {
        quitRouteRestoreDialog = false;
        Runnable encapsulate = new Runnable() {

            int delay = 7;

            Runnable delayDisplay = null;

            @Override
            public void run() {
                AlertDialog.Builder builder = new AlertDialog.Builder(ma);
                final TextView tv = new TextView(ma);
                tv.setText(ma.getString(R.string.continue_follow_previous_route_auto, delay + ""));
                tv.setPadding(7, 5, 7, 5);
                builder.setView(tv);
                builder.setPositiveButton(R.string.shared_string_yes, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        quitRouteRestoreDialog = true;
                        restoreRoutingModeInner();
                    }
                });
                builder.setNegativeButton(R.string.shared_string_no, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        quitRouteRestoreDialog = true;
                        notRestoreRoutingMode(ma, app);
                    }
                });
                final AlertDialog dlg = builder.show();
                dlg.setOnDismissListener(new OnDismissListener() {

                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        quitRouteRestoreDialog = true;
                    }
                });
                dlg.setOnCancelListener(new OnCancelListener() {

                    @Override
                    public void onCancel(DialogInterface dialog) {
                        quitRouteRestoreDialog = true;
                    }
                });
                delayDisplay = new Runnable() {

                    @Override
                    public void run() {
                        if (!quitRouteRestoreDialog) {
                            delay--;
                            tv.setText(ma.getString(R.string.continue_follow_previous_route_auto, delay + ""));
                            if (delay <= 0) {
                                try {
                                    if (dlg.isShowing() && !quitRouteRestoreDialog) {
                                        dlg.dismiss();
                                    }
                                    quitRouteRestoreDialog = true;
                                    restoreRoutingModeInner();
                                } catch (Exception e) {
                                    // swalow view not attached exception
                                    log.error(e.getMessage() + "", e);
                                }
                            } else {
                                uiHandler.postDelayed(delayDisplay, 1000);
                            }
                        }
                    }
                };
                delayDisplay.run();
            }

            private void restoreRoutingModeInner() {
                AsyncTask<String, Void, GPXFile> task = new AsyncTask<String, Void, GPXFile>() {

                    @Override
                    protected GPXFile doInBackground(String... params) {
                        if (gpxPath != null) {
                            // Reverse also should be stored ?
                            GPXFile f = GPXUtilities.loadGPXFile(app, new File(gpxPath));
                            if (f.warning != null) {
                                return null;
                            }
                            return f;
                        } else {
                            return null;
                        }
                    }

                    @Override
                    protected void onPostExecute(GPXFile result) {
                        final GPXRouteParamsBuilder gpxRoute;
                        if (result != null) {
                            gpxRoute = new GPXRouteParamsBuilder(result, settings);
                            if (settings.GPX_ROUTE_CALC_OSMAND_PARTS.get()) {
                                gpxRoute.setCalculateOsmAndRouteParts(true);
                            }
                            if (settings.GPX_CALCULATE_RTEPT.get()) {
                                gpxRoute.setUseIntermediatePointsRTE(true);
                            }
                            if (settings.GPX_ROUTE_CALC.get()) {
                                gpxRoute.setCalculateOsmAndRoute(true);
                            }
                        } else {
                            gpxRoute = null;
                        }
                        TargetPoint endPoint = pointToNavigate;
                        if (endPoint == null) {
                            notRestoreRoutingMode(ma, app);
                        } else {
                            enterRoutingMode(ma, gpxRoute);
                        }
                    }
                };
                task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, gpxPath);
            }
        };
        encapsulate.run();
    }
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) OsmandApplication(net.osmand.plus.OsmandApplication) DialogInterface(android.content.DialogInterface) GPXRouteParamsBuilder(net.osmand.plus.routing.RouteProvider.GPXRouteParamsBuilder) GPXRouteParamsBuilder(net.osmand.plus.routing.RouteProvider.GPXRouteParamsBuilder) OnDismissListener(android.content.DialogInterface.OnDismissListener) AsyncTask(android.os.AsyncTask) Handler(android.os.Handler) TargetPoint(net.osmand.plus.TargetPointsHelper.TargetPoint) OsmandSettings(net.osmand.plus.OsmandSettings) TargetPoint(net.osmand.plus.TargetPointsHelper.TargetPoint) TextView(android.widget.TextView) GPXFile(net.osmand.plus.GPXUtilities.GPXFile) TargetPointsHelper(net.osmand.plus.TargetPointsHelper) GPXFile(net.osmand.plus.GPXUtilities.GPXFile) File(java.io.File) OnCancelListener(android.content.DialogInterface.OnCancelListener)

Example 3 with GPXFile

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

the class QuickSearchDialogFragment method shareHistory.

private void shareHistory(final List<HistoryEntry> historyEntries) {
    if (!historyEntries.isEmpty()) {
        final AsyncTask<Void, Void, GPXFile> exportTask = new AsyncTask<Void, Void, GPXFile>() {

            @Override
            protected GPXFile doInBackground(Void... params) {
                GPXFile gpx = new GPXFile();
                for (HistoryEntry h : historyEntries) {
                    WptPt pt = new WptPt();
                    pt.lat = h.getLat();
                    pt.lon = h.getLon();
                    pt.name = h.getName().getName();
                    boolean hasTypeInDescription = !Algorithms.isEmpty(h.getName().getTypeName());
                    if (hasTypeInDescription) {
                        pt.desc = h.getName().getTypeName();
                    }
                    gpx.addPoint(pt);
                }
                return gpx;
            }

            @Override
            protected void onPreExecute() {
                showProgressBar();
            }

            @Override
            protected void onPostExecute(GPXFile gpxFile) {
                hideProgressBar();
                File dir = new File(getActivity().getCacheDir(), "share");
                if (!dir.exists()) {
                    dir.mkdir();
                }
                File dst = new File(dir, "History.gpx");
                GPXUtilities.writeGpxFile(dst, gpxFile, app);
                final Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_SEND);
                sendIntent.putExtra(Intent.EXTRA_TEXT, "History.gpx:\n\n\n" + GPXUtilities.asString(gpxFile, app));
                sendIntent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.share_history_subject));
                sendIntent.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(getActivity(), getActivity().getPackageName() + ".fileprovider", dst));
                sendIntent.setType("text/plain");
                startActivity(sendIntent);
            }
        };
        exportTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    }
}
Also used : WptPt(net.osmand.plus.GPXUtilities.WptPt) AsyncTask(android.os.AsyncTask) HistoryEntry(net.osmand.plus.helpers.SearchHistoryHelper.HistoryEntry) Intent(android.content.Intent) GPXFile(net.osmand.plus.GPXUtilities.GPXFile) File(java.io.File) GPXFile(net.osmand.plus.GPXUtilities.GPXFile)

Example 4 with GPXFile

use of net.osmand.plus.GPXUtilities.GPXFile 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 5 with GPXFile

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

the class QuickSearchListItem method getTypeName.

public static String getTypeName(OsmandApplication app, SearchResult searchResult) {
    switch(searchResult.objectType) {
        case CITY:
            City city = (City) searchResult.object;
            return getCityTypeStr(app, city.getType());
        case POSTCODE:
            return app.getString(R.string.postcode);
        case VILLAGE:
            city = (City) searchResult.object;
            if (!Algorithms.isEmpty(searchResult.localeRelatedObjectName)) {
                if (searchResult.distRelatedObjectName > 0) {
                    return getCityTypeStr(app, city.getType()) + " • " + OsmAndFormatter.getFormattedDistance((float) searchResult.distRelatedObjectName, app) + " " + app.getString(R.string.shared_string_from) + " " + searchResult.localeRelatedObjectName;
                } else {
                    return getCityTypeStr(app, city.getType()) + ", " + searchResult.localeRelatedObjectName;
                }
            } else {
                return getCityTypeStr(app, city.getType());
            }
        case STREET:
            StringBuilder streetBuilder = new StringBuilder();
            if (searchResult.localeName.endsWith(")")) {
                int i = searchResult.localeName.indexOf('(');
                if (i > 0) {
                    streetBuilder.append(searchResult.localeName.substring(i + 1, searchResult.localeName.length() - 1));
                }
            }
            if (!Algorithms.isEmpty(searchResult.localeRelatedObjectName)) {
                if (streetBuilder.length() > 0) {
                    streetBuilder.append(", ");
                }
                streetBuilder.append(searchResult.localeRelatedObjectName);
            }
            return streetBuilder.toString();
        case HOUSE:
            if (searchResult.relatedObject != null) {
                Street relatedStreet = (Street) searchResult.relatedObject;
                if (relatedStreet.getCity() != null) {
                    return searchResult.localeRelatedObjectName + ", " + relatedStreet.getCity().getName(searchResult.requiredSearchPhrase.getSettings().getLang(), true);
                } else {
                    return searchResult.localeRelatedObjectName;
                }
            }
            return "";
        case STREET_INTERSECTION:
            Street street = (Street) searchResult.object;
            if (street.getCity() != null) {
                return street.getCity().getName(searchResult.requiredSearchPhrase.getSettings().getLang(), true);
            }
            return "";
        case POI_TYPE:
            String res = "";
            if (searchResult.object instanceof AbstractPoiType) {
                AbstractPoiType abstractPoiType = (AbstractPoiType) searchResult.object;
                if (abstractPoiType instanceof PoiCategory) {
                    res = "";
                } else if (abstractPoiType instanceof PoiFilter) {
                    PoiFilter poiFilter = (PoiFilter) abstractPoiType;
                    res = poiFilter.getPoiCategory() != null ? poiFilter.getPoiCategory().getTranslation() : "";
                } else if (abstractPoiType instanceof PoiType) {
                    PoiType poiType = (PoiType) abstractPoiType;
                    res = poiType.getParentType() != null ? poiType.getParentType().getTranslation() : null;
                    if (res == null) {
                        res = poiType.getCategory() != null ? poiType.getCategory().getTranslation() : null;
                    }
                    if (res == null) {
                        res = "";
                    }
                } else {
                    res = "";
                }
            } else if (searchResult.object instanceof CustomSearchPoiFilter) {
                res = ((CustomSearchPoiFilter) searchResult.object).getName();
            }
            return res;
        case POI:
            Amenity amenity = (Amenity) searchResult.object;
            PoiCategory pc = amenity.getType();
            PoiType pt = pc.getPoiTypeByKeyName(amenity.getSubType());
            String typeStr = amenity.getSubType();
            if (pt != null) {
                typeStr = pt.getTranslation();
            } else if (typeStr != null) {
                typeStr = Algorithms.capitalizeFirstLetterAndLowercase(typeStr.replace('_', ' '));
            }
            return typeStr;
        case LOCATION:
            LatLon latLon = searchResult.location;
            if (latLon != null && searchResult.localeRelatedObjectName == null) {
                String locationCountry = app.getRegions().getCountryName(latLon);
                searchResult.localeRelatedObjectName = locationCountry == null ? "" : locationCountry;
            }
            return searchResult.localeRelatedObjectName;
        case FAVORITE:
            FavouritePoint fav = (FavouritePoint) searchResult.object;
            return fav.getCategory().length() == 0 ? app.getString(R.string.shared_string_favorites) : fav.getCategory();
        case FAVORITE_GROUP:
            return app.getString(R.string.shared_string_my_favorites);
        case REGION:
            BinaryMapIndexReader binaryMapIndexReader = (BinaryMapIndexReader) searchResult.object;
            System.out.println(binaryMapIndexReader.getFile().getAbsolutePath() + " " + binaryMapIndexReader.getCountryName());
            break;
        case RECENT_OBJ:
            HistoryEntry entry = (HistoryEntry) searchResult.object;
            boolean hasTypeInDescription = !Algorithms.isEmpty(entry.getName().getTypeName());
            if (hasTypeInDescription) {
                return entry.getName().getTypeName();
            } else {
                return "";
            }
        case WPT:
            StringBuilder sb = new StringBuilder();
            GPXFile gpx = (GPXFile) searchResult.relatedObject;
            if (!Algorithms.isEmpty(searchResult.localeRelatedObjectName)) {
                sb.append(searchResult.localeRelatedObjectName);
            }
            if (gpx != null && !Algorithms.isEmpty(gpx.path)) {
                if (sb.length() > 0) {
                    sb.append(", ");
                }
                sb.append(new File(gpx.path).getName());
            }
            return sb.toString();
        case UNKNOWN_NAME_FILTER:
            break;
    }
    return searchResult.objectType.name();
}
Also used : Amenity(net.osmand.data.Amenity) PoiFilter(net.osmand.osm.PoiFilter) CustomSearchPoiFilter(net.osmand.search.core.CustomSearchPoiFilter) FavouritePoint(net.osmand.data.FavouritePoint) AbstractPoiType(net.osmand.osm.AbstractPoiType) PoiType(net.osmand.osm.PoiType) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) City(net.osmand.data.City) AbstractPoiType(net.osmand.osm.AbstractPoiType) FavouritePoint(net.osmand.data.FavouritePoint) LatLon(net.osmand.data.LatLon) PoiCategory(net.osmand.osm.PoiCategory) Street(net.osmand.data.Street) HistoryEntry(net.osmand.plus.helpers.SearchHistoryHelper.HistoryEntry) CustomSearchPoiFilter(net.osmand.search.core.CustomSearchPoiFilter) GPXFile(net.osmand.plus.GPXUtilities.GPXFile) GPXFile(net.osmand.plus.GPXUtilities.GPXFile) File(java.io.File)

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