Example #1
0
        /// <summary>
        /// Raises the TrackRead event
        /// </summary>
        /// <param name="node">The track read from the xml</param>
        protected void OnTrackRead(GPXTrack track)
        {
            GPXTrackReadHandler temp = TrackRead;

            if (temp != null)
            {
                temp(track);
            }
        }
Example #2
0
        /// <summary>
        /// Writes the specific track to the output
        /// </summary>
        /// <param name="track">The track to be written</param>
        public void WriteTrack(GPXTrack track)
        {
            _xmlWriter.WriteStartElement("trk");

            if (string.IsNullOrEmpty(track.Name) == false)
            {
                _xmlWriter.WriteElementString("name", track.Name);
            }

            foreach (GPXTrackSegment segment in track.Segments)
            {
                _xmlWriter.WriteStartElement("trkseg");

                if (segment.Id != 0)
                {
                    _xmlWriter.WriteAttributeString("id", segment.Id.ToString(System.Globalization.CultureInfo.InvariantCulture));
                }

                // Traffic
                if (segment.Traffic != null)
                {
                    _xmlWriter.WriteStartElement("traffiseg");
                    _xmlWriter.WriteAttributeString("v", segment.Traffic.Count.ToString(System.Globalization.CultureInfo.InvariantCulture));
                    _xmlWriter.WriteEndElement();
                }

                // Average Speed
                if (segment.AvgSpeed != 0)
                {
                    _xmlWriter.WriteStartElement("avgSpeed");
                    _xmlWriter.WriteAttributeString("v", segment.AvgSpeed.ToString("R"));
                    _xmlWriter.WriteEndElement();
                }

                // Route Speed
                if (segment.Speed != 0)
                {
                    _xmlWriter.WriteStartElement("speed");
                    _xmlWriter.WriteAttributeString("v", segment.Speed.ToString("R"));
                    _xmlWriter.WriteEndElement();
                }

                // Points
                foreach (GPXPoint point in segment.Nodes)
                {
                    WritePointData(point, "trkpt");
                }

                _xmlWriter.WriteEndElement();
            }

            _xmlWriter.WriteEndElement();
        }
Example #3
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
                }
            }
        }
Example #4
0
        /// <summary>
        /// Reads track from the gpx
        /// </summary>
        private void ReadTrack()
        {
            GPXTrack parsedTrack = new GPXTrack();

            if (_xmlReader.IsEmptyElement == false)
            {
                _xmlReader.Read();

                while (_xmlReader.NodeType != XmlNodeType.EndElement)
                {
                    if (_xmlReader.NodeType == XmlNodeType.Element)
                    {
                        switch (_xmlReader.Name)
                        {
                        case "trkseg":
                            long id = 0;
                            if (_xmlReader.GetAttribute("id") != null)
                            {
                                id = long.Parse(_xmlReader.GetAttribute("id"));
                            }

                            parsedTrack.Segments.Add(ReadTrackSegment(id));


                            break;

                        case "name":
                            parsedTrack.Name = _xmlReader.ReadString();
                            _xmlReader.Skip();
                            break;

                        default:
                            _xmlReader.Skip();
                            break;
                        }
                    }
                    else
                    {
                        _xmlReader.Skip();
                    }
                }
            }

            _xmlReader.Skip();
            OnTrackRead(parsedTrack);
        }
Example #5
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");
        }
        /// <summary>
        /// Writes the specific track to the output
        /// </summary>
        /// <param name="track">The track to be written</param>
        public void WriteTrack(GPXTrack track)
        {
            _xmlWriter.WriteStartElement("trk");

            if (string.IsNullOrEmpty(track.Name) == false) {
                _xmlWriter.WriteElementString("name", track.Name);
            }

            foreach (GPXTrackSegment segment in track.Segments) {
                _xmlWriter.WriteStartElement("trkseg");

                foreach (GPXPoint point in segment.Nodes) {
                    WritePointData(point, "trkpt");
                }

                _xmlWriter.WriteEndElement();
            }

            _xmlWriter.WriteEndElement();
        }
Example #7
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.");
        }