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