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