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