/// <summary> /// Returns a new router. /// </summary> /// <param name="interpreter"></param> /// <param name="embeddedName"></param> /// <param name="contract"></param> /// <returns></returns> public override Router BuildRouter(IOsmRoutingInterpreter interpreter, string embeddedName, bool contract) { if (_data == null) { _data = new Dictionary<string, DynamicGraphRouterDataSource<LiveEdge>>(); } DynamicGraphRouterDataSource<LiveEdge> data = null; if (!_data.TryGetValue(embeddedName, out data)) { var tagsIndex = new TagsTableCollectionIndex(); // do the data processing. data = new DynamicGraphRouterDataSource<LiveEdge>(tagsIndex); var targetData = new LiveGraphOsmStreamTarget( data, interpreter, tagsIndex, new Vehicle[] { Vehicle.Car }, false); var dataProcessorSource = new XmlOsmStreamSource( Assembly.GetExecutingAssembly().GetManifestResourceStream(string.Format( "OsmSharp.Test.Unittests.{0}", embeddedName))); var sorter = new OsmStreamFilterSort(); sorter.RegisterSource(dataProcessorSource); targetData.RegisterSource(sorter); targetData.Pull(); _data[embeddedName] = data; } return Router.CreateLiveFrom(data, new Dykstra(), interpreter); }
/// <summary> /// Tests preprocessing data from a PBF file. /// </summary> /// <param name="name"></param> /// <param name="pbfFile"></param> public static void TestPreprocessing(string name, string pbfFile) { FileInfo testFile = new FileInfo(string.Format(@".\TestFiles\{0}", pbfFile)); Stream stream = testFile.OpenRead(); var progress = new OsmStreamFilterProgress(); progress.RegisterSource(new PBFOsmStreamSource(stream)); var performanceInfo = new PerformanceInfoConsumer("LivePreProcessor", 20000); performanceInfo.Start(); performanceInfo.Report("Pulling from {0}...", testFile.Name); var tagsIndex = new TagsTableCollectionIndex(); // creates a tagged index. // read from the OSM-stream. // var memoryGraph = new MemoryMappedGraph<LiveEdge>(1000000, @"c:\temp\"); var memoryData = new DynamicGraphRouterDataSource<LiveEdge>(tagsIndex); var targetData = new LiveGraphOsmStreamTarget(memoryData, new OsmRoutingInterpreter(), tagsIndex); targetData.RegisterSource(progress); targetData.Pull(); stream.Dispose(); performanceInfo.Stop(); // make sure the router is still here after GC to note the memory difference. OsmSharp.Logging.Log.TraceEvent("LivePreProcessor", Logging.TraceEventType.Information, memoryData.ToString()); memoryData = null; GC.Collect(); }
/// <summary> /// Tests routing from a serialized routing file. /// </summary> /// <param name="name"></param> /// <param name="stream"></param> /// <param name="testCount"></param> public static void TestRouting(string name, Stream stream, int testCount) { var vehicle = Vehicle.Car; var tagsIndex = new TagsTableCollectionIndex(); // creates a tagged index. // read from the OSM-stream. var source = new OsmSharp.Osm.Streams.Filters.OsmStreamFilterProgress(); source.RegisterSource(new OsmSharp.Osm.PBF.Streams.PBFOsmStreamSource(stream)); var data = CHEdgeGraphOsmStreamTarget.Preprocess(source, new OsmRoutingInterpreter(), vehicle); var router = new CHRouter(); var performanceInfo = new PerformanceInfoConsumer("CHRouting"); performanceInfo.Start(); performanceInfo.Report("Routing {0} routes...", testCount); var successCount = 0; var totalCount = testCount; var latestProgress = -1.0f; while (testCount > 0) { var from = (uint)OsmSharp.Math.Random.StaticRandomGenerator.Get().Generate(data.VertexCount - 1) + 1; var to = (uint)OsmSharp.Math.Random.StaticRandomGenerator.Get().Generate(data.VertexCount - 1) + 1; var route = router.Calculate(data, from, to); if(route != null) { successCount++; } testCount--; // report progress. var progress = (float)System.Math.Round(((double)(totalCount - testCount) / (double)totalCount) * 100); if (progress != latestProgress) { OsmSharp.Logging.Log.TraceEvent("CHRouting", TraceEventType.Information, "Routing... {0}%", progress); latestProgress = progress; } } performanceInfo.Stop(); OsmSharp.Logging.Log.TraceEvent("CHRouting", OsmSharp.Logging.TraceEventType.Information, string.Format("{0}/{1} routes successfull!", successCount, totalCount)); }
/// <summary> /// Builds a raw router to compare against. /// </summary> /// <returns></returns> public void BuildDykstraRouter(string embeddedName, IOsmRoutingInterpreter interpreter) { var tagsIndex = new TagsTableCollectionIndex(); // do the data processing. var data = new DynamicGraphRouterDataSource<LiveEdge>(tagsIndex); var targetData = new LiveGraphOsmStreamTarget( data, interpreter, tagsIndex, new Vehicle[] { Vehicle.Car }); var dataProcessorSource = new XmlOsmStreamSource( Assembly.GetExecutingAssembly().GetManifestResourceStream(embeddedName)); var sorter = new OsmStreamFilterSort(); sorter.RegisterSource(dataProcessorSource); targetData.RegisterSource(sorter); targetData.Pull(); // initialize the router. _referenceRouter = Router.CreateLiveFrom(data, new Dykstra(), interpreter); }
/// <summary> /// Returns a new router. /// </summary> /// <param name="interpreter"></param> /// <param name="embeddedName"></param> /// <param name="contract"></param> /// <returns></returns> public override Router BuildRouter(IOsmRoutingInterpreter interpreter, string embeddedName, bool contract) { if (_data == null) { _data = new Dictionary<string, DynamicGraphRouterDataSource<CHEdgeData>>(); } DynamicGraphRouterDataSource<CHEdgeData> data = null; if (!_data.TryGetValue(embeddedName, out data)) { var tagsIndex = new TagsTableCollectionIndex(); // do the data processing. data = new DynamicGraphRouterDataSource<CHEdgeData>(tagsIndex); var targetData = new CHEdgeGraphOsmStreamTarget( data, interpreter, tagsIndex, Vehicle.Car); var dataProcessorSource = new XmlOsmStreamSource( Assembly.GetExecutingAssembly().GetManifestResourceStream(string.Format( "OsmSharp.Test.Unittests.{0}", embeddedName))); var sorter = new OsmStreamFilterSort(); sorter.RegisterSource(dataProcessorSource); targetData.RegisterSource(sorter); targetData.Pull(); if (contract) { // do the pre-processing part. var witnessCalculator = new DykstraWitnessCalculator(); var preProcessor = new CHPreProcessor(data, new EdgeDifference(data, witnessCalculator), witnessCalculator); preProcessor.Start(); } _data[embeddedName] = data; } return Router.CreateCHFrom(data, new CHRouter(), interpreter); }
/// <summary> /// Executes the CH contractions while verifying each step. /// </summary> /// <param name="stream"></param> public void DoTestCHSparseVerifiedContraction(Stream stream) { _pathsBeforeContraction = new Dictionary<uint, Dictionary<uint, Dictionary<uint, PathSegment<long>>>>(); _interpreter = new OsmRoutingInterpreter(); var tagsIndex = new TagsTableCollectionIndex(); // do the data processing. _data = new DynamicGraphRouterDataSource<CHEdgeData>(new MemoryDirectedGraph<CHEdgeData>(), tagsIndex); var targetData = new CHEdgeGraphOsmStreamTarget( _data, _interpreter, tagsIndex, Vehicle.Car); var dataProcessorSource = new XmlOsmStreamSource(stream); var sorter = new OsmStreamFilterSort(); sorter.RegisterSource(dataProcessorSource); targetData.RegisterSource(sorter); targetData.Pull(); // do the pre-processing part. var witnessCalculator = new DykstraWitnessCalculator(); var preProcessor = new CHPreProcessor(_data, new EdgeDifferenceContractedSearchSpace(_data, witnessCalculator), witnessCalculator); preProcessor.OnBeforeContractionEvent += new CHPreProcessor.VertexDelegate(pre_processor_OnBeforeContractionEvent); preProcessor.OnAfterContractionEvent += new CHPreProcessor.VertexDelegate(pre_processor_OnAfterContractionEvent); preProcessor.Start(); }
/// <summary> /// Tests routing from a serialized routing file. /// </summary> /// <param name="name"></param> /// <param name="stream"></param> /// <param name="box"></param> /// <param name="testCount"></param> public static void TestRouting(string name, Stream stream, GeoCoordinateBox box, int testCount) { var vehicle = Vehicle.Car; var tagsIndex = new TagsTableCollectionIndex(); // creates a tagged index. // read from the OSM-stream. var data = CHEdgeGraphOsmStreamTarget.Preprocess( new OsmSharp.Osm.PBF.Streams.PBFOsmStreamSource(stream), new OsmRoutingInterpreter(), vehicle); var router = Router.CreateCHFrom(data, new CHRouter(), new OsmRoutingInterpreter()); var performanceInfo = new PerformanceInfoConsumer("CHRouting"); performanceInfo.Start(); performanceInfo.Report("Routing {0} routes...", testCount); int successCount = 0; int totalCount = testCount; float latestProgress = -1; while (testCount > 0) { var from = box.GenerateRandomIn(); var to = box.GenerateRandomIn(); var fromPoint = router.Resolve(vehicle, from); var toPoint = router.Resolve(vehicle, to); if (fromPoint != null && toPoint != null) { Route route = null; try { route = router.Calculate(vehicle, fromPoint, toPoint); } catch { } if (route != null) { successCount++; } } testCount--; // report progress. float progress = (float)System.Math.Round(((double)(totalCount - testCount) / (double)totalCount) * 100); if (progress != latestProgress) { OsmSharp.Logging.Log.TraceEvent("CHRouting", TraceEventType.Information, "Routing... {0}%", progress); latestProgress = progress; } } performanceInfo.Stop(); OsmSharp.Logging.Log.TraceEvent("CHRouting", OsmSharp.Logging.TraceEventType.Information, string.Format("{0}/{1} routes successfull!", successCount, totalCount)); }