Ejemplo n.º 1
0
        static void GenerateOsmFiles(List <Bucket> buckets, PathReconstructer reconstructor, OSMDB map, List <GPXTrack> gpxTrackList)
        {
            foreach (var b in buckets)
            {
                if (b.Paths.Any())
                {
                    var mapCopy = ObjectCopier.Clone <OSMDB>(map);
                    List <Polyline <IPointGeo> > pathList = new List <Polyline <IPointGeo> >();

                    OSMNode bucketInfo = new OSMNode(0, 0, 0);
                    OSMTag  start      = new OSMTag("start", b.Start.ToString());
                    OSMTag  end        = new OSMTag("end", b.End.ToString());
                    bucketInfo.Tags.Add(start);
                    bucketInfo.Tags.Add(end);

                    foreach (var p in b.Paths)
                    {
                        var uniquePath = p.Value.GroupBy(x => new { x.Id }).Select(x => x.First());

                        foreach (var seg in uniquePath)
                        {
                            if (seg.Id != 0)
                            {
                                var matchingWay = mapCopy.Ways[seg.Id];
                                var avgSpeed    = getAverageSpeed(p.Key, gpxTrackList);

                                if (avgSpeed != null)
                                {
                                    if (matchingWay.Tags.ContainsTag("avgSpeed"))
                                    {
                                        matchingWay.Tags["avgSpeed"].Value = avgSpeed;
                                    }
                                    else
                                    {
                                        matchingWay.Tags.Add(new OSMTag("avgSpeed", avgSpeed));
                                    }
                                }

                                if (matchingWay.Tags.ContainsTag("traffic"))
                                {
                                    matchingWay.Tags["traffic"].Value += "," + p.Key;
                                }
                                else
                                {
                                    matchingWay.Tags.Add(new OSMTag("traffic", p.Key));
                                }
                            }
                        }
                        pathList.AddRange(uniquePath);
                    }

                    //OSMDB resultMap = reconstructor.SaveToOSM(pathList);
                    //resultMap.Save("map" + b.Name + ".osm");

                    mapCopy.Nodes.Add(bucketInfo);
                    mapCopy.Save("map" + b.Name + ".osm");
                }
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            DateTime span = DateTime.Now;

            string osmPath        = "";
            string gpxPath        = "";
            string xmlPath        = "";
            string outputPath     = ".";
            int    samplingPeriod = 0;
            bool   showHelp       = false;
            bool   filter         = false;

            OptionSet parameters = new OptionSet()
            {
                { "osm=", "path to the routable map file", v => osmPath = v },
                { "gpx=", "path to the GPX file to process or to the directory to process", v => gpxPath = v },
                { "xml=", "path to the XML file with the time buckets", v => xmlPath = v },
                { "o|output=", "path to the output directory", v => outputPath = v },
                { "p|period=", "sampling period of the GPX file", v => samplingPeriod = int.Parse(v) },
                { "f|filter", "enables output post processing", v => filter = v != null },
                { "h|?|help", v => showHelp = v != null },
            };

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


            if (showHelp || string.IsNullOrEmpty(osmPath) || string.IsNullOrEmpty(gpxPath) || string.IsNullOrEmpty(xmlPath) || string.IsNullOrEmpty(outputPath))
            {
                ShowHelp(parameters);
                return;
            }


            if (outputPath[outputPath.Length - 1] == '"')
            {
                outputPath = outputPath.Substring(0, outputPath.Length - 1);
            }


            Console.Write("Loading OSM file ...");
            OSMDB map = new OSMDB();

            map.Load(osmPath);
            Console.WriteLine("\t\t\tDone.");

            Console.Write("Building routable road graph ...");
            RoadGraph graph = new RoadGraph();

            graph.Build(map);
            Console.WriteLine("\tDone.");

            TMM processor = new TMM(graph)
            {
                _db = map
            };
            PathReconstructer reconstructor = new PathReconstructer(graph)
            {
                _db = map
            };

            XMLDocument xml = new XMLDocument();

            xml.Load(xmlPath);
            var buckets = xml.Buckets;

            // Process single file
            if (File.Exists(gpxPath))
            {
                List <GPXTrack> result = new List <GPXTrack>();
                result.AddRange(getAllGpxTrackList(gpxPath));

                ProcessGPXFile(gpxPath, processor, reconstructor, outputPath, samplingPeriod, filter, buckets);
                GenerateOsmFiles(buckets, reconstructor, map, result);
                GenerateGpxFiles(buckets, gpxPath, 0);
            }
            // Process all GPX in directory
            else if (Directory.Exists(gpxPath))
            {
                var             files  = Directory.GetFiles(gpxPath, "*.gpx");
                List <GPXTrack> result = new List <GPXTrack>();

                Console.WriteLine("Found {0} GPX file(s).", files.Length);

                for (int i = 0; i < files.Length; i++)
                {
                    ProcessGPXFile(files[i], processor, reconstructor, outputPath, samplingPeriod, filter, buckets);
                    GenerateGpxFiles(buckets, gpxPath, i);
                    result.AddRange(getAllGpxTrackList(files[i]));

                    Console.WriteLine("NEW FILE BEING PROCESSED");
                }

                GenerateOsmFiles(buckets, reconstructor, map, result);
            }
            else
            {
                Console.WriteLine("No GPX files found");
            }

            Console.WriteLine("\tDone.");
            Console.WriteLine("\tSpan=" + (DateTime.Now - span));
        }
Ejemplo n.º 3
0
        static void ProcessGPXFile(string path, TMM processor, PathReconstructer reconstructor, string outputPath, int samplingPeriod,
                                   bool filterOutput, List <Bucket> buckets)
        {
            GPXUtils.Filters.FrequencyFilter filter = new GPXUtils.Filters.FrequencyFilter();

            Console.Write("Loading {0} ...", Path.GetFileName(path));
            GPXDocument gpx = new GPXDocument();

            gpx.Load(path);

            Console.WriteLine("[{0} track(s); {1} segment(s)]", gpx.Tracks.Count, gpx.Tracks.Sum(track => track.Segments.Count));
            for (int trackIndex = 0; trackIndex < gpx.Tracks.Count; trackIndex++)
            {
                Console.WriteLine(gpx.Tracks[trackIndex].Name);

                for (int segmentIndex = 0; segmentIndex < gpx.Tracks[trackIndex].Segments.Count; segmentIndex++)
                {
                    string name = string.IsNullOrEmpty(gpx.Tracks[trackIndex].Name) ? "t" + trackIndex.ToString() : gpx.Tracks[trackIndex].Name.Replace('\\', '-').Replace(":", "");
                    name += "_s" + segmentIndex.ToString();
                    Console.Write("\t" + name + " ");

                    try
                    {
                        GPXTrackSegment toProcess = gpx.Tracks[trackIndex].Segments[segmentIndex];

                        if (samplingPeriod > 0)
                        {
                            toProcess = filter.Filter(new TimeSpan(0, 0, samplingPeriod), toProcess);
                        }

                        if (toProcess.NodesCount > 1)
                        {
                            var result = processor.Match(toProcess);
                            Console.Write(".");

                            var reconstructedPath = reconstructor.Reconstruct(result);

                            Console.Write(".");

                            if (filterOutput)
                            {
                                reconstructor.FilterUturns(reconstructedPath, 100);
                            }

                            Console.WriteLine(".");

                            var trackId = gpx.Tracks[trackIndex].Name.Replace("trk_", "");
                            buckets = GetUpdatedBuckets(toProcess, reconstructedPath, buckets, trackId);
                        }
                        else
                        {
                            throw new Exception(string.Format("Track segment discarded because number of nodes is less than 2."));
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Error: " + e.Message);
                    }
                }
            }
        }