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();
}
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;
}
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;
}
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));
}
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;
}
Aggregations