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