Пример #1
0
        /// <summary>
        /// Adds a new vertex to this graph.
        /// </summary>
        /// <param name="latitude"></param>
        /// <param name="longitude"></param>
        /// <param name="neighboursEstimate"></param>
        /// <returns></returns>
        public uint AddVertex(float latitude, float longitude, byte neighboursEstimate)
        {
            uint vertex = _graph.AddVertex(latitude, longitude, neighboursEstimate);

            _vertexIndex.Add(new GeoCoordinate(latitude, longitude),
                             vertex);
            return(vertex);
        }
Пример #2
0
        /// <summary>
        /// Adds a new vertex.
        /// </summary>
        /// <param name="latitude"></param>
        /// <param name="longitude"></param>
        /// <returns></returns>
        public override uint AddVertex(float latitude, float longitude)
        {
            uint vertex = _graph.AddVertex(latitude, longitude);

            if (_vertexIndex != null)
            {
                _vertexIndex.Add(new GeoCoordinate(latitude, longitude),
                                 vertex);
            }
            return(vertex);
        }
Пример #3
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);
            }
        }
Пример #4
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);
         }
     }
 }
Пример #5
0
        /// <summary>
        /// Loads the missing tiles.
        /// </summary>
        /// <param name="tile"></param>
        internal void LoadMissingTile(Tile tile)
        {
            if (!_loadedTiles.Contains(tile))
            { // the tile was not loaded yet.
                TileStreamPosition meta;
                if (_graphTileMetas.TryGetValue(tile, out meta))
                { // the meta data is available.
                    CHEdgeDataDataSourceSerializer.SerializableGraphTile tileData =
                        _routingDataSourceSerializer.DeserializeTile(_stream, meta.Offset, meta.Length, _compressed);
                    double top  = tile.TopLeft.Latitude;
                    double left = tile.TopLeft.Longitude;
                    for (int vertexIdx = 0; vertexIdx < tileData.Ids.Length; vertexIdx++)
                    {
                        // resize.
                        this.Resize(tileData.Ids[vertexIdx] + 1);

                        // create the location and calculate lat/lon.
                        var vertexLocation = new Location();
                        vertexLocation.Latitude = (float)(top - (((double)tileData.Latitude[vertexIdx] / (double)ushort.MaxValue)
                                                                 * tile.Box.DeltaLat));
                        vertexLocation.Longitude = (float)((((double)tileData.Longitude[vertexIdx] / (double)ushort.MaxValue)
                                                            * tile.Box.DeltaLon) +
                                                           left);
                        _coordinates[(int)tileData.Ids[vertexIdx]] = vertexLocation;

                        // convert the arcs.
                        if (tileData.Arcs[vertexIdx] != null && tileData.Arcs[vertexIdx].DestinationId != null)
                        {
                            var arcs = new KeyValuePair <uint, CHEdgeData> [tileData.Arcs[vertexIdx].DestinationId.Length];
                            for (int idx = 0; idx < tileData.Arcs[vertexIdx].DestinationId.Length; idx++)
                            {
                                // create the tags collection.
                                TagsCollection tagsCollection = new SimpleTagsCollection();
                                if (tileData.Arcs[vertexIdx].Tags[idx].Keys != null)
                                {
                                    for (int tagsIdx = 0;
                                         tagsIdx < tileData.Arcs[vertexIdx].Tags[idx].Keys.Length;
                                         tagsIdx++)
                                    {
                                        string key   = tileData.StringTable[tileData.Arcs[vertexIdx].Tags[idx].Keys[tagsIdx]];
                                        string value = tileData.StringTable[tileData.Arcs[vertexIdx].Tags[idx].Values[tagsIdx]];

                                        tagsCollection.Add(key, value);
                                    }
                                }
                                uint tags = _tagsIndex.Add(tagsCollection);

                                // create the liveedge.
                                var edge = new CHEdgeData();
                                edge.SetDirection(tileData.Arcs[vertexIdx].Forward[idx],
                                                  tileData.Arcs[vertexIdx].Backward[idx], true);
                                edge.Weight = tileData.Arcs[vertexIdx].Weight[idx];
                                edge.Tags   = tags;

                                // convert the arc.
                                arcs[idx] = new KeyValuePair <uint, CHEdgeData>(
                                    tileData.Arcs[vertexIdx].DestinationId[idx], edge);

                                // store the target tile.
                                var targetTile = new Tile(tileData.Arcs[vertexIdx].TileX[idx],
                                                          tileData.Arcs[vertexIdx].TileY[idx], _zoom);
                                if (!targetTile.Equals(tile) && !_loadedTiles.Contains(targetTile))
                                {
                                    _tilesPerVertex[tileData.Arcs[vertexIdx].DestinationId[idx]] = targetTile;
                                }
                            }
                            _vertices[(int)tileData.Ids[vertexIdx]] = new Vertex()
                            {
                                Arcs = arcs
                            };
                        }
                        _vertexIndex.Add(new GeoCoordinate(vertexLocation.Latitude,
                                                           vertexLocation.Longitude), tileData.Ids[vertexIdx]);
                    }
                }

                _loadedTiles.Add(tile); // tile is loaded.
            }
        }
Пример #6
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));
        }
Пример #7
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));
            }
        }