use of net.osmand.plus.GPXUtilities.TrkSegment in project Osmand by osmandapp.
the class ShowRouteInfoDialogFragment method openDetails.
void openDetails() {
if (gpxItem != null) {
LatLon location = null;
WptPt wpt = null;
gpxItem.chartTypes = new GPXDataSetType[] { GPXDataSetType.ALTITUDE, GPXDataSetType.SLOPE };
if (gpxItem.chartHighlightPos != -1) {
TrkSegment segment = gpx.tracks.get(0).segments.get(0);
if (segment != null) {
float distance = gpxItem.chartHighlightPos * elevationDataSet.getDivX();
for (WptPt p : segment.points) {
if (p.distance >= distance) {
wpt = p;
break;
}
}
if (wpt != null) {
location = new LatLon(wpt.lat, wpt.lon);
}
}
}
if (location == null) {
location = new LatLon(gpxItem.locationStart.lat, gpxItem.locationStart.lon);
}
if (wpt != null) {
gpxItem.locationOnMap = wpt;
} else {
gpxItem.locationOnMap = gpxItem.locationStart;
}
final MapActivity activity = (MapActivity) getActivity();
if (activity != null) {
dismiss();
final OsmandSettings settings = activity.getMyApplication().getSettings();
settings.setMapLocationToShow(location.getLatitude(), location.getLongitude(), settings.getLastKnownMapZoom(), new PointDescription(PointDescription.POINT_TYPE_WPT, gpxItem.name), false, gpxItem);
final MapRouteInfoMenu mapRouteInfoMenu = activity.getMapLayers().getMapControlsLayer().getMapRouteInfoMenu();
if (MapRouteInfoMenu.isVisible()) {
// We arrived here by the route info menu.
// First, we close it and then show the details.
mapRouteInfoMenu.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
mapRouteInfoMenu.setOnDismissListener(null);
MapActivity.launchMapActivityMoveToTop(activity);
}
});
mapRouteInfoMenu.hide();
} else {
// We arrived here by the dashboard.
MapActivity.launchMapActivityMoveToTop(activity);
}
}
}
}
use of net.osmand.plus.GPXUtilities.TrkSegment in project Osmand by osmandapp.
the class GpxSelectionHelper method processGroupTrack.
private static void processGroupTrack(OsmandApplication app, GpxDisplayGroup group) {
List<GpxDisplayItem> list = group.getModifiableList();
String timeSpanClr = Algorithms.colorToString(ContextCompat.getColor(app, R.color.gpx_time_span_color));
String speedClr = Algorithms.colorToString(ContextCompat.getColor(app, R.color.gpx_speed));
String ascClr = Algorithms.colorToString(ContextCompat.getColor(app, R.color.gpx_altitude_asc));
String descClr = Algorithms.colorToString(ContextCompat.getColor(app, R.color.gpx_altitude_desc));
String distanceClr = Algorithms.colorToString(ContextCompat.getColor(app, R.color.gpx_distance_color));
final float eleThreshold = 3;
// int t = 1;
for (TrkSegment r : group.track.segments) {
if (r.points.size() == 0) {
continue;
}
GPXTrackAnalysis[] as;
boolean split = true;
if (group.splitDistance > 0) {
List<GPXTrackAnalysis> trackSegments = r.splitByDistance(group.splitDistance);
as = trackSegments.toArray(new GPXTrackAnalysis[trackSegments.size()]);
} else if (group.splitTime > 0) {
List<GPXTrackAnalysis> trackSegments = r.splitByTime(group.splitTime);
as = trackSegments.toArray(new GPXTrackAnalysis[trackSegments.size()]);
} else {
split = false;
as = new GPXTrackAnalysis[] { GPXTrackAnalysis.segment(0, r) };
}
for (GPXTrackAnalysis analysis : as) {
GpxDisplayItem item = new GpxDisplayItem();
item.group = group;
if (split) {
item.splitMetric = analysis.metricEnd;
item.secondarySplitMetric = analysis.secondaryMetricEnd;
item.splitName = formatSplitName(analysis.metricEnd, group, app);
item.splitName += " (" + formatSecondarySplitName(analysis.secondaryMetricEnd, group, app) + ") ";
}
item.description = GpxUiHelper.getDescription(app, analysis, true);
item.analysis = analysis;
String name = "";
// }
if (!group.isSplitDistance()) {
name += GpxUiHelper.getColorValue(distanceClr, OsmAndFormatter.getFormattedDistance(analysis.totalDistance, app));
}
if ((analysis.timeSpan > 0 || analysis.timeMoving > 0) && !group.isSplitTime()) {
long tm = analysis.timeMoving;
if (tm == 0) {
tm = analysis.timeSpan;
}
if (name.length() != 0)
name += ", ";
name += GpxUiHelper.getColorValue(timeSpanClr, Algorithms.formatDuration((int) (tm / 1000), app.accessibilityEnabled()));
}
if (analysis.isSpeedSpecified()) {
if (name.length() != 0)
name += ", ";
name += GpxUiHelper.getColorValue(speedClr, OsmAndFormatter.getFormattedSpeed(analysis.avgSpeed, app));
}
// add min/max elevation data to split track analysis to facilitate easier track/segment identification
if (analysis.isElevationSpecified()) {
if (name.length() != 0)
name += ", ";
name += GpxUiHelper.getColorValue(descClr, OsmAndFormatter.getFormattedAlt(analysis.minElevation, app));
name += " - ";
name += GpxUiHelper.getColorValue(ascClr, OsmAndFormatter.getFormattedAlt(analysis.maxElevation, app));
}
if (analysis.isElevationSpecified() && (analysis.diffElevationUp > eleThreshold || analysis.diffElevationDown > eleThreshold)) {
if (name.length() != 0)
name += ", ";
if (analysis.diffElevationDown > eleThreshold) {
name += GpxUiHelper.getColorValue(descClr, " \u2193 " + OsmAndFormatter.getFormattedAlt(analysis.diffElevationDown, app));
}
if (analysis.diffElevationUp > eleThreshold) {
name += GpxUiHelper.getColorValue(ascClr, " \u2191 " + OsmAndFormatter.getFormattedAlt(analysis.diffElevationUp, app));
}
}
item.name = name;
item.locationStart = analysis.locationStart;
item.locationEnd = analysis.locationEnd;
list.add(item);
}
}
}
use of net.osmand.plus.GPXUtilities.TrkSegment 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;
}
use of net.osmand.plus.GPXUtilities.TrkSegment in project Osmand by osmandapp.
the class RouteProvider method parseOsmAndGPXRoute.
private static List<RouteDirectionInfo> parseOsmAndGPXRoute(List<Location> res, GPXFile gpxFile, boolean osmandRouter, boolean leftSide, float defSpeed) {
List<RouteDirectionInfo> directions = null;
if (!osmandRouter) {
for (WptPt pt : gpxFile.getPoints()) {
res.add(createLocation(pt));
}
} else {
for (Track tr : gpxFile.tracks) {
for (TrkSegment ts : tr.segments) {
for (WptPt p : ts.points) {
res.add(createLocation(p));
}
}
}
}
float[] distanceToEnd = new float[res.size()];
for (int i = res.size() - 2; i >= 0; i--) {
distanceToEnd[i] = distanceToEnd[i + 1] + res.get(i).distanceTo(res.get(i + 1));
}
Route route = null;
if (gpxFile.routes.size() > 0) {
route = gpxFile.routes.get(0);
}
RouteDirectionInfo previous = null;
if (route != null && route.points.size() > 0) {
directions = new ArrayList<RouteDirectionInfo>();
Iterator<WptPt> iterator = route.points.iterator();
while (iterator.hasNext()) {
WptPt item = iterator.next();
try {
String stime = item.getExtensionsToRead().get("time");
int time = 0;
if (stime != null) {
time = Integer.parseInt(stime);
}
// $NON-NLS-1$
int offset = Integer.parseInt(item.getExtensionsToRead().get("offset"));
if (directions.size() > 0) {
RouteDirectionInfo last = directions.get(directions.size() - 1);
// update speed using time and idstance
last.setAverageSpeed((distanceToEnd[last.routePointOffset] - distanceToEnd[offset]) / last.getAverageSpeed());
last.distance = (int) Math.round(distanceToEnd[last.routePointOffset] - distanceToEnd[offset]);
}
// save time as a speed because we don't know distance of the route segment
float avgSpeed = time;
if (!iterator.hasNext() && time > 0) {
avgSpeed = distanceToEnd[offset] / time;
}
// $NON-NLS-1$
String stype = item.getExtensionsToRead().get("turn");
TurnType turnType;
if (stype != null) {
turnType = TurnType.fromString(stype.toUpperCase(), leftSide);
} else {
turnType = TurnType.straight();
}
// $NON-NLS-1$
String sturn = item.getExtensionsToRead().get("turn-angle");
if (sturn != null) {
turnType.setTurnAngle((float) Double.parseDouble(sturn));
}
RouteDirectionInfo dirInfo = new RouteDirectionInfo(avgSpeed, turnType);
// $NON-NLS-1$
dirInfo.setDescriptionRoute(item.desc);
dirInfo.routePointOffset = offset;
// Issue #2894
// $NON-NLS-1$
String sref = item.getExtensionsToRead().get("ref");
if (sref != null && !"null".equals(sref)) {
// $NON-NLS-1$
dirInfo.setRef(sref);
}
// $NON-NLS-1$
String sstreetname = item.getExtensionsToRead().get("street-name");
if (sstreetname != null && !"null".equals(sstreetname)) {
// $NON-NLS-1$
dirInfo.setStreetName(sstreetname);
}
// $NON-NLS-1$
String sdest = item.getExtensionsToRead().get("dest");
if (sdest != null && !"null".equals(sdest)) {
// $NON-NLS-1$
dirInfo.setDestinationName(sdest);
}
if (previous != null && TurnType.C != previous.getTurnType().getValue() && !osmandRouter) {
// calculate angle
if (previous.routePointOffset > 0) {
float paz = res.get(previous.routePointOffset - 1).bearingTo(res.get(previous.routePointOffset));
float caz;
if (previous.getTurnType().isRoundAbout() && dirInfo.routePointOffset < res.size() - 1) {
caz = res.get(dirInfo.routePointOffset).bearingTo(res.get(dirInfo.routePointOffset + 1));
} else {
caz = res.get(dirInfo.routePointOffset - 1).bearingTo(res.get(dirInfo.routePointOffset));
}
float angle = caz - paz;
if (angle < 0) {
angle += 360;
} else if (angle > 360) {
angle -= 360;
}
// that magic number helps to fix some errors for turn
angle += 75;
if (previous.getTurnType().getTurnAngle() < 0.5f) {
previous.getTurnType().setTurnAngle(angle);
}
}
}
directions.add(dirInfo);
previous = dirInfo;
} catch (NumberFormatException e) {
// $NON-NLS-1$
log.info("Exception", e);
} catch (IllegalArgumentException e) {
// $NON-NLS-1$
log.info("Exception", e);
}
}
}
if (previous != null && TurnType.C != previous.getTurnType().getValue()) {
// calculate angle
if (previous.routePointOffset > 0 && previous.routePointOffset < res.size() - 1) {
float paz = res.get(previous.routePointOffset - 1).bearingTo(res.get(previous.routePointOffset));
float caz = res.get(previous.routePointOffset).bearingTo(res.get(res.size() - 1));
float angle = caz - paz;
if (angle < 0) {
angle += 360;
}
if (previous.getTurnType().getTurnAngle() < 0.5f) {
previous.getTurnType().setTurnAngle(angle);
}
}
}
return directions;
}
use of net.osmand.plus.GPXUtilities.TrkSegment in project Osmand by osmandapp.
the class GPXLayer method drawSelectedFileSegments.
private void drawSelectedFileSegments(SelectedGpxFile selectedGpxFile, boolean currentTrack, Canvas canvas, RotatedTileBox tileBox, DrawSettings settings) {
GpxDataItem gpxDataItem = null;
if (!currentTrack) {
gpxDataItem = view.getApplication().getGpxDatabase().getItem(new File(selectedGpxFile.getGpxFile().path));
}
List<TrkSegment> segments = selectedGpxFile.getPointsToDisplay();
for (TrkSegment ts : segments) {
int color = gpxDataItem != null ? gpxDataItem.getColor() : 0;
if (currentTrack) {
color = currentTrackColor;
}
if (color == 0) {
color = ts.getColor(cachedColor);
}
if (// only do once (CODE HERE NEEDS TO BE UI INSTEAD)
ts.renders.isEmpty() && !ts.points.isEmpty()) {
// hmmm. 0-point tracks happen, but.... how?
if (currentTrack) {
ts.renders.add(new Renderable.CurrentTrack(ts.points));
} else {
ts.renders.add(new Renderable.StandardTrack(ts.points, 17.2));
}
}
updatePaints(color, selectedGpxFile.isRoutePoints(), currentTrack, settings, tileBox);
ts.drawRenderers(view.getZoom(), paint, canvas, tileBox);
}
}
Aggregations