use of net.osmand.Location in project Osmand by osmandapp.
the class RouteCalculationResult method getDistanceToNextIntermediate.
public int getDistanceToNextIntermediate(Location fromLoc) {
if (listDistance != null && currentRoute < listDistance.length) {
int dist = listDistance[currentRoute];
Location l = locations.get(currentRoute);
if (fromLoc != null) {
dist += fromLoc.distanceTo(l);
}
if (nextIntermediate >= intermediatePoints.length) {
return 0;
} else {
int directionInd = intermediatePoints[nextIntermediate];
return dist - listDistance[directions.get(directionInd).routePointOffset];
}
}
return 0;
}
use of net.osmand.Location in project Osmand by osmandapp.
the class RouteCalculationResult method addMissingTurnsToRoute.
protected static void addMissingTurnsToRoute(List<Location> locations, List<RouteDirectionInfo> originalDirections, Location start, LatLon end, ApplicationMode mode, Context ctx, boolean leftSide) {
if (locations.isEmpty()) {
return;
}
// speed m/s
float speed = mode.getDefaultSpeed();
int minDistanceForTurn = mode.getMinDistanceForTurn();
List<RouteDirectionInfo> computeDirections = new ArrayList<RouteDirectionInfo>();
int[] listDistance = new int[locations.size()];
listDistance[locations.size() - 1] = 0;
for (int i = locations.size() - 1; i > 0; i--) {
listDistance[i - 1] = (int) Math.round(locations.get(i - 1).distanceTo(locations.get(i)));
listDistance[i - 1] += listDistance[i];
}
int previousLocation = 0;
int prevBearingLocation = 0;
RouteDirectionInfo previousInfo = new RouteDirectionInfo(speed, TurnType.straight());
previousInfo.routePointOffset = 0;
previousInfo.setDescriptionRoute(ctx.getString(R.string.route_head));
computeDirections.add(previousInfo);
int distForTurn = 0;
float previousBearing = 0;
int startTurnPoint = 0;
for (int i = 1; i < locations.size() - 1; i++) {
Location next = locations.get(i + 1);
Location current = locations.get(i);
float bearing = current.bearingTo(next);
// try to get close to current location if possible
while (prevBearingLocation < i - 1) {
if (locations.get(prevBearingLocation + 1).distanceTo(current) > 70) {
prevBearingLocation++;
} else {
break;
}
}
if (distForTurn == 0) {
// measure only after turn
previousBearing = locations.get(prevBearingLocation).bearingTo(current);
startTurnPoint = i;
}
TurnType type = null;
String description = null;
float delta = previousBearing - bearing;
while (delta < 0) {
delta += 360;
}
while (delta > 360) {
delta -= 360;
}
distForTurn += locations.get(i).distanceTo(locations.get(i + 1));
if (i < locations.size() - 1 && distForTurn < minDistanceForTurn) {
// 2) if there is a small gap between roads (turn right and after 4m next turn left) - so the direction head
continue;
}
if (delta > 45 && delta < 315) {
if (delta < 60) {
type = TurnType.valueOf(TurnType.TSLL, leftSide);
description = ctx.getString(R.string.route_tsll);
} else if (delta < 120) {
type = TurnType.valueOf(TurnType.TL, leftSide);
description = ctx.getString(R.string.route_tl);
} else if (delta < 150) {
type = TurnType.valueOf(TurnType.TSHL, leftSide);
description = ctx.getString(R.string.route_tshl);
} else if (delta < 210) {
type = TurnType.valueOf(TurnType.TU, leftSide);
description = ctx.getString(R.string.route_tu);
} else if (delta < 240) {
description = ctx.getString(R.string.route_tshr);
type = TurnType.valueOf(TurnType.TSHR, leftSide);
} else if (delta < 300) {
description = ctx.getString(R.string.route_tr);
type = TurnType.valueOf(TurnType.TR, leftSide);
} else {
description = ctx.getString(R.string.route_tslr);
type = TurnType.valueOf(TurnType.TSLR, leftSide);
}
// calculate for previousRoute
previousInfo.distance = listDistance[previousLocation] - listDistance[i];
type.setTurnAngle(360 - delta);
previousInfo = new RouteDirectionInfo(speed, type);
previousInfo.setDescriptionRoute(description);
previousInfo.routePointOffset = startTurnPoint;
computeDirections.add(previousInfo);
previousLocation = startTurnPoint;
// for bearing using current location
prevBearingLocation = i;
}
// clear dist for turn
distForTurn = 0;
}
previousInfo.distance = listDistance[previousLocation];
if (originalDirections.isEmpty()) {
originalDirections.addAll(computeDirections);
} else {
int currentDirection = 0;
// one more
for (int i = 0; i <= originalDirections.size() && currentDirection < computeDirections.size(); i++) {
while (currentDirection < computeDirections.size()) {
int distanceAfter = 0;
if (i < originalDirections.size()) {
RouteDirectionInfo resInfo = originalDirections.get(i);
int r1 = computeDirections.get(currentDirection).routePointOffset;
int r2 = resInfo.routePointOffset;
distanceAfter = listDistance[resInfo.routePointOffset];
float dist = locations.get(r1).distanceTo(locations.get(r2));
// take into account that move roundabout is special turn that could be very lengthy
if (dist < 100) {
// the same turn duplicate
currentDirection++;
// while cycle
continue;
} else if (computeDirections.get(currentDirection).routePointOffset > resInfo.routePointOffset) {
// check it at the next point
break;
}
}
// add turn because it was missed
RouteDirectionInfo toAdd = computeDirections.get(currentDirection);
if (i > 0) {
// update previous
RouteDirectionInfo previous = originalDirections.get(i - 1);
toAdd.setAverageSpeed(previous.getAverageSpeed());
}
toAdd.distance = listDistance[toAdd.routePointOffset] - distanceAfter;
if (i < originalDirections.size()) {
originalDirections.add(i, toAdd);
} else {
originalDirections.add(toAdd);
}
i++;
currentDirection++;
}
}
}
int sum = 0;
for (int i = originalDirections.size() - 1; i >= 0; i--) {
originalDirections.get(i).afterLeftTime = sum;
sum += originalDirections.get(i).getExpectedTime();
}
}
use of net.osmand.Location in project Osmand by osmandapp.
the class RouteProvider method findStraightRoute.
private RouteCalculationResult findStraightRoute(RouteCalculationParams params) {
double[] lats = new double[] { params.start.getLatitude(), params.end.getLatitude() };
double[] lons = new double[] { params.start.getLongitude(), params.end.getLongitude() };
List<LatLon> intermediates = params.intermediates;
List<Location> dots = new ArrayList<Location>();
// writing start location
Location location = new Location(String.valueOf("start"));
location.setLatitude(lats[0]);
location.setLongitude(lons[0]);
// adding intermediate dots if they exists
if (intermediates != null) {
for (int i = 0; i < intermediates.size(); i++) {
location = new Location(String.valueOf(i));
location.setLatitude(intermediates.get(i).getLatitude());
location.setLongitude(intermediates.get(i).getLongitude());
dots.add(location);
}
}
// writing end location
location = new Location(String.valueOf("end"));
location.setLatitude(lats[1]);
location.setLongitude(lons[1]);
dots.add(location);
return new RouteCalculationResult(dots, null, params, null, true);
}
use of net.osmand.Location 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.Location in project Osmand by osmandapp.
the class RouteProvider method findYOURSRoute.
protected RouteCalculationResult findYOURSRoute(RouteCalculationParams params) throws MalformedURLException, IOException, ParserConfigurationException, FactoryConfigurationError, SAXException {
List<Location> res = new ArrayList<Location>();
StringBuilder uri = new StringBuilder();
// $NON-NLS-1$
uri.append("http://www.yournavigation.org/api/1.0/gosmore.php?format=kml");
// $NON-NLS-1$
uri.append("&flat=").append(params.start.getLatitude());
// $NON-NLS-1$
uri.append("&flon=").append(params.start.getLongitude());
// $NON-NLS-1$
uri.append("&tlat=").append(params.end.getLatitude());
// $NON-NLS-1$
uri.append("&tlon=").append(params.end.getLongitude());
if (params.mode.isDerivedRoutingFrom(ApplicationMode.BICYCLE)) {
// $NON-NLS-1$
uri.append("&v=bicycle");
} else if (params.mode.isDerivedRoutingFrom(ApplicationMode.PEDESTRIAN)) {
// $NON-NLS-1$
uri.append("&v=foot");
} else if (params.mode.isDerivedRoutingFrom(ApplicationMode.CAR)) {
// $NON-NLS-1$
uri.append("&v=motorcar");
} else {
return applicationModeNotSupported(params);
}
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
uri.append("&fast=").append(params.fast ? "1" : "0").append("&layer=mapnik");
log.info("URL route " + uri);
URLConnection connection = NetworkUtils.getHttpURLConnection(uri.toString());
connection.setRequestProperty("User-Agent", Version.getFullVersion(params.ctx));
DocumentBuilder dom = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = dom.parse(new InputSource(new InputStreamReader(connection.getInputStream())));
// $NON-NLS-1$
NodeList list = doc.getElementsByTagName("coordinates");
for (int i = 0; i < list.getLength(); i++) {
Node item = list.item(i);
String str = item.getFirstChild().getNodeValue();
if (str == null) {
continue;
}
int st = 0;
int next = 0;
while ((next = str.indexOf('\n', st)) != -1) {
String coordinate = str.substring(st, next + 1);
int s = coordinate.indexOf(',');
if (s != -1) {
try {
double lon = Double.parseDouble(coordinate.substring(0, s));
double lat = Double.parseDouble(coordinate.substring(s + 1));
// $NON-NLS-1$
Location l = new Location("router");
l.setLatitude(lat);
l.setLongitude(lon);
res.add(l);
} catch (NumberFormatException e) {
}
}
st = next + 1;
}
}
if (list.getLength() == 0) {
if (doc.getChildNodes().getLength() == 1) {
Node item = doc.getChildNodes().item(0);
return new RouteCalculationResult(item.getNodeValue());
}
}
params.intermediates = null;
return new RouteCalculationResult(res, null, params, null, true);
}
Aggregations