/// <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); }
/// <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 (this.Arguments.Length != 1) { throw new ArgumentException("Exactly one argument is expected."); } var localFile = Downloader.DownloadOrOpen(this.Arguments[0]); var file = new FileInfo(localFile); if (!file.Exists) { throw new FileNotFoundException("File not found.", file.FullName); } var pbfSource = new OsmSharp.Streams.PBFOsmStreamSource(file.OpenRead()); processor = new Processors.Osm.ProcessorOsmStreamSource(pbfSource); return(0); }
static void AddFromSourceOsmData(string osmFile, string osmTransitFile) { // load transit data formatted as osm data. using var transitSource = File.OpenRead(osmTransitFile); var osmTransitSource = new OsmSharp.Streams.BinaryOsmStreamSource(transitSource); using var source = File.OpenRead(osmFile); var osmSource = new OsmSharp.Streams.PBFOsmStreamSource(source); var merged = new OsmSharp.Streams.Filters.OsmStreamFilterMerge(); merged.RegisterSource(osmSource); merged.RegisterSource(osmTransitSource); // run through transit handler filter. var transitFilter = new TransitDataHandlerOsmStream((osmGeo) => { if (osmGeo.Tags != null && osmGeo.Tags.Contains("source", "ANYWAYS:transitdb")) { return(true); } return(false); }); transitFilter.RegisterSource(merged); // build routerdb. var routerDb = new RouterDb(); routerDb.LoadOsmData(transitFilter, Vehicle.Bicycle); // after routerdb is finished, use filtered data to properly add transit data. routerDb.AddPublicTransport(transitFilter, new IProfileInstance[] { Vehicle.Bicycle.Fastest() }); // write the result. routerDb.WriteToShape("output.shp"); }
public static void Main(string[] args) { // 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(); // download test data. Download.ToFile("http://files.itinero.tech/data/OSM/planet/europe/luxembourg-latest.osm.pbf", "test.osm.pbf").Wait(); // test read/writing an existing OSM file. Log.Information("Testing reading/writing via OSM binary format..."); using (var sourceStream = File.OpenRead("test.osm.pbf")) using (var targetStream = File.Open("test1.osm.bin", FileMode.Create)) { var source = new OsmSharp.Streams.PBFOsmStreamSource(sourceStream); var target = new OsmSharp.Streams.BinaryOsmStreamTarget(targetStream); target.RegisterSource(source); target.Pull(); } using (var sourceStream = File.OpenRead("test1.osm.bin")) using (var targetStream = File.Open("test2.osm.pbf", FileMode.Create)) { var source = new OsmSharp.Streams.BinaryOsmStreamSource(sourceStream); var target = new OsmSharp.Streams.PBFOsmStreamTarget(targetStream); target.RegisterSource(source); target.Pull(); } // test read/writing an existing OSM file via a compressed stream. Log.Information("Testing reading/writing via OSM binary format using a compressed stream..."); using (var sourceStream = File.OpenRead("test.osm.pbf")) using (var targetStream = File.Open("test2.osm.bin.zip", FileMode.Create)) using (var targetStreamCompressed = new System.IO.Compression.DeflateStream(targetStream, CompressionLevel.Fastest)) { var source = new OsmSharp.Streams.PBFOsmStreamSource(sourceStream); var target = new OsmSharp.Streams.BinaryOsmStreamTarget(targetStreamCompressed); target.RegisterSource(source); target.Pull(); } using (var sourceStream = File.OpenRead("test2.osm.bin.zip")) using (var sourceStreamCompressed = new System.IO.Compression.DeflateStream(sourceStream, CompressionMode.Decompress)) using (var targetStream = File.Open("test2.osm.pbf", FileMode.Create)) { var source = new OsmSharp.Streams.BinaryOsmStreamSource(sourceStreamCompressed); var target = new OsmSharp.Streams.PBFOsmStreamTarget(targetStream); target.RegisterSource(source); target.Pull(); } // test reading/writing edited OSM-data. Log.Information("Testing reading/writing via OSM binary format of edited incomplete OSM data..."); using (var sourceStream = File.OpenRead("./test-data/data.osm")) using (var targetStream = File.Open("test3.osm.bin", FileMode.Create)) { var source = new OsmSharp.Streams.XmlOsmStreamSource(sourceStream); var target = new OsmSharp.Streams.BinaryOsmStreamTarget(targetStream); target.RegisterSource(source); target.Pull(); } using (var sourceStream = File.OpenRead("test3.osm.bin")) using (var targetStream = File.Open("test2.osm.pbf", FileMode.Create)) { var source = new OsmSharp.Streams.BinaryOsmStreamSource(sourceStream); var target = new OsmSharp.Streams.PBFOsmStreamTarget(targetStream); target.RegisterSource(source); target.Pull(); } }
public static void Main(string[] args) { // enable logging. OsmSharp.Logging.Logger.LogAction = (o, level, message, parameters) => { Console.WriteLine(string.Format("[{0}-{3}] {1} - {2}", o, level, message, DateTime.Now.ToString())); }; // download and extract test-data if not already there. OsmSharp.Logging.Logger.Log("Program", TraceEventType.Information, "Downloading Belgium..."); Download.DownloadBelgiumAll(); // create a source. var source = new OsmSharp.Streams.PBFOsmStreamSource(File.OpenRead(Download.BelgiumLocal)); // loop over all objects and count them. int nodes = 0, ways = 0, relations = 0; var count = new Action(() => { foreach (var osmGeo in source) { if (osmGeo.Type == OsmGeoType.Node) { nodes++; } if (osmGeo.Type == OsmGeoType.Way) { ways++; } if (osmGeo.Type == OsmGeoType.Relation) { relations++; } } }); count.TestPerf("Test counting objects."); OsmSharp.Logging.Logger.Log("Program", TraceEventType.Information, "Counted {0} nodes, {1} ways and {2} relations.", nodes, ways, relations); // loop over all objects and count them ignoring nodes. nodes = 0; ways = 0; relations = 0; source.Reset(); count = new Action(() => { foreach (var osmGeo in source.EnumerateAndIgore(true, false, false)) { if (osmGeo.Type == OsmGeoType.Node) { nodes++; } if (osmGeo.Type == OsmGeoType.Way) { ways++; } if (osmGeo.Type == OsmGeoType.Relation) { relations++; } } }); count.TestPerf("Test counting objects without nodes."); OsmSharp.Logging.Logger.Log("Program", TraceEventType.Information, "Counted {0} nodes, {1} ways and {2} relations.", nodes, ways, relations); // loop over all objects and count them ignoring nodes. nodes = 0; ways = 0; relations = 0; source.Reset(); count = new Action(() => { foreach (var osmGeo in source.EnumerateAndIgore(true, true, false)) { if (osmGeo.Type == OsmGeoType.Node) { nodes++; } if (osmGeo.Type == OsmGeoType.Way) { ways++; } if (osmGeo.Type == OsmGeoType.Relation) { relations++; } } }); count.TestPerf("Test counting objects without nodes and ways."); OsmSharp.Logging.Logger.Log("Program", TraceEventType.Information, "Counted {0} nodes, {1} ways and {2} relations.", nodes, ways, relations); OsmSharp.Logging.Logger.Log("Program", TraceEventType.Information, "Testing finished."); Console.ReadLine(); }