示例#1
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();
        }
示例#2
0
        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));
        }
        public void PBFDataProcessorSourceReset()
        {
            // generate the source.
            PBFDataProcessorSource source = new PBFDataProcessorSource(
                Assembly.GetExecutingAssembly().GetManifestResourceStream("OsmSharp.UnitTests.api.osm.pbf"));

            // 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();
            }
        }
示例#4
0
        /// <summary>
        /// Tests reading a PBF and see if the data is there.
        /// </summary>
        /// <param name="resource"></param>
        private void TestReadPBF(string resource)
        {
            // create the pbf source from a pbf in the resources of this assembly.
            PBFDataProcessorSource source = new PBFDataProcessorSource(
                Assembly.GetExecutingAssembly().GetManifestResourceStream(resource));

            // pull the data from the source into the memory data source.
            MemoryDataSource data = new MemoryDataSource();
            data.PullFromSource(source);

            // test the data.
            this.TestData(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!
        }