SUMOSAXHandler.java
Go to the documentation of this file.00001 package de.psi.telco.sumoplayer;
00002
00003 import java.util.HashSet;
00004 import java.util.Set;
00005
00006 import org.xml.sax.Attributes;
00007 import org.xml.sax.SAXException;
00008 import org.xml.sax.helpers.DefaultHandler;
00009
00010 import de.psi.telco.sumoplayer.util.GeoCalc;
00011 import de.psi.telco.sumoplayer.util.Point;
00012 import de.psi.telco.sumoplayer.util.PointImpl;
00013
00021 public class SUMOSAXHandler extends DefaultHandler{
00022
00027 private double car4carEquippedPropbaility = 0.01;
00028 private int jitter = 0;
00029
00030 private Set<String> trackedVehicles = new HashSet<String>();
00031 private Set<String> knownVehicles = new HashSet<String>();
00032
00033 private int timestep;
00034 private long startTime;
00035
00036 private String edgeId;
00037 private String laneId;
00038
00039 private String vehicleId;
00040 private double pos;
00041 private double speed;
00042
00043 private SUMOLocationListener listener;
00044 private SUMOGeoCoordinatesResolver resolver;
00045
00046 public SUMOSAXHandler(String netfilename){
00047 this.resolver = new SUMOGeoCoordinatesResolver(netfilename);
00048 }
00049
00050 public void startDocument(){
00051 startTime = System.currentTimeMillis();
00052 }
00053
00054 public void startElement (String uri, String localName, String qName, Attributes attributes) throws SAXException{
00055 if (qName.equals("timestep")){
00056 timestep = Integer.parseInt(attributes.getValue("time"));
00057
00058 long timeToBe = startTime + timestep*1000;
00059 long now = System.currentTimeMillis();
00060 try {
00061 if (timeToBe-now>0){
00062 Thread.sleep(timeToBe-now);
00063 }
00064 } catch (InterruptedException e) {
00065
00066 e.printStackTrace();
00067 }
00068 }else if(qName.equals("edge")){
00069 edgeId = attributes.getValue("id");
00070 }else if(qName.equals("lane")){
00071 laneId = attributes.getValue("id");
00072 }else if(qName.equals("vehicle")){
00073 vehicleId = attributes.getValue("id");
00074 pos = Double.parseDouble(attributes.getValue("pos"));
00075 speed = Double.parseDouble(attributes.getValue("speed"));
00076
00077 handleVehicle();
00078 }
00079 }
00080
00081 private void handleVehicle(){
00082 if (trackedVehicles.size() == 0){
00083 knownVehicles.add(vehicleId);
00084 trackedVehicles.add(vehicleId);
00085 }else{
00086 if (!knownVehicles.contains(vehicleId)){
00087
00088 knownVehicles.add(vehicleId);
00089 if (car4carEquippedPropbaility > Math.random()){
00090 trackedVehicles.add(vehicleId);
00091 }
00092 }
00093 }
00094
00095 if (trackedVehicles.contains(vehicleId)){
00096
00097 Point p = resolver.resolv(edgeId, laneId, pos);
00098
00099
00100 if (jitter > 0){
00101 double jitterLon = GeoCalc.getLonOffset(p.getX(), p.getY(), jitter)*((Math.random()*2)-1);
00102 double jitterLat = GeoCalc.getLatOffset(p.getX(), p.getY(), jitter)*((Math.random()*2)-1);
00103 p = new PointImpl(p.getX()+jitterLon,p.getY()+jitterLat);
00104 }
00105
00106
00107 this.listener.LocationUpdated(vehicleId, timestep, p.getX(), p.getY(), speed*3.6);
00108 }
00109 }
00110
00111 public double getCar4carEquippedPropbaility() {
00112 return car4carEquippedPropbaility;
00113 }
00114
00115 public void setCar4carEquippedPropability(double car4carEquippedPropbaility) {
00116 this.car4carEquippedPropbaility = car4carEquippedPropbaility;
00117 }
00118
00119 public void setListener(SUMOLocationListener listener) {
00120 this.listener = listener;
00121 }
00122
00123 public void setResolver(SUMOGeoCoordinatesResolver resolver) {
00124 this.resolver = resolver;
00125 }
00126
00131 public void setJitter(int jitter){
00132 this.jitter = jitter;
00133 }
00134 }