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                  }
95                  catch (SimRuntimeException | NamingException | RemoteException | OTSDrawingException | DSOLException exception)
96                  {
97                      exception.printStackTrace();
98                  }
99              }
100         });
101     }
102 
103     /** {@inheritDoc} */
104     @Override
105     public final String toString()
106     {
107         return "TestXMLParserN201 []";
108     }
109 
110     /**
111      * Model to test the XML parser.
112      * <p>
113      * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. <br>
114      * All rights reserved. BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim
115      * License</a>.
116      * <p>
117      * $LastChangedDate: 2015-09-14 01:33:02 +0200 (Mon, 14 Sep 2015) $, @version $Revision: 1401 $, by $Author: averbraeck $,
118      * initial version un 27, 2015 <br>
119      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
120      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
121      */
122     static class TestXMLModelN201 extends AbstractOTSModel
123     {
124         /** */
125         private static final long serialVersionUID = 20141121L;
126 
127         /** the network. */
128         private OTSRoadNetwork network;
129 
130         /** the GIS map. */
131         private GisRenderable2D gisMap;
132 
133         /**
134          * @param simulator the simulator
135          */
136         TestXMLModelN201(final OTSSimulatorInterface simulator)
137         {
138             super(simulator);
139         }
140 
141         /** {@inheritDoc} */
142         @Override
143         public final void constructModel() throws SimRuntimeException
144         {
145             URL xmlURL = URLResource.getResource("/xml/N201.xml");
146             this.network = new OTSRoadNetwork("Example network", true, getSimulator());
147             try
148             {
149                 XmlNetworkLaneParser.build(xmlURL, this.network, false);
150             }
151             catch (NetworkException | ParserConfigurationException | SAXException | OTSGeometryException | JAXBException
152                     | URISyntaxException | XmlParserException | GTUException | IOException | TrafficControlException exception)
153             {
154                 exception.printStackTrace();
155             }
156 
157             for (TrafficLight tl : this.network.getObjectMap(TrafficLight.class).values())
158             {
159                 tl.setTrafficLightColor(TrafficLightColor.GREEN);
160             }
161 
162             URL gisURL = URLResource.getResource("/xml/N201/map.xml");
163             System.err.println("GIS-map file: " + gisURL.toString());
164             CoordinateTransform rdto0 = new CoordinateTransformWGS84toRDNew(0, 0);
165             this.gisMap = new GisRenderable2D(this.simulator, gisURL, rdto0);
166         }
167 
168         /**
169          * @return gisMap
170          */
171         public final GisRenderable2D getGisMap()
172         {
173             return this.gisMap;
174         }
175 
176         /** {@inheritDoc} */
177         @Override
178         public OTSRoadNetwork getNetwork()
179         {
180             return this.network;
181         }
182 
183         /** {@inheritDoc} */
184         @Override
185         public final String toString()
186         {
187             return "TestXMLModelN201 [simulator=" + this.simulator + "]";
188         }
189 
190         /** {@inheritDoc} */
191         @Override
192         public Serializable getSourceId()
193         {
194             return "TestXMLModelN201";
195         }
196 
197     }
198 }