FindCity() public method

public FindCity ( string cName ) : City
cName string
return City
Example #1
0
        /// <summary>
        /// Reads a list of links from the given file.
        /// Reads only links where the cities exist.
        /// </summary>
        /// <param name="filename">name of links file</param>
        /// <returns>number of read route</returns>
        public int ReadRoutes(string filename)
        {
            try {
                using (TextReader reader = new StreamReader(filename))
                {
                    traceSource.TraceInformation("Read Routes started");
                    traceSource.Flush();
                    IEnumerable <string[]> linkAsString = reader.GetSplittedLines('\t');
                    foreach (string[] ls in linkAsString)
                    {
                        City city1 = cities.FindCity(ls[0]);
                        City city2 = cities.FindCity(ls[1]);

                        // only add links, where the cities are found
                        if ((city1 != null) && (city2 != null))
                        {
                            routes.Add(new Link(city1, city2, city1.Location.Distance(city2.Location),
                                                TransportModes.Rail));
                        }
                    }
                    traceSource.TraceInformation("Read Routes ended");
                    traceSource.Flush();
                    return(Count);
                }
            }
            catch (FileNotFoundException e)
            {
                traceSourceErrors.TraceData(TraceEventType.Critical, 1, e.StackTrace);
                traceSource.Flush();
            }
            return(-1);
        }
Example #2
0
        /// <summary>
        /// Reads a list of links from the given file.
        /// Reads only links where the cities exist.
        /// </summary>
        /// <param name="filename">name of links file</param>
        /// <returns>number of read route</returns>
        public int ReadRoutes(string filename)
        {
            source.TraceEvent(TraceEventType.Information, 42, "ReadRoutes started");

            using (TextReader reader = new StreamReader(filename))
            {
                IEnumerable <string[]> citiesAsStrings = reader.GetSplittedLines('\t');

                foreach (var route in reader.GetSplittedLines('\t'))
                {
                    City city1 = cities.FindCity(route[0]);
                    City city2 = cities.FindCity(route[1]);

                    // only add links, where the cities are found
                    if ((city1 != null) && (city2 != null))
                    {
                        routes.Add(new Link(city1, city2, city1.Location.Distance(city2.Location),
                                            TransportModes.Rail));
                    }
                }
            }

            source.TraceEvent(TraceEventType.Information, 42, "ReadRoutes ended");
            source.Flush();
            return(Count);
        }
Example #3
0
        public void TestTask1FindCityInCities()
        {
            var expectedCity = "Zürich";
            Cities cities = new Cities();
            cities.ReadCities(CitiesTestFile);

            var notFound = cities.FindCity("noCity");
            Assert.IsNull(notFound);
            var found = cities.FindCity(expectedCity);
            Assert.AreEqual(expectedCity, found.Name);

            // should work case insensitive
            found = cities.FindCity("züRicH");
            Assert.AreEqual(expectedCity, found.Name);
        }
Example #4
0
        /// <summary>
        /// Reads a list of links from the given file.
        /// Reads only links where the cities exist.
        /// </summary>
        /// <param name="filename">name of links file</param>
        /// <returns>number of read route</returns>
        public int ReadRoutes(string filename)
        {
            RoutesLogger.TraceEvent(TraceEventType.Information, 3, "ReadRoutes started");
            try
            {
                TextReader reader = new StreamReader(filename);

                foreach (var line in reader.GetSplittedLines('\t'))
                {
                    City city1 = Cities.FindCity(line[0].Trim(' '));
                    City city2 = Cities.FindCity(line[1].Trim(' '));

                    // only add links, where the cities are found
                    if ((city1 != null) && (city2 != null))
                    {
                        Links.Add(new Link(city1, city2, city1.Location.Distance(city2.Location), TransportModes.Rail));
                    }
                }
            }
            catch (FileNotFoundException e)
            {
                RoutesLogger.TraceEvent(TraceEventType.Critical, 9, e.ToString());
            }

            RoutesLogger.TraceEvent(TraceEventType.Information, 4, "ReadRoutes ended");
            return(Count);
        }
Example #5
0
        public void TestCorrectIndexingOfCities()
        {
            const int readCitiesExpected = 10;
            var cities = new Cities();

            Assert.AreEqual(readCitiesExpected, cities.ReadCities(CitiesTestFile));

            City from = cities.FindCity("Mumbai");
            City to = cities.FindCity("Istanbul");
            List<City> foundCities = cities.FindCitiesBetween(from, to);

            // verify that Index property is initialized
            int i = 0;
            foreach (var city in foundCities)
            {
                Assert.AreEqual(i, city.Index);
                i++;
            }
        }
Example #6
0
        /// <summary>
        /// Reads a list of links from the given file.
        /// Reads only links where the cities exist.
        /// </summary>
        /// <param name="filename">name of links file</param>
        /// <returns>number of read route</returns>
        public int ReadRoutes(string filename)
        {
            using (TextReader reader = new StreamReader(filename))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    var linkAsString = line.Split('\t');

                    City city1 = cities.FindCity(linkAsString[0]);
                    City city2 = cities.FindCity(linkAsString[1]);

                    // only add links, where the cities are found
                    if ((city1 != null) && (city2 != null))
                    {
                        routes.Add(new Link(city1, city2, city1.Location.Distance(city2.Location),
                                            TransportModes.Rail));
                    }
                }
            }

            return(Count);
        }
Example #7
0
        /// <summary>
        /// Reads a list of links from the given file.
        /// Reads only links where the cities exist.
        /// </summary>
        /// <param name="filename">name of links file</param>
        /// <returns>number of read route</returns>
        public int ReadRoutes(string filename)
        {
            logger.TraceInformation("ReadRoutes started");
            using (TextReader reader = new StreamReader(filename))
            {
                List <string[]> existingCities = reader.GetSplittedLines('\t')
                                                 .Where(s => cities.FindCity(s[0]) != null && cities.FindCity(s[1]) != null)
                                                 .ToList();

                List <City[]> cits = existingCities.Select(str =>
                                                           str.Select(
                                                               s => cities.FindCity(s))
                                                           .ToArray()
                                                           ).ToList();

                List <Link> links = cits.Select(
                    la => new Link(la[0], la[1], la[0].Location.Distance(la[1].Location), TransportModes.Rail))
                                    .ToList();

                routes.AddRange(links);
            }
            logger.TraceInformation("ReadRoutes ended");
            return(Count);
        }
        public override List <Link> FindShortestRouteBetween(string fromCity, string toCity,
                                                             TransportModes mode, IProgress <string> reportProgress)
        {
            List <City> citiesBetween = Cities.FindCitiesBetween(Cities.FindCity(fromCity), Cities.FindCity(toCity));

            if (citiesBetween == null || citiesBetween.Count < 1)
            {
                return(null);
            }

            List <Link> links = FindAllLinks(citiesBetween, mode);

            if (links == null || links.Count < 1)
            {
                return(null);
            }

            /*var stopWatch = new Stopwatch();
             * stopWatch.Start();
             * long ts0=stopWatch.ElapsedMilliseconds;*/

            Setup(citiesBetween, links);

            City source = FindCity(fromCity, citiesBetween);
            City target = FindCity(toCity, citiesBetween);

            if (D[source.Index, target.Index] == Double.MaxValue)
            {
                return(new List <Link>()); // no path between source and target
            }
            List <City> path = GetIntermediatePath(source, target);

            // must construct route from path
            var route = new List <Link>();

            route.Add(new Link(source, path.ElementAt(0), D[source.Index, path.ElementAt(0).Index]));
            for (var i = 0; i < path.Count - 1; i++)
            {
                City from = path.ElementAt(i);
                City to   = path.ElementAt(i + 1);
                route.Add(new Link(from, to, D[from.Index, to.Index]));
            }
            route.Add(new Link(path.ElementAt(path.Count - 1), target,
                               D[path.ElementAt(path.Count - 1).Index, target.Index]));
            return(route);
        }
        public override List <Link> FindShortestRouteBetween(string fromCity, string toCity, TransportModes mode,
                                                             IProgress <string> reportProgress)
        {
            NotifyObservers(this, new RouteRequestEventArgs(fromCity, toCity, mode));
            ProgressReport(reportProgress, "<notified observers> done");

            var citiesBetween = Cities.FindCitiesBetween(Cities.FindCity(fromCity), Cities.FindCity(toCity));

            ProgressReport(reportProgress, "<found cities between> done");

            if (citiesBetween == null || citiesBetween.Count < 1 || Links == null || Links.Count < 1)
            {
                return(null);
            }

            var source = citiesBetween[0];
            var target = citiesBetween[citiesBetween.Count - 1];

            Dictionary <City, double> dist;
            Dictionary <City, City>   previous;
            var q = FillListOfNodes(citiesBetween, out dist, out previous);

            ProgressReport(reportProgress, "<created nodes> done");
            dist[source] = 0.0;

            // the actual algorithm
            previous = SearchShortestPath(mode, q, dist, previous);
            ProgressReport(reportProgress, "<search shortest path> done");

            // create a list with all cities on the route
            var citiesOnRoute = GetCitiesOnRoute(source, target, previous);

            ProgressReport(reportProgress, "<got cities on route> done");

            // prepare final list if links
            return(FindPath(citiesOnRoute, mode));
        }
        static void Main(string[] args)
        {
            Version version = Assembly.GetEntryAssembly().GetName().Version;
            Console.WriteLine("Welcome to RoutePlanner ({0})", version);


            WayPoint wayPoint = new WayPoint("Windisch", 47.479319847061966, 8.212966918945312);
            Console.WriteLine(wayPoint.ToString());
            WayPoint wpBern = new WayPoint("Bern", 46.9472221, 7.451202500000022);
            WayPoint wpTripolis = new WayPoint("Tripolis", 59.86062519999999, 17.650885199999948);
            Console.WriteLine("Distanz Bern-Tripolis: {0}km", wpBern.Distance(wpTripolis));

            City cBern = new City("Bern", "Schweiz", 75000, 47.479319847061966, 8.212966918945312);
            City c0 = new City("Mumbai", "India", 12383146, 18.96, 72.82);

            string serializedCity = string.Empty;
            using (StringWriter outstream = new StringWriter())
            {
                SimpleObjectWriter writer = new SimpleObjectWriter(outstream);
                writer.Next(cBern);
                serializedCity = outstream.ToString();
            }
            Console.WriteLine(serializedCity);

            using(StringReader inStream = new StringReader(serializedCity)) {
                SimpleObjectReader reader = new SimpleObjectReader(inStream);
                object o = reader.Next();
            }
        

            WayPoint wp = c0.Location;
            Cities c = new Cities();
            c.ReadCities("citiesTestDataLab2.txt");
            c.FindNeighbours(wp,2000);
            c.ReadCities("citiesTestDataLab2.txt");

            var routes = new RoutesDijkstra(c);
            var reqWatch = new RouteRequestWatcher();
            routes.RouteRequestEvent += reqWatch.LogRouteRequests;
            routes.FindShortestRouteBetween("Mumbai", "India", TransportModes.Rail);
            routes.FindShortestRouteBetween("Mumbai", "India", TransportModes.Rail);
            routes.FindShortestRouteBetween("India", "Mumbai", TransportModes.Rail);

            Console.WriteLine("City found: {0}", c.FindCity("Mumbai").Name);

            c.ReadCities("citiesTestDataLab4.txt");
            Routes r = new RoutesDijkstra(c);
            r.RouteRequestEvent += reqWatch.LogRouteRequests;
            r.ReadRoutes("linksTestDataLab4.txt");
            List<Link> l = r.FindShortestRouteBetween("Zürich", "Winterthur", TransportModes.Rail);
            foreach (Link link in l)
            {
                Console.WriteLine("from {0} to {1} in {2}", link.FromCity.Name, link.ToCity.Name, link.Distance);
            }
            Console.ReadKey();

            City zurich = c.FindCity("Zürich");
            City winterthur = c.FindCity("Winterthur");
            ExcelExchange export = new ExcelExchange();
            export.WriteToFile("Test.xls", zurich, winterthur,l);
            
        }