ns2::NetReader Class Reference


Detailed Description

class for reading net file
Author:
Thimor Bohn <bohn@itm.uni-luebeck.de>

Definition at line 19 of file ns2/NetReader.java.


Static Public Member Functions

static void read (String net, List< Edge > edges, Map< String, Junction > junctions)

Member Function Documentation

static void ns2::NetReader::read ( String  net,
List< Edge edges,
Map< String, Junction junctions 
) [inline, static]

method for reading sumo net file

Parameters:
net name of sumo net file
edges holds net after execution

Definition at line 25 of file ns2/NetReader.java.

References ns2::Edge::lanes.

00025                                                                                            {        
00026         try {
00027             InputStream in = new FileInputStream(net);
00028             XMLInputFactory factory = XMLInputFactory.newInstance();
00029             XMLStreamReader parser = factory.createXMLStreamReader(in);
00030             // parse net file
00031             Edge edge = null;
00032             for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
00033                 if (event == XMLStreamConstants.START_ELEMENT) {
00034                     // edge element found
00035                     if (parser.getLocalName().equals("edge")) {
00036                         String id    = "";
00037                         String from  = "";
00038                         String to    = "";
00039                         // parse attributes of element
00040                         for (int attr=0; attr < parser.getAttributeCount(); attr++) {
00041                             String attrName = parser.getAttributeLocalName(attr);
00042                             String value = parser.getAttributeValue(attr);
00043                             if ("id".equals(attrName)) {
00044                                 id = value;
00045                             }
00046                             if ("from".equals(attrName)) {
00047                                 from = value;
00048                             }
00049                             if ("to".equals(attrName)) {
00050                                 to = value;
00051                             }
00052                             
00053                         }
00054                         // construct edge element
00055                         edge = new Edge(id, from, to);
00056                         // store edge
00057                         edges.add(edge);
00058                     }
00059                     if (parser.getLocalName().equals("lane")) {
00060                         String id      = "";
00061                         float xfrom    = 0;
00062                         float xto      = 0;
00063                         float yfrom    = 0;
00064                         float yto      = 0;
00065                         float length   = 0;
00066                         boolean hadShape = false;
00067                         // parse attributes of element
00068                         for (int attr=0; attr < parser.getAttributeCount(); attr++) {
00069                             String attrName = parser.getAttributeLocalName(attr);
00070                             String value = parser.getAttributeValue(attr);
00071                             if ("id".equals(attrName)) {
00072                                 id = value;
00073                             }
00074                             if ("length".equals(attrName)) {
00075                                 length = Float.parseFloat(value);
00076                             }
00077                             if ("shape".equals(attrName)) {
00078                                 String[] vals = value.split("[,\\ ]"); // separate by delimiters "," and " "
00079                                 xfrom = Float.parseFloat(vals[0]);
00080                                 yfrom = Float.parseFloat(vals[1]);
00081                                 xto   = Float.parseFloat(vals[2]);
00082                                 yto   = Float.parseFloat(vals[3]);
00083                                 hadShape = true;
00084                             }
00085                         }
00086                         if(!hadShape) {
00087                             String text   = parser.getElementText();
00088                             String[] vals = text.split("[,\\ ]"); // separate by delimiters "," and " "
00089                             xfrom = Float.parseFloat(vals[0]);
00090                             yfrom = Float.parseFloat(vals[1]);
00091                             xto   = Float.parseFloat(vals[2]);
00092                             yto   = Float.parseFloat(vals[3]);
00093                         }
00094                         edge.lanes.put(id, new Lane(id, xfrom, xto, yfrom, yto, length));
00095                     }
00096                     if (parser.getLocalName().equals("junction")) {
00097                         String id = "";
00098                         float x = 0;
00099                         float y = 0;
00100                         // parse attributes of element
00101                         for (int attr=0; attr < parser.getAttributeCount(); attr++) {
00102                             String attrName = parser.getAttributeLocalName(attr);
00103                             String value = parser.getAttributeValue(attr);
00104                             if ("id".equals(attrName)) {
00105                                 id = value;
00106                             }
00107                             if ("x".equals(attrName)) {
00108                                 x = Float.parseFloat(value);
00109                             }
00110                             if ("y".equals(attrName)) {
00111                                 y = Float.parseFloat(value);
00112                             }
00113                         }
00114                         Junction junction = new Junction(id, x, y);
00115                         junctions.put(id, junction);
00116                     }
00117                 }
00118             }
00119             parser.close();
00120         } catch (XMLStreamException ex) {
00121             System.err.println(ex);
00122         } catch (IOException ex) {
00123             System.err.println(ex);
00124         }
00125     }


The documentation for this class was generated from the following file:

Generated on Wed May 5 00:07:02 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6