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.TestXMLParserA58.TestXMLModelA58;
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 TestXMLParserA58 extends OTSSimulationApplication<TestXMLModelA58>
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 TestXMLParserA58(final TestXMLModelA58 model, final OTSAnimationPanel animationPanel) throws OTSDrawingException
60      {
61          super(model, animationPanel);
62      }
63  
64      /** {@inheritDoc} */
65      @Override
66      protected void setAnimationToggles()
67      {
68          super.setAnimationToggles();
69          getAnimationPanel().addAllToggleGISButtonText(" GIS Layers:", getModel().getGisMap(), "Turn GIS map layer on or off");
70      }
71  
72      /**
73       * Main program.
74       * @param args String[]; the command line arguments (not used)
75       * @throws SimRuntimeException should never happen
76       */
77      public static void main(final String[] args) throws SimRuntimeException
78      {
79          SwingUtilities.invokeLater(new Runnable()
80          {
81              @Override
82              public void run()
83              {
84                  try
85                  {
86                      OTSAnimator simulator = new OTSAnimator("TestXMLParserA58");
87                      TestXMLModelA58 xmlModel = new TestXMLModelA58(simulator);
88                      simulator.initialize(Time.ZERO, Duration.ZERO, Duration.instantiateSI(3600.0), xmlModel);
89                      OTSAnimationPanel animationPanel = new OTSAnimationPanel(xmlModel.getNetwork().getExtent(),
90                              new Dimension(800, 600), simulator, xmlModel, DEFAULT_COLORER, xmlModel.getNetwork());
91                      new TestXMLParserA58(xmlModel, animationPanel);
92                  }
93                  catch (SimRuntimeException | NamingException | RemoteException | OTSDrawingException | DSOLException exception)
94                  {
95                      exception.printStackTrace();
96                  }
97              }
98          });
99      }
100 
101     /** {@inheritDoc} */
102     @Override
103     public final String toString()
104     {
105         return "TestXMLParserA58 []";
106     }
107 
108     /**
109      * Model to test the XML parser.
110      * <p>
111      * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. <br>
112      * All rights reserved. BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim
113      * License</a>.
114      * <p>
115      * $LastChangedDate: 2015-09-14 01:33:02 +0200 (Mon, 14 Sep 2015) $, @version $Revision: 1401 $, by $Author: averbraeck $,
116      * initial version un 27, 2015 <br>
117      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
118      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
119      */
120     static class TestXMLModelA58 extends AbstractOTSModel
121     {
122         /** */
123         private static final long serialVersionUID = 20141121L;
124 
125         /** the network. */
126         private OTSRoadNetwork network;
127 
128         /** the GIS map. */
129         private GisRenderable2D gisMap;
130 
131         /**
132          * @param simulator the simulator
133          */
134         TestXMLModelA58(final OTSSimulatorInterface simulator)
135         {
136             super(simulator);
137         }
138 
139         /** {@inheritDoc} */
140         @Override
141         public final void constructModel() throws SimRuntimeException
142         {
143             URL xmlURL = URLResource.getResource("/xml/A58.xml");
144             this.network = new OTSRoadNetwork("Example network", true);
145             try
146             {
147                 XmlNetworkLaneParser.build(xmlURL, this.network, getSimulator(), false);
148             }
149             catch (NetworkException | ParserConfigurationException | SAXException | OTSGeometryException | JAXBException
150                     | URISyntaxException | XmlParserException | GTUException exception)
151             {
152                 exception.printStackTrace();
153             }
154 
155             for (TrafficLight tl : this.network.getObjectMap(TrafficLight.class).values())
156             {
157                 tl.setTrafficLightColor(TrafficLightColor.GREEN);
158             }
159 
160             URL gisURL = URLResource.getResource("/xml/A58/map.xml");
161             System.err.println("GIS-map file: " + gisURL.toString());
162             CoordinateTransform rdto0 = new CoordinateTransformWGS84toRDNew(0, 0);
163             this.gisMap = new GisRenderable2D(this.simulator, gisURL, rdto0);
164         }
165 
166         /**
167          * @return gisMap
168          */
169         public final GisRenderable2D getGisMap()
170         {
171             return this.gisMap;
172         }
173 
174         /** {@inheritDoc} */
175         @Override
176         public OTSRoadNetwork getNetwork()
177         {
178             return this.network;
179         }
180 
181         /** {@inheritDoc} */
182         @Override
183         public final String toString()
184         {
185             return "TestXMLModelA58 [simulator=" + this.simulator + "]";
186         }
187 
188         /** {@inheritDoc} */
189         @Override
190         public Serializable getSourceId()
191         {
192             return "TestXMLModelA58";
193         }
194 
195     }
196 }