using System; using System.Collections.Generic; //using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Drawing; using System.Text; using System.IO; using BusScheduler; public partial class _Default : System.Web.UI.Page { public TransportSystem aTransportSystem = new TransportSystem(); System.Text.StringBuilder sb = new System.Text.StringBuilder(); public List allRoutes = new List(); public Route aRoute; public TripPattern pattern1 = new TripPattern(); public TripPattern aTripPattern; protected void Page_Load(object sender, EventArgs e) { checkMobile(); //read from the file TextReader fileReader = new StreamReader(System.AppDomain.CurrentDomain.BaseDirectory.ToString() + "\\Bus Schedule2.csv"); try { string title = fileReader.ReadLine(); title = fileReader.ReadLine(); string[] bits = title.Split(','); aTransportSystem.name = bits[0]; lblTitle.Text = aTransportSystem.name + " Scheduler"; lblTitle.Font.Size = 30; string line = fileReader.ReadLine(); string line2 = fileReader.ReadLine(); string line3 = fileReader.ReadLine(); bits = line3.Split(','); while (bits[0] != "") { BusScheduler.Location aLocation = new BusScheduler.Location(); aLocation.name = bits[0].Replace("\"", ""); aLocation.address = bits[1]; aLocation.type = bits[2]; aLocation.latitude = double.Parse(bits[3]); aLocation.longitude = double.Parse(bits[4]); aTransportSystem.cLocation.Add(aLocation); line3 = fileReader.ReadLine(); bits = line3.Split(','); //add for the combo box ListItem addList = new ListItem(); string location_name = aLocation.name.Replace("\"", ""); string latlong = aLocation.latitude + "~" + aLocation.longitude; if (!IsPostBack) { addList.Text = location_name; addList.Value = latlong; if (aLocation.type == "Time Point") lstTimePoint.Items.Add(addList); else if (aLocation.type == "Bus Stop") lstBusStop.Items.Add(addList); else if (aLocation.type == "Interchange") lstInterchange.Items.Add(addList); } } line = fileReader.ReadLine(); bits = line.Split(','); while (bits[0] != "" && bits[0] != "Bus Reroute") { List table = new List(); aRoute = new Route(); aRoute.name = bits[0]; line2 = fileReader.ReadLine(); line3 = fileReader.ReadLine(); bits = line3.Split(','); while (bits[0] != "") { table.Add(bits); line3 = fileReader.ReadLine(); if (line3 == null) { bits = "".Split(','); } else { bits = line3.Split(','); } } List cLocation = new List(); List c2ArrivalTime = new List(); List c1WaitTime = new List(); string[] firstRow = table[0]; List firstList = new List(); firstList.AddRange(firstRow); int index = firstList.FindIndex( delegate(string str) { return str == ""; }); int n1TripPattern; if (index > 0) { n1TripPattern = index / 5; //5 = arrival time, wait time, repeat time, first repeat, ;last repeat } else { n1TripPattern = firstList.Count / 5; } for (int i = 0; i < n1TripPattern; i++) { aTripPattern = new TripPattern(); foreach (string[] cString in table) { if (i == 0) { string locationName = cString[0]; BusScheduler.Location aLocation = aTransportSystem.aLocationAt(locationName); cLocation.Add(aLocation); } if (cString[1] != "") { int offset = 5 * i; BusScheduler.Time aArrivalTime = ConvertTime2(cString[offset + 1]); BusScheduler.Time aWaitTime = ConvertTime2(cString[offset + 2]); if (cString[offset + 3] != "") { aTripPattern.repeatMinutes = ConvertTime2(cString[offset + 3]); aTripPattern.firstRepeatTime = ConvertTime(cString[offset + 4]); aTripPattern.lastRepeatTime = ConvertTime(cString[offset + 5]); } aTripPattern.c1ArrivalTime.Add(aArrivalTime); aTripPattern.cWaitTime.Add(aWaitTime); } } aRoute.addTripPattern(aTripPattern); } aRoute.cLocation = cLocation; allRoutes.Add(aRoute); line3 = fileReader.ReadLine(); if (line3 == null) { bits = "".Split(','); } else { bits = line3.Split(','); } } foreach (Route r in allRoutes) { r.setcTrip(); } line = fileReader.ReadLine(); line3 = fileReader.ReadLine(); bits = line3.Split(','); while (bits[0] != "") { foreach (Route r1 in allRoutes) { if (r1.name.IndexOf(bits[0]) != -1) { foreach (Route r2 in allRoutes) { if (r2.name.IndexOf(bits[1]) != -1) { BusScheduler.Routing route = new BusScheduler.Routing(); route.from = r1; route.to = r2; aTransportSystem.cBusRoute.Add(route); } } } } line3 = fileReader.ReadLine(); if (line3 == null) { bits = "".Split(','); } else { bits = line3.Split(','); } } string[] Items = new string[aTransportSystem.cLocation.Count]; for (int i = 0; i < aTransportSystem.cLocation.Count; i++) { Items[i] = aTransportSystem.cLocation[i].name; } } catch { } fileReader.Close(); if (!IsPostBack) { drawMarker(""); } } public BusScheduler.Time ConvertTime2(string time) { BusScheduler.Time newTime = new BusScheduler.Time(); newTime.setTime(Convert.ToDouble(time)); return newTime; } public BusScheduler.Time ConvertTime(string time) { BusScheduler.Time newTime = new BusScheduler.Time(); string[] splitTime = time.Split(':'); newTime.setTime(Convert.ToDouble(splitTime[0].Trim()) * 60 + Convert.ToDouble(splitTime[1].Trim())); return newTime; } public void drawMarker(string script) { string javascriptCode = "resetMarker();DrawMap();"; double lat, lng; int type = 0; foreach (BusScheduler.Location location in aTransportSystem.cLocation) { lat = location.latitude; lng = location.longitude; if (location.type == "Time Point") type = 0; else if (location.type == "Bus Stop") type = 1; else if (location.type == "Interchange") type = 2; javascriptCode += "setLatLong(" + lat + "," + lng + ");dragMarker(" + type + ");"; } Page.RegisterStartupScript("myscript", ""); } public string clockTime(BusScheduler.Time min) { string hour = (Convert.ToInt16(min.total) / 60).ToString(); int minutes = (Convert.ToInt16(min.total) % 60); string digits; if (minutes < 10) { digits = "0" + minutes.ToString(); } else { digits = minutes.ToString(); } string clockTime = hour + ":" + digits; return clockTime; } protected void btnGetDirection_Click(object sender, EventArgs e) { string textFrom = txtFrom.Text; string textTo = txtTo.Text; string fromScript = "", toScript = ""; string[] CoorFrom; string[] CoorTo; double distance1 = 0, distance2 = 0; BusScheduler.Location NearestlocationFrom = new BusScheduler.Location(); BusScheduler.Location NearestlocationTo = new BusScheduler.Location(); lblNotification.Text = ""; //lblWalkWarning.Visible = false; if (textFrom != "" && textTo != "") { CoorFrom = textFrom.Split(','); CoorTo = textTo.Split(','); double result1, result2; if (CoorFrom.Length == 2 && double.TryParse(CoorFrom[0], out result1) && double.TryParse(CoorFrom[1], out result2)) { //double hyp, x, y, hypShortest = 0; //NearestlocationFrom = aTransportSystem.cLocation[0]; //x = NearestlocationFrom.latitude - result1; //y = NearestlocationFrom.longitude - result2; //hypShortest = Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)); ////find the nearest point //foreach (BusScheduler.Location location in aTransportSystem.cLocation) //{ // x = location.latitude - result1; // y = location.longitude - result2; // hyp = Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)); // if (hyp < hypShortest) // { // NearestlocationFrom = location; // hypShortest = hyp; // } //} //distance1 = distanceCalculator(result1, result2, NearestlocationFrom.latitude, NearestlocationFrom.longitude); //int distanceMeter1 = Convert.ToInt16(distance1 * 1000); //fromScript = "Walk " + distanceMeter1 + "m to " + NearestlocationFrom.name + " station."; //lblWalkWarning.Visible = true; } else { foreach (BusScheduler.Location location in aTransportSystem.cLocation) { if (textFrom.Trim() == location.name.Trim()) { NearestlocationFrom = location; break; } } } if (CoorTo.Length == 2 && double.TryParse(CoorTo[0], out result1) && double.TryParse(CoorTo[1], out result2)) { //double hyp, x, y, hypShortest = 0; //NearestlocationTo = aTransportSystem.cLocation[0]; //x = NearestlocationTo.latitude - result1; //y = NearestlocationTo.longitude - result2; //hypShortest = Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)); ////find the nearest point //foreach (BusScheduler.Location location in aTransportSystem.cLocation) //{ // x = location.latitude - result1; // y = location.longitude - result2; // hyp = Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)); // if (hyp < hypShortest) // { // NearestlocationTo = location; // hypShortest = hyp; // } //} //distance2 = distanceCalculator(result1, result2, NearestlocationTo.latitude, NearestlocationTo.longitude); //int distanceMeter2 = Convert.ToInt16(distance2*1000); //toScript = "Walk " + distanceMeter2 + "m to your destination from " + NearestlocationTo.name + " station."; //lblWalkWarning.Visible = true; } else { foreach (BusScheduler.Location location in aTransportSystem.cLocation) { if (textTo.Trim() == location.name.Trim()) { NearestlocationTo = location; break; } } } if (NearestlocationFrom.name != null && NearestlocationTo.name != null) { if (NearestlocationFrom.name != NearestlocationTo.name) findDirection(NearestlocationFrom, NearestlocationTo, fromScript, toScript, distance1, distance2); else { lblNotification.Text = "Please select different From/To location."; drawMarker("setAB();"); } } else { lblNotification.Text = "No such location"; drawMarker("setAB();"); } } else { lblNotification.Text = "Please select From/To location."; drawMarker("setAB();"); } } public BusScheduler.Location startDest, endDest; public int hour, minute; public void findDirection(BusScheduler.Location from, BusScheduler.Location to, string fromScript, string toScript, double Fromdistance, double ToDistance) { bool solutionFound = false; startDest = from; endDest = to; hour = int.Parse(ddlHour.SelectedItem.Text); if (ddlTime.SelectedIndex == 1) hour += 12; minute = int.Parse(ddlMin.SelectedItem.Text); List endRoutes = new List(); foreach (Route r in allRoutes) { if (r.ContainsEnd(endDest)) { endRoutes.Add(r); } } List startRoutes = new List(); foreach (Route r in allRoutes) { if (r.ContainsStart(startDest)) { startRoutes.Add(r); } } List goodRoutes = new List(); foreach (Route r in startRoutes) { if (endRoutes.Contains(r)) { goodRoutes.Add(r); } } //int StepTime = 0; //if (ToDistance > 0) { // //est 1m = 1sec // StepTime = calculateStep(ToDistance); //} TabMenu.Items.Clear(); BusScheduler.Time aArrivalTime = new BusScheduler.Time(); aArrivalTime.setTime(hour * 60 + minute); List goodTrips = new List(); string mapInfo = ""; string[] path = new string[10]; foreach (Route r in goodRoutes) { List startandEndTime = r.startandEndTime(startDest, endDest, aArrivalTime); BusScheduler.Time startTime= new BusScheduler.Time(); BusScheduler.Time endTime = new BusScheduler.Time(); ; if (startandEndTime[1].total < startandEndTime[3].total) { startTime = startandEndTime[2]; endTime = startandEndTime[3]; } else { startTime = startandEndTime[0]; endTime = startandEndTime[1]; } if (startTime.total > 0 & endTime.total > (aArrivalTime.total - 60)) { solutionFound = true; path[0] = r.getLocationIndex(startDest, endDest, startTime, endTime); mapInfo = r.name + "~" + startDest.name + "~" + clockTime(startTime) + "~" + endDest.name + "~" + clockTime(endTime) + "~" + path[0]; addDirection(mapInfo); } } if (goodRoutes.Count == 0) { foreach (Route rs in startRoutes) { foreach (Route re in endRoutes) { try { BusScheduler.Location changeDest = re.joinStartandEnd(rs, startDest, endDest); List startandEndTime = re.startandEndTime(changeDest, endDest, aArrivalTime); BusScheduler.Time getOnTime = startandEndTime[0]; BusScheduler.Time arriveTime = startandEndTime[1]; startandEndTime = rs.startandEndTime(startDest, changeDest, getOnTime); BusScheduler.Time startTime = startandEndTime[0]; BusScheduler.Time getOffTime = startandEndTime[1]; if ((startTime.total > 0) & (arriveTime.total - startTime.total < 180) & arriveTime.total > (aArrivalTime.total - 60)) { solutionFound = true; path[0] = rs.getLocationIndex(startDest, changeDest, startTime, getOffTime); path[1] = re.getLocationIndex(changeDest, endDest, getOnTime, arriveTime); bool checkPath = aTransportSystem.checkBusTransit(rs, re); mapInfo = rs.name + "~" + startDest.name + "~" + clockTime(startTime) + "~" + endDest.name + "~" + clockTime(arriveTime) + "~" + re.name + "~" + changeDest.name + "~" + clockTime(getOffTime) + "~" + clockTime(getOnTime) + "~" + path[0] + "~" + checkPath + "~" + path[1]; addDirection(mapInfo); } } catch { continue; } } } } if (solutionFound == false) { lblResult.Text = ""; //lblWalkWarning.Visible = false; lblNotification.Text = "The bus doesn't run this early."; drawMarker("setAB();"); } else { lblResult.Text = ""; string select = TabMenu.Items[0].Value; TabMenu.Items[0].Text = "" + TabMenu.Items[0].Text + ""; string script = callDirection(select); drawMarker("ShowResult();DrawPath('" + script + "');"); } } public void addDirection(string info) { MenuItem addList = new MenuItem(); addList.Text = "schedule" + (TabMenu.Items.Count + 1); addList.Value = info; TabMenu.Items.Add(addList); } public void clearTabFormat() { int findFormat; for (int i = 0; i < TabMenu.Items.Count; i++) { findFormat = TabMenu.Items[i].Text.IndexOf(""); if (findFormat > 0) { TabMenu.Items[i].Text = TabMenu.Items[i].Text.Substring(0, TabMenu.Items[i].Text.Length - 7); findFormat = TabMenu.Items[i].Text.LastIndexOf(">"); TabMenu.Items[i].Text = TabMenu.Items[i].Text.Substring(findFormat + 1); } } } protected void TabMenu_MenuItemClick(object sender, MenuEventArgs e) { lblResult.Text = ""; clearTabFormat(); TabMenu.SelectedItem.Text = "" + TabMenu.SelectedItem.Text + ""; string script = callDirection(TabMenu.SelectedItem.Value); drawMarker("ShowResult();DrawPath('" + script + "');"); } public string callDirection(String info) { string[] detail = info.Split('~'); string txtStart = "", txtEnd = "", txtTransOff = "", txtTransOn = ""; string script = ""; /*detail 0: r.name 1: startDest.name 2: clockTime(startTime) 3: endDest.name 4: clockTime(endTime) 5: re.name || for length=6: path[0] 6: changeDest.name 7: clockTime(getOffTime) 8: clockTime(getOnTime) 9: path[0] 10: same bus 11: path[1] */ int index = 0; if (detail.Length == 6) { txtStart = "From: \"" + detail[1] + "\" Departure Time: " + detail[2] + " Route: " + detail[0]; txtEnd = "To: \"" + detail[3] + "\" Arrival Time: " + detail[4]; txtStart = "" + txtStart + "

"; index++; txtEnd = "" + txtEnd + "

"; index++; lblResult.Text += "
" + txtStart + txtEnd; script = detail[5]; //lstResult.Items.Add(txtStart); //lstResult.Items.Add(txtEnd); //draw map //txtStart = "From: " + info[1] + "
Time: " + info[2] + "
Route: " + info[0]; //txtEnd = "To: " + info[3] + "
Time: " + info[4] + "
Route: " + info[0]; //script = "DrawMap('lstStart',0);DrawMap('lstDest',0);DrawDestination( \"" + txtStart + "\" , \"\" , \"" + txtEnd + "\" , \"\" ,\"" + info[5] + "\" ,\"\");"; } else { if (detail[10] == "False") { txtStart = "From: \"" + detail[1] + "\" Departure Time: " + detail[2] + " Route: " + detail[0]; txtTransOff = "Get Off at: \"" + detail[6] + "\" Arrival Time: " + detail[7]; txtTransOn = "Get On from: \"" + detail[6] + "\" Departure Time: " + detail[8] + " Route: " + detail[5]; txtEnd = "To: \"" + detail[3] + "\" Arrival Time: " + detail[4]; } else { txtStart = "From: \"" + detail[1] + "\" Departure Time: " + detail[2] + " Route: " + detail[0]; txtTransOff = "The Bus will stop at: \"" + detail[6] + "\" at time: " + detail[7]; txtTransOn = "The Bus will continue with Route: \"" + detail[4] + "\" at time: " + detail[8]; txtEnd = "To: \"" + detail[3] + "\" Arrival Time: " + detail[4]; } txtStart = " " + txtStart + "

"; index++; txtTransOff = "" + txtTransOff + "

"; index++; txtTransOn = "" + txtTransOn + "

"; index++; txtEnd = "" + txtEnd + "
"; index++; lblResult.Text += "
" + txtStart + txtTransOff + txtTransOn + txtEnd; script = detail[9] + "~" + detail[11]; } hdnNumResult.Value = Convert.ToString(index); return script; } public double distanceCalculator(double FromLat, double FromLng, double ToLat, double ToLng) { double radius = 6371; double lat = ToRad(ToLat - FromLat); double lng = ToRad(ToLng - FromLng); double a = Math.Pow(Math.Sin(lat / 2), 2) + Math.Cos(ToRad(FromLat)) * Math.Cos(ToRad(ToLat)) * Math.Pow(Math.Sin(lng / 2), 2); double b = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a)); return radius * b; } public double ToRad(double degree) { return degree * Math.PI / 180; } public int calculateStep(double distance) { return Convert.ToInt16(distance * 1000) / 60 + 3; } public void checkMobile() { HttpContext context = HttpContext.Current; //http://www.codeproject.com/KB/aspnet/mobiledetect.aspx if (context.Request.ServerVariables["HTTP_USER_AGENT"] != null) { //Create a list of all mobile types string[] mobiles = new[] { "midp", "j2me", "avant", "docomo", "novarra", "palmos", "palmsource", "240x320", "opwv", "chtml", "pda", "windows ce", "mmp/", "blackberry", "mib/", "symbian", "wireless", "nokia", "hand", "mobi", "phone", "cdm", "up.b", "audio", "SIE-", "SEC-", "samsung", "HTC", "mot-", "mitsu", "sagem", "sony" , "alcatel", "lg", "eric", "vx", "NEC", "philips", "mmm", "xx", "panasonic", "sharp", "wap", "sch", "rover", "pocket", "benq", "java", "pt", "pg", "vox", "amoi", "bird", "compal", "kg", "voda", "sany", "kdd", "dbt", "sendo", "sgh", "gradi", "jb", "dddi", "moto", "iphone" }; foreach (string s in mobiles) { if (context.Request.ServerVariables["HTTP_USER_AGENT"].ToLower().Contains(s.ToLower())) { Response.Redirect("DefaultMobile.aspx"); } } } } }