/* * Check where the line from the departure point to the destination point intersects the map boundaries * These new coordinates will be used to find the closest PT stop. This stop will be the new starting point of the ride. */ public bool checkIntersection(double x1, double y1, double x2, double y2, double minX, double minY, double maxX, double maxY, out Point newStartingPoint) { newStartingPoint = null; // Completely outside. if ((x1 <= minX && x2 <= minX) || (y1 <= minY && y2 <= minY) || (x1 >= maxX && x2 >= maxX) || (y1 >= maxY && y2 >= maxY)) { return(false); } double m = (y2 - y1) / (x2 - x1); double q = y1 - (m * x1); double x = (minY - q) / m; if ((x > minX && x < maxX) && (minY > y1 && minY < y2)) { //From South newStartingPoint = new Routing.Nodes.Point(minY, x); return(true); } x = (maxY - q) / m; if ((x > minX && x < maxX) && (maxY > y2 && maxY < y1)) { //From North newStartingPoint = new Routing.Nodes.Point(maxY, x); return(true); } double y = (m * minX) + q; if ((y > minY && y < maxY) && (minX > x1 && minX < x2)) { //From East newStartingPoint = new Routing.Nodes.Point(y, minX); return(true); } y = (m * maxX) + q; if ((y > minY && y < maxY) && (maxX > x2 && maxX < x1)) { //From West newStartingPoint = new Routing.Nodes.Point(y, maxX); return(true); } return(false); }
public static List <TrafficReport> getTrafficDataFromBackend(Routing.Nodes.Point MinPoint, Point MaxPoint, double TrafficPropagationMaxDistance) { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3; List <TrafficReport> TrafficReport = new List <TrafficReport> { }; TrafficJson TrafficJson = new TrafficJson { }; string url = reports_boundary + "?" + "min_lat=" + MinPoint.Latitude + "&" + "min_lon=" + MinPoint.Longitude + "&" + "max_lat=" + MaxPoint.Latitude + "&" + "max_lon=" + MaxPoint.Longitude; try { HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url); httpWebRequest.Method = WebRequestMethods.Http.Get; httpWebRequest.Accept = "application/json"; httpWebRequest.Credentials = new NetworkCredential(user, pw); httpWebRequest.UserAgent = "RP client"; HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse(); log.Info(url); if (response != null && response.StatusCode == HttpStatusCode.OK) { Stream stream = response.GetResponseStream(); StreamReader reader = new StreamReader(stream, Encoding.UTF8); String responseString = reader.ReadToEnd(); TrafficJson = JsonConvert.DeserializeObject <TrafficJson>(responseString); ///* ONLY FOR TESTING: add one fake traffic report in "Massagno, Praccio" */ //// Accident in "Massagno Praccio" //TrafficJson.Report testReport = new TrafficJson.Report(); //TrafficJson.reports = new List<TrafficJson.Report>(); //testReport.category = "ACCIDENT"; //testReport.severity = "HIGH"; //testReport._id = "000000000000000000000001"; //testReport.loc = new TrafficJson.Loc(); //testReport.loc.coordinates = new List<double> { 8.943813, 46.01345 }; //testReport.loc.type = "Point"; //TrafficJson.reports.Add(testReport); ///* END TESTING */ ///* ONLY FOR TESTING: add one fake traffic report in "Chiasso" */ //// Accident in "Chiasso" //TrafficJson.Report testReport = new TrafficJson.Report(); //TrafficJson.reports = new List<TrafficJson.Report>(); //testReport.category = "ACCIDENT"; //testReport.severity = "MEDIUM"; //testReport._id = "000000000000000000000001"; //testReport.loc = new TrafficJson.Loc(); //testReport.loc.coordinates = new List<double> { 9.022495, 45.839520 }; //testReport.loc.type = "Point"; //TrafficJson.reports.Add(testReport); ///* END TESTING */ if ((TrafficJson == null) || (TrafficJson.reports.Count == 0)) { log.Warn("Traffic: no data available from backend"); } else { foreach (TrafficJson.Report el in TrafficJson.reports) { TrafficReport Traffic = new TrafficReport(el._id, new Point(el.location.geometry.coordinates.Last(), el.location.geometry.coordinates.First()), el.category, el.severity, el.location.address, TrafficPropagationMaxDistance); TrafficReport.Add(Traffic); } } } else { log.Error("An error occured while calling the traffic data service: StatusCode=" + response.StatusCode); return(null); } } catch (Exception ex) { log.Error("An error occured during the traffic data get: " + ex.ToString()); return(null); } if (TrafficReport != null) { if (TrafficReport.Count == 0) { log.Warn(TrafficReport.Count + " traffic reports received"); } else { log.Info(TrafficReport.Count + " traffic reports received"); } } return(TrafficReport); }
private static readonly string pw = ""; /* PW */ public static List <Carpooler> getCarpoolingDataFromBackend(Routing.Nodes.Point MinPoint, Point MaxPoint, bool updateExternalRides) { /* Set this to true if you want to use real waypoints instead of intermediate ptstops, remember to also change the use_waypoints value in the CarpoolParser.cs file */ bool use_waypoints = false; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3; List <Carpooler> CarpoolRides = new List <Carpooler> { }; CarpoolerJson Carpooler = new CarpoolerJson { }; string url = rides_boundary + "?" + "min_lat=" + MinPoint.Latitude + "&" + "min_lon=" + MinPoint.Longitude + "&" + "max_lat=" + MaxPoint.Latitude + "&" + "max_lon=" + MaxPoint.Longitude + "&" + "site=" + Program.site; try { HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url); httpWebRequest.Method = WebRequestMethods.Http.Get; httpWebRequest.Accept = "application/json"; httpWebRequest.Credentials = new NetworkCredential(user, pw); httpWebRequest.UserAgent = "RP client"; HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse(); log.Info(url); if (response != null && response.StatusCode == HttpStatusCode.OK) { Stream stream = response.GetResponseStream(); StreamReader reader = new StreamReader(stream, Encoding.UTF8); String responseString = reader.ReadToEnd(); Carpooler = JsonConvert.DeserializeObject <CarpoolerJson>(responseString); if ((Carpooler == null) || (Carpooler.rides.Count == 0)) { log.Warn("Carpooling: no data available from backend"); } else { int numExtRidesIgnored = 0; foreach (CarpoolerJson.Ride el in Carpooler.rides) { // Just for testing if the carpooling rides are expired if (el.date < Program.DynamicData.sites.First().carpooling_info.nightly_updated) { log.Error("Ride should be expired but it is present el.date:" + el.date + " nightly_timestampVersion:" + Program.DynamicData.sites.First().carpooling_info.nightly_updated); } //!!!DEBUG EDIMBURGO!!! //if ((String.Compare(el._id, "5976e793e04bd56a9a84ffa8") == 0) || // (String.Compare(el._id, "1dfd2cd0faebcf1b768a77bd") == 0) ) //{ //!!!DEBUG TICINO!!! //if ((String.Compare(el.name, "Test Milano Lugano 01") == 0) || // (String.Compare(el.name, "Test Biella Lugano 01") == 0) || // (String.Compare(el.name, "Test Cavaglietto Lugano 01") == 0) || // (String.Compare(el.name, "Test Biasca Lugano 01") == 0) || // (String.Compare(el.name, "Test Airolo Lugano 01") == 0) || // (String.Compare(el.name, "Test Bergamo Lugano 01") == 0) || // (String.Compare(el._id, "5d85a82ea07e1160a5669e29") == 0)) //{ IEnumerable <Routing.Nodes.Point> Points = GooglePoints.Decode(el.polyline.Replace(@"\\", @"\")); //List<Routing.Nodes.Point> Points = GooglePoints.DecodePolylinePoints(el.polyline.Replace(@"\\", @"\")); //List<Routing.Nodes.Point> Points = GooglePoints.decodePoly(el.polyline); //string test = GooglePoints.DecodeLocations(el.polyline.Replace(@"\\", @"\")); //foreach (Point p in Points) //log.Info(p.Latitude + ", " + p.Longitude); string startDate = Globals.GetDateFromTimestamp(el.date); SourceCPRide provider = SourceCPRide.Unknown; if (el.extras != null) { provider = SourceCPRide.External; } else { provider = SourceCPRide.SocialCar; } if ((provider == SourceCPRide.SocialCar) || ((provider == SourceCPRide.External) && (updateExternalRides == true))) { Carpooler Pooler = new Carpooler(el._id, el.name, Globals.GetLocalTimeSinceMidnight(el.date), int.MaxValue, el.activated, startDate, provider); /* Add start point to the ride */ Pooler.WayPointsOrig.Add(new Point(el.start_point.lat, el.start_point.lon)); Pooler.WayPointsUsed.Add(new Point(el.start_point.lat, el.start_point.lon)); /* Add all waypoints to the ride (THIS DOES NOT WORK SINCE THE WAYPOINTS DON'T MATCH WITH OUR NETWORK )*/ if (use_waypoints == true) { //Pooler.WayPointsTmpList.Add(new Point(el.start_point.lat, el.start_point.lon)); //foreach (Point p in Points) //{ // Pooler.WayPointsOrig.Add(p); // Pooler.WayPointsUsed.Add(p); //} } /* Add end point to the ride */ Pooler.WayPointsUsed.Add(new Point(el.end_point.lat, el.end_point.lon)); Pooler.WayPointsOrig.Add(new Point(el.end_point.lat, el.end_point.lon)); CarpoolRides.Add(Pooler); } else { if (updateExternalRides == false) { numExtRidesIgnored++; } } //} } if (updateExternalRides == false) { log.Info("updateExternalRides:" + updateExternalRides + " Ignored:" + numExtRidesIgnored + " external rides"); } } } else { log.Error("An error occured while calling the carpooling data service: StatusCode=" + response.StatusCode); return(null); } } catch (Exception ex) { log.Error("An error occured during the carpooling data get: " + ex.ToString()); return(null); } if (CarpoolRides != null) { if (CarpoolRides.Count == 0) { log.Warn(CarpoolRides.Count + " carpooling rides received"); } else { log.Info(CarpoolRides.Count + " carpooling rides received"); } } return(CarpoolRides); }