SUMOSAXNeworkfileHandler.java

Go to the documentation of this file.
00001 package de.psi.telco.sumoplayer;
00002 
00003 import org.xml.sax.Attributes;
00004 import org.xml.sax.SAXException;
00005 import org.xml.sax.helpers.DefaultHandler;
00006 
00007 import de.psi.telco.sumoplayer.util.PointImpl;
00008 
00015 public class SUMOSAXNeworkfileHandler extends DefaultHandler{
00016 
00017     // build data
00018     public SUMONetwork network = new SUMONetwork();;
00019     
00020     // element state
00021     private static int UNKNOWN = 0;
00022     private static int EDGE = 1;
00023     private static int LANE = 2;
00024     private static int CEDGE = 3;
00025     private static int JUNCTION = 4;
00026     private static int OFFSET = 5;
00027     private static int PROJ = 6;
00028     private int currentElementType;
00029     
00030     // store
00031     private String sumoEdgeId; 
00032     private String sumoLaneId;
00033     private String cEdgeId;
00034     private String junctionId;
00035     
00036     public void startElement (String uri, String localName, String qName, Attributes attributes) throws SAXException{
00037         if (qName.equals("edge")){
00038             this.currentElementType = EDGE;
00039             sumoEdgeId = attributes.getValue("id");
00040             
00041         }else if (qName.equals("lane")){
00042             this.currentElementType = LANE;
00043             sumoLaneId = attributes.getValue("id");
00044             
00045             network.lanes.put(sumoLaneId, new SUMOLane(sumoLaneId));
00046             
00047         }else if (qName.equals("cedge")){   // cedges seem to be mappings from sumo.edg.xml IDs to sumoIDs
00048             this.currentElementType = CEDGE;
00049             cEdgeId = attributes.getValue("id");
00050         }
00051         
00052         /*  // note junctions not interesting. all coordinates are stored within lanes
00053         else if (qName.equals("junction")){ // junction found. add it
00054             this.currentEementType = JUNCTION;
00055             
00056             //  i.e. <junction id="124667945" type="priority" x="3186.50" y="17778.50">...</junction>
00057             junctionId = attributes.getValue("id");
00058             double x = Double.parseDouble(attributes.getValue("x"));
00059             double y = Double.parseDouble(attributes.getValue("y"));
00060             network.addJunction(junctionId, x, y);
00061         }*/
00062         
00063         // some network cfg
00064         else if (qName.equals("net-offset")){
00065             this.currentElementType = OFFSET;
00066         }else if (qName.equals("orig-proj")){
00067             this.currentElementType = PROJ;
00068         }
00069         
00070     }
00071     
00072     public void endElement (String uri, String localName, String qName) throws SAXException{
00073         this.currentElementType = UNKNOWN;
00074     }
00075     
00076     // this can be used to read coordinates from edges for example:  <edge id="bla"...>COORD1X,COORD1Y COORD2X,COORD2Y</edge>  
00077     public void characters (char ch[], int start, int length){
00078         
00079         if (currentElementType == LANE){    // read coords from lane
00080             
00081             char[] chars = new char[length];    // copy stuff from sax buffer
00082             for (int i = 0; i<length; i++){
00083                 chars[i] = ch[start+i];
00084             }
00085             String data = new String(chars);
00086             
00087             String[] coordPairs = data.split(" ");  // split by pairs (  COORD1X,COORD1Y COORD2X,COORD2Y ...)
00088             for (int i = 0 ; i < coordPairs.length; i++){
00089                 String[] coordElements = coordPairs[i].split(",");
00090                 if (coordElements != null && coordElements.length == 2){    // split by comma ( COORD1X,COORD1Y )
00091                     try{
00092                         double x = Double.parseDouble(coordElements[0]);
00093                         double y = Double.parseDouble(coordElements[1]);
00094                         
00095                         network.lanes.get(sumoLaneId).points.add(new PointImpl(x,y));
00096                     }catch(NumberFormatException e){
00097                     }
00098                 }               
00099             }
00100 
00101         }else if (currentElementType == OFFSET){    // read offset. i.e:  <net-offset>-755969.000000,-5660071.000000</net-offset>
00102             char[] chars = new char[length];    // copy stuff from sax buffer
00103             for (int i = 0; i<length; i++){
00104                 chars[i] = ch[start+i];
00105             }
00106             String data = new String(chars);
00107             String[] vectorElements = data.split(",");
00108             if (vectorElements.length==2){
00109                 network.offset = new PointImpl(Double.parseDouble(vectorElements[0]),Double.parseDouble(vectorElements[1]));
00110             }
00111         }else if (currentElementType == PROJ){  // read projection string. i.e:  <orig-proj>+proj=utm +ellps=bessel +units=m</orig-proj>
00112             char[] chars = new char[length];    // copy stuff from sax buffer
00113             for (int i = 0; i<length; i++){
00114                 chars[i] = ch[start+i];
00115             }
00116             network.projString = new String(chars); 
00117         }
00118     }
00119 }

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