static void Main() { Location fromLocation = new Location(-22.910194, -43.212211); Location toLocation = new Location(-22.9105337, -43.2123576); double Lat = fromLocation.Lat - toLocation.Lat; double Lng = fromLocation.Lng - toLocation.Lng; var result = Math.Sqrt(Math.Pow(Lat, 2) + Math.Pow(Lng, 2)); int ocho = 9; /* var partnerConfigurations = GetPartnersConfigurations(); using (var sr = new StreamReader("App_Data\\Geo-Routes.txt")) { var lines = sr.ReadToEnd(); MapTools.routes = JsonConvert.DeserializeObject<Dictionary<string, Route>>(lines) ?? new Dictionary<string, Route>(); } using (var sr = new StreamReader("App_Data\\Geo-Location-Names.txt")) { var lines = sr.ReadToEnd(); MapTools.locationNames = JsonConvert.DeserializeObject<Dictionary<string, string>>(lines) ?? new Dictionary<string, string>(); } using (var sr = new StreamReader("App_Data\\Geo-Location-Addresses.txt")) { var lines = sr.ReadToEnd(); MapTools.locationAddresses = JsonConvert.DeserializeObject<Dictionary<string, Pair<string, string>>>(lines) ?? new Dictionary<string, Pair<string, string>>(); } foreach (var possibleTrip in partnerConfigurations.SelectMany(partnerConfiguration => partnerConfiguration.Fleets.ElementAt(0).PossibleTrips)) { MapTools.GetRoute(possibleTrip.Start, possibleTrip.End); } var routesString = JsonConvert.SerializeObject(MapTools.routes); var locationNamesString = JsonConvert.SerializeObject(MapTools.locationNames); var locationAddresses = JsonConvert.SerializeObject(MapTools.locationAddresses); File.WriteAllText("App_Data\\Geo-Routes.txt", String.Empty); using (var sr = new StreamWriter("App_Data\\Geo-Routes.txt")) { sr.Write(routesString); } File.WriteAllText("App_Data\\Geo-Location-Names.txt", String.Empty); using (var sr = new StreamWriter("App_Data\\Geo-Location-Names.txt")) { sr.Write(locationNamesString); } File.WriteAllText("App_Data\\Geo-Location-Addresses.txt", String.Empty); using (var sr = new StreamWriter("App_Data\\Geo-Location-Addresses.txt")) { sr.Write(locationAddresses); } * */ }
public Driver(string name, PartnerFleet PartnerFleet = null, Location location = null) : base(name) { if (PartnerFleet != null) PartnerFleet.AddDriver(this); this.PartnerFleet = PartnerFleet; this.location = location; lastUpdate = DateTime.UtcNow; }
private static List<Location> DecodePolylinePoints(string encodedPoints) { if (encodedPoints == null || encodedPoints == "") return null; List<Location> poly = new List<Location>(); char[] polylinechars = encodedPoints.ToCharArray(); int index = 0; int currentLat = 0; int currentLng = 0; int next5bits; int sum; int shifter; try { while (index < polylinechars.Length) { // calculate next latitude sum = 0; shifter = 0; do { next5bits = (int)polylinechars[index++] - 63; sum |= (next5bits & 31) << shifter; shifter += 5; } while (next5bits >= 32 && index < polylinechars.Length); if (index >= polylinechars.Length) break; currentLat += (sum & 1) == 1 ? ~(sum >> 1) : (sum >> 1); //calculate next longitude sum = 0; shifter = 0; do { next5bits = (int)polylinechars[index++] - 63; sum |= (next5bits & 31) << shifter; shifter += 5; } while (next5bits >= 32 && index < polylinechars.Length); if (index >= polylinechars.Length && next5bits >= 32) break; currentLng += (sum & 1) == 1 ? ~(sum >> 1) : (sum >> 1); Location p = new Location(Convert.ToDouble(currentLat) / 100000.0, Convert.ToDouble(currentLng) / 100000.0); poly.Add(p); } } catch (Exception) { // log it } return poly; }
public static Route GetRoute(Location from, Location to) { var key = Route.GetKey(from, to); var route = StorageManager.GetRoute(key); if (route != null) return route; 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" || status.InnerText == "OVER_QUERY_LIMIT") { Logger.LogDebug("Google request error", status != null ? status.InnerText : "status is null"); throw new Exception("Bad route request"); } 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(IncreaseLocationsEnumerable(locations, duration, timeSpanTemp.TotalSeconds, distance, totalDistanceTemp)); } else { waypoints.Add(new Waypoint(end, elapse, totalDistance)); } } } waypoints.Add(new Waypoint(to, elapse, totalDistance)); route = new Route(waypoints.ToArray()); StorageManager.SaveRoute(route); return route; }
// http://code.google.com/apis/maps/documentation/geocoding/#ReverseGeocoding public static string GetReverseGeoLoc(Location location) { lock (locationNames) { string key = location.getID(); if (locationNames.ContainsKey(key)) return locationNames[key]; return GetReverseGeoLocationNameFromMapService(location); } }
public static Pair<string, string> GetReverseGeoLocAddress(Location location) { lock (locationAddresses) { string key = location.getID(); if (locationAddresses.ContainsKey(key)) return locationAddresses[key]; return GetReverseGeoAddressFromMapService(location); } }
private static IEnumerable<Location> IncreaseGranularityDistance(Location from, Location to, double maxLatLng) { var locations = new List<Location>(); var lat = from.Lat - to.Lat; var lng = from.Lng - to.Lng; var latCount = from.Lat; var lngCount = from.Lng; var result = Math.Sqrt(Math.Pow(lat, 2) + Math.Pow(lng, 2)); //Calculamos la hipotenusa. if (result > maxLatLng)//Preguntamos si es necesario Granular { var repeatTimes = (uint)(result / maxLatLng); //Obtenemos las veces que se repetira el proceso. var stepLat = lat / repeatTimes; var stepLng = lng / repeatTimes; for (var i = 1; i < repeatTimes; i++) { latCount -= stepLat; lngCount -= stepLng; locations.Add(new Location(latCount, lngCount)); } } locations.Add(new Location(to.Lat, to.Lng)); return locations; }
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 bool FleetServesLocation(Location l) { foreach (Zone z in coverage) { if (z.IsInside(l)) return true; } return false; }
private void NotifyForeignPartner(Status status, Location driverLocation, DateTime? eta) { Logger.Log("Since trip has foreign dependency, notify partner through TripThru"); Logger.Tab(); Gateway.UpdateTripStatusRequest request = new Gateway.UpdateTripStatusRequest( clientID: partner.ID, tripID: ID, status: status, driverLocation: driverLocation, eta: eta ); partner.tripthru.UpdateTripStatus(request); Logger.Untab(); }
private static IEnumerable<Location> IncreaseGranularityDistance(Location from, Location to, double maxLatLng) { var locations = new List<Location>(); double Lat = from.Lat - to.Lat; double Lng = from.Lng - from.Lat; var latCount = from.Lat; var lngCount = from.Lng; var result = Math.Sqrt(Math.Pow(Lat, 2) + Math.Pow(Lng, 2)); //Calculamos la hipotenusa. if (result > maxLatLng)//Preguntamos si es necesario Granular { var repeatTimes = (int)(result/maxLatLng); //Obtenemos las veces que se repetira el proceso. var stepLat = Lat / repeatTimes; var stepLng = Lng / repeatTimes; for (var i = 0; i < repeatTimes; i++) { latCount += stepLat; lngCount += stepLng; locations.Add(new Location(latCount,lngCount)); } } return null; /* List<Waypoint> wayPoints = new List<Waypoint>(); double granularity = duration / maxDuration; double stepDistance = (distance / granularity) + totalDistance; 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)); } wayPoints.Add(new Waypoint(from, new TimeSpan(0, 0, ((int)totalDuration + duration)), stepDistaceCount)); return wayPoints; */ }
public Zone(Location center, double radius) { this.Center = center; this.Radius = radius; }
public bool IsInside(Location l) { double lat1 = DegreesToRadians(Center.Lat); double lng1 = DegreesToRadians(Center.Lng); double lat2 = DegreesToRadians(l.Lat); double lng2 = DegreesToRadians(l.Lng); double dlon = lng2 - lng1; double dlat = lat2 - lat1; double a = Math.Pow(Math.Sin(dlat / 2.0), 2) + Math.Cos(lat1) * Math.Cos(lat2) * Math.Pow(Math.Sin(dlon / 2.0), 2); double c = 2.0 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a)); double d = 3961.0 * c; // (where 3961 is the radius of the Earth in miles return d < Radius; }
public bool Equals(Location l, double tolerance = .005) { double distance = GetDistance(l); return distance < tolerance; }
public UpdateTripStatusRequest(string clientID, string tripID, Status status, Location driverLocation = null, DateTime? eta = null) { this.clientID = clientID; this.tripID = tripID; this.status = status; this.driverLocation = driverLocation; this.eta = eta; }
/* // 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"); using (StreamWriter writer = new StreamWriter("debug.txt", true)) { writer.WriteLine("Status: " + status.InnerText); } 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; } }
static void Main() { Location fromLocation = new Location(37.78374, -122.4143); Location toLocation = new Location(37.78466, -122.41447); double Lat = fromLocation.Lat - toLocation.Lat; double Lng = fromLocation.Lng - toLocation.Lng; var timeSpan = new TimeSpan(0, 0, 300); List<Location> locations = DecodePolylinePoints(@"upreFx_djVuAPwD`@{BRaAJeCVs@HyDd@_CX}@L}BXy@J}BX{@H"); var wayPoints = IncreaseLocationsEnumerable(locations, 300, 10, 0, 500, 0); //var locations = IncreaseGranularityDistance(fromLocation, toLocation, 0.0001); var result = Math.Sqrt(Math.Pow(Lat, 2) + Math.Pow(Lng, 2)); int ocho = 9; /* var partnerConfigurations = GetPartnersConfigurations(); using (var sr = new StreamReader("App_Data\\Geo-Routes.txt")) { var lines = sr.ReadToEnd(); MapTools.routes = JsonConvert.DeserializeObject<Dictionary<string, Route>>(lines) ?? new Dictionary<string, Route>(); } using (var sr = new StreamReader("App_Data\\Geo-Location-Names.txt")) { var lines = sr.ReadToEnd(); MapTools.locationNames = JsonConvert.DeserializeObject<Dictionary<string, string>>(lines) ?? new Dictionary<string, string>(); } using (var sr = new StreamReader("App_Data\\Geo-Location-Addresses.txt")) { var lines = sr.ReadToEnd(); MapTools.locationAddresses = JsonConvert.DeserializeObject<Dictionary<string, Pair<string, string>>>(lines) ?? new Dictionary<string, Pair<string, string>>(); } foreach (var possibleTrip in partnerConfigurations.SelectMany(partnerConfiguration => partnerConfiguration.Fleets.ElementAt(0).PossibleTrips)) { MapTools.GetRoute(possibleTrip.Start, possibleTrip.End); } var routesString = JsonConvert.SerializeObject(MapTools.routes); var locationNamesString = JsonConvert.SerializeObject(MapTools.locationNames); var locationAddresses = JsonConvert.SerializeObject(MapTools.locationAddresses); File.WriteAllText("App_Data\\Geo-Routes.txt", String.Empty); using (var sr = new StreamWriter("App_Data\\Geo-Routes.txt")) { sr.Write(routesString); } File.WriteAllText("App_Data\\Geo-Location-Names.txt", String.Empty); using (var sr = new StreamWriter("App_Data\\Geo-Location-Names.txt")) { sr.Write(locationNamesString); } File.WriteAllText("App_Data\\Geo-Location-Addresses.txt", String.Empty); using (var sr = new StreamWriter("App_Data\\Geo-Location-Addresses.txt")) { sr.Write(locationAddresses); } * */ }
public PartnerTrip(Partner partner, string ID, Origination origination, Location pickupLocation, DateTime pickupTime, PaymentMethod? paymentMethod = null, string passengerID = null, string passengerName = null, Location dropoffLocation = null, DateTime? dropoffTime = null, List<Location> waypoints = null, VehicleType? vehicleType = null, double? maxPrice = null, int? minRating = null, PartnerFleet fleet = null, Driver driver = null, TimeSpan? duration = null, TimeSpan? driverRouteDuration = null, double? price = null) { this.ID = ID; this.origination = origination; this.service = Origination.Local; this.partner = partner; this.passengerID = passengerID; this.passengerName = passengerName; this.pickupLocation = pickupLocation; this.pickupTime = pickupTime; this.duration = duration; this.dropoffLocation = dropoffLocation; this.dropoffTime = dropoffTime; this.waypoints = waypoints; this.paymentMethod = paymentMethod; this.vehicleType = vehicleType; this.maxPrice = maxPrice; this.minRating = minRating; this.PartnerFleet = fleet; this.driver = driver; this.price = price; this.UpdateTripStatus(notifyPartner: false, status: Status.New); }
public GetTripStatusResponse(string partnerID = null, string partnerName = null, string fleetID = null, string fleetName = null, string originatingPartnerName = null, string servicingPartnerName = null, string driverID = null, string driverName = null, Location driverLocation = null, Location driverInitialLocation = null, VehicleType? vehicleType = null, string passengerName = null, DateTime? ETA = null, Status? status = null, DateTime? pickupTime = null, Location pickupLocation = null, DateTime? dropoffTime = null, Location dropoffLocation = null, double? price = null, double? distance = null, double? driverRouteDuration = null, Result result = Result.OK ) { this.partnerID = partnerID; this.partnerName = partnerName; this.fleetID = fleetID; this.fleetName = fleetName; this.passengerName = passengerName; this.driverID = driverID; this.driverName = driverName; this.driverLocation = driverLocation; this.driverInitialLocation = driverInitialLocation; this.vehicleType = vehicleType; this.ETA = ETA; this.pickupTime = pickupTime; this.pickupLocation = pickupLocation; this.dropoffLocation = dropoffLocation; this.dropoffTime = dropoffTime; this.price = price; this.distance = distance; this.driverRouteDuration = driverRouteDuration; this.result = result; this.status = status; this.originatingPartnerName = originatingPartnerName; this.servicingPartnerName = servicingPartnerName; }
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 PartnerTrip(PartnerTrip t) { this.ID = t.ID; this.passengerID = t.passengerID; this.passengerName = t.passengerName; this.origination = t.origination; this.service = t.service; this.luggage = t.luggage; this.persons = t.persons; this.pickupLocation = t.pickupLocation; this.pickupTime = t.pickupTime; this.duration = t.duration; this.dropoffLocation = t.dropoffLocation; this.dropoffTime = t.dropoffTime; this.waypoints = t.waypoints; this.paymentMethod = t.paymentMethod; this.vehicleType = t.vehicleType; this.price = t.price; this.maxPrice = t.maxPrice; this.minRating = t.minRating; this.PartnerFleet = t.PartnerFleet; this.driver = t.driver; this.lastUpdate = t.lastUpdate; this._status = t._status; this.partner = t.partner; this.lastDispatchAttempt = DateTime.MinValue; }
public QuoteTripRequest(string clientID, Location pickupLocation, DateTime pickupTime, string passengerID = null, string passengerName = null, int? luggage = null, int? persons = null, Location dropoffLocation = null, List<Location> waypoints = null, PaymentMethod? paymentMethod = null, VehicleType? vehicleType = null, double? maxPrice = null, int? minRating = null, string partnerID = null, string fleetID = null, string driverID = null) { this.clientID = clientID; this.passengerID = passengerID; this.passengerName = passengerName; this.pickupLocation = pickupLocation; this.pickupTime = pickupTime; this.dropoffLocation = dropoffLocation; this.waypoints = waypoints; this.paymentMethod = paymentMethod; this.vehicleType = vehicleType; this.maxPrice = maxPrice; this.minRating = minRating; this.partnerID = partnerID; this.fleetID = fleetID; this.driverID = driverID; }
public void UpdateTripStatus(bool notifyPartner, Status status, Location driverLocation = null, DateTime? eta = null) { if (TripStatusHasChanged(status, driverLocation, eta)) { Logger.Log("Trip status changed from " + _status + " to " + status + (driverLocation != null ? (" and driver's location has changed to " + driverLocation) : "") + (eta != null ? (" and eta has changed to " + eta) : "")); _status = status; if (driverLocation != null) this.driverLocation = driverLocation; if (eta != null) this.ETA = eta; if (IsOneOfTheActiveTrips()) { partner.activeTrips[ID].Status = status; if (TripHasForeignDependency() && lastStatusNotifiedToPartner != status && notifyPartner) NotifyForeignPartner(status, driverLocation, eta); } else Logger.Log("Cannot set status: because cannot find active trip with ID = " + this.ID); } if (status == Status.Complete) { if (service == Origination.Foreign) { Gateway.GetTripStatusResponse resp = GetStatsFromForeignServiceProvider(); partner.DeactivateTripAndUpdateStats(ID, Status.Complete, resp.price, resp.distance); } else partner.DeactivateTripAndUpdateStats(ID, Status.Complete, PartnerFleet.GetPrice(this), PartnerFleet.GetDistance(this)); } else if (status == Status.Cancelled || status == Status.Rejected) partner.DeactivateTripAndUpdateStats(ID, status); lastStatusNotifiedToPartner = status; }
private static Pair<string, string> GetReverseGeoAddressFromMapService(Location location) { Pair<string, string> address = new Pair<string, string>(); XmlDocument doc = new XmlDocument(); doc.Load("http://maps.googleapis.com/maps/api/geocode/xml?latlng=" + location.Lat + "," + location.Lng + "&sensor=false"); XmlNode element = doc.SelectSingleNode("//GeocodeResponse/status"); /*if (element.InnerText == "OVER_QUERY_LIMIT") { System.Threading.Thread.Sleep(new TimeSpan(0, 1, 10)); doc.Load("http://maps.googleapis.com/maps/api/geocode/xml?latlng=" + location.Lat + "," + location.Lng + "&sensor=false"); element = doc.SelectSingleNode("//GeocodeResponse/status"); }*/ if (!(element.InnerText == "ZERO_RESULTS" || element.InnerText == "OVER_QUERY_LIMIT")) { var streetNumberNode = doc.SelectSingleNode( "//GeocodeResponse/result/address_component[type=\"street_number\"]/short_name"); string street_number = streetNumberNode != null ? streetNumberNode.InnerText : ""; var routeNode = doc.SelectSingleNode("//GeocodeResponse/result/address_component[type=\"route\"]/short_name"); string route = routeNode != null ? routeNode.InnerText : ""; var postalCodeNode = doc.SelectSingleNode( "//GeocodeResponse/result/address_component[type=\"postal_code\"]/short_name"); string postal_code = postalCodeNode != null ? postalCodeNode.InnerText : ""; address = new Pair<string, string>(street_number + " " + route, postal_code); locationAddresses.Add(location.getID(), address); WriteGeoLocationAddresses(); return address; } else { return new Pair<string, string>("reached query limit", "reached query limit"); } }
private bool TripStatusHasChanged(Status status, Location driverLocation, DateTime? eta) { return this._status != status || driverLocation != this.driverLocation || this.ETA != eta; }
private static string GetReverseGeoLocationNameFromMapService(Location location) { //return "Google -- Over query limit"; XmlDocument doc = new XmlDocument(); { doc.Load("http://maps.googleapis.com/maps/api/geocode/xml?latlng=" + location.Lat + "," + location.Lng + "&sensor=false"); XmlNode element = doc.SelectSingleNode("//GeocodeResponse/status"); /*if (element.InnerText == "OVER_QUERY_LIMIT") { System.Threading.Thread.Sleep(new TimeSpan(0, 1, 10)); doc.Load("http://maps.googleapis.com/maps/api/geocode/xml?latlng=" + location.Lat + "," + location.Lng + "&sensor=false"); element = doc.SelectSingleNode("//GeocodeResponse/status"); }*/ if (element.InnerText == "ZERO_RESULTS" || element.InnerText == "OVER_QUERY_LIMIT") return "Google -- Over query limit"; else { element = doc.SelectSingleNode("//GeocodeResponse/result/formatted_address"); locationNames.Add(location.getID(), element.InnerText); WriteGeoLocationNames(); return element.InnerText; } } }
public PartnerFleet(string name, Location location, List<Zone> coverage, List<Driver> drivers, List<VehicleType> vehicleTypes, List<Pair<Location, Location>> possibleTrips, double costPerMile, double baseCost, double tripsPerHour, List<Passenger> passengers, Partner partner = null) : base(name) { this.coverage = coverage; this.partner = partner; this.location = location; foreach (Passenger p in passengers) p.PartnerFleet = this; random = new Random(); this.passengers = passengers.ToArray(); this.possibleTrips = possibleTrips.ToArray(); List<Pair<Location, Location>> coveredTrips = new List<Pair<Location, Location>>(); foreach (Pair<Location, Location> trip in possibleTrips) { if (FleetServesLocation(trip.First)) coveredTrips.Add(trip); } this.tripsPerHour = tripsPerHour; this.costPerMile = costPerMile; this.baseCost = baseCost; this.queue = new LinkedList<PartnerTrip>(); this.drivers = new Dictionary<string, Driver>(); availableDrivers = new LinkedList<Driver>(); returningDrivers = new LinkedList<Driver>(); this.vehicleTypes = new List<VehicleType>(vehicleTypes); if (drivers != null) { foreach (Driver d in drivers) AddDriver(d); } if (partner != null) partner.AddPartnerFleet(this); }
private static IEnumerable<Waypoint> IncreaseGranularity(int duration, int maxDuration, double totalDuration, double distance, double totalDistance, Location from, Location to) { List<Waypoint> wayPoints = new List<Waypoint>(); double granularity = duration / maxDuration; double stepDistance = (distance / granularity) + totalDistance; 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)); } wayPoints.Add(new Waypoint(from, new TimeSpan(0, 0, ((int)totalDuration + duration)), stepDistaceCount)); return wayPoints; }
public DateTime UpdateDriverRouteAndGetETA(Driver driver, Location destination) { driver.routeStartTime = DateTime.UtcNow; driver.route = MapTools.GetRoute(driver.location, destination); DateTime eta = DateTime.UtcNow + driver.route.duration; Logger.Log(driver.name + " has a new route from " + driver.location + " to " + destination + ": ETA = " + eta); return eta; }
private static IEnumerable<Waypoint> IncreaseGranularityInPolylineList(IEnumerable<Location> locations, int duration, int maxDuration, double totalDuration, double distance, double totalDistance) { const double metersToMiles = 0.000621371192; var waypoints = new List<Waypoint>(); var doc = new XmlDocument(); Location prevLocation = null; var elapse = new TimeSpan(0, 0, (int)totalDuration); foreach (var location in locations) { if (prevLocation == null) { prevLocation = location; continue; } XmlNode status; var trying = 0; do { if (trying > 0) Thread.Sleep(1000); var url = "http://maps.googleapis.com/maps/api/directions/xml?origin=" + prevLocation.Lat + ", " + prevLocation.Lng + "&destination=" + location.Lat + ", " + location.Lng + "&sensor=false&units=imperial"; doc.Load(url); status = doc.SelectSingleNode("//DirectionsResponse/status"); if (status == null || status.InnerText == "ZERO_RESULTS" || trying == 5) break; trying++; } while (status.InnerText == "OVER_QUERY_LIMIT"); var legs = doc.SelectNodes("//DirectionsResponse/route/leg"); foreach (XmlNode leg in legs) { var stepNodes = leg.SelectNodes("step"); foreach (XmlNode stepNode in stepNodes) { var durationInt = int.Parse(stepNode.SelectSingleNode("duration/value").InnerText); var distance2 = 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)); if (durationInt > maxDuration) { var start = new Location(double.Parse(stepNode.SelectSingleNode("start_location/lat").InnerText), double.Parse(stepNode.SelectSingleNode("start_location/lng").InnerText)); waypoints.AddRange(IncreaseGranularity(durationInt, maxDuration, elapse.TotalSeconds, distance2, totalDistance, start, end)); totalDistance += distance2; elapse += duration2; } else { elapse += duration2; totalDistance += distance; waypoints.Add(new Waypoint(end, elapse, totalDistance + distance2)); } } } prevLocation = location; } return waypoints; }