Example #1
0
        /// <summary>
        /// Creates a new router data source.
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="compressed"></param>
        /// <param name="tileMetas"></param>
        /// <param name="zoom"></param>
        /// <param name="v1RoutingDataSourceSerializer"></param>
        /// <param name="initialCapacity"></param>
        internal CHEdgeDataDataSource(
            Stream stream, bool compressed,
            CHEdgeDataDataSourceSerializer.SerializableGraphTileMetas tileMetas,
            int zoom, CHEdgeDataDataSourceSerializer v1RoutingDataSourceSerializer,
            int initialCapacity = 1000)
        {
            _tagsIndex   = new SimpleTagsIndex();
            _vertices    = new SparseArray <Vertex>(initialCapacity);
            _coordinates = new SparseArray <Location>(initialCapacity);

            _vertexIndex = new QuadTree <GeoCoordinate, uint>();

            _graphTileMetas = new Dictionary <Tile, TileStreamPosition>();
            for (int tileIdx = 0; tileIdx < tileMetas.TileX.Length; tileIdx++)
            {
                _graphTileMetas.Add(
                    new Tile(tileMetas.TileX[tileIdx], tileMetas.TileY[tileIdx], zoom),
                    new TileStreamPosition()
                {
                    Offset = tileMetas.Offset[tileIdx],
                    Length = tileMetas.Length[tileIdx]
                });
            }

            _loadedTiles    = new HashSet <Tile>();
            _tilesPerVertex = new Dictionary <uint, Tile>();
            _zoom           = zoom;
            _routingDataSourceSerializer = v1RoutingDataSourceSerializer;
            _stream     = stream;
            _compressed = compressed;
        }
Example #2
0
        /// <summary>
        /// Creates a new osm memory router data source.
        /// </summary>
        /// <param name="graph"></param>
        /// <param name="tagsIndex"></param>
        /// <exception cref="ArgumentNullException"></exception>
        public DynamicGraphRouterDataSource(IDynamicGraph <TEdgeData> graph, ITagsIndex tagsIndex)
        {
            if (graph == null)
            {
                throw new ArgumentNullException("graph");
            }
            if (tagsIndex == null)
            {
                throw new ArgumentNullException("tagsIndex");
            }

            _graph       = graph;
            _vertexIndex = new QuadTree <GeoCoordinate, uint>();
            _tagsIndex   = tagsIndex;

            _supportedVehicles = new HashSet <Vehicle>();

            // add the current graph's vertices to the vertex index.
            for (uint newVertexId = 1; newVertexId < graph.VertexCount + 1; newVertexId++)
            {
                // add to the CHRegions.
                float latitude, longitude;
                graph.GetVertex(newVertexId, out latitude, out longitude);
                _vertexIndex.Add(new GeoCoordinate(latitude, longitude), newVertexId);
            }
        }
Example #3
0
        /// <summary>
        /// Try and find matching lines.
        /// </summary>
        /// <param name="lines"></param>
        /// <param name="points"></param>
        /// <returns></returns>
        private MatchPosition FindMatch(ILocatedObjectIndex <PointF2D, Scene2D.ScenePoints> linesIndex, Dictionary <Scene2D.ScenePoints, Scene2DStylesSet> lines,
                                        double[] x, double[] y, Scene2DStylesSet style, float epsilon, out Scene2D.ScenePoints found)
        {
            // build box.
            var box = new BoxF2D(x, y);

            box = box.ResizeWith(epsilon * 1.1);

            // get all geometries in this box.
            var potentialMatches = linesIndex.GetInside(box);

            // find a match in the potential matches list.
            PointF2D first = new PointF2D(x[0], y[0]);
            PointF2D last  = new PointF2D(x[x.Length - 1], y[y.Length - 1]);

            MatchPosition position = MatchPosition.None;

            found = null;
            foreach (var line in potentialMatches)
            {
                // check first.
                PointF2D potentialFirst = new PointF2D(line.X[0], line.Y[0]);
                PointF2D potentialLast  = new PointF2D(line.X[line.X.Length - 1], line.Y[line.Y.Length - 1]);
                if (first.Distance(potentialFirst) < epsilon)
                {
                    found    = line;
                    position = MatchPosition.FirstFirst;
                }
                else if (last.Distance(potentialFirst) < epsilon)
                {
                    found    = line;
                    position = MatchPosition.LastFirst;
                }
                else if (first.Distance(potentialLast) < epsilon)
                {
                    found    = line;
                    position = MatchPosition.FirstLast;
                }
                else if (last.Distance(potentialLast) < epsilon)
                {
                    found    = line;
                    position = MatchPosition.LastLast;
                }

                Scene2DStylesSet styleValue;
                if (position != MatchPosition.None && lines.TryGetValue(line, out styleValue) && styleValue.Equals(style))
                {
                    break;
                }
                else
                {
                    position = MatchPosition.None;
                    found    = null;
                }
            }
            return(position);
        }
Example #4
0
        /// <summary>
        /// Creates a new osm memory router data source.
        /// </summary>
        /// <exception cref="ArgumentNullException"></exception>
        public DynamicGraphRouterDataSource(ITagsIndex tagsIndex)
        {
            if (tagsIndex == null)
            {
                throw new ArgumentNullException("tagsIndex");
            }

            _graph       = new MemoryDynamicGraph <TEdgeData>();
            _vertexIndex = new QuadTree <GeoCoordinate, uint>();
            _tagsIndex   = tagsIndex;

            _supportedVehicles = new HashSet <Vehicle>();
        }
Example #5
0
 /// <summary>
 /// Rebuilds the vertex index.
 /// </summary>
 public void RebuildVertexIndex()
 {
     _vertexIndex = new QuadTree <GeoCoordinate, uint>();
     for (uint vertex = 0; vertex <= _graph.VertexCount; vertex++)
     {
         float latitude, longitude;
         if (_graph.GetVertex(vertex, out latitude, out longitude))
         {
             _vertexIndex.Add(new GeoCoordinate(latitude, longitude),
                              vertex);
         }
     }
 }
Example #6
0
        /// <summary>
        /// Creates a new osm memory router data source.
        /// </summary>
        /// <exception cref="ArgumentNullException"></exception>
        public DynamicGraphRouterDataSource(ITagsCollectionIndexReadonly tagsIndex, int initSize)
        {
            if (tagsIndex == null)
            {
                throw new ArgumentNullException("tagsIndex");
            }

            _graph       = new MemoryDynamicGraph <TEdgeData>(initSize);
            _vertexIndex = null; // do not create an index initially.
            _tagsIndex   = tagsIndex;

            _supportedVehicles = new HashSet <Vehicle>();
        }
Example #7
0
        /// <summary>
        /// Creates a new osm memory router data source.
        /// </summary>
        /// <param name="graph"></param>
        /// <param name="tagsIndex"></param>
        /// <exception cref="ArgumentNullException"></exception>
        public RouterDataSource(GraphBase <TEdgeData> graph, ITagsIndex tagsIndex)
        {
            if (graph == null)
            {
                throw new ArgumentNullException("graph");
            }
            if (tagsIndex == null)
            {
                throw new ArgumentNullException("tagsIndex");
            }

            _graph       = graph;
            _vertexIndex = null; // do not create an index initially.
            _tagsIndex   = tagsIndex;

            _supportedVehicles = new HashSet <Vehicle>();
        }
Example #8
0
 /// <summary>
 /// Deactivates the vertex index.
 /// </summary>
 public void DropVertexIndex()
 {
     _vertexIndex = null;
 }
Example #9
0
        /// <summary>
        /// Tests adding some simple data.
        /// </summary>
        protected void DoTestSimple()
        {
            // create the index.
            ILocatedObjectIndex <GeoCoordinate, LocatedObjectData> index = this.CreateIndex();

            // add the data.
            GeoCoordinate     point1      = new GeoCoordinate(0, 0);
            LocatedObjectData point1_data = new LocatedObjectData()
            {
                SomeData = point1.ToString()
            };
            GeoCoordinate     point2      = new GeoCoordinate(1, 1);
            LocatedObjectData point2_data = new LocatedObjectData()
            {
                SomeData = point2.ToString()
            };

            GeoCoordinateBox location_box = new GeoCoordinateBox(
                new GeoCoordinate(point1.Latitude - 0.0001, point1.Longitude - 0.0001),
                new GeoCoordinate(point1.Latitude + 0.0001, point1.Longitude + 0.0001));

            // try and get data from empty index.
            // regression test for issue: https://osmsharp.codeplex.com/workitem/1244
            IEnumerable <LocatedObjectData> location_box_data = index.GetInside(location_box);

            Assert.IsNotNull(location_box_data);
            Assert.AreEqual(0, location_box_data.Count());

            // try point1.
            index.Add(point1, point1_data);

            location_box_data = index.GetInside(
                location_box);
            Assert.IsNotNull(location_box_data);

            bool found = false;

            foreach (LocatedObjectData location_data in location_box_data)
            {
                if (location_data.SomeData == point1.ToString())
                {
                    found = true;
                }
            }
            Assert.IsTrue(found, string.Format("Data added at location {0} not found in box {1}!",
                                               point1, location_box));

            // try point2.
            index.Add(point2, point2_data);
            location_box = new GeoCoordinateBox(
                new GeoCoordinate(point2.Latitude - 0.0001, point2.Longitude - 0.0001),
                new GeoCoordinate(point2.Latitude + 0.0001, point2.Longitude + 0.0001));

            location_box_data = index.GetInside(
                location_box);
            Assert.IsNotNull(location_box_data);

            found = false;
            foreach (LocatedObjectData location_data in location_box_data)
            {
                if (location_data.SomeData == point2.ToString())
                {
                    found = true;
                }
            }
            Assert.IsTrue(found, string.Format("Data added at location {0} not found in box {1}!",
                                               point2, location_box));
        }
Example #10
0
        /// <summary>
        /// Tests adding a lot of random data.
        /// </summary>
        /// <param name="count"></param>
        public void DoTestAddingRandom(int count)
        {
            ILocatedObjectIndex <GeoCoordinate, LocatedObjectData> index = this.CreateIndex();

            GeoCoordinateBox        box       = new GeoCoordinateBox(new GeoCoordinate(50, 3), new GeoCoordinate(40, 2));
            HashSet <GeoCoordinate> locations = new HashSet <GeoCoordinate>();
            Random random = new Random();

            while (count > 0)
            {
                GeoCoordinate     location = box.GenerateRandomIn(random);
                LocatedObjectData data     = new LocatedObjectData()
                {
                    SomeData = location.ToString()
                };
                locations.Add(location);
                index.Add(location, data);

                // try immidiately after.
                GeoCoordinateBox location_box = new GeoCoordinateBox(
                    new GeoCoordinate(location.Latitude - 0.0001, location.Longitude - 0.0001),
                    new GeoCoordinate(location.Latitude + 0.0001, location.Longitude + 0.0001));

                IEnumerable <LocatedObjectData> location_box_data = index.GetInside(
                    location_box);

                Assert.IsNotNull(location_box_data);

                bool found = false;
                foreach (LocatedObjectData location_data in location_box_data)
                {
                    if (location_data.SomeData == location.ToString())
                    {
                        found = true;
                    }
                }
                Assert.IsTrue(found, string.Format("Data added at location {0} not found in box {1}!",
                                                   location, location_box));

                count--;
            }

            foreach (GeoCoordinate location in locations)
            {
                GeoCoordinateBox location_box = new GeoCoordinateBox(
                    new GeoCoordinate(location.Latitude - 0.0001, location.Longitude - 0.0001),
                    new GeoCoordinate(location.Latitude + 0.0001, location.Longitude + 0.0001));

                IEnumerable <LocatedObjectData> location_box_data = index.GetInside(
                    location_box);

                Assert.IsNotNull(location_box_data);

                bool found = false;
                foreach (LocatedObjectData location_data in location_box_data)
                {
                    if (location_data.SomeData == location.ToString())
                    {
                        found = true;
                    }
                }
                Assert.IsTrue(found, string.Format("Data added at location {0} not found in box {1}!",
                                                   location, location_box));
            }
        }
Example #11
0
        /// <summary>
        /// Try and find matching lines.
        /// </summary>
        /// <param name="lines"></param>
        /// <param name="points"></param>
        /// <returns></returns>
        private MatchPosition FindMatch(ILocatedObjectIndex<PointF2D, Scene2D.ScenePoints> linesIndex, Dictionary<Scene2D.ScenePoints, Scene2DStylesSet> lines, 
            double[] x, double[] y, Scene2DStylesSet style, float epsilon, out Scene2D.ScenePoints found)
        {
            // build box.
            var box = new BoxF2D(x, y);
            box = box.ResizeWith(epsilon * 1.1);

            // get all geometries in this box.
            var potentialMatches = linesIndex.GetInside(box);

            // find a match in the potential matches list.
            PointF2D first = new PointF2D(x[0], y[0]);
            PointF2D last = new PointF2D(x[x.Length - 1], y[y.Length - 1]);

            MatchPosition position = MatchPosition.None;
            found = null;
            foreach (var line in potentialMatches)
            {
                // check first.
                PointF2D potentialFirst = new PointF2D(line.X[0], line.Y[0]);
                PointF2D potentialLast = new PointF2D(line.X[line.X.Length - 1], line.Y[line.Y.Length - 1]);
                if (first.Distance(potentialFirst) < epsilon)
                {
                    found = line;
                    position = MatchPosition.FirstFirst;
                }
                else if (last.Distance(potentialFirst) < epsilon)
                {
                    found = line;
                    position = MatchPosition.LastFirst;
                }
                else if (first.Distance(potentialLast) < epsilon)
                {
                    found = line;
                    position = MatchPosition.FirstLast;
                }
                else if (last.Distance(potentialLast) < epsilon)
                {
                    found = line;
                    position = MatchPosition.LastLast;
                }

                Scene2DStylesSet styleValue;
                if (position != MatchPosition.None && lines.TryGetValue(line, out styleValue) && styleValue.Equals(style))
                {
                    break;
                }
                else
                {
                    position = MatchPosition.None;
                    found = null;
                }
            }
            return position;
        }