Beispiel #1
0
        // Graphs

        public async Task <List <JsonGraphRootObject> > GetLocalStorageGraphs()
        {
            var localStorageGraphs = new List <JsonGraphRootObject>();

            for (var i = 0; i < await _localStorage.LengthAsync(); i++)
            {
                var key = await _localStorage.KeyAsync(i);

                JsonGraphRootObject jsonObject = null;

                try
                {
                    jsonObject = await _localStorage.GetItemAsync <JsonGraphRootObject>(key);

                    if (!(jsonObject.Vertices?.Count > 0))
                    {
                        continue;
                    }
                }
                catch (Exception)
                {
                    Console.WriteLine($"Could not convert local storage object with key '{ key }' to graph object, ignoring object.");
                }

                if (jsonObject != null)
                {
                    localStorageGraphs.Add(jsonObject);
                }
            }

            return(localStorageGraphs);
        }
Beispiel #2
0
        public static Graph <VertexInfo, EdgeInfo> GenerateGraph(JsonGraphRootObject json)
        {
            var graph = new Graph <VertexInfo, EdgeInfo>();

            // Add vertices
            foreach (var vertex in json.Vertices)
            {
                graph.AddVertex(new VertexInfo()
                {
                    Name     = vertex.Name,
                    Type     = DetermineVertexType(vertex.Type),
                    Position = new Tuple <double, double>(vertex.Latitude, vertex.Longitude)
                });
            }

            // Add edges
            foreach (var vertex in json.Vertices)
            {
                foreach (var edge in vertex.Edges)
                {
                    var origin      = graph.CustomFirstOrDefault(v => v.Name == vertex.Name);
                    var destination = graph.CustomFirstOrDefault(v => v.Name == edge.Target);

                    if (origin != null && destination != null)
                    {
                        graph.AddEdge(origin, destination, new EdgeInfo()
                        {
                            Distance             = GeographicalHelpers.CalculateGeographicalDistanceInMeters(origin.Info.Position, destination.Info.Position),
                            GMapsDistanceAndTime = edge.Modes.ToDictionary(mode => Enum.Parse <TravelMode>(mode.TravelMode), mode => Tuple.Create((double)mode.Distance, (double)mode.Time))
                        });
                    }
                }
            }

            return(graph);
        }