/// <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();
        }
示例#2
0
        /// <summary>
        /// Returns the data from the given data source that is inside the given box.
        /// </summary>
        /// <param name="dataSourceName">The name of the datasource.</param>
        /// <param name="box">The bounding box.</param>
        /// <returns></returns>
        public string RequestData(string dataSourceName, GeoCoordinateBox box)
        {
            string dataPath = ConfigurationManager.AppSettings["datapath"];

            // check of the file exists.
            var pbfFile = new FileInfo(dataPath + dataSourceName + ".pbf");
            var xmlFile = new FileInfo(dataPath + dataSourceName);

            DataProcessorSource source = null;
            FileStream sourceStream = null;
            NamedSource namedSource;
            if (pbfFile.Exists)
            { // first try PBF: more efficient.
                // create PBF source.
                sourceStream = pbfFile.OpenRead();
                source = new PBFDataProcessorSource(sourceStream);

                // create filter.
                DataProcessorFilter filter = new DataProcessorFilterBoundingBox(box);
                filter.RegisterSource(source);
                source = filter;
            }
            else if (xmlFile.Exists)
            { // then try XML.
                // create XML source.
                sourceStream = xmlFile.OpenRead();
                source = new XmlDataProcessorSource(sourceStream);

                // create filter.
                DataProcessorFilter filter = new DataProcessorFilterBoundingBox(box);
                filter.RegisterSource(source);
                source = filter;
            }
            else if (NamedSourceCollection.Instance.TryGetSource(dataSourceName, out namedSource))
            { // then try a named source.
                source = namedSource.Get(box);
            }
            else
            { // oeps! file or named source not found!
                throw new FileNotFoundException("File or name source {0} not found!", dataSourceName);
            }

            // create the target.
            var result = new StringBuilder();
            var writer = new StringWriter(result);
            var target = new XmlDataProcessorTarget(writer);

            // execute the processing.
            target.RegisterSource(source);
            target.Pull();

            // close the source stream if needed.
            if (sourceStream != null)
            {
                sourceStream.Close();
                sourceStream.Dispose();
            }

            return result.ToString();
        }
        /// <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));
        }
        public void SQLServerRoutingRegressionTests1()
        {
            // the connectionstring.
            string connectionString =
                @"Server=windows8-test\sqlexpress;User Id=OsmSharp;Password=OsmSharp;Database=OsmData;";

            // drop whatever data is there.
            SqlConnection sqlConnection = new SqlConnection(connectionString);
            sqlConnection.Open();
            SQLServer.SimpleSchema.SchemaTools.SQLServerSimpleSchemaTools.Remove(sqlConnection);
            sqlConnection.Close();

            // create the source from the osm file.
            var xmlSource = new XmlDataProcessorSource(
                Assembly.GetExecutingAssembly().GetManifestResourceStream(
                    "OsmSharp.Osm.Data.SQLServer.Unittests.Data.ukraine1.osm"));

            // create the SQLServer processor target.
            var testTarget = new SQLServerSimpleSchemaDataProcessorTarget(connectionString, true);
            testTarget.RegisterSource(xmlSource); // register the source.
            testTarget.Pull(); // pull the data from source to target.

            var source = new SQLServerSimpleSchemaSource(
                connectionString);
            var tagsIndex = new OsmTagsIndex();
            var interpreter = new OsmRoutingInterpreter();
            var routingData = new OsmSourceRouterDataSource(
                interpreter, tagsIndex, source, VehicleEnum.Car);

            IRouter<RouterPoint> router = new Router<PreProcessedEdge>(routingData, interpreter,
                new DykstraRoutingPreProcessed(routingData.TagsIndex));

            OsmSharpRoute route;

            RouterPoint point1 = router.Resolve(VehicleEnum.Car,
                new GeoCoordinate(50.3150034243338, 34.8784106812928));
            RouterPoint point2 = router.Resolve(VehicleEnum.Car,
                new GeoCoordinate(50.3092549484347, 34.8894929841615));
            route = router.Calculate(VehicleEnum.Car, point1, point2);

            Assert.IsNotNull(route);
        }
        /// <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;
        }
        public void XmlDataProcessorSourceReset()
        {
            // generate the source.
            XmlDataProcessorSource source = new XmlDataProcessorSource(
                Assembly.GetExecutingAssembly().GetManifestResourceStream("OsmSharp.UnitTests.api.osm"));

            // pull the data out.
            DataProcessorTargetEmpty target = new DataProcessorTargetEmpty();
            target.RegisterSource(source);
            target.Pull();

            // reset the source.
            if (source.CanReset)
            {
                source.Reset();

                // pull the data again.
                target.Pull();
            }
        }
        /// <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);
        }
示例#8
0
        /// <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;
        }
        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);
        }
示例#12
0
        /// <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;
        }