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