Ejemplo n.º 1
0
        /// <summary>
        /// Tests building a router db.
        /// </summary>
        public static Func <PerformanceTestResult <RouterDb> > GetTestBuildRouterDb(string file, bool allcore, bool processRestrictions, params Vehicle[] vehicles)
        {
            return(() =>
            {
                OsmStreamSource source;
                using (var stream = File.OpenRead(file))
                {
                    var routerdb = new RouterDb();
                    if (file.ToLowerInvariant().EndsWith("osm.pbf"))
                    {
                        source = new OsmSharp.Streams.PBFOsmStreamSource(stream);
                    }
                    else
                    {
                        source = new OsmSharp.Streams.XmlOsmStreamSource(stream);
                    }
                    var progress = new OsmSharp.Streams.Filters.OsmStreamFilterProgress();
                    progress.RegisterSource(source);

                    routerdb.LoadOsmData(progress, new LoadSettings()
                    {
                        AllCore = allcore,
                        ProcessRestrictions = processRestrictions,
                        OptimizeNetwork = true,
                        NetworkSimplificationEpsilon = 1,
                        KeepNodeIds = true
                    }, vehicles);

                    return new PerformanceTestResult <RouterDb>(routerdb);
                }
            });
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Tests building a router db.
        /// </summary>
        public static Func <RouterDb> GetTestBuildRouterDb(string file, bool allcore, bool processRestrictions, params Vehicle[] vehicles)
        {
            return(() =>
            {
                OsmStreamSource source;
                using (var stream = File.OpenRead(file))
                {
                    var routerdb = new RouterDb();
                    if (file.ToLowerInvariant().EndsWith("osm.pbf"))
                    {
                        source = new OsmSharp.Streams.PBFOsmStreamSource(stream);
                    }
                    else
                    {
                        source = new OsmSharp.Streams.XmlOsmStreamSource(stream);
                    }
                    var progress = new OsmSharp.Streams.Filters.OsmStreamFilterProgress();
                    progress.RegisterSource(source);

                    routerdb.LoadOsmData(progress, allcore, processRestrictions, vehicles);

                    return routerdb;
                }
            });
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Parses this command into a processor given the arguments for this switch. Consumes the previous processors and returns how many it consumes.
        /// </summary>
        public override int Parse(List <Processor> previous, out Processor processor)
        {
            if (!(previous[previous.Count - 1] is Processors.Osm.IProcessorOsmStreamSource))
            {
                throw new Exception("Expected an OSM stream source.");
            }

            var progressFilter = new OsmSharp.Streams.Filters.OsmStreamFilterProgress();

            progressFilter.RegisterSource((previous[previous.Count - 1] as Processors.Osm.IProcessorOsmStreamSource).Source);
            processor = new Processors.Osm.ProcessorOsmStreamSource(progressFilter);

            return(1);
        }
        /// <summary>
        /// Loads a router db given the OSM file.
        /// </summary>
        /// <param name="osmPbfFile">An OSM-PBF file.</param>
        /// <returns>A loaded router db</returns>
        public static RouterDb LoadFrom(string osmPbfFile)
        {
            var routerDb = new RouterDb();

            using (var stream = File.OpenRead(osmPbfFile))
            {
                var source   = new OsmSharp.Streams.PBFOsmStreamSource(stream);
                var progress = new OsmSharp.Streams.Filters.OsmStreamFilterProgress();
                progress.RegisterSource(source);

                var target = new RouterDbStreamTarget(routerDb);
                target.RegisterSource(progress);
                target.Initialize();

                target.Pull();
            }

            return(routerDb);
        }
Ejemplo n.º 5
0
        private async Task RunAsync(CancellationToken stoppingToken)
        {
            // download file (if md5 files don't match).
            var local = Path.Combine(_configuration.DataPath, Local);

            if (!await _downloader.Get(_configuration.SourceUrl, local, _configuration.TempPath, cancellationToken: stoppingToken))
            {
                return;
            }

            _logger.LogInformation("Downloaded new file, refreshing tiles");

            if (!File.Exists(local))
            {
                _logger.LogCritical("Local file not found: {Local}", local);
                return;
            }

            try
            {
                bool IsRelevant(Relation current)
                {
                    var networks = new HashSet <string> {
                        "lcn", "rcn", "ncn"
                    };

                    if (!current.Tags.TryGetValue("type", out var type))
                    {
                        return(false);
                    }

                    switch (type)
                    {
                    case "network":
                    {
                        if (current.Tags.TryGetValue("network", out var network) &&
                            networks.Contains(network))
                        {
                            return(true);
                        }

                        break;
                    }

                    case "route":
                    {
                        if (current.Tags.Contains("route", "bicycle"))
                        {
                            return(true);
                        }

                        break;
                    }
                    }

                    return(false);
                }

                var localStream = File.OpenRead(local);
                var pbfSource   = new PBFOsmStreamSource(localStream);
                var source      = new OsmSharp.Streams.Filters.OsmStreamFilterProgress();
                source.RegisterSource(pbfSource);

                // pass over source stream and:
                // - determine all members of route relations where are interested in.
                // - keep master relations relationships.
                var membersInRelations  = new Dictionary <OsmGeoKey, List <Relation> >();
                var foundRouteRelations = 0;
                while (true)
                {
                    if (stoppingToken.IsCancellationRequested)
                    {
                        break;
                    }
                    if (!source.MoveNext(true, true, false))
                    {
                        break;
                    }
                    if (source.Current() is not Relation current)
                    {
                        continue;
                    }
                    if (current.Members == null)
                    {
                        continue;
                    }
                    if (current.Tags == null)
                    {
                        continue;
                    }
                    if (!IsRelevant(current))
                    {
                        continue;
                    }

                    // this is a network relation.
                    foundRouteRelations++;
                    foreach (var member in current.Members)
                    {
                        var memberKey = new OsmGeoKey
                        {
                            Id   = member.Id,
                            Type = member.Type
                        };
                        if (!membersInRelations.TryGetValue(memberKey, out var masters))
                        {
                            masters = new List <Relation>(1);
                            membersInRelations[memberKey] = masters;
                        }

                        masters.Add(current);
                    }
                }

                if (stoppingToken.IsCancellationRequested)
                {
                    this.CancelledWhenProcessing();
                    return;
                }

                _logger.LogInformation("Found {Relation} with {MembersCount} members",
                                       foundRouteRelations, membersInRelations.Count);

                // filter stream, keeping only the relevant objects.
                IEnumerable <OsmGeo> FilterSource()
                {
                    foreach (var x in source)
                    {
                        if (stoppingToken.IsCancellationRequested)
                        {
                            yield break;
                        }
                        if (!x.Id.HasValue)
                        {
                            yield break;
                        }

                        var key = new OsmGeoKey
                        {
                            Id   = x.Id.Value,
                            Type = x.Type
                        };

                        switch (x.Type)
                        {
                        case OsmGeoType.Node:
                            yield return(x);

                            break;

                        case OsmGeoType.Way:
                            if (membersInRelations.ContainsKey(key))
                            {
                                yield return(x);
                            }

                            break;

                        case OsmGeoType.Relation:
                            if (x is Relation r && IsRelevant(r))
                            {
                                yield return(x);
                            }

                            break;
                        }
                    }
                }

                var filteredSource = FilterSource();
                if (stoppingToken.IsCancellationRequested)
                {
                    this.CancelledWhenProcessing();
                    return;
                }

                // convert this to complete objects.
                var features = new FeatureCollection();
                foreach (var osmComplete in filteredSource.ToComplete())
                {
                    if (stoppingToken.IsCancellationRequested)
                    {
                        break;
                    }
                    switch (osmComplete)
                    {
                    case Node {
                            Id: null
                    } :
                        continue;

                    case Node node:
                    {
                        var key = new OsmGeoKey
                        {
                            Id   = node.Id.Value,
                            Type = node.Type
                        };

                        if (membersInRelations.TryGetValue(key, out var masters))
                        {
                            foreach (var master in masters)
                            {
                                var nodeFeatures = node.ToFeatureCollection(master.Tags);
                                foreach (var feature in nodeFeatures)
                                {
                                    features.Add(feature);
                                }
                            }
                        }

                        break;
                    }
Ejemplo n.º 6
0
        static void Main(string[] args)
        {
//#if DEBUG
            if (args == null || args.Length == 0)
            {
                args = new string[]
                {
                    @"/data/work/data/OSM/belgium-latest.osm.pbf",
                    @"/data/work/openplannerteam/data/tilesdb/",
                    @"14"
                };
            }
//#endif

            // enable logging.
            OsmSharp.Logging.Logger.LogAction = (origin, level, message, parameters) =>
            {
                var formattedMessage = $"{origin} - {message}";
                switch (level)
                {
                case "critical":
                    Log.Fatal(formattedMessage);
                    break;

                case "error":
                    Log.Error(formattedMessage);
                    break;

                case "warning":
                    Log.Warning(formattedMessage);
                    break;

                case "verbose":
                    Log.Verbose(formattedMessage);
                    break;

                case "information":
                    Log.Information(formattedMessage);
                    break;

                default:
                    Log.Debug(formattedMessage);
                    break;
                }
            };

            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.Information()
                         .WriteTo.Console()
                         .WriteTo.File(Path.Combine("logs", "log-.txt"), rollingInterval: RollingInterval.Day)
                         .CreateLogger();

//            try
//            {
            // validate arguments.
            if (args.Length < 3)
            {
                Log.Fatal("Expected 3 arguments: inputfile outputdir zoom");
                return;
            }
            if (!File.Exists(args[0]))
            {
                Log.Fatal("Input file not found: {0}", args[0]);
                return;
            }
            if (!Directory.Exists(args[1]))
            {
                Log.Fatal("Cache directory doesn't exist: {0}", args[1]);
                return;
            }
            if (!uint.TryParse(args[2], out var zoom))
            {
                Log.Fatal("Can't parse zoom: {0}", args[2]);
                return;
            }

            var        ticks      = DateTime.Now.Ticks;
            const bool compressed = false;

            if (!File.Exists(Path.Combine(args[1], "0", "0", "0.nodes.idx")))
            {
                Log.Information("Building the tiled DB...");
                var source = new OsmSharp.Streams.PBFOsmStreamSource(
                    File.OpenRead(args[0]));
                var progress = new OsmSharp.Streams.Filters.OsmStreamFilterProgress();
                progress.RegisterSource(source);

                // filter out routeable data only.
                var nodeIndex = new OsmIdIndex();
                while (progress.MoveNext(true, false, true))
                {
                    if (!(progress.Current() is Way w) || w.Nodes == null)
                    {
                        continue;
                    }
                    if (w.Tags == null || !w.Tags.ContainsKey("highway"))
                    {
                        continue;
                    }

                    foreach (var n in w.Nodes)
                    {
                        nodeIndex.Add(n);
                    }
                }

                var filtered = new OsmEnumerableStreamSource(progress.Where(x =>
                {
                    if (x is Node)
                    {
                        return(nodeIndex.Contains(x.Id.Value));
                    }
                    else if (x is Way)
                    {
                        return(x.Tags != null && x.Tags.ContainsKey("highway"));
                    }
                    else
                    {
                        if (x.Tags == null)
                        {
                            return(false);
                        }
                        return(x.Tags.ContainsKey("route") ||
                               x.Tags.ContainsKey("restriction"));
                    }
                }));

                // splitting tiles and writing indexes.
                Build.Builder.Build(filtered, args[1], zoom, compressed);
            }
            else
            {
                Log.Error("The tiled DB already exists, cannot overwrite, delete it first...");
            }
//            }
//            catch (Exception e)
//            {
//                Log.Fatal(e, "Unhandled exception.");
//                throw;
//            }
        }