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                     animationPanel.enableSimulationControlButtons();
114                 }
115                 catch (SimRuntimeException | NamingException | RemoteException | OTSDrawingException | DSOLException exception)
116                 {
117                     exception.printStackTrace();
118                 }
119             }
120         });
121     }
122 
123     /** {@inheritDoc} */
124     @Override
125     public final String toString()
126     {
127         return "TestXMLParserAmsterdam []";
128     }
129 
130     /**
131      * Model to test the XML parser.
132      * <p>
133      * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. <br>
134      * All rights reserved. BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim
135      * License</a>.
136      * <p>
137      * $LastChangedDate: 2015-09-14 01:33:02 +0200 (Mon, 14 Sep 2015) $, @version $Revision: 1401 $, by $Author: averbraeck $,
138      * initial version un 27, 2015 <br>
139      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
140      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
141      */
142     static class TestXMLModelAmsterdam extends AbstractOTSModel
143     {
144         /** */
145         private static final long serialVersionUID = 20141121L;
146 
147         /** the network. */
148         private OTSRoadNetwork network;
149 
150         /** the GIS map. */
151         private GisRenderable2D gisMap;
152 
153         /**
154          * @param simulator the simulator
155          */
156         TestXMLModelAmsterdam(final OTSSimulatorInterface simulator)
157         {
158             super(simulator);
159         }
160 
161         /** {@inheritDoc} */
162         @Override
163         public final void constructModel() throws SimRuntimeException
164         {
165             URL xmlURL = URLResource.getResource("/xml/Amsterdam.xml");
166             this.network = new OTSRoadNetwork("Amsterdam network", true, getSimulator());
167             try
168             {
169                 XmlNetworkLaneParser.build(xmlURL, this.network, true);
170                 List<Node> unbalancedCentroids = this.network.getUnbalancedCentroids(this.network.getGtuType("Car.53"));
171                 System.out.println("The network has " + unbalancedCentroids.size() + " unbalanced centroids");
172                 // System.out.println("OBJECTMAP.SIZE  = " + this.network.getObjectMap().size());
173             }
174             catch (NetworkException | ParserConfigurationException | SAXException | OTSGeometryException | JAXBException
175                     | URISyntaxException | XmlParserException | GTUException | IOException | TrafficControlException exception)
176             {
177                 exception.printStackTrace();
178             }
179 
180             for (TrafficLight tl : this.network.getObjectMap(TrafficLight.class).values())
181             {
182                 tl.setTrafficLightColor(TrafficLightColor.GREEN);
183             }
184 
185             URL gisURL = URLResource.getResource("/xml/Amsterdam/map.xml");
186             System.err.println("GIS-map file: " + gisURL.toString());
187             CoordinateTransform rdto0 = new CoordinateTransformWGS84toRDNew(0, 0);
188             this.gisMap = new GisRenderable2D(this.simulator, gisURL, rdto0);
189         }
190 
191         /**
192          * @return gisMap
193          */
194         public final GisRenderable2D getGisMap()
195         {
196             return this.gisMap;
197         }
198 
199         /** {@inheritDoc} */
200         @Override
201         public OTSRoadNetwork getNetwork()
202         {
203             return this.network;
204         }
205 
206         /** {@inheritDoc} */
207         @Override
208         public final String toString()
209         {
210             return "TestXMLModelAmsterdam [simulator=" + this.simulator + "]";
211         }
212 
213         /** {@inheritDoc} */
214         @Override
215         public Serializable getSourceId()
216         {
217             return "TestXMLModelAmsterdam";
218         }
219 
220     }
221 }