Exemplo n.º 1
0
        static void GenerateGpxFiles(List <Bucket> buckets, string file, int positionFile)
        {
            foreach (var b in buckets)
            {
                if (b.Paths.Any())
                {
                    // Saving the Map to GPX instead of OSM - START
                    var tracks = new List <GPXTrack>();

                    foreach (var p in b.Paths)
                    {
                        GPXTrack track = new GPXTrack(p.Key);
                        foreach (var t in p.Value)
                        {
                            List <GPXPoint> list = new List <GPXPoint>();

                            foreach (var s in t.Segments)
                            {
                                GPXPoint start = new GPXPoint()
                                {
                                    Latitude = s.StartPoint.Latitude, Longitude = s.StartPoint.Longitude
                                };
                                GPXPoint end = new GPXPoint()
                                {
                                    Latitude = s.EndPoint.Latitude, Longitude = s.EndPoint.Longitude
                                };

                                list.Add(start);
                                list.Add(end);
                            }

                            GPXTrackSegment gpxTrack = new GPXTrackSegment(list);
                            track.Segments.Add(gpxTrack);
                        }
                        tracks.Add(track);
                    }

                    var gpx = new GPXDocument()
                    {
                        Tracks = tracks
                    };
                    gpx.Save("mapGpx" + positionFile + ".gpx");
                    // END
                }
            }
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            string osmPath    = "";
            int    eps        = -1;
            int    minTraffic = -1;
            bool   showHelp   = false;

            OptionSet parameters = new OptionSet()
            {
                { "osm=", "path to the map file", v => osmPath = v },
                { "eps=", "size of the eps-neighborhood to be considered (integer)", v => eps = Convert.ToInt32(v) },
                { "minTraffic=", "minimum traffic considered (integer)", v => minTraffic = Convert.ToInt32(v) },
                { "h|?|help", v => showHelp = v != null },
            };

            try
            {
                parameters.Parse(args);
            }
            catch (OptionException e)
            {
                Console.Write("TRoute: ");
                Console.WriteLine(e.Message);
                Console.WriteLine("Try `TRoute --help' for more information.");
                return;
            }

            if (showHelp || string.IsNullOrEmpty(osmPath) || eps < 0 || minTraffic < 0)
            {
                ShowHelp(parameters);
                return;
            }

            var osmFile = new OSMDB();

            osmFile.Load(osmPath);
            var roadGraph = new RoadGraph();

            roadGraph.Build(osmFile);

            // Getting all the nodes with traffic signals
            Dictionary <double, OSMNode> tagsNodes = new Dictionary <double, OSMNode>();

            foreach (var node in osmFile.Nodes)
            {
                foreach (var tag in node.Tags)
                {
                    if (tag.Value.Equals("traffic_signals"))
                    {
                        if (!tagsNodes.Keys.Contains(node.Latitude + node.Longitude))
                        {
                            tagsNodes.Add(node.Latitude + node.Longitude, node);
                        }
                    }
                }
            }

            var hotRoutes = new FlowScan().Run(roadGraph, eps, minTraffic);

            // Saving GPX file of the Hot Route
            HashSet <GPXPoint>        listPoints;
            HashSet <GPXTrackSegment> listSegments;
            GPXTrackSegment           segTrack;

            List <GPXTrack> track = new List <GPXTrack>();
            GPXTrack        tr;

            //Console.WriteLine(hotRoutes.Count);
            foreach (var hr in hotRoutes)
            {
                //Console.WriteLine("Number segs: " + hr.Segments.Count);
                listSegments = new HashSet <GPXTrackSegment>();

                foreach (var seg in hr.Segments)
                {
                    listPoints = new HashSet <GPXPoint>();

                    foreach (var segInner in seg.Geometry.Segments)
                    {
                        GPXPoint start;
                        if (tagsNodes.Keys.Contains(segInner.StartPoint.Latitude + segInner.StartPoint.Longitude))
                        {
                            OSMNode osmNode = tagsNodes[segInner.StartPoint.Latitude + segInner.StartPoint.Longitude];
                            start = new GPXPoint()
                            {
                                Id        = osmNode.ID, Latitude = segInner.StartPoint.Latitude,
                                Longitude = segInner.StartPoint.Longitude, TrafficSignal = true
                            };
                        }
                        else
                        {
                            OSMNode osmNode = osmFile.Nodes.ToList().First(x => x.Latitude == segInner.StartPoint.Latitude &&
                                                                           x.Longitude == segInner.StartPoint.Longitude);
                            start = new GPXPoint()
                            {
                                Id        = osmNode.ID, Latitude = segInner.StartPoint.Latitude,
                                Longitude = segInner.StartPoint.Longitude, TrafficSignal = false
                            };
                        }

                        GPXPoint end;
                        if (tagsNodes.Keys.Contains(segInner.EndPoint.Latitude + segInner.EndPoint.Longitude))
                        {
                            OSMNode osmNode = tagsNodes[segInner.EndPoint.Latitude + segInner.EndPoint.Longitude];
                            end = new GPXPoint()
                            {
                                Id        = osmNode.ID, Latitude = segInner.EndPoint.Latitude,
                                Longitude = segInner.EndPoint.Longitude, TrafficSignal = true
                            };
                        }
                        else
                        {
                            OSMNode osmNode = osmFile.Nodes.ToList().First(x => x.Latitude == segInner.EndPoint.Latitude &&
                                                                           x.Longitude == segInner.EndPoint.Longitude);
                            end = new GPXPoint()
                            {
                                Id        = osmNode.ID, Latitude = segInner.EndPoint.Latitude,
                                Longitude = segInner.EndPoint.Longitude, TrafficSignal = false
                            };
                        }

                        listPoints.Add(start);
                        listPoints.Add(end);
                    }

                    segTrack = new GPXTrackSegment(listPoints, seg.AvgSpeed, seg.Speed, seg.Id);
                    // passing the traffic
                    segTrack.Traffic = seg.Traffic;
                    listSegments.Add(segTrack);
                }

                tr = new GPXTrack();
                tr.Segments.AddRange(listSegments);
                track.Add(tr);
            }

            // Bucket Information
            GPXTrack        tBucket = new GPXTrack();
            GPXPoint        pBucket = new GPXPoint(0, 0, 0, false);
            GPXTrackSegment sBucket = new GPXTrackSegment();

            var bucketInfo = osmFile.Nodes.ToList().Find(x => x.ID == 0);

            if (bucketInfo != null)
            {
                pBucket.StartBucket = TimeSpan.Parse(bucketInfo.Tags.First().Value);
                pBucket.EndBucket   = TimeSpan.Parse(bucketInfo.Tags.Last().Value);
            }

            sBucket.Nodes.Add(pBucket);
            tBucket.Segments.Add(sBucket);

            var gpx = new GPXDocument()
            {
                Tracks = track
            };

            gpx.Tracks.Add(tBucket);
            gpx.Save("mapWithHotRoutes.gpx");
        }
Exemplo n.º 3
0
        static void ProcessCSVFile(string path, string outputPath, int samplingPeriod)
        {
            double max_lat = -3.691978693, min_lat = -3.8881242275, max_lon = -38.4018707275, min_lon = -38.6374359131; //geo-bounding box for Fortaleza

            GPXDocument gpx = new GPXDocument();

            Console.Write("Parsing {0} ...", Path.GetFileName(path));

            string line;

            char[] seps = { ';', ',', '\t' };
            char   sep  = '.';

            String[] fields = { };

            long last_id = -1;

            int      id_field   = 0;
            int      lat_field  = 1;
            int      lon_field  = 2;
            int      time_field = 7;
            int      uf1_field  = 3;
            int      uf2_field  = 4;
            int      uf3_field  = 5;
            int      uf4_field  = 6;
            Boolean  header     = true;
            GPXTrack trk        = null;

            List <long> ids = new List <long>();

            if (samplingPeriod > 0)
            {
                DateTime dt = File.GetLastWriteTime(path);
                if ((DateTime.Now - dt).Days > samplingPeriod)
                {
                    Console.WriteLine("skiping");
                    return;
                }
            }

            // Read the file and display it line by line.
            System.IO.StreamReader csv = new System.IO.StreamReader(path);

            line = csv.ReadLine();
            if (line != null)
            {
                for (int i = 0; i < seps.Length; i++)
                {
                    if ((fields = line.Split(seps[i])).Length > 1)
                    {
                        sep = seps[i];
                        break;
                    }
                }
                if (sep == '.')
                {
                    throw new Exception(string.Format("Can not find any known separators.{';',',','TAB'}"));
                }

                for (int i = 0; i < fields.Length; i++)
                {
                    if (fields[i] == "id")
                    {
                        id_field = i;
                    }
                    else
                    if (fields[i] == "lat")
                    {
                        lat_field = i;
                    }
                    else
                    if (fields[i] == "lon")
                    {
                        lon_field = i;
                    }
                    else
                    if (fields[i] == "time")
                    {
                        time_field = i;
                    }
                    else
                    if (fields[i] == "uf1")
                    {
                        uf1_field = i;
                    }
                    else
                    if (fields[i] == "uf2")
                    {
                        uf2_field = i;
                    }
                    else
                    if (fields[i] == "uf3")
                    {
                        uf3_field = i;
                    }
                    else
                    if (fields[i] == "uf4")
                    {
                        uf4_field = i;
                    }
                    else
                    {
                        header = false;
                        Console.WriteLine(string.Format("Can not find header line.(id, lat, lon, time)"));
                        csv.Close();
                        csv = new System.IO.StreamReader(path);
                        break;
                    }
                }
            }
            if (!header)
            {
                line = csv.ReadLine();
            }
            while ((line = csv.ReadLine()) != null)
            {
                fields = line.Split(sep);
                long id = Convert.ToInt64(fields[id_field]);
                if (id != last_id)
                {
                    if (ids.Find(item => item == id) == 0)
                    {
                        ids.Add(id);
                    }
                    last_id = id;
                    //break;
                }
            }
            csv.Close();


            foreach (long next_id in ids)
            {
                Console.Write(next_id);

                int count = 0;

                csv = new System.IO.StreamReader(path);
                trk = new GPXTrack();
                GPXTrackSegment trkseg = new GPXTrackSegment();
                gpx.Tracks.Add(trk);
                trk.Name = "trk_" + next_id;
                line     = csv.ReadLine();

                while ((line = csv.ReadLine()) != null)
                {
                    fields = line.Split(sep);
                    long id = Convert.ToInt64(fields[id_field]);
                    if (id == next_id)
                    {
                        if (!(fields[lat_field] == "null" && fields[lon_field] == "null"))
                        {
                            var lat = Double.Parse(fields[lat_field], System.Globalization.CultureInfo.InvariantCulture);
                            var lon = Double.Parse(fields[lon_field], System.Globalization.CultureInfo.InvariantCulture);

                            if (lat > min_lat && lat < max_lat && lon > min_lon && lon < max_lon)
                            {
                                GPXPoint p = new GPXPoint();

                                p.Latitude  = lat;
                                p.Longitude = lon;
                                p.Time      = DateTime.ParseExact(fields[time_field], "ddd MMM dd yyyy HH:mm:ss 'GMT-0300 (BRT)'", System.Globalization.CultureInfo.InvariantCulture);

                                trkseg.Nodes.Add(p);
                                count++;
                            }
                        }
                    }
                }
                trk.Segments.Add(trkseg);
                csv.Close();
                Console.WriteLine("[" + count + "]");
            }

            String fileout = Path.Combine(outputPath, Path.GetFileNameWithoutExtension(path) + ".gpx");

            Console.WriteLine(fileout);
            gpx.Save(fileout);
            Console.WriteLine("\tDone.");
        }