Exemplo n.º 1
0
        /// <summary>
        /// Load the network from a 211 file
        /// </summary>
        /// <param name="fileName211"></param>
        public void LoadNetwork(string fileName211)
        {
            Dictionary<int, Node> nodes = new Dictionary<int, Node>();
            Dictionary<Pair<int, int>, Link> links = new Dictionary<Pair<int, int>, Link>();
            using ( StreamReader reader = new StreamReader( fileName211 ) )
            {
                string line = null;

                // run until we get to the link information
                while ( ( line = reader.ReadLine() ) != null )
                {
                    string[] parts = line.Split( SplitCharacters, StringSplitOptions.RemoveEmptyEntries );
                    try
                    {
                        var numberOfParts = parts.Length;
                        if ( numberOfParts >= 3 && parts[0].Length > 0 )
                        {
                            int offset = -1;
                            if ( parts[0][0] == 'a' )
                            {
                                offset = 0;
                            }
                            else if ( parts[0][0] == 'c' )
                            {
                                continue;
                            }
                            Node node = new Node();
                            if ( offset == -1 )
                            {
                                node.IsCentroid = false;
                            }
                            else
                            {
                                node.IsCentroid = ( parts[0].Length >= 2 && parts[0] == "a*" );
                            }
                            node.Number = int.Parse( parts[1 + offset] );
                            node.X = float.Parse( parts[2 + offset] );
                            node.Y = float.Parse( parts[3 + offset] );
                            if ( numberOfParts > 4 + offset )
                            {
                                node.USER1 = int.Parse( parts[4 + offset] );
                                if ( numberOfParts > 5 + offset )
                                {
                                    node.USER2 = int.Parse( parts[5 + offset] );
                                    if ( numberOfParts > 6 + offset )
                                    {
                                        node.NodeType = int.Parse( parts[6 + offset] );
                                        node.Modified = false;
                                        if ( parts.Length > 7 + offset )
                                        {
                                            node.NodeLabel = parts[7 + offset];
                                        }
                                        else
                                        {
                                            node.NodeLabel = node.Number.ToString();
                                        }
                                    }
                                }
                            }
                            nodes.Add( node.Number, node );
                        }
                    }
                    catch { }
                    if ( line != null && line.StartsWith( "t links" ) )
                    {
                        break;
                    }
                }

                while ( ( line = reader.ReadLine() ) != null )
                {
                    try
                    {
                        string[] parts = line.Split( SplitCharacters, StringSplitOptions.RemoveEmptyEntries );
                        if ( parts.Length > 7 )
                        {
                            Link link;
                            link.I = int.Parse( parts[1] );
                            link.J = int.Parse( parts[2] );
                            link.Length = float.Parse( parts[3] );
                            link.Modes = parts[4].ToLower().ToCharArray();
                            link.LinkType = int.Parse( parts[5] );
                            link.Lanes = float.Parse( parts[6] );
                            link.VDF = float.Parse( parts[7] );
                            // We don't load [8]
                            link.Speed = float.Parse( parts[9] );
                            link.Capacity = float.Parse( parts[10] );
                            link.Modified = false;
                            links.Add( new Pair<int, int>( link.I, link.J ), link );
                        }
                    }
                    catch { }
                }
            }

            // Now that we have loaded the data it is time to create the sparse structures
            var numberOfLinks = links.Count;
            var first = new int[numberOfLinks];
            var second = new int[numberOfLinks];
            var data = new Link[numberOfLinks];
            int i = 0;
            foreach ( var l in links.Values )
            {
                first[i] = l.I;
                second[i] = l.J;
                data[i] = l;
                i++;
            }
            if ( nodes.Values.Count == 0 )
            {
                this.Nodes = null;
            }
            else
            {
                this.Nodes = SparseArray<Node>.CreateSparseArray( ( n => n.Number ), nodes.Values.ToArray() );
            }
            if ( numberOfLinks == 0 )
            {
                this.Links = null;
            }
            else
            {
                this.Links = SparseTwinIndex<Link>.CreateTwinIndex( first, second, data );
            }
        }
Exemplo n.º 2
0
 private void WriteLink(StreamWriter writer, Link link)
 {
     writer.WriteLine( "m {0} {1} {2} {3} {4} {5} {6} 0 {7} {8}",
         link.I, link.J, link.Length, new string( link.Modes ), link.LinkType, link.Lanes, link.VDF, link.Speed, link.Capacity );
 }