/// <summary> /// Executes the CH contractions while verifying each step. /// </summary> /// <param name="stream"></param> public void DoTestCHEdgeDifferenceVerifiedContraction(Stream stream) { _interpreter = new OsmRoutingInterpreter(); OsmTagsIndex tags_index = new OsmTagsIndex(); // do the data processing. _data = new DynamicGraphRouterDataSource<CHEdgeData>(tags_index); CHEdgeDataGraphProcessingTarget target_data = new CHEdgeDataGraphProcessingTarget( _data, _interpreter, _data.TagsIndex, VehicleEnum.Car); XmlDataProcessorSource data_processor_source = new XmlDataProcessorSource(stream); DataProcessorFilterSort sorter = new DataProcessorFilterSort(); sorter.RegisterSource(data_processor_source); target_data.RegisterSource(sorter); target_data.Pull(); // do the pre-processing part. DykstraWitnessCalculator witness_calculator = new DykstraWitnessCalculator( _data); CHPreProcessor pre_processor = new CHPreProcessor(_data, new EdgeDifference(_data, witness_calculator), witness_calculator); pre_processor.OnBeforeContractionEvent += new CHPreProcessor.VertexDelegate(pre_processor_OnBeforeContractionEvent); pre_processor.OnAfterContractionEvent += new CHPreProcessor.VertexDelegate(pre_processor_OnAfterContractionEvent); pre_processor.Start(); }
/// <summary> /// Returns a new router. /// </summary> /// <param name="interpreter"></param> /// <param name="embedded_name"></param> /// <returns></returns> public override IRouter<RouterPoint> BuildRouter(IRoutingInterpreter interpreter, string embedded_name) { if (_data == null) { _data = new Dictionary<string, DynamicGraphRouterDataSource<CHEdgeData>>(); } DynamicGraphRouterDataSource<CHEdgeData> data = null; if (!_data.TryGetValue(embedded_name, out data)) { OsmTagsIndex tags_index = new OsmTagsIndex(); // do the data processing. data = new DynamicGraphRouterDataSource<CHEdgeData>(tags_index); CHEdgeDataGraphProcessingTarget target_data = new CHEdgeDataGraphProcessingTarget( data, interpreter, data.TagsIndex, VehicleEnum.Car); XmlDataProcessorSource data_processor_source = new XmlDataProcessorSource( Assembly.GetExecutingAssembly().GetManifestResourceStream(string.Format( "OsmSharp.UnitTests.{0}", embedded_name))); DataProcessorFilterSort sorter = new DataProcessorFilterSort(); sorter.RegisterSource(data_processor_source); target_data.RegisterSource(sorter); target_data.Pull(); // do the pre-processing part. CHPreProcessor pre_processor = new CHPreProcessor(data, new SparseOrdering(data), new DykstraWitnessCalculator(data)); pre_processor.Start(); _data[embedded_name] = data; } return new Router<CHEdgeData>(data, interpreter, new CHRouter( data)); }
private Engine() { if (string.IsNullOrWhiteSpace(PbfDataFilePath)) { throw new NullReferenceException("PbfDataFilePath must be set."); } if (!File.Exists(PbfDataFilePath)) { throw new NullReferenceException("PbfDataFilePath '" + PbfDataFilePath + "' does not exist."); } // keeps a memory-efficient version of the osm-tags. var tagsIndex = new OsmTagsIndex(); // creates a routing interpreter. (used to translate osm-tags into a routable network) var interpreter = new OsmRoutingInterpreter(); // create a routing datasource, keeps all processed osm routing data. var osmData = new MemoryRouterDataSource<SimpleWeighedEdge>(tagsIndex); // load data into this routing datasource. Stream osmPbfData = new FileInfo(PbfDataFilePath).OpenRead(); // for example moscow! using (osmPbfData) { var targetData = new SimpleWeighedDataGraphProcessingTarget( osmData, interpreter, osmData.TagsIndex, VehicleEnum.Car); // replace this with PBFdataProcessSource when having downloaded a PBF file. var dataProcessorSource = new PBFDataProcessorSource(osmPbfData); // pre-process the data. var stopwatch = new Stopwatch(); stopwatch.Start(); var sorter = new DataProcessorFilterSort(); sorter.RegisterSource(dataProcessorSource); targetData.RegisterSource(sorter); targetData.Pull(); stopwatch.Stop(); } // create the router object: there all routing functions are available. router = new Router<SimpleWeighedEdge>( osmData, interpreter, new DykstraRoutingLive(osmData.TagsIndex)); }
/// <summary> /// Builds a raw data source. /// </summary> /// <returns></returns> public DynamicGraphRouterDataSource<PreProcessedEdge> BuildDykstraDataSource( IRoutingInterpreter interpreter, string embedded_name) { OsmTagsIndex tags_index = new OsmTagsIndex(); // do the data processing. DynamicGraphRouterDataSource<PreProcessedEdge> data = new DynamicGraphRouterDataSource<PreProcessedEdge>(tags_index); PreProcessedDataGraphProcessingTarget target_data = new PreProcessedDataGraphProcessingTarget( data, interpreter, data.TagsIndex, VehicleEnum.Car); XmlDataProcessorSource data_processor_source = new XmlDataProcessorSource( Assembly.GetExecutingAssembly().GetManifestResourceStream(string.Format( "OsmSharp.UnitTests.{0}", embedded_name))); DataProcessorFilterSort sorter = new DataProcessorFilterSort(); sorter.RegisterSource(data_processor_source); target_data.RegisterSource(sorter); target_data.Pull(); return data; }
/// <summary> /// Initializes a new instance of the <see cref="Engine"/> class. /// </summary> protected Engine() { // keeps a memory-efficient version of the osm-tags. var tagsIndex = new OsmTagsIndex(); // creates a routing interpreter. (used to translate osm-tags into a routable network) interpreter = new OsmRoutingInterpreter(); // create a routing datasource, keeps all processed osm routing data. var osmData = new MemoryRouterDataSource<SimpleWeighedEdge>(tagsIndex); // load data into this routing datasource. var fileSource = HostingEnvironment.MapPath("~/App_Data/Manchester.osm.pbf"); Stream osmXmlData = new FileInfo(fileSource).OpenRead(); // for example moscow! using (osmXmlData) { var targetData = new SimpleWeighedDataGraphProcessingTarget( osmData, interpreter, osmData.TagsIndex, VehicleEnum.Car); // replace this with PBFdataProcessSource when having downloaded a PBF file. var dataProcessorSource = new OsmSharp.Osm.Data.PBF.Raw.Processor.PBFDataProcessorSource(osmXmlData); // pre-process the data. var sorter = new DataProcessorFilterSort(); sorter.RegisterSource(dataProcessorSource); targetData.RegisterSource(sorter); targetData.Pull(); } // create the router object: there all routing functions are available. router = new Router<SimpleWeighedEdge>( osmData, interpreter, new DykstraRoutingLive(osmData.TagsIndex)); }
/// <summary> /// Calculates a route to test on. /// </summary> /// <param name="from"></param> /// <param name="to"></param> /// <returns></returns> private OsmSharpRoute Calculate(GeoCoordinate from, GeoCoordinate to) { if (_router == null) { var interpreter = new OsmRoutingInterpreter(); var tagsIndex = new OsmTagsIndex(); // do the data processing. var memoryData = new DynamicGraphRouterDataSource<SimpleWeighedEdge>(tagsIndex); var targetData = new SimpleWeighedDataGraphProcessingTarget( memoryData, interpreter, memoryData.TagsIndex, VehicleEnum.Car); var dataProcessorSource = new XmlDataProcessorSource( Assembly.GetExecutingAssembly().GetManifestResourceStream( "OsmSharp.UnitTests.test_instructions.osm")); var sorter = new DataProcessorFilterSort(); sorter.RegisterSource(dataProcessorSource); targetData.RegisterSource(sorter); targetData.Pull(); _router = new Router<SimpleWeighedEdge>( memoryData, interpreter, new DykstraRoutingLive(memoryData.TagsIndex)); } RouterPoint fromPoint = _router.Resolve(VehicleEnum.Car, from); RouterPoint toPoint = _router.Resolve(VehicleEnum.Car, to); return _router.Calculate(VehicleEnum.Car, fromPoint, toPoint); }
/// <summary> /// Calculates the TSP. /// </summary> /// <param name="dataStream"></param> /// <param name="csvStream"></param> /// <param name="pbf"></param> /// <param name="vehicleEnum"></param> /// <returns></returns> private OsmSharpRoute CalculateTSP(Stream dataStream, Stream csvStream, bool pbf, VehicleEnum vehicleEnum) { // create the router. var interpreter = new OsmRoutingInterpreter(); var tagsIndex = new OsmTagsIndex(); // do the data processing. var osmData = new DynamicGraphRouterDataSource<PreProcessedEdge>(tagsIndex); var targetData = new PreProcessedDataGraphProcessingTarget( osmData, interpreter, osmData.TagsIndex, vehicleEnum); var dataProcessorSource = new XmlDataProcessorSource(dataStream); var sorter = new DataProcessorFilterSort(); sorter.RegisterSource(dataProcessorSource); targetData.RegisterSource(sorter); targetData.Pull(); IRouter<RouterPoint> router = new Router<PreProcessedEdge>(osmData, interpreter, new DykstraRoutingPreProcessed(osmData.TagsIndex)); // read the source files. const int latitudeIdx = 2; const int longitudeIdx = 3; string[][] pointStrings = OsmSharp.Tools.DelimitedFiles.DelimitedFileHandler.ReadDelimitedFileFromStream( csvStream, DelimiterType.DotCommaSeperated); var points = new List<RouterPoint>(); int cnt = 10; foreach (string[] line in pointStrings) { if (points.Count >= cnt) { break; } var latitudeString = (string)line[latitudeIdx]; var longitudeString = (string)line[longitudeIdx]; //string route_ud = (string)line[1]; double longitude = 0; double latitude = 0; if (double.TryParse(longitudeString, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out longitude) && double.TryParse(latitudeString, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out latitude)) { var point = new GeoCoordinate(latitude, longitude); RouterPoint resolved = router.Resolve(VehicleEnum.Car, point); if (resolved != null && router.CheckConnectivity(VehicleEnum.Car, resolved, 100)) { points.Add(resolved); } } } var tspSolver = new RouterTSPWrapper<RouterPoint, RouterTSP>( new RouterTSPAEXGenetic(), router, interpreter); return tspSolver.CalculateTSP(vehicleEnum, points.ToArray()); }
/// <summary> /// Builds the data source. /// </summary> /// <returns></returns> private DynamicGraphRouterDataSource<CHEdgeData> BuildData(IRoutingInterpreter interpreter) { DynamicGraphRouterDataSource<CHEdgeData> data = null; if (data == null) { OsmTagsIndex tags_index = new OsmTagsIndex(); // do the data processing. data = new DynamicGraphRouterDataSource<CHEdgeData>(tags_index); CHEdgeDataGraphProcessingTarget target_data = new CHEdgeDataGraphProcessingTarget( data, interpreter, data.TagsIndex, VehicleEnum.Car); XmlDataProcessorSource data_processor_source = new XmlDataProcessorSource( Assembly.GetExecutingAssembly().GetManifestResourceStream("OsmSharp.UnitTests.test_network.osm")); DataProcessorFilterSort sorter = new DataProcessorFilterSort(); sorter.RegisterSource(data_processor_source); target_data.RegisterSource(sorter); target_data.Pull(); } return data; }
/// <summary> /// Tests the edge matcher in combination with dykstra routing. /// </summary> /// <param name="name"></param> /// <param name="highway"></param> /// <param name="vehicle"></param> /// <param name="matcher"></param> /// <param name="point_name"></param> /// <param name="not_found"></param> private void TestResolveOnEdgeSingle(string name, string highway, VehicleEnum vehicle, IEdgeMatcher matcher, string point_name, bool not_found) { GeoCoordinate from_name = new GeoCoordinate(51.0003, 4.0007); GeoCoordinate to_name = new GeoCoordinate(51.0003, 4.0008); GeoCoordinate from_noname = new GeoCoordinate(51.0, 4.0007); GeoCoordinate to_noname = new GeoCoordinate(51.0, 4.0008); Dictionary<string, string> point_tags = new Dictionary<string, string>(); point_tags["name"] = point_name; Dictionary<string, string> tags = new Dictionary<string, string>(); tags["highway"] = highway; //tags["name"] = name; OsmTagsIndex tags_index = new OsmTagsIndex(); // do the data processing. DynamicGraphRouterDataSource<SimpleWeighedEdge> data = new DynamicGraphRouterDataSource<SimpleWeighedEdge>(tags_index); uint vertex_noname1 = data.AddVertex((float)from_noname.Latitude, (float)from_noname.Longitude); uint vertex_noname2 = data.AddVertex((float)to_noname.Latitude, (float)to_noname.Longitude); data.AddArc(vertex_noname1, vertex_noname2, new SimpleWeighedEdge() { IsForward = true, Tags = tags_index.Add(tags), Weight = 100 }, null); tags = new Dictionary<string, string>(); tags["highway"] = highway; tags["name"] = name; uint vertex_name1 = data.AddVertex((float)from_name.Latitude, (float)from_name.Longitude); uint vertex_name2 = data.AddVertex((float)to_name.Latitude, (float)to_name.Longitude); data.AddArc(vertex_name1, vertex_name2, new SimpleWeighedEdge() { IsForward = true, Tags = tags_index.Add(tags), Weight = 100 }, null); IRoutingInterpreter interpreter = new OsmRoutingInterpreter(); // creates the data. IBasicRouter<SimpleWeighedEdge> router = new DykstraRoutingLive( data.TagsIndex); GeoCoordinate noname_location = new GeoCoordinate( (from_noname.Latitude + to_noname.Latitude) / 2.0, (from_noname.Longitude + to_noname.Longitude) / 2.0); GeoCoordinate name_location = new GeoCoordinate( (from_name.Latitude + to_name.Latitude) / 2.0, (from_name.Longitude + to_name.Longitude) / 2.0); float delta = 0.01f; SearchClosestResult result = router.SearchClosest(data, interpreter, vehicle, noname_location, delta, matcher, point_tags); if (result.Distance < double.MaxValue) { // there is a result. Assert.IsFalse(not_found, "A result was found but was supposed not to be found!"); if (name == point_name) { // the name location was supposed to be found! Assert.IsTrue(result.Vertex1 == vertex_name1 || result.Vertex1 == vertex_name2); Assert.IsTrue(result.Vertex2 == vertex_name1 || result.Vertex2 == vertex_name2); } else { // the noname location was supposed to be found! Assert.IsTrue(result.Vertex1 == vertex_noname1 || result.Vertex1 == vertex_noname2); Assert.IsTrue(result.Vertex2 == vertex_noname1 || result.Vertex2 == vertex_noname2); } return; } Assert.IsTrue(not_found, "A result was not found but was supposed to be found!"); }
public void RoutingSerializationDataSourceTest() { const string embeddedString = "OsmSharp.UnitTests.test_network.osm"; // create the tags index. var tagsIndex = new OsmTagsIndex(); // creates a new interpreter. var interpreter = new OsmRoutingInterpreter(); // do the data processing. var original = new DynamicGraphRouterDataSource<PreProcessedEdge>(tagsIndex); var targetData = new PreProcessedDataGraphProcessingTarget( original, interpreter, original.TagsIndex, VehicleEnum.Car); var dataProcessorSource = new XmlDataProcessorSource( Assembly.GetExecutingAssembly().GetManifestResourceStream(embeddedString)); targetData.RegisterSource(dataProcessorSource); targetData.Pull(); // create serializer. var routingSerializer = new V1RoutingSerializer(); // serialize/deserialize. IBasicRouterDataSource<PreProcessedEdge> deserializedVersion; byte[] byteArray; using (var stream = new MemoryStream()) { try { routingSerializer.Serialize(stream, original); byteArray = stream.ToArray(); } catch (Exception ex) { if (Debugger.IsAttached) { Debugger.Break(); } throw; } } using (var stream = new MemoryStream(byteArray)) { try { deserializedVersion = routingSerializer.Deserialize(stream); } catch (Exception ex) { if (Debugger.IsAttached) { Debugger.Break(); } throw; } } //Assert.AreEqual(original.VertexCount, deserializedVersion.VertexCount); Assert.AreEqual(original.TagsIndex.Get(0), deserializedVersion.TagsIndex.Get(0)); }
public void RoutingSerializationRoutingTest() { const string embeddedString = "OsmSharp.UnitTests.test_network.osm"; // create the tags index. var tagsIndex = new OsmTagsIndex(); // creates a new interpreter. var interpreter = new OsmRoutingInterpreter(); // do the data processing. var original = new DynamicGraphRouterDataSource<PreProcessedEdge>(tagsIndex); var targetData = new PreProcessedDataGraphProcessingTarget( original, interpreter, original.TagsIndex, VehicleEnum.Car); var dataProcessorSource = new XmlDataProcessorSource( Assembly.GetExecutingAssembly().GetManifestResourceStream(embeddedString)); targetData.RegisterSource(dataProcessorSource); targetData.Pull(); // create serializer. var routingSerializer = new V1RoutingSerializer(); // serialize/deserialize. byte[] byteArray; using (var stream = new MemoryStream()) { try { routingSerializer.Serialize(stream, original); byteArray = stream.ToArray(); } catch (Exception ex) { if (Debugger.IsAttached) { Debugger.Break(); } throw; } } IBasicRouterDataSource<PreProcessedEdge> deserializedVersion = routingSerializer.Deserialize(new MemoryStream(byteArray)); Assert.AreEqual(original.TagsIndex.Get(0), deserializedVersion.TagsIndex.Get(0)); // try to do some routing on the deserialized version. var basicRouter = new DykstraRoutingPreProcessed(deserializedVersion.TagsIndex); IRouter<RouterPoint> router = new Router<PreProcessedEdge>( deserializedVersion, interpreter, basicRouter); RouterPoint source = router.Resolve(VehicleEnum.Car, new GeoCoordinate(51.0578532, 3.7192229)); RouterPoint target = router.Resolve(VehicleEnum.Car, new GeoCoordinate(51.0576193, 3.7191801)); // calculate the route. OsmSharpRoute route = router.Calculate(VehicleEnum.Car, source, target); Assert.IsNotNull(route); Assert.AreEqual(5, route.Entries.Length); float latitude, longitude; deserializedVersion.GetVertex(20, out latitude, out longitude); Assert.AreEqual(latitude, route.Entries[0].Latitude, 0.00001); Assert.AreEqual(longitude, route.Entries[0].Longitude, 0.00001); Assert.AreEqual(RoutePointEntryType.Start, route.Entries[0].Type); deserializedVersion.GetVertex(21, out latitude, out longitude); Assert.AreEqual(latitude, route.Entries[1].Latitude, 0.00001); Assert.AreEqual(longitude, route.Entries[1].Longitude, 0.00001); Assert.AreEqual(RoutePointEntryType.Along, route.Entries[1].Type); deserializedVersion.GetVertex(16, out latitude, out longitude); Assert.AreEqual(latitude, route.Entries[2].Latitude, 0.00001); Assert.AreEqual(longitude, route.Entries[2].Longitude, 0.00001); Assert.AreEqual(RoutePointEntryType.Along, route.Entries[2].Type); deserializedVersion.GetVertex(22, out latitude, out longitude); Assert.AreEqual(latitude, route.Entries[3].Latitude, 0.00001); Assert.AreEqual(longitude, route.Entries[3].Longitude, 0.00001); Assert.AreEqual(RoutePointEntryType.Along, route.Entries[3].Type); deserializedVersion.GetVertex(23, out latitude, out longitude); Assert.AreEqual(latitude, route.Entries[4].Latitude, 0.00001); Assert.AreEqual(longitude, route.Entries[4].Longitude, 0.00001); Assert.AreEqual(RoutePointEntryType.Stop, route.Entries[4].Type); }
/// <summary> /// Builds an in-memory data source from an xml data stream. /// </summary> /// <param name="stream"></param> /// <returns></returns> private DynamicGraphRouterDataSource<CHEdgeData> BuildData(Stream stream) { OsmRoutingInterpreter interpreter = new OsmRoutingInterpreter(); OsmTagsIndex tags_index = new OsmTagsIndex(); // do the data processing. DynamicGraphRouterDataSource<CHEdgeData> data = new DynamicGraphRouterDataSource<CHEdgeData>(tags_index); CHEdgeDataGraphProcessingTarget target_data = new CHEdgeDataGraphProcessingTarget( data, interpreter, data.TagsIndex, VehicleEnum.Car); XmlDataProcessorSource data_processor_source = new XmlDataProcessorSource(stream); DataProcessorFilterSort sorter = new DataProcessorFilterSort(); sorter.RegisterSource(data_processor_source); target_data.RegisterSource(sorter); target_data.Pull(); return data; }
/// <summary> /// Prepares the router. /// </summary> private void PrepareRouter() { #if DEBUG OsmSharp.Tools.Output.OutputStreamHost.RegisterOutputStream( new OsmSharp.Tools.Output.DebugOutputStream()); #endif // initialize the interpreters. _interpreter = new OsmRoutingInterpreter(); string file = OperationProcessor.Settings["pbf_file"]; var tagsIndex = new OsmTagsIndex(new ObjectTable<OsmTagsIndex.OsmTags>(true)); // do the data processing. var data = new DynamicGraphRouterDataSource<SimpleWeighedEdge>(tagsIndex); var targetData = new SimpleWeighedDataGraphProcessingTarget( data, _interpreter, data.TagsIndex, VehicleEnum.Car); var dataProcessorSource = new PBFDataProcessorSource((new FileInfo( file)).OpenRead()); var progressSource = new ProgressDataProcessorSource(dataProcessorSource); targetData.RegisterSource(progressSource); targetData.Pull(); _data = data; // only set the data property here now after pre-processing! }