Esempio n. 1
0
 /// <summary>
 /// Creates a new routing network from existing data.
 /// </summary>
 private RoutingNetwork(GeometricGraph graph, ArrayBase <uint> edgeData,
                        float maxEdgeDistance = Constants.DefaultMaxEdgeDistance)
 {
     _graph           = graph;
     _edgeData        = edgeData;
     _maxEdgeDistance = maxEdgeDistance;
 }
Esempio n. 2
0
 /// <summary>
 /// Creates a new routing network.
 /// </summary>
 public RoutingNetwork(MemoryMap map, GeometricGraph graph,
                       float maxEdgeDistance = Constants.DefaultMaxEdgeDistance)
 {
     _graph           = graph;
     _edgeData        = new MemoryArray <uint>(_edgeDataSize * graph.EdgeCount);
     _maxEdgeDistance = maxEdgeDistance;
 }
Esempio n. 3
0
        public void SortHilbertTestSteps4()
        {
            var n = 4;

            // build locations.
            var locations = new List <Coordinate>();

            locations.Add(new Coordinate(-90, -180));
            locations.Add(new Coordinate(-90, -60));
            locations.Add(new Coordinate(-90, 60));
            locations.Add(new Coordinate(-90, 180));
            locations.Add(new Coordinate(-30, -180));
            locations.Add(new Coordinate(-30, -60));
            locations.Add(new Coordinate(-30, 60));
            locations.Add(new Coordinate(-30, 180));
            locations.Add(new Coordinate(30, -180));
            locations.Add(new Coordinate(30, -60));
            locations.Add(new Coordinate(30, 60));
            locations.Add(new Coordinate(30, 180));
            locations.Add(new Coordinate(90, -180));
            locations.Add(new Coordinate(90, -60));
            locations.Add(new Coordinate(90, 60));
            locations.Add(new Coordinate(90, 180));

            // build graph.
            var graph = new GeometricGraph(1);

            for (var vertex = 0; vertex < locations.Count; vertex++)
            {
                graph.AddVertex((uint)vertex, locations[vertex].Latitude,
                                locations[vertex].Longitude);
            }

            // build a sorted version in-place.
            graph.Sort(n);

            // test if sorted.
            for (uint vertex = 1; vertex < graph.VertexCount - 1; vertex++)
            {
                Assert.IsTrue(
                    graph.Distance(n, vertex) <=
                    graph.Distance(n, vertex + 1));
            }

            // sort locations.
            locations.Sort((x, y) =>
            {
                return(HilbertCurve.HilbertDistance(x.Latitude, x.Longitude, n).CompareTo(
                           HilbertCurve.HilbertDistance(y.Latitude, y.Longitude, n)));
            });

            // confirm sort.
            for (uint vertex = 0; vertex < graph.VertexCount; vertex++)
            {
                float latitude, longitude;
                graph.GetVertex(vertex, out latitude, out longitude);
                Assert.AreEqual(latitude, locations[(int)vertex].Latitude);
                Assert.AreEqual(longitude, locations[(int)vertex].Longitude);
            }
        }
Esempio n. 4
0
 /// <summary>
 /// Creates a new tile-based isochrone builder.
 /// </summary>
 /// <param name="edgeVisitor">The algorithm that visits the edges.</param>
 /// <param name="limits">The limits to generate isochrones for.</param>
 /// <param name="level">The level of detail specified as an OpenStreetMap tile zoom level.</param>
 public TileBasedIsochroneBuilder(GeometricGraph graph, IEdgeVisitor <float> edgeVisitor, List <float> limits, int level)
 {
     _graph       = graph;
     _limits      = limits;
     _level       = level;
     _edgeVisitor = edgeVisitor;
 }
Esempio n. 5
0
        public void TestEdgeCount()
        {
            var graph = new GeometricGraph(1, 100);

            // add edge.
            graph.AddVertex(0, 0, 0);
            graph.AddVertex(1, 0, 0);
            graph.AddEdge(0, 1, new uint[] { 10 }, null);
            Assert.AreEqual(1, graph.EdgeCount);

            graph = new GeometricGraph(1, 100);

            // add edge.
            graph.AddVertex(0, 0, 0);
            graph.AddVertex(1, 0, 0);
            graph.AddVertex(11001, 0, 0);
            graph.AddEdge(0, 1, new uint[] { 10 }, null);
            graph.AddEdge(0, 11001, new uint[] { 10 }, null);
            Assert.AreEqual(2, graph.EdgeCount);

            graph.AddEdge(0, 11001, new uint[] { 20 }, null);
            Assert.AreEqual(3, graph.EdgeCount);

            Assert.AreEqual(2, graph.RemoveEdges(0, 11001));
            Assert.AreEqual(1, graph.EdgeCount);

            Assert.AreEqual(1, graph.RemoveEdges(0, 1));
            Assert.AreEqual(0, graph.EdgeCount);
        }
Esempio n. 6
0
        public void TestSetVertex()
        {
            var graph = new GeometricGraph(1, 100);

            graph.AddVertex(0, 0, 0);
            graph.AddVertex(1, 1, 1);
            graph.AddVertex(2, 2, 2);

            float latitude, longitude;

            Assert.IsTrue(graph.GetVertex(0, out latitude, out longitude));
            Assert.AreEqual(0, latitude);
            Assert.AreEqual(0, longitude);
            Assert.AreEqual(0, graph.GetVertex(0).Latitude);
            Assert.AreEqual(0, graph.GetVertex(0).Longitude);
            Assert.IsTrue(graph.GetVertex(1, out latitude, out longitude));
            Assert.AreEqual(1, latitude);
            Assert.AreEqual(1, longitude);
            Assert.AreEqual(1, graph.GetVertex(1).Latitude);
            Assert.AreEqual(1, graph.GetVertex(1).Longitude);
            Assert.IsTrue(graph.GetVertex(2, out latitude, out longitude));
            Assert.AreEqual(2, latitude);
            Assert.AreEqual(2, longitude);
            Assert.AreEqual(2, graph.GetVertex(2).Latitude);
            Assert.AreEqual(2, graph.GetVertex(2).Longitude);

            Assert.IsFalse(graph.GetVertex(10000, out latitude, out longitude));
        }
Esempio n. 7
0
        /// <summary>
        /// Creates a router point for the given vertex.
        /// </summary>
        public static RouterPoint CreateRouterPointForVertex(this GeometricGraph graph, uint vertex, uint neighbour)
        {
            float latitude, longitude;

            if (!graph.GetVertex(vertex, out latitude, out longitude))
            {
                throw new ArgumentException("Vertex doesn't exist, cannot create routerpoint.");
            }
            var edges = graph.GetEdgeEnumerator(vertex);

            while (true)
            {
                if (!edges.MoveNext())
                {
                    throw new ArgumentException("No edges associated with vertex and it's neigbour, cannot create routerpoint.");
                }
                if (edges.To == neighbour)
                {
                    break;
                }
            }
            if (edges.DataInverted)
            {
                return(new RouterPoint(latitude, longitude, edges.Id, ushort.MaxValue));
            }
            return(new RouterPoint(latitude, longitude, edges.Id, 0));
        }
Esempio n. 8
0
 /// <summary>
 /// Creates a new routing network.
 /// </summary>
 public RoutingNetwork(MemoryMap map, GeometricGraph graph,
                       float maxEdgeDistance = Constants.DefaultMaxEdgeDistance)
 {
     _graph           = graph;
     _edgeData        = Context.ArrayFactory.CreateMemoryBackedArray <uint>(_edgeDataSize * graph.EdgeCount);
     _maxEdgeDistance = maxEdgeDistance;
 }
Esempio n. 9
0
        /// <summary>
        /// Searches for an edge that has the exact start- and endpoints given within the given tolerance.
        /// </summary>
        public static uint SearchEdgeExact(this GeometricGraph graph, Coordinate location1, Coordinate location2, float tolerance)
        {
            var vertex1 = graph.SearchClosest(location1.Latitude, location1.Longitude, 0.01f, 0.01f);

            if (vertex1 == Constants.NO_VERTEX)
            {
                return(Constants.NO_EDGE);
            }
            var vertex1Location = graph.GetVertex(vertex1);

            if (Coordinate.DistanceEstimateInMeter(location1, vertex1Location) > tolerance)
            {
                return(Constants.NO_EDGE);
            }
            var edgeEnumerator = graph.GetEdgeEnumerator();
            var best           = float.MaxValue;
            var bestEdge       = Constants.NO_EDGE;

            while (edgeEnumerator.MoveNext())
            {
                var vertex2Location = graph.GetVertex(edgeEnumerator.To);
                var dist            = Coordinate.DistanceEstimateInMeter(location2, vertex2Location);
                if (dist < tolerance &&
                    dist < best)
                {
                    best     = dist;
                    bestEdge = edgeEnumerator.Id;
                }
            }
            return(bestEdge);
        }
Esempio n. 10
0
        public void TestVertexCountAndTrim()
        {
            var graph = new GeometricGraph(1, 100);

            // add edge.
            graph.AddVertex(0, 0, 0);
            graph.AddVertex(1, 0, 0);
            graph.AddEdge(0, 1, new uint[] { 10 }, null);

            // trim.
            graph.Trim();
            Assert.AreEqual(2, graph.VertexCount);
            Assert.AreEqual(1, graph.EdgeCount);

            graph = new GeometricGraph(1, 100);

            // add edge.
            graph.AddVertex(0, 0, 0);
            graph.AddVertex(1, 0, 0);
            graph.AddVertex(11001, 0, 0);
            graph.AddEdge(0, 1, new uint[] { 10 }, null);
            graph.AddEdge(0, 11001, new uint[] { 10 }, null);

            // trim.
            graph.Trim();
            Assert.AreEqual(11002, graph.VertexCount);
            Assert.AreEqual(2, graph.EdgeCount);

            graph = new GeometricGraph(1, 100);

            // trim.
            graph.Trim(); // keep minimum one vertex.
            Assert.AreEqual(0, graph.VertexCount);
        }
Esempio n. 11
0
 /// <summary>
 /// Creates a new dykstra edge visitor.
 /// </summary>
 public DykstraEdgeVisitor(GeometricGraph geometricGraph,
                           Func <ushort, Factor> getFactor, IEnumerable <EdgePath <float> > source, float limitInSeconds)
 {
     _geometricGraph = geometricGraph;
     _getFactor      = getFactor;
     _source         = source;
     _limitInSeconds = limitInSeconds;
 }
 public ResolveMultipleAlgorithm(GeometricGraph graph, float latitude, float longitude, float maxOffset, float maxDistance, Func <GeometricEdge, bool> isAcceptable)
 {
     this._graph        = graph;
     this._latitude     = latitude;
     this._longitude    = longitude;
     this._maxDistance  = maxDistance;
     this._maxOffset    = maxOffset;
     this._isAcceptable = isAcceptable;
 }
Esempio n. 13
0
        public void TestRemoveEdges()
        {
            var graph = new GeometricGraph(1, 100);

            // add and remove edge.
            graph.AddVertex(0, 0, 0);
            graph.AddVertex(1, 0, 0);
            graph.AddEdge(0, 1, new uint[] { 10 }, null);
            Assert.AreEqual(1, graph.RemoveEdges(0));
            Assert.AreEqual(0, graph.RemoveEdges(1));

            // verify all edges.
            var edges = graph.GetEdgeEnumerator(0);

            Assert.AreEqual(0, edges.Count());

            edges = graph.GetEdgeEnumerator(1);
            Assert.AreEqual(0, edges.Count());

            graph = new GeometricGraph(1, 100);

            // add and remove edges.
            graph.AddVertex(0, 0, 0);
            graph.AddVertex(1, 0, 0);
            graph.AddVertex(2, 0, 0);
            graph.AddEdge(0, 1, new uint[] { 10 }, null);
            graph.AddEdge(0, 2, new uint[] { 10 }, null);
            Assert.AreEqual(2, graph.RemoveEdges(0));

            // verify all edges.
            edges = graph.GetEdgeEnumerator(0);
            Assert.AreEqual(0, edges.Count());
            edges = graph.GetEdgeEnumerator(1);
            Assert.AreEqual(0, edges.Count());
            edges = graph.GetEdgeEnumerator(2);
            Assert.AreEqual(0, edges.Count());

            graph = new GeometricGraph(1, 100);

            // add and remove edges.
            graph.AddVertex(0, 0, 0);
            graph.AddVertex(1, 0, 0);
            graph.AddVertex(2, 0, 0);
            graph.AddEdge(0, 1, new uint[] { 10 }, null);
            graph.AddEdge(0, 2, new uint[] { 20 }, null);
            graph.AddEdge(1, 2, new uint[] { 30 }, null);
            Assert.AreEqual(2, graph.RemoveEdges(0));

            // verify all edges.
            edges = graph.GetEdgeEnumerator(0);
            Assert.AreEqual(0, edges.Count());
            edges = graph.GetEdgeEnumerator(1);
            Assert.AreEqual(1, edges.Count());
            edges = graph.GetEdgeEnumerator(2);
            Assert.AreEqual(1, edges.Count());
        }
Esempio n. 14
0
 /// <summary>
 /// Creates a new resolve algorithm.
 /// </summary>
 public ResolveVertexAlgorithm(GeometricGraph graph, float latitude, float longitude,
                               float maxOffsetInMeter, float maxDistance, Func <GeometricEdge, bool> isAcceptable)
 {
     _graph            = graph;
     _latitude         = latitude;
     _longitude        = longitude;
     _maxDistance      = maxDistance;
     _maxOffsetInMeter = maxOffsetInMeter;
     _isAcceptable     = isAcceptable;
 }
Esempio n. 15
0
 public ResolveAlgorithm(GeometricGraph graph, float latitude, float longitude, float maxOffsetInMeter, float maxDistance, Func <GeometricEdge, bool> isAcceptable, Func <GeometricEdge, bool> isBetter)
 {
     this._graph            = graph;
     this._latitude         = latitude;
     this._longitude        = longitude;
     this._maxDistance      = maxDistance;
     this._maxOffsetInMeter = maxOffsetInMeter;
     this._isAcceptable     = isAcceptable;
     this._isBetter         = isBetter;
 }
Esempio n. 16
0
        public static long Distance(this GeometricGraph graph, int n, uint vertex)
        {
            float latitude;
            float longitude;

            if (!graph.GetVertex(vertex, out latitude, out longitude))
            {
                throw new Exception(string.Format("Cannot calculate hilbert distance, vertex {0} does not exist.", (object)vertex));
            }
            return(HilbertCurve.HilbertDistance(latitude, longitude, (long)n));
        }
Esempio n. 17
0
        public void TestGetShape()
        {
            var graph = new GeometricGraph(2, 100);

            graph.AddVertex(0, 0, 0);
            graph.AddVertex(1, 1, 1);
            graph.AddVertex(2, 0, 1);
            graph.AddVertex(3, 0, 2);
            var edge1 = graph.AddEdge(0, 1, new uint[] { 0, 10 },
                                      new Coordinate(0.25f, 0.25f),
                                      new Coordinate(0.50f, 0.50f),
                                      new Coordinate(0.75f, 0.75f));

            var shape = graph.GetShape(graph.GetEdge(edge1));

            Assert.IsNotNull(shape);
            Assert.AreEqual(5, shape.Count);
            Assert.AreEqual(0, shape[0].Latitude);
            Assert.AreEqual(0, shape[0].Longitude);
            Assert.AreEqual(0.25, shape[1].Latitude);
            Assert.AreEqual(0.25, shape[1].Longitude);
            Assert.AreEqual(0.5, shape[2].Latitude);
            Assert.AreEqual(0.5, shape[2].Longitude);
            Assert.AreEqual(0.75, shape[3].Latitude);
            Assert.AreEqual(0.75, shape[3].Longitude);
            Assert.AreEqual(1, shape[4].Latitude);
            Assert.AreEqual(1, shape[4].Longitude);
            shape = graph.GetShape(graph.GetEdgeEnumerator(0).First());
            Assert.IsNotNull(shape);
            Assert.AreEqual(5, shape.Count);
            Assert.AreEqual(0, shape[0].Latitude);
            Assert.AreEqual(0, shape[0].Longitude);
            Assert.AreEqual(0.25, shape[1].Latitude);
            Assert.AreEqual(0.25, shape[1].Longitude);
            Assert.AreEqual(0.5, shape[2].Latitude);
            Assert.AreEqual(0.5, shape[2].Longitude);
            Assert.AreEqual(0.75, shape[3].Latitude);
            Assert.AreEqual(0.75, shape[3].Longitude);
            Assert.AreEqual(1, shape[4].Latitude);
            Assert.AreEqual(1, shape[4].Longitude);
            shape = graph.GetShape(graph.GetEdgeEnumerator(1).First(x => x.To == 0));
            Assert.IsNotNull(shape);
            Assert.AreEqual(5, shape.Count);
            Assert.AreEqual(1, shape[0].Latitude);
            Assert.AreEqual(1, shape[0].Longitude);
            Assert.AreEqual(0.75, shape[1].Latitude);
            Assert.AreEqual(0.75, shape[1].Longitude);
            Assert.AreEqual(0.5, shape[2].Latitude);
            Assert.AreEqual(0.5, shape[2].Longitude);
            Assert.AreEqual(0.25, shape[3].Latitude);
            Assert.AreEqual(0.25, shape[3].Longitude);
            Assert.AreEqual(0, shape[4].Latitude);
            Assert.AreEqual(0, shape[4].Longitude);
        }
Esempio n. 18
0
        public static HashSet <uint> Search(this GeometricGraph graph, int n, float minLatitude, float minLongitude, float maxLatitude, float maxLongitude)
        {
            List <long> longList = HilbertCurve.HilbertDistances(System.Math.Max(minLatitude, -90f), System.Math.Max(minLongitude, -180f), System.Math.Min(maxLatitude, 90f), System.Math.Min(maxLongitude, 180f), (long)n);

            longList.Sort();
            HashSet <uint> uintSet = new HashSet <uint>();
            int            index   = 0;
            uint           vertex1 = 0;
            uint           vertex2 = graph.VertexCount - 1U;

            for (; index < longList.Count && vertex1 < graph.VertexCount; ++index)
            {
                long num1 = longList[index];
                long maxHilbert;
                for (maxHilbert = num1; index < longList.Count - 1 && longList[index + 1] <= maxHilbert + 1L; ++index)
                {
                    maxHilbert = longList[index + 1];
                }
                uint  vertex;
                int   count;
                float latitude;
                float longitude;
                if (num1 == maxHilbert)
                {
                    if (graph.Search(num1, n, vertex1, vertex2, out vertex, out count))
                    {
                        int num2 = count;
                        for (; count > 0; --count)
                        {
                            if (graph.GetVertex(vertex + (uint)(count - 1), out latitude, out longitude) && (double)minLatitude < (double)latitude && ((double)minLongitude < (double)longitude && (double)maxLatitude > (double)latitude) && (double)maxLongitude > (double)longitude)
                            {
                                uintSet.Add(vertex + (uint)(count - 1));
                            }
                        }
                        vertex1 = vertex + (uint)num2;
                    }
                }
                else if (graph.SearchRange(num1, maxHilbert, n, vertex1, vertex2, out vertex, out count))
                {
                    int num2 = count;
                    for (; count > 0; --count)
                    {
                        if (graph.GetVertex(vertex + (uint)(count - 1), out latitude, out longitude) && (double)minLatitude < (double)latitude && ((double)minLongitude < (double)longitude && (double)maxLatitude > (double)latitude) && (double)maxLongitude > (double)longitude)
                        {
                            uintSet.Add(vertex + (uint)(count - 1));
                        }
                    }
                    vertex1 = vertex + (uint)num2;
                }
            }
            return(uintSet);
        }
Esempio n. 19
0
        public void SearchClosestVertexTest()
        {
            var graph = new GeometricGraph(1);

            graph.AddVertex(0, 1, 1);
            graph.AddVertex(1, 2, 2);

            graph.Sort();

            Assert.AreEqual(0, graph.SearchClosest(1, 1, 1, 1));
            Assert.AreEqual(1, graph.SearchClosest(2, 2, 1, 1));
            Assert.AreEqual(Constants.NO_VERTEX, graph.SearchClosest(3, 3, .5f, .5f));
        }
Esempio n. 20
0
        /// <summary>
        /// Deserializes from a stream.
        /// </summary>
        public static RoutingNetwork Deserialize(Stream stream, RoutingNetworkProfile profile)
        {
            var version = stream.ReadByte();

            if (version > 2)
            {
                throw new Exception(string.Format("Cannot deserialize routing network: Invalid version #: {0}, upgrade Itinero.", version));
            }

            var position        = stream.Position;
            var initialPosition = stream.Position;

            // read maxEdgeDistance if version # = 2.
            var maxEdgeDistance = Constants.DefaultMaxEdgeDistance;

            if (version == 2)
            {
                var bytes = new byte[4];
                stream.Read(bytes, 0, 4);
                maxEdgeDistance = BitConverter.ToSingle(bytes, 0);
            }

            // deserialize graph.
            var graph = GeometricGraph.Deserialize(stream, profile == null ? null : profile.GeometricGraphProfile);
            var size  = stream.Position - position;

            var edgeLength = graph.EdgeCount;
            var edgeSize   = 1;

            ArrayBase <uint> edgeData;

            if (profile == null)
            { // just create arrays and read the data.
                edgeData = Context.ArrayFactory.CreateMemoryBackedArray <uint>(edgeLength * edgeSize);
                edgeData.CopyFrom(stream);
                size += edgeLength * edgeSize * 4;
            }
            else
            { // create accessors over the exact part of the stream that represents vertices/edges.
                position = stream.Position;
                var map = new MemoryMapStream(new CappedStream(stream, position,
                                                               edgeLength * edgeSize * 4));
                edgeData = new Array <uint>(map.CreateUInt32(edgeLength * edgeSize), profile.EdgeDataProfile);
                size    += edgeLength * edgeSize * 4;
            }

            // make stream is positioned correctly.
            stream.Seek(initialPosition + size, System.IO.SeekOrigin.Begin);

            return(new RoutingNetwork(graph, edgeData, maxEdgeDistance));
        }
Esempio n. 21
0
 public RoutingNetwork(MemoryMap map, RoutingNetworkProfile profile, float maxEdgeDistance = 5000f)
 {
     this._maxEdgeDistance = maxEdgeDistance;
     if (profile == null)
     {
         this._graph    = new GeometricGraph(map, 1);
         this._edgeData = (ArrayBase <uint>) new Array <uint>(map, (long)this._edgeDataSize * this._graph.EdgeCount);
     }
     else
     {
         this._graph    = new GeometricGraph(map, profile.GeometricGraphProfile, 1);
         this._edgeData = (ArrayBase <uint>) new Array <uint>(map, (long)this._edgeDataSize * this._graph.EdgeCount, profile.EdgeDataProfile);
     }
 }
Esempio n. 22
0
 public static void Sort(this GeometricGraph graph, int n)
 {
     if (graph.VertexCount <= 0U)
     {
         return;
     }
     QuickSort.Sort((Func <long, long>)(vertex => graph.Distance(n, (uint)vertex)), (Action <long, long>)((vertex1, vertex2) =>
     {
         if (vertex1 == vertex2)
         {
             return;
         }
         graph.Switch((uint)vertex1, (uint)vertex2);
     }), 0L, (long)(graph.VertexCount - 1U));
 }
Esempio n. 23
0
 /// <summary>
 /// Creates a new routing network.
 /// </summary>
 public RoutingNetwork(MemoryMap map, RoutingNetworkProfile profile,
                       float maxEdgeDistance = Constants.DefaultMaxEdgeDistance)
 {
     _maxEdgeDistance = maxEdgeDistance;
     if (profile == null)
     {
         _graph    = new GeometricGraph(map, 1);
         _edgeData = new Array <uint>(map, _edgeDataSize * _graph.EdgeCount);
     }
     else
     {
         _graph    = new GeometricGraph(map, profile.GeometricGraphProfile, 1);
         _edgeData = new Array <uint>(map, _edgeDataSize * _graph.EdgeCount, profile.EdgeDataProfile);
     }
 }
Esempio n. 24
0
        public void TestArgumentExcptions()
        {
            // create graph with one vertex and start adding 2.
            var graph = new GeometricGraph(1, 2);

            // make sure to add 1 and 2.
            graph.AddVertex(0, 0, 0);
            graph.AddVertex(1, 1, 1);
            graph.AddEdge(0, 1, new uint[] { 1 }, null);

            Assert.Catch <ArgumentOutOfRangeException>(() =>
            {
                graph.GetEdgeEnumerator(2);
            });
        }
Esempio n. 25
0
        public void TestTwoEdgesWithIsBetter()
        {
            var vertex0 = new Coordinate(51.26779566943717f, 4.801347255706787f);
            var vertex1 = new Coordinate(51.26689735000000f, 4.801347255706787f);
            var vertex2 = new Coordinate(51.26689735000000f, 4.801347255706787f);

            var graph = new GeometricGraph(1);

            graph.AddVertex(0, vertex0.Latitude, vertex0.Longitude);
            graph.AddVertex(1, vertex1.Latitude, vertex1.Longitude);
            graph.AddVertex(2, vertex2.Latitude, vertex2.Longitude);
            graph.AddEdge(0, 1, new uint[] { 0 }, null);
            var expectedEdgeId = graph.AddEdge(0, 2, new uint[] { 1 }, null);

            // resolve on vertex0.
            var location = vertex0;
            var resolver = new ResolveAlgorithm(graph, location.Latitude, location.Longitude, Constants.DefaultMaxEdgeDistance, 50f,
                                                (edge) => { return(true); }, (edge) => { return(edge.Data[0] == 1); });

            resolver.Run();

            var result = resolver.Result;

            Assert.IsNotNull(result);
            Assert.AreEqual(expectedEdgeId, result.EdgeId);

            // resolve on vertex1.
            location = vertex1;
            resolver = new ResolveAlgorithm(graph, location.Latitude, location.Longitude, Constants.DefaultMaxEdgeDistance, 50f,
                                            (edge) => { return(true); }, (edge) => { return(edge.Data[0] == 1); });
            resolver.Run();

            result = resolver.Result;
            Assert.IsNotNull(result);
            Assert.AreEqual(expectedEdgeId, result.EdgeId);

            // resolve right in between.
            location = new Coordinate((vertex0.Latitude + vertex1.Latitude) / 2,
                                      (vertex0.Longitude + vertex1.Longitude) / 2);
            resolver = new ResolveAlgorithm(graph, location.Latitude, location.Longitude, Constants.DefaultMaxEdgeDistance, 50f,
                                            (edge) => { return(true); }, (edge) => { return(edge.Data[0] == 1); });
            resolver.Run();

            result = resolver.Result;
            Assert.IsNotNull(result);
            Assert.AreEqual(expectedEdgeId, result.EdgeId);
        }
Esempio n. 26
0
        public void TestSerialize()
        {
            var graph = new GeometricGraph(1, 100);

            // add one edge.
            graph.AddVertex(0, 0, 0);
            graph.AddVertex(1, 0, 0);
            graph.AddEdge(0, 1, new uint[] { 10 }, null);

            // serialize.
            graph.Compress();
            var expectedSize = graph.SizeInBytes;

            using (var stream = new System.IO.MemoryStream())
            {
                Assert.AreEqual(expectedSize, graph.Serialize(stream));
            }

            graph = new GeometricGraph(1, 100);

            // add one edge.
            graph.AddVertex(0, 0, 0);
            graph.AddVertex(1, 0, 0);
            graph.AddVertex(2, 0, 0);
            graph.AddVertex(3, 0, 0);
            graph.AddVertex(4, 0, 0);
            graph.AddVertex(5, 0, 0);
            graph.AddEdge(0, 1, new uint[] { 10 }, null);
            graph.AddEdge(0, 2, new uint[] { 20 }, null);
            graph.AddEdge(0, 3, new uint[] { 30 }, null);
            graph.AddEdge(0, 4, new uint[] { 40 }, null);
            graph.AddEdge(5, 1, new uint[] { 50 }, null);
            graph.AddEdge(5, 2, new uint[] { 60 }, null);
            graph.AddEdge(5, 3, new uint[] { 70 }, null);
            graph.AddEdge(5, 4, new uint[] { 80 }, null);

            // serialize.
            graph.Compress();
            expectedSize = graph.SizeInBytes;
            using (var stream = new System.IO.MemoryStream())
            {
                Assert.AreEqual(expectedSize, graph.Serialize(stream));
            }
        }
Esempio n. 27
0
        public static RoutingNetwork Deserialize(Stream stream, RoutingNetworkProfile profile)
        {
            int num1 = stream.ReadByte();

            if (num1 > 2)
            {
                throw new Exception(string.Format("Cannot deserialize routing network: Invalid version #: {0}, upgrade OsmSharp.Routing.", (object)num1));
            }
            long  position1 = stream.Position;
            long  position2 = stream.Position;
            float num2      = 5000f;

            if (num1 == 2)
            {
                byte[] buffer = new byte[4];
                stream.Read(buffer, 0, 4);
                num2 = BitConverter.ToSingle(buffer, 0);
            }
            GeometricGraph   graph     = GeometricGraph.Deserialize(stream, profile == null ? (GeometricGraphProfile)null : profile.GeometricGraphProfile);
            long             num3      = stream.Position - position1;
            long             edgeCount = graph.EdgeCount;
            int              num4      = 1;
            ArrayBase <uint> arrayBase;
            long             num5;

            if (profile == null)
            {
                arrayBase = (ArrayBase <uint>) new MemoryArray <uint>(edgeCount * (long)num4);
                arrayBase.CopyFrom(stream);
                num5 = num3 + edgeCount * (long)num4 * 4L;
            }
            else
            {
                long position3 = stream.Position;
                arrayBase = (ArrayBase <uint>) new Array <uint>(((MemoryMap) new MemoryMapStream((Stream) new CappedStream(stream, position3, edgeCount * (long)num4 * 4L))).CreateUInt32(edgeCount * (long)num4), profile.EdgeDataProfile);
                num5      = num3 + edgeCount * (long)num4 * 4L;
            }
            stream.Seek(position2 + num5, SeekOrigin.Begin);
            ArrayBase <uint> edgeData = arrayBase;
            double           num6     = (double)num2;

            return(new RoutingNetwork(graph, edgeData, (float)num6));
        }
Esempio n. 28
0
        public void TestRemoveEdge()
        {
            var graph = new GeometricGraph(1, 100);

            // add and remove edge.
            graph.AddVertex(0, 0, 0);
            graph.AddVertex(1, 0, 0);
            graph.AddEdge(0, 1, new uint[] { 1 }, null);
            Assert.IsTrue(graph.RemoveEdges(0, 1) != 0);

            graph = new GeometricGraph(1, 100);

            // add and remove edge.
            graph.AddVertex(0, 0, 0);
            graph.AddVertex(1, 0, 0);
            var edge = graph.AddEdge(0, 1, new uint[] { 10 }, null);

            Assert.IsTrue(graph.RemoveEdge(edge));
        }
Esempio n. 29
0
        public void TestOneEdge()
        {
            var vertex0 = new Coordinate(51.26779566943717f, 4.801347255706787f);
            var vertex1 = new Coordinate(51.26689735000000f, 4.801347255706787f);

            var graph = new GeometricGraph(1);

            graph.AddVertex(0, (float)vertex0.Latitude, (float)vertex0.Longitude);
            graph.AddVertex(1, (float)vertex1.Latitude, (float)vertex1.Longitude);
            graph.AddEdge(0, 1, new uint[] { 0 }, null);

            // resolve on vertex0.
            var location = vertex0;
            var resolver = new ResolveAlgorithm(graph, (float)location.Latitude, (float)location.Longitude,
                                                Constants.DefaultMaxEdgeDistance, 50f,
                                                (edge) => { return(true); });

            resolver.Run();

            var result = resolver.Result;

            Assert.IsNotNull(result);

            // resolve on vertex1.
            location = vertex1;
            resolver = new ResolveAlgorithm(graph, (float)location.Latitude, (float)location.Longitude, Constants.DefaultMaxEdgeDistance, 50f,
                                            (edge) => { return(true); });
            resolver.Run();

            result = resolver.Result;
            Assert.IsNotNull(result);

            // resolve right in between.
            location = new Coordinate((vertex0.Latitude + vertex1.Latitude) / 2,
                                      (vertex0.Longitude + vertex1.Longitude) / 2);
            resolver = new ResolveAlgorithm(graph, (float)location.Latitude, (float)location.Longitude, Constants.DefaultMaxEdgeDistance, 50f,
                                            (edge) => { return(true); });
            resolver.Run();

            result = resolver.Result;
            Assert.IsNotNull(result);
        }
Esempio n. 30
0
        public static RouterPoint CreateRouterPointForVertex(this GeometricGraph graph, uint vertex)
        {
            float latitude;
            float longitude;

            if (!graph.GetVertex(vertex, out latitude, out longitude))
            {
                throw new ArgumentException("Vertex doesn't exist, cannot create routerpoint.");
            }
            GeometricGraph.EdgeEnumerator edgeEnumerator = graph.GetEdgeEnumerator(vertex);
            if (!edgeEnumerator.MoveNext())
            {
                throw new ArgumentException("No edges associated with vertex, cannot create routerpoint.");
            }
            if (edgeEnumerator.DataInverted)
            {
                return(new RouterPoint(latitude, longitude, edgeEnumerator.Id, ushort.MaxValue));
            }
            return(new RouterPoint(latitude, longitude, edgeEnumerator.Id, (ushort)0));
        }