Ejemplo n.º 1
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);
    }