Exemplo n.º 1
0
        private (IWeightedGraph <FindingDirectionsState, double>, Dictionary <string, FindingDirectionsState>) GetRomaniaGraph()
        {
            ulong nodeId       = 0;
            var   nodes        = new Dictionary <string, FindingDirectionsState>();
            var   edges        = new List <RomaniaEdge>();
            var   romaniaLines = File.ReadAllLines("RomaniaGraph.csv");

            foreach (var line in romaniaLines.Skip(1)) // skip header
            {
                var parts = line.Split(',');
                if (!nodes.ContainsKey(parts[0]))
                {
                    nodes.Add(parts[0], new FindingDirectionsState(++nodeId, name: parts[0]));
                }
                if (!nodes.ContainsKey(parts[1]))
                {
                    nodes.Add(parts[1], new FindingDirectionsState(++nodeId, name: parts[1]));
                }
                edges.Add(new RomaniaEdge {
                    From = nodes[parts[0]], To = nodes[parts[1]], Weight = double.Parse(parts[2])
                });
            }
            var graph = new AdjacencyListWeightedGraph <FindingDirectionsState, double>(edges);

            return(graph, nodes);
        }
Exemplo n.º 2
0
        public void TestSimpleGraph()
        {
            var nodes = Enumerable.Range(1, 5).Select(m => new TestNode {
                NodeId = (ulong)m
            }).ToArray();
            var edges = from x in nodes
                        from y in nodes
                        where x.NodeId > y.NodeId
                        select new TestEdge {
                From = x, To = y, Weight = x.NodeId * y.NodeId
            };
            var graph = new AdjacencyListWeightedGraph <TestNode, double>(edges);

            foreach (var node in nodes)
            {
                var neighbors = graph.GetNeighbors(node).ToList();
                Assert.Equal((int)node.NodeId - 1, neighbors.Count);
                foreach (var neighbor in neighbors)
                {
                    var edgeWeight = graph.GetEdgeWeight(node, neighbor);
                    var expected   = node.NodeId * neighbor.NodeId * 1d;
                    Assert.Equal(expected, edgeWeight);
                }
            }
        }
Exemplo n.º 3
0
 MakeIntersectionGraph(IWeightedGraph<FindingDirectionsState, double> graph, HashSet<FindingDirectionsState> nodes)
 {
     // identify the intersections
     // reduce graph to only intersections
     var intersections = nodes
         .Where(m => IsIntersection(graph, m))
         .ToHashSet();
     var newEdges = intersections
         .SelectMany(i => ReduceEdges(graph, i, intersections))
         .GroupBy(m => m)
         .Select(m => m.OrderBy(j => j.Weight).First())
         .ToArray();
     var intersectionsGraph = new AdjacencyListWeightedGraph<FindingDirectionsState, double>(newEdges);
     var segmentEdges = newEdges.Select(m => m.ToSegments());
     var segmentsGraph = new AdjacencyListWeightedGraph<FindingDirectionsState, IEnumerable<FindingDirectionsState>>(segmentEdges);
     return (intersectionsGraph, segmentsGraph, intersections);
 }
Exemplo n.º 4
0
        public void OsmEdgeDedupesCorrectly()
        {
            var from_a = new FindingDirectionsState(139489);
            var to_a   = new FindingDirectionsState(198573);
            var from_b = new FindingDirectionsState(139489);
            var to_b   = new FindingDirectionsState(198573);
            var from_c = new FindingDirectionsState(139489);
            var to_c   = new FindingDirectionsState(298573);
            IWeightedGraphEdge <FindingDirectionsState, double> a = new OpenStreetMapDataHelper.OsmEdge(from_a, to_a, 0d);
            IWeightedGraphEdge <FindingDirectionsState, double> b = new OpenStreetMapDataHelper.OsmEdge(from_a, to_a, 1d);
            IWeightedGraphEdge <FindingDirectionsState, double> c = new OpenStreetMapDataHelper.OsmEdge(from_a, to_c, 3d);
            var hashSet = new HashSet <IWeightedGraphEdge <FindingDirectionsState, double> >();

            hashSet.Add(a);
            var actual = hashSet.Add(b);

            Assert.False(actual, "It did not dedupe correctly.");
            actual = hashSet.Add(c);
            Assert.True(actual, "Should have added that last point.");
            var graph = new AdjacencyListWeightedGraph <FindingDirectionsState, double>(hashSet);

            Assert.Equal(2, graph.EnumerateEdges().Count());
        }
Exemplo n.º 5
0
 ExtractMapGraph(string osm_pbf_filename)
 {
     var recreationalVechicleRoadTags = new HashSet<string>(
         new[] {
             // https://wiki.openstreetmap.org/wiki/Key:highway
             "motorway",
             "trunk",
             "primary",
             "secondary",
             "tertiary",
             "unclassified",
             "residential",
             "service",
             // ramps and other road connectors
             "motorway_link",
             "trunk_link",
             "primary_link",
             "secondary_link",
             "tertiary_link",
         }
     );
     var onewayIdentifiers = new HashSet<string>(new [] { "yes", "1", "true" });
     var nodes = new Dictionary<long, FindingDirectionsState>();
     // TODO: figure out why hashset of OSM edge is fine but IWeightedGraphEdge fails to dedupe
     var edges = new HashSet<OsmEdge>();
     var graphNodes = new HashSet<FindingDirectionsState>();
     using(var fileStream = File.OpenRead(osm_pbf_filename))
     using(var source = new PBFOsmStreamSource(fileStream))
     foreach (var element in source)
     {
         switch(element.Type)
         {
             case OsmGeoType.Node:
                 if (!element.Id.HasValue) continue;
                 var osm_node = (Node)element;
                 if (!osm_node.Latitude.HasValue || !osm_node.Longitude.HasValue) continue;
                 var graph_node = new FindingDirectionsState(
                         nodeId: (ulong)osm_node.Id.Value, 
                         latitude: osm_node.Latitude.Value,
                         longitude: osm_node.Longitude.Value
                     );
                 nodes[element.Id.Value] = graph_node;
                 break;
             case OsmGeoType.Way:
                 if (!element.Tags.ContainsKey("highway")) continue;
                 var highwayType = element.Tags.GetValue("highway");
                 if (!recreationalVechicleRoadTags.Contains(highwayType)) continue;
                 var way = (Way)element;
                 if (way.Nodes.Length < 2) continue;
                 bool isOneWay = element.Tags.ContainsKey("oneway") && onewayIdentifiers.Contains(element.Tags["oneway"]);
                 for(int i = 1; i < way.Nodes.Length; i++)
                 {
                     var a = nodes[way.Nodes[i-1]];
                     var b = nodes[way.Nodes[i]];
                     graphNodes.Add(a);
                     graphNodes.Add(b);
                     var distance = DistanceHelper.Haversine(a.Latitude, a.Longitude, b.Latitude, b.Longitude);
                     edges.Add(new OsmEdge(a, b, distance));
                     if (!isOneWay) edges.Add(new OsmEdge(b, a, distance));
                 }
                 break;
             case OsmGeoType.Relation:
                 // don't need these?
                 break;
         }
     }
     var graph = new AdjacencyListWeightedGraph<FindingDirectionsState, double>(edges);
     return (graph, graphNodes);
 }