Exemple #1
0
        internal static BusRouteStop[] GetBusStops(BusRoute route, string csv)
        {
            if (csv == null)
            {
                return(null);
            }

            string[] lines     = csv.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
            string[] stopNames = lines[0].Split(',');

            bool multipleRoutes = false;

            BusStop[] stops = new BusStop[stopNames.Length];
            for (int i = 0; i < stopNames.Length; i++)
            {
                if (string.Compare(stopNames[i], "route", true) == 0)
                {
                    multipleRoutes = true;
                    continue;
                }

                //Parse individual stop
                //int lastDash = stopNames[i].LastIndexOf('-')
                string[] stopArgs = stopNames[i].Split(new string[] { " - " }, StringSplitOptions.RemoveEmptyEntries);
                if (stopArgs.Length < 2)
                {
                    continue;
                }

                string crossSt    = HttpUtility.HtmlDecode(stopArgs[stopArgs.Length > 2 ? 1 : 0].Trim('*', ' '));
                string stopName   = HttpUtility.HtmlDecode(stopArgs.Length > 2 ? stopArgs[0].Trim('*', ' ') : crossSt);
                string stopNumStr = stopArgs[stopArgs.Length - 1].Trim('*', ' ');

                int stopNum = -1;
                if (stopNumStr.StartsWith("Stop #"))
                {
                    int.TryParse(stopNumStr.Substring(6), out stopNum);
                }

                BusStop stop = new BusStop(stopName, crossSt, stopNum);
                stops[i] = stop;
            }

            BusRouteStop[] routeStops = new BusRouteStop[(lines.Length - 1) * stops.Length];
            for (int i = 1; i < lines.Length; i++)
            {
                //string routeTimes
                string[] times = lines[i].Split(',');
                if (multipleRoutes)
                {
                    string routeNameStr = times[0];
                    if (string.Compare(routeNameStr, route.RouteName) != 0)
                    {
                        continue;
                    }
                }

                for (int t = 0; t < times.Length; t++)
                {
                    string time = times[t].Length > 8 ? times[t].Substring(0, times[t].LastIndexOf(' ')) : times[t];
                    time = time.Replace("\'B\'", "").Replace("\'AB\'", "").Replace("\'H\'", "").Trim('\'').Trim(' ');
                    if (time.Contains("---") || time.Length > 8 || time.Length < 4)
                    {
                        continue;
                    }

                    DateTime?date = null;
                    try
                    {
                        date = DateTime.ParseExact(time, "h:mm tt", CultureInfo.InvariantCulture);
                    }
                    catch
                    {
                        try
                        {
                            date = DateTime.ParseExact(time, "h:mm", CultureInfo.InvariantCulture);
                        }
                        catch
                        {
                        }
                    }

                    if (date.HasValue)
                    {
                        TimeSpan     eta       = date.Value.TimeOfDay;
                        BusRouteStop routeStop = new BusRouteStop(route, stops[t], eta, t);
                        routeStops[(i - 1) * times.Length + t] = routeStop;
                    }
                }
            }

            return(routeStops);
        }
Exemple #2
0
        internal static string GenerateRouteInserts(List <BusRoute> routes)
        {
            //Get list of stops
            List <BusStop> stops        = new List <BusStop>();
            List <int>     availableIds = Enumerable.Range(10000, 89999).ToList();

            for (int i = 0; i < routes.Count; i++)
            {
                for (int j = 0; j < routes[i].Stops.Length; j++)
                {
                    if (routes[i].Stops[j] != null)
                    {
                        BusStop curStop = routes[i].Stops[j].Stop;
                        if (curStop != null && !stops.Contains(curStop))
                        {
                            stops.Add(curStop);
                            if (curStop.StopID != -1)
                            {
                                availableIds.Remove(curStop.StopID);
                            }
                        }
                    }
                }
            }

            //Give stops without an ID an ID.
            Random rng = new Random();

            for (int j = 0; j < stops.Count; j++)
            {
                if (stops[j].StopID == -1)
                {
                    int index    = rng.Next(availableIds.Count);
                    int uniqueId = availableIds[index];
                    availableIds.RemoveAt(index);
                    stops[j].StopID = uniqueId;
                }
            }

            StringBuilder bldr = new StringBuilder();

            bldr.AppendLine("-- Insert routes");
            for (int i = 0; i < routes.Count; i++)
            {
                bldr.AppendFormat("INSERT INTO BUS_ROUTE Values (\"{0}\", \"{1}\", \"{2}\", {3});\r\n",
                                  routes[i].ToName,
                                  routes[i].FromName,
                                  routes[i].RouteNumber,
                                  routes[i].DaysOfOperation
                                  );
            }

            bldr.AppendLine();
            bldr.AppendLine("-- Insert stops");
            for (int i = 0; i < stops.Count; i++)
            {
                bldr.AppendFormat("INSERT INTO BUS_STOP Values ({0}, \"{1}\", \"{2}\");\r\n",
                                  stops[i].StopID,
                                  stops[i].Name,
                                  stops[i].CrossStreet
                                  );
            }

            bldr.AppendLine();
            bldr.AppendLine("-- Insert route stops");
            HashSet <string> routesInserted = new HashSet <string>();

            for (int i = 0; i < routes.Count; i++)
            {
                for (int j = 0; j < routes[i].Stops.Length; j++)
                {
                    BusRoute     route     = routes[i];
                    BusRouteStop routeStop = route?.Stops[j];
                    BusStop      stop      = routeStop?.Stop;
                    if (routeStop != null && stop != null)
                    {
                        if (stop.StopID == -1)
                        {
                            stop = stops.Find(t => t.Equals(stop));
                        }

                        if (stop != null)
                        {
                            string routeStr = string.Format("INSERT INTO BUS_ROUTE_STOPS Values (\"{0}\", \"{1}\", \"{2}\", {3}, \"{4}\", {5});",
                                                            route.FromName,
                                                            route.ToName,
                                                            route.RouteNumber,
                                                            stop.StopID,
                                                            new DateTime(routeStop.ETA.Ticks).ToString("hh:mm:sss"),
                                                            routeStop.StopNumber
                                                            );
                            if (!routesInserted.Contains(routeStr))
                            {
                                routesInserted.Add(routeStr);
                                bldr.AppendLine(routeStr);
                            }
                        }
                    }
                }
            }

            return(bldr.ToString());
        }