/* * // http://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&sensor=false * public static Route GetRoute(Location from, Location to) * {//GETROUTEOSCAR * lock (routes) * { * string key = Route.GetKey(from, to); * if (routes.ContainsKey(key)) * return routes[key]; * double METERS_TO_MILES = 0.000621371192; * int MAX_DURATION = 10; * XmlDocument doc = new XmlDocument(); * TimeSpan elapse = new TimeSpan(0, 0, 0); * double totalDistance = 0; * string url = "http://maps.googleapis.com/maps/api/directions/xml?origin=" + from.Lat + ", " + from.Lng + "&destination=" + to.Lat + ", " + to.Lng + "&sensor=false&units=imperial"; * doc.Load(url); * XmlNode status = doc.SelectSingleNode("//DirectionsResponse/status"); * if (status == null || status.InnerText == "ZERO_RESULTS") * return null; * List<Waypoint> waypoints = new List<Waypoint>(); * waypoints.Add(new Waypoint(from, new TimeSpan(0), 0)); * var legs = doc.SelectNodes("//DirectionsResponse/route/leg"); * foreach (XmlNode leg in legs) * { * var stepNodes = leg.SelectNodes("step"); * foreach (XmlNode stepNode in stepNodes) * { * int duration = int.Parse(stepNode.SelectSingleNode("duration/value").InnerText); * double distance = double.Parse(stepNode.SelectSingleNode("distance/value").InnerText) * METERS_TO_MILES; * TimeSpan duration2 = new TimeSpan(0, 0, int.Parse(stepNode.SelectSingleNode("duration/value").InnerText)); * Location end = new Location(double.Parse(stepNode.SelectSingleNode("end_location/lat").InnerText), double.Parse(stepNode.SelectSingleNode("end_location/lng").InnerText)); * totalDistance += distance; * TimeSpan timeSpanTemp = elapse; * elapse += duration2; * * if (duration > MAX_DURATION) * { * Location start = new Location(double.Parse(stepNode.SelectSingleNode("start_location/lat").InnerText), double.Parse(stepNode.SelectSingleNode("start_location/lng").InnerText)); * waypoints.AddRange(IncreaseGranularity(duration, MAX_DURATION, timeSpanTemp.TotalSeconds, distance, start, end)); * } * else * { * waypoints.Add(new Waypoint(end, elapse, totalDistance)); * } * * } * } * waypoints.Add(new Waypoint(to, elapse, totalDistance)); * Route route = new Route(waypoints.ToArray()); * routes.Add(key, route); * return route; * } * } * * private static List<Waypoint> IncreaseGranularity(int duration, int maxDuration, double totalDuration, double distance, Location from, Location to) * { * List<Waypoint> wayPoints = new List<Waypoint>(); * * double granularity = (duration / maxDuration); * double stepDistance = distance / granularity; * double stepDistaceCount = 0; * double durationCount = totalDuration; * double stepLat = from.Lat - to.Lat; * double stepLng = from.Lng - to.Lng; * double subStepLat = stepLat / granularity; * double subStepLng = stepLng / granularity; * for (int i = 0; i < (granularity - 1); i++) * { * from.Lat -= subStepLat; * from.Lng -= subStepLng; * Location location = new Location(from.Lat, from.Lng); * * durationCount += maxDuration; * stepDistaceCount += stepDistance; * * TimeSpan timeSpan = new TimeSpan(0, 0, (int)durationCount); * wayPoints.Add(new Waypoint(location, timeSpan, stepDistaceCount)); * * } * return wayPoints; * } */ /*public static Route GetCachedRoute(Location from, Location to) * { * string key = Route.GetKey(from, to); * if (routes.ContainsKey(key)) * return routes[key]; * /* foreach (Route r in routes.Values) * { * if (r.ContainsSubRoute(from, to)) * { * Route route = r.MakeSubRoute(from, to); * routes.Add(key, route); * } * } */ // return null; //} // http://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&sensor=false /*public static Route GetRoute(Location from, Location to) * { * lock (routes) * { * Route route = GetCachedRoute(from, to); * if (route != null) * return route; * route = GetRouteFromMapService(from, to, route); * string key = Route.GetKey(from, to); * routes.Add(key, route); * MapTools.WriteGeoRoutes(); * * return route; * } * }*/ /*private static Route GetRouteFromMapService(Location from, Location to, Route route) * { * double METERS_TO_MILES = 0.000621371192; * XmlDocument doc = new XmlDocument(); * TimeSpan elapse = new TimeSpan(0, 0, 0); * double totalDistance = 0; * string url = "http://maps.googleapis.com/maps/api/directions/xml?origin=" + from.Lat + ", " + from.Lng + "&destination=" + to.Lat + ", " + to.Lng + "&sensor=false"; * doc.Load(url); * XmlNode status = doc.SelectSingleNode("//DirectionsResponse/status"); * if (status != null && status.InnerText != "ZERO_RESULTS") * { * List<Waypoint> waypoints = new List<Waypoint>(); * waypoints.Add(new Waypoint(from, new TimeSpan(0), 0)); * var legs = doc.SelectNodes("//DirectionsResponse/route/leg"); * foreach (XmlNode leg in legs) * { * var stepNodes = leg.SelectNodes("step"); * foreach (XmlNode stepNode in stepNodes) * { * TimeSpan duration = new TimeSpan(0, 0, (int)(int.Parse(stepNode.SelectSingleNode("duration/value").InnerText) * distance_and_time_scale)); * Location end = new Location(double.Parse(stepNode.SelectSingleNode("end_location/lat").InnerText), double.Parse(stepNode.SelectSingleNode("end_location/lng").InnerText)); * double distance = double.Parse(stepNode.SelectSingleNode("distance/value").InnerText) * METERS_TO_MILES * distance_and_time_scale; * totalDistance += distance; * elapse += duration; * waypoints.Add(new Waypoint(end, elapse, totalDistance)); * } * } * waypoints.Add(new Waypoint(to, elapse, totalDistance)); * route = new Route(waypoints.ToArray()); * } * return route; * }*/ public static Route GetRoute(Location from, Location to) { lock (routes) { var key = Route.GetKey(from, to); if (routes.ContainsKey(key)) { return(routes[key]); } const double metersToMiles = 0.000621371192; const int maxDuration = 10; var doc = new XmlDocument(); var elapse = new TimeSpan(0, 0, 0); double totalDistance = 0; var url = "http://maps.googleapis.com/maps/api/directions/xml?origin=" + from.Lat + ", " + from.Lng + "&destination=" + to.Lat + ", " + to.Lng + "&sensor=false&units=imperial"; doc.Load(url); var status = doc.SelectSingleNode("//DirectionsResponse/status"); if (status == null || status.InnerText == "ZERO_RESULTS") { return(null); } var waypoints = new List <Waypoint> { new Waypoint(@from, new TimeSpan(0), 0) }; var legs = doc.SelectNodes("//DirectionsResponse/route/leg"); foreach (XmlNode leg in legs) { var stepNodes = leg.SelectNodes("step"); foreach (XmlNode stepNode in stepNodes) { var duration = int.Parse(stepNode.SelectSingleNode("duration/value").InnerText); var distance = double.Parse(stepNode.SelectSingleNode("distance/value").InnerText) * metersToMiles; var duration2 = new TimeSpan(0, 0, int.Parse(stepNode.SelectSingleNode("duration/value").InnerText)); var end = new Location(double.Parse(stepNode.SelectSingleNode("end_location/lat").InnerText), double.Parse(stepNode.SelectSingleNode("end_location/lng").InnerText)); var totalDistanceTemp = totalDistance; totalDistance += distance; var timeSpanTemp = elapse; elapse += duration2; if (duration > maxDuration) { var polyline = stepNode.SelectSingleNode("polyline/points").InnerText; var locations = DecodePolylinePoints(polyline); waypoints.AddRange(IncreaseGranularityInPolylineList(locations, duration, maxDuration, timeSpanTemp.TotalSeconds, distance, totalDistanceTemp)); } else { waypoints.Add(new Waypoint(end, elapse, totalDistance)); } } } waypoints.Add(new Waypoint(to, elapse, totalDistance)); var route = new Route(waypoints.ToArray()); routes.Add(key, route); return(route); } }
public static void CsvLoadTripRoutes(string filename, bool lngFirst) { // load trip routes Dictionary <string, LinkedList <Waypoint> > routes = new Dictionary <string, LinkedList <Waypoint> >(); using (CsvFileReader reader = new CsvFileReader(filename)) { CsvRow row = new CsvRow(); while (reader.ReadRow(row, ',')) { string routeID = row[0]; double distance = 0; double lat = Convert.ToDouble(lngFirst ? row[2] : row[1]); double lng = Convert.ToDouble(lngFirst ? row[1] : row[2]); if (routes.ContainsKey(routeID)) { distance = routes[routeID].First.Value.GetDistance(new Location(lat, lng, "null")); } Waypoint waypoint = new Waypoint(lat, lng, TimeSpan.Parse(row[3]), distance, row[4].Replace("\"", "")); // Scenario #1 if (!routes.ContainsKey(routeID)) { routes[routeID] = new LinkedList <Waypoint>(); } routes[routeID].AddLast(waypoint); } } foreach (LinkedList <Waypoint> w in routes.Values) { Route r = new Route(w.ToArray()); string key = Route.GetKey(r.start, r.end); MapTools.routes.Add(key, r); } }
private static void LoadGeoRoutes() { Dictionary <string, LinkedList <Waypoint> > routes = new Dictionary <string, LinkedList <Waypoint> >(); using (CsvFileReader reader = new CsvFileReader(routes_Filename)) { CsvRow row = new CsvRow(); while (reader.ReadRow(row, ',')) { string routeID = row[0]; Waypoint waypoint = new Waypoint(Convert.ToDouble(row[1]), Convert.ToDouble(row[2]), TimeSpan.Parse(row[3]), Convert.ToDouble(row[4]), row[5]); // Scenario #1 if (!routes.ContainsKey(routeID)) { routes[routeID] = new LinkedList <Waypoint>(); } routes[routeID].AddLast(waypoint); } } foreach (LinkedList <Waypoint> w in routes.Values) { Route r = new Route(w.ToArray()); string key = Route.GetKey(r.start, r.end); MapTools.routes.Add(key, r); } }
/* * // http://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&sensor=false * public static Route GetRoute(Location from, Location to) * {//GETROUTEOSCAR * lock (routes) * { * string key = Route.GetKey(from, to); * if (routes.ContainsKey(key)) * return routes[key]; * double METERS_TO_MILES = 0.000621371192; * int MAX_DURATION = 10; * XmlDocument doc = new XmlDocument(); * TimeSpan elapse = new TimeSpan(0, 0, 0); * double totalDistance = 0; * string url = "http://maps.googleapis.com/maps/api/directions/xml?origin=" + from.Lat + ", " + from.Lng + "&destination=" + to.Lat + ", " + to.Lng + "&sensor=false&units=imperial"; * doc.Load(url); * XmlNode status = doc.SelectSingleNode("//DirectionsResponse/status"); * if (status == null || status.InnerText == "ZERO_RESULTS") * return null; * List<Waypoint> waypoints = new List<Waypoint>(); * waypoints.Add(new Waypoint(from, new TimeSpan(0), 0)); * var legs = doc.SelectNodes("//DirectionsResponse/route/leg"); * foreach (XmlNode leg in legs) * { * var stepNodes = leg.SelectNodes("step"); * foreach (XmlNode stepNode in stepNodes) * { * int duration = int.Parse(stepNode.SelectSingleNode("duration/value").InnerText); * double distance = double.Parse(stepNode.SelectSingleNode("distance/value").InnerText) * METERS_TO_MILES; * TimeSpan duration2 = new TimeSpan(0, 0, int.Parse(stepNode.SelectSingleNode("duration/value").InnerText)); * Location end = new Location(double.Parse(stepNode.SelectSingleNode("end_location/lat").InnerText), double.Parse(stepNode.SelectSingleNode("end_location/lng").InnerText)); * totalDistance += distance; * TimeSpan timeSpanTemp = elapse; * elapse += duration2; * * if (duration > MAX_DURATION) * { * Location start = new Location(double.Parse(stepNode.SelectSingleNode("start_location/lat").InnerText), double.Parse(stepNode.SelectSingleNode("start_location/lng").InnerText)); * waypoints.AddRange(IncreaseGranularity(duration, MAX_DURATION, timeSpanTemp.TotalSeconds, distance, start, end)); * } * else * { * waypoints.Add(new Waypoint(end, elapse, totalDistance)); * } * * } * } * waypoints.Add(new Waypoint(to, elapse, totalDistance)); * Route route = new Route(waypoints.ToArray()); * routes.Add(key, route); * return route; * } * } * * private static List<Waypoint> IncreaseGranularity(int duration, int maxDuration, double totalDuration, double distance, Location from, Location to) * { * List<Waypoint> wayPoints = new List<Waypoint>(); * * double granularity = (duration / maxDuration); * double stepDistance = distance / granularity; * double stepDistaceCount = 0; * double durationCount = totalDuration; * double stepLat = from.Lat - to.Lat; * double stepLng = from.Lng - to.Lng; * double subStepLat = stepLat / granularity; * double subStepLng = stepLng / granularity; * for (int i = 0; i < (granularity - 1); i++) * { * from.Lat -= subStepLat; * from.Lng -= subStepLng; * Location location = new Location(from.Lat, from.Lng); * * durationCount += maxDuration; * stepDistaceCount += stepDistance; * * TimeSpan timeSpan = new TimeSpan(0, 0, (int)durationCount); * wayPoints.Add(new Waypoint(location, timeSpan, stepDistaceCount)); * * } * return wayPoints; * } */ /*public static Route GetCachedRoute(Location from, Location to) * { * string key = Route.GetKey(from, to); * if (routes.ContainsKey(key)) * return routes[key]; * /* foreach (Route r in routes.Values) * { * if (r.ContainsSubRoute(from, to)) * { * Route route = r.MakeSubRoute(from, to); * routes.Add(key, route); * } * } */ // return null; //} // http://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&sensor=false /*public static Route GetRoute(Location from, Location to) * { * lock (routes) * { * Route route = GetCachedRoute(from, to); * if (route != null) * return route; * route = GetRouteFromMapService(from, to, route); * string key = Route.GetKey(from, to); * routes.Add(key, route); * MapTools.WriteGeoRoutes(); * * return route; * } * }*/ /*private static Route GetRouteFromMapService(Location from, Location to, Route route) * { * double METERS_TO_MILES = 0.000621371192; * XmlDocument doc = new XmlDocument(); * TimeSpan elapse = new TimeSpan(0, 0, 0); * double totalDistance = 0; * string url = "http://maps.googleapis.com/maps/api/directions/xml?origin=" + from.Lat + ", " + from.Lng + "&destination=" + to.Lat + ", " + to.Lng + "&sensor=false"; * doc.Load(url); * XmlNode status = doc.SelectSingleNode("//DirectionsResponse/status"); * if (status != null && status.InnerText != "ZERO_RESULTS") * { * List<Waypoint> waypoints = new List<Waypoint>(); * waypoints.Add(new Waypoint(from, new TimeSpan(0), 0)); * var legs = doc.SelectNodes("//DirectionsResponse/route/leg"); * foreach (XmlNode leg in legs) * { * var stepNodes = leg.SelectNodes("step"); * foreach (XmlNode stepNode in stepNodes) * { * TimeSpan duration = new TimeSpan(0, 0, (int)(int.Parse(stepNode.SelectSingleNode("duration/value").InnerText) * distance_and_time_scale)); * Location end = new Location(double.Parse(stepNode.SelectSingleNode("end_location/lat").InnerText), double.Parse(stepNode.SelectSingleNode("end_location/lng").InnerText)); * double distance = double.Parse(stepNode.SelectSingleNode("distance/value").InnerText) * METERS_TO_MILES * distance_and_time_scale; * totalDistance += distance; * elapse += duration; * waypoints.Add(new Waypoint(end, elapse, totalDistance)); * } * } * waypoints.Add(new Waypoint(to, elapse, totalDistance)); * route = new Route(waypoints.ToArray()); * } * return route; * }*/ public static Route GetRoute(Location from, Location to) { lock (routes) { string key = Route.GetKey(from, to); if (routes.ContainsKey(key)) { return(routes[key]); } double METERS_TO_MILES = 0.000621371192; int MAX_DURATION = 10; XmlDocument doc = new XmlDocument(); TimeSpan elapse = new TimeSpan(0, 0, 0); double totalDistance = 0; string url = "http://maps.googleapis.com/maps/api/directions/xml?origin=" + from.Lat + ", " + from.Lng + "&destination=" + to.Lat + ", " + to.Lng + "&sensor=false&units=imperial"; doc.Load(url); XmlNode status = doc.SelectSingleNode("//DirectionsResponse/status"); if (status == null || status.InnerText == "ZERO_RESULTS") { return(null); } List <Waypoint> waypoints = new List <Waypoint>(); waypoints.Add(new Waypoint(from, new TimeSpan(0), 0)); var legs = doc.SelectNodes("//DirectionsResponse/route/leg"); foreach (XmlNode leg in legs) { var stepNodes = leg.SelectNodes("step"); foreach (XmlNode stepNode in stepNodes) { int duration = int.Parse(stepNode.SelectSingleNode("duration/value").InnerText); double distance = double.Parse(stepNode.SelectSingleNode("distance/value").InnerText) * METERS_TO_MILES; TimeSpan duration2 = new TimeSpan(0, 0, int.Parse(stepNode.SelectSingleNode("duration/value").InnerText)); Location end = new Location(double.Parse(stepNode.SelectSingleNode("end_location/lat").InnerText), double.Parse(stepNode.SelectSingleNode("end_location/lng").InnerText)); double totalDistanceTemp = totalDistance; totalDistance += distance; TimeSpan timeSpanTemp = elapse; elapse += duration2; if (duration > MAX_DURATION) { string polyline = stepNode.SelectSingleNode("polyline/points").InnerText; List <Location> locations = DecodePolylinePoints(polyline); waypoints.AddRange(increaseGranularityInPolylineList(locations, duration, MAX_DURATION, timeSpanTemp.TotalSeconds, distance, totalDistanceTemp)); } else { waypoints.Add(new Waypoint(end, elapse, totalDistance)); } } } waypoints.Add(new Waypoint(to, elapse, totalDistance)); Route route = new Route(waypoints.ToArray()); routes.Add(key, route); return(route); } }
// http://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&sensor=false public static Route GetRoute(Location from, Location to) { lock (routes) { Route route = GetCachedRoute(from, to); if (route != null) { return(route); } route = GetRouteFromMapService(from, to, route); string key = Route.GetKey(from, to); routes.Add(key, route); MapTools.WriteGeoRoutes(); return(route); } }
/* * // http://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&sensor=false * public static Route GetRoute(Location from, Location to) * {//GETROUTEOSCAR * lock (routes) * { * string key = Route.GetKey(from, to); * if (routes.ContainsKey(key)) * return routes[key]; * double METERS_TO_MILES = 0.000621371192; * int MAX_DURATION = 10; * XmlDocument doc = new XmlDocument(); * TimeSpan elapse = new TimeSpan(0, 0, 0); * double totalDistance = 0; * string url = "http://maps.googleapis.com/maps/api/directions/xml?origin=" + from.Lat + ", " + from.Lng + "&destination=" + to.Lat + ", " + to.Lng + "&sensor=false&units=imperial"; * doc.Load(url); * XmlNode status = doc.SelectSingleNode("//DirectionsResponse/status"); * if (status == null || status.InnerText == "ZERO_RESULTS") * return null; * List<Waypoint> waypoints = new List<Waypoint>(); * waypoints.Add(new Waypoint(from, new TimeSpan(0), 0)); * var legs = doc.SelectNodes("//DirectionsResponse/route/leg"); * foreach (XmlNode leg in legs) * { * var stepNodes = leg.SelectNodes("step"); * foreach (XmlNode stepNode in stepNodes) * { * int duration = int.Parse(stepNode.SelectSingleNode("duration/value").InnerText); * double distance = double.Parse(stepNode.SelectSingleNode("distance/value").InnerText) * METERS_TO_MILES; * TimeSpan duration2 = new TimeSpan(0, 0, int.Parse(stepNode.SelectSingleNode("duration/value").InnerText)); * Location end = new Location(double.Parse(stepNode.SelectSingleNode("end_location/lat").InnerText), double.Parse(stepNode.SelectSingleNode("end_location/lng").InnerText)); * totalDistance += distance; * TimeSpan timeSpanTemp = elapse; * elapse += duration2; * * if (duration > MAX_DURATION) * { * Location start = new Location(double.Parse(stepNode.SelectSingleNode("start_location/lat").InnerText), double.Parse(stepNode.SelectSingleNode("start_location/lng").InnerText)); * waypoints.AddRange(IncreaseGranularity(duration, MAX_DURATION, timeSpanTemp.TotalSeconds, distance, start, end)); * } * else * { * waypoints.Add(new Waypoint(end, elapse, totalDistance)); * } * * } * } * waypoints.Add(new Waypoint(to, elapse, totalDistance)); * Route route = new Route(waypoints.ToArray()); * routes.Add(key, route); * return route; * } * } * * private static List<Waypoint> IncreaseGranularity(int duration, int maxDuration, double totalDuration, double distance, Location from, Location to) * { * List<Waypoint> wayPoints = new List<Waypoint>(); * * double granularity = (duration / maxDuration); * double stepDistance = distance / granularity; * double stepDistaceCount = 0; * double durationCount = totalDuration; * double stepLat = from.Lat - to.Lat; * double stepLng = from.Lng - to.Lng; * double subStepLat = stepLat / granularity; * double subStepLng = stepLng / granularity; * for (int i = 0; i < (granularity - 1); i++) * { * from.Lat -= subStepLat; * from.Lng -= subStepLng; * Location location = new Location(from.Lat, from.Lng); * * durationCount += maxDuration; * stepDistaceCount += stepDistance; * * TimeSpan timeSpan = new TimeSpan(0, 0, (int)durationCount); * wayPoints.Add(new Waypoint(location, timeSpan, stepDistaceCount)); * * } * return wayPoints; * } */ public static Route GetCachedRoute(Location from, Location to) { string key = Route.GetKey(from, to); if (routes.ContainsKey(key)) { return(routes[key]); } /* foreach (Route r in routes.Values) * { * if (r.ContainsSubRoute(from, to)) * { * Route route = r.MakeSubRoute(from, to); * routes.Add(key, route); * } * } */ return(null); }