Пример #1
0
 private void SetPoints(List <Coordinate> points)
 {
     ClearGraph();
     CreateRoad(points);
     graph.CreateDefaultGrid(grid);
     grid.maxValue = ClassificationValue.HighwayLink;
     grid.minValue = 0;
 }
Пример #2
0
 public void CreateDefaultGrid()
 {
     graph.CreateDefaultGrid(grid);
 }
Пример #3
0
    private static void ParseCsv(ParseTaskData data)
    {
        GraphData graph = new GraphData();

        graph.cellSizeX = double.MaxValue;
        graph.cellSizeY = double.MaxValue;
        graph.west      = GeoCalculator.MaxLongitude;
        graph.east      = GeoCalculator.MinLongitude;
        graph.north     = GeoCalculator.MinLongitude;
        graph.south     = GeoCalculator.MaxLongitude;

        Dictionary <int, GraphNode> nodes = new Dictionary <int, GraphNode>();
        var highwaySet  = new HashSet <int>();
        var highwayList = new List <int>();
        var cultureInfo = CultureInfo.InvariantCulture;

        // Read/skip header
        data.sr.ReadLine();

        // Read each data row at a time
        string line;

        while ((line = data.sr.ReadLine()) != null)
        {
            string[] cells = line.Split(',');

            int sourceId = int.Parse(cells[1]);
            int targetId = int.Parse(cells[2]);

            if (sourceId == targetId)
            {
                continue;
            }

            int classification = int.Parse(cells[7]);

            float  distance = float.Parse(cells[0], cultureInfo);
            double x1       = double.Parse(cells[3], cultureInfo);
            double y1       = double.Parse(cells[4], cultureInfo);
            double x2       = double.Parse(cells[5], cultureInfo);
            double y2       = double.Parse(cells[6], cultureInfo);

            if (classification >= ClassificationValue.Highway)
            {
                // Use a negative sourceId & targetId to indicate another layer
                AddLink(graph, nodes, -sourceId, -targetId, x1, y1, x2, y2, distance, ClassificationValue.Highway);
                classification -= ClassificationValue.Highway;
            }

            if (classification > 0)
            {
                AddLink(graph, nodes, sourceId, targetId, x1, y1, x2, y2, distance, classification);
            }
        }

        AddHalfSizeToGraph(graph);

        // Once the graph is fully loaded, each node's index will be updated
        graph.indexToNode.Clear();

        double kX     = 1.0 / graph.cellSizeX;
        double kY     = 1.0 / graph.cellSizeY;
        int    countX = (int)Math.Round((graph.east - graph.west) * kX);

        for (int i = graph.nodes.Count - 1; i >= 0; i--)
        {
            var node  = graph.nodes[i];
            int index = (int)((node.longitude - graph.west) * kX) + countX * (int)((graph.north - node.latitude) * kY);

            // Highway and other road are on the different layer, they could be overlapped
            if (node.classifications == ClassificationValue.Highway)  // 16
            {
                if (!highwaySet.Contains(index))
                {
                    highwaySet.Add(index);
                    highwayList.Add(index);
                }
                index = -index;
            }

            if (!graph.indexToNode.ContainsKey(index))
            {
                graph.indexToNode.Add(index, node);
            }

            node.index = index;
        }

        // Connect Highways to the other roads with 'HighwayLink' links
        foreach (var idx in highwayList)
        {
            // Does it have both Highway and HighwayLink?
            if (graph.indexToNode.TryGetValue(idx, out GraphNode linkNode) &&
                linkNode.classifications >= ClassificationValue.HighwayLink)
            {
                // Connect highwayNode to linkNode's neighbours (only if the links are HighwayLink)
                var highwayNode = graph.indexToNode[-idx];
                for (int i = linkNode.links.Count - 1; i >= 0; --i)
                {
                    if (linkNode.linkClassifications[i] == ClassificationValue.HighwayLink)
                    {
                        GraphNode.AddLink(highwayNode, linkNode.links[i], linkNode.linkDistances[i], ClassificationValue.HighwayLink);
                        GraphNode.RemoveLink(linkNode, linkNode.links[i], ClassificationValue.HighwayLink);
                    }
                }

                if (linkNode.links.Count > 0)
                {
                    // Remove the 'HighwayLink' classification from the linkNode
                    linkNode.classifications &= ~ClassificationValue.HighwayLink;
                }
                else
                {
                    // Remove the linkNode if all of its links were removed
                    graph.nodes.Remove(linkNode);
                    graph.indexToNode.Remove(idx);
                }
            }
        }

        RemoveExtraLongLinks(graph);

        var grid = new GridData();

        graph.InitGrid(grid);
        graph.CreateDefaultGrid(grid);

        data.patch = graph;
    }
Пример #4
0
    private static GraphData ParseCsv(TextReader tr)
    {
        GraphData graph = new GraphData();

        graph.cellSizeX = double.MaxValue;
        graph.cellSizeY = double.MaxValue;
        graph.west      = GeoCalculator.MaxLongitude;
        graph.east      = GeoCalculator.MinLongitude;
        graph.north     = GeoCalculator.MinLongitude;
        graph.south     = GeoCalculator.MaxLongitude;

        Dictionary <int, GraphNode> nodes = new Dictionary <int, GraphNode>();

        // Read/skip header
        string line = tr.ReadLine();

        // Read each data row at a time
        while ((line = tr.ReadLine()) != null)
        {
            string[] cells = line.Split(',');

            int sourceId = int.Parse(cells[1]);
            int targetId = int.Parse(cells[2]);

            if (sourceId == targetId)
            {
                continue;
            }

            int value = int.Parse(cells[7]);

            float  distance = float.Parse(cells[0]);
            double x1       = double.Parse(cells[3]);
            double y1       = double.Parse(cells[4]);
            double x2       = double.Parse(cells[5]);
            double y2       = double.Parse(cells[6]);

            if (value >= ClassificationValue.Highway)
            {
                // Use a negative sourceId & targetId to indicate another layer
                AddNode(graph, nodes, -sourceId, -targetId, x1, y1, x2, y2, distance, ClassificationValue.Highway);
                value -= ClassificationValue.Highway;
            }

            if (value > 0)
            {
                AddNode(graph, nodes, sourceId, targetId, x1, y1, x2, y2, distance, value);
            }
        }

        AddHalfSizeToGraph(graph);

        GridData tempGrid = new GridData();

        graph.InitGrid(tempGrid);
        graph.CreateDefaultGrid(tempGrid);

        graph.indexToNode.Clear();

        double kX = 1.0 / graph.cellSizeX;
        double kY = 1.0 / graph.cellSizeY;

        foreach (var node in graph.nodes)
        {
            int index = (int)((node.longitude - graph.west) * kX) + tempGrid.countX * (int)((graph.north - node.latitude) * kY);
            if (node.value == ClassificationValue.Highway)
            {
                index = -index;                 // highway and other road are on the different layer, they could be overlapped
            }
            if (!graph.indexToNode.ContainsKey(index))
            {
                graph.indexToNode.Add(index, node);
            }
            node.index = index;
        }

        graph.CreatePotentialNetwork(tempGrid);

        return(graph);
    }