View Javadoc
1   package org.opentrafficsim.demo.network.xml;
2   
3   import java.awt.Dimension;
4   import java.io.Serializable;
5   import java.net.URISyntaxException;
6   import java.net.URL;
7   import java.rmi.RemoteException;
8   
9   import javax.naming.NamingException;
10  import javax.swing.SwingUtilities;
11  import javax.xml.bind.JAXBException;
12  import javax.xml.parsers.ParserConfigurationException;
13  
14  import org.djunits.value.vdouble.scalar.Duration;
15  import org.djunits.value.vdouble.scalar.Time;
16  import org.djutils.io.URLResource;
17  import org.opentrafficsim.core.dsol.AbstractOTSModel;
18  import org.opentrafficsim.core.dsol.OTSAnimator;
19  import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
20  import org.opentrafficsim.core.geometry.OTSGeometryException;
21  import org.opentrafficsim.core.gis.CoordinateTransformWGS84toRDNew;
22  import org.opentrafficsim.core.gtu.GTUException;
23  import org.opentrafficsim.core.network.NetworkException;
24  import org.opentrafficsim.demo.network.xml.TestXMLParserEindhoven.TestXMLModelEindhoven;
25  import org.opentrafficsim.draw.core.OTSDrawingException;
26  import org.opentrafficsim.road.network.OTSRoadNetwork;
27  import org.opentrafficsim.road.network.factory.xml.XmlParserException;
28  import org.opentrafficsim.road.network.factory.xml.parser.XmlNetworkLaneParser;
29  import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLight;
30  import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLightColor;
31  import org.opentrafficsim.swing.gui.OTSAnimationPanel;
32  import org.opentrafficsim.swing.gui.OTSSimulationApplication;
33  import org.xml.sax.SAXException;
34  
35  import nl.javel.gisbeans.io.esri.CoordinateTransform;
36  import nl.tudelft.simulation.dsol.SimRuntimeException;
37  import nl.tudelft.simulation.dsol.animation.D2.GisRenderable2D;
38  import nl.tudelft.simulation.language.DSOLException;
39  
40  /**
41   * <p>
42   * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
43   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
44   * <p>
45   * $LastChangedDate: 2015-09-14 01:33:02 +0200 (Mon, 14 Sep 2015) $, @version $Revision: 1401 $, by $Author: averbraeck $,
46   * initial version Oct 17, 2014 <br>
47   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
48   */
49  public class TestXMLParserEindhoven extends OTSSimulationApplication<TestXMLModelEindhoven>
50  {
51      /** */
52      private static final long serialVersionUID = 1L;
53  
54      /**
55       * @param model the model
56       * @param animationPanel the animation panel
57       * @throws OTSDrawingException on drawing error
58       */
59      public TestXMLParserEindhoven(final TestXMLModelEindhoven model, final OTSAnimationPanel animationPanel)
60              throws OTSDrawingException
61      {
62          super(model, animationPanel);
63      }
64  
65      /** {@inheritDoc} */
66      @Override
67      protected void setAnimationToggles()
68      {
69          super.setAnimationToggles();
70          getAnimationPanel().addAllToggleGISButtonText(" GIS Layers:", getModel().getGisMap(), "Turn GIS map layer on or off");
71      }
72  
73      /**
74       * Main program.
75       * @param args String[]; the command line arguments (not used)
76       * @throws SimRuntimeException should never happen
77       */
78      public static void main(final String[] args) throws SimRuntimeException
79      {
80          SwingUtilities.invokeLater(new Runnable()
81          {
82              @Override
83              public void run()
84              {
85                  try
86                  {
87                      OTSAnimator simulator = new OTSAnimator("TestXMLParserEindhoven");
88                      TestXMLModelEindhoven xmlModel = new TestXMLModelEindhoven(simulator);
89                      simulator.initialize(Time.ZERO, Duration.ZERO, Duration.instantiateSI(3600.0), xmlModel);
90                      OTSAnimationPanel animationPanel = new OTSAnimationPanel(xmlModel.getNetwork().getExtent(),
91                              new Dimension(800, 600), simulator, xmlModel, DEFAULT_COLORER, xmlModel.getNetwork());
92                      new TestXMLParserEindhoven(xmlModel, animationPanel);
93                  }
94                  catch (SimRuntimeException | NamingException | RemoteException | OTSDrawingException | DSOLException exception)
95                  {
96                      exception.printStackTrace();
97                  }
98              }
99          });
100     }
101 
102     /** {@inheritDoc} */
103     @Override
104     public final String toString()
105     {
106         return "TestXMLParserEindhoven []";
107     }
108 
109     /**
110      * Model to test the XML parser.
111      * <p>
112      * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. <br>
113      * All rights reserved. BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim
114      * License</a>.
115      * <p>
116      * $LastChangedDate: 2015-09-14 01:33:02 +0200 (Mon, 14 Sep 2015) $, @version $Revision: 1401 $, by $Author: averbraeck $,
117      * initial version un 27, 2015 <br>
118      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
119      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
120      */
121     static class TestXMLModelEindhoven extends AbstractOTSModel
122     {
123         /** */
124         private static final long serialVersionUID = 20141121L;
125 
126         /** the network. */
127         private OTSRoadNetwork network;
128 
129         /** the GIS map. */
130         private GisRenderable2D gisMap;
131 
132         /**
133          * @param simulator the simulator
134          */
135         TestXMLModelEindhoven(final OTSSimulatorInterface simulator)
136         {
137             super(simulator);
138         }
139 
140         /** {@inheritDoc} */
141         @Override
142         public final void constructModel() throws SimRuntimeException
143         {
144             URL xmlURL = URLResource.getResource("/xml/Eindhoven.xml");
145             this.network = new OTSRoadNetwork("Example network", true);
146             try
147             {
148                 XmlNetworkLaneParser.build(xmlURL, this.network, getSimulator(), false);
149             }
150             catch (NetworkException | ParserConfigurationException | SAXException | OTSGeometryException | JAXBException
151                     | URISyntaxException | XmlParserException | GTUException exception)
152             {
153                 exception.printStackTrace();
154             }
155 
156             for (TrafficLight tl : this.network.getObjectMap(TrafficLight.class).values())
157             {
158                 tl.setTrafficLightColor(TrafficLightColor.GREEN);
159             }
160 
161             URL gisURL = URLResource.getResource("/xml/A58/map.xml");
162             System.err.println("GIS-map file: " + gisURL.toString());
163             CoordinateTransform rdto0 = new CoordinateTransformWGS84toRDNew(0, 0);
164             this.gisMap = new GisRenderable2D(this.simulator, gisURL, rdto0);
165         }
166 
167         /**
168          * @return gisMap
169          */
170         public final GisRenderable2D getGisMap()
171         {
172             return this.gisMap;
173         }
174 
175         /** {@inheritDoc} */
176         @Override
177         public OTSRoadNetwork getNetwork()
178         {
179             return this.network;
180         }
181 
182         /** {@inheritDoc} */
183         @Override
184         public final String toString()
185         {
186             return "TestXMLModelEindhoven [simulator=" + this.simulator + "]";
187         }
188 
189         /** {@inheritDoc} */
190         @Override
191         public Serializable getSourceId()
192         {
193             return "TestXMLModelEindhoven";
194         }
195 
196     }
197 }