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