View Javadoc
1   package nl.tudelft.simulation.examples.dsol.animation.gis;
2   
3   import java.io.IOException;
4   import java.net.URL;
5   import java.rmi.RemoteException;
6   
7   import javax.naming.NamingException;
8   
9   import org.djutils.draw.bounds.Bounds2d;
10  import org.djutils.io.URLResource;
11  
12  import nl.tudelft.simulation.dsol.SimRuntimeException;
13  import nl.tudelft.simulation.dsol.animation.d2.RenderableScale;
14  import nl.tudelft.simulation.dsol.animation.gis.GisRenderable2d;
15  import nl.tudelft.simulation.dsol.animation.gis.esri.EsriFileXmlParser;
16  import nl.tudelft.simulation.dsol.animation.gis.esri.EsriRenderable2d;
17  import nl.tudelft.simulation.dsol.experiment.Replication;
18  import nl.tudelft.simulation.dsol.experiment.SingleReplication;
19  import nl.tudelft.simulation.dsol.model.AbstractDsolModel;
20  import nl.tudelft.simulation.dsol.simulators.DevsRealTimeAnimator;
21  import nl.tudelft.simulation.dsol.simulators.DevsSimulatorInterface;
22  import nl.tudelft.simulation.dsol.swing.gui.DsolPanel;
23  import nl.tudelft.simulation.dsol.swing.gui.animation.DsolAnimationApplication;
24  import nl.tudelft.simulation.dsol.swing.gui.animation.DsolAnimationGisTab;
25  import nl.tudelft.simulation.dsol.swing.gui.control.RealTimeControlPanel;
26  import nl.tudelft.simulation.language.DsolException;
27  
28  /**
29   * GIS demo to show a map using ESRI shape files.
30   * <p>
31   * Copyright (c) 2002-2023 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
32   * for project information <a href="https://simulation.tudelft.nl/" target="_blank"> https://simulation.tudelft.nl</a>. The DSOL
33   * project is distributed under a three-clause BSD-style license, which can be found at
34   * <a href="https://https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank">
35   * https://https://simulation.tudelft.nl/dsol/docs/latest/license.html</a>.
36   * </p>
37   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
38   */
39  public class EsriXmlSwingApplication extends DsolAnimationApplication
40  {
41      /**
42       * @param title String; the title
43       * @param panel DsolPanel; the panel
44       * @param animationTab DsolAnimationGisTab; the (custom) animation tab
45       * @throws DsolException when simulator is not an animator
46       * @throws IllegalArgumentException for illegal bounds
47       * @throws RemoteException on network error
48       */
49      public EsriXmlSwingApplication(final String title, final DsolPanel panel, final DsolAnimationGisTab animationTab)
50              throws RemoteException, IllegalArgumentException, DsolException
51      {
52          super(panel, title, animationTab);
53          panel.enableSimulationControlButtons();
54      }
55  
56      /** */
57      private static final long serialVersionUID = 1L;
58  
59      /**
60       * @param args String[]; arguments, expected to be empty
61       * @throws SimRuntimeException on error
62       * @throws RemoteException on error
63       * @throws NamingException on error
64       * @throws DsolException when simulator is not an animator
65       */
66      public static void main(final String[] args) throws SimRuntimeException, RemoteException, NamingException, DsolException
67      {
68          DevsRealTimeAnimator.TimeDouble simulator = new DevsRealTimeAnimator.TimeDouble("EsriSwingApplication", 0.001);
69          EmptyModel model = new EmptyModel(simulator);
70          Replication<Double> replication = new SingleReplication<Double>("rep1", 0.0, 0.0, 1000000.0);
71          simulator.initialize(model, replication);
72  
73          DsolPanel panel = new DsolPanel(new RealTimeControlPanel.TimeDouble(model, simulator));
74          Bounds2d mapBounds = new Bounds2d(4.355, 4.386, 51.995, 52.005);
75          DsolAnimationGisTab animationTab = new DsolAnimationGisTab(mapBounds, simulator);
76          animationTab.getAnimationPanel().setRenderableScale(
77                  new RenderableScale(Math.cos(Math.toRadians(mapBounds.midPoint().getY())), 1.0 / 111319.24));
78          animationTab.addAllToggleGISButtonText("MAP LAYERS", model.getGisMap(), "hide or show this GIS layer");
79          new EsriXmlSwingApplication("EsriSwingApplication", panel, animationTab);
80      }
81  
82      /** The empty model -- this demo is just to show a map on the screen. */
83      static class EmptyModel extends AbstractDsolModel<Double, DevsSimulatorInterface<Double>>
84      {
85          /** The default serial version UID for serializable classes. */
86          private static final long serialVersionUID = 1L;
87  
88          /** the GIS map. */
89          private GisRenderable2d gisMap;
90  
91          /**
92           * constructs a new EmptyModel.
93           * @param simulator DevsSimulatorInterface&lt;Double&gt;; the simulator
94           */
95          EmptyModel(final DevsSimulatorInterface<Double> simulator)
96          {
97              super(simulator);
98          }
99  
100         /** {@inheritDoc} */
101         @Override
102         public void constructModel() throws SimRuntimeException
103         {
104             URL gisURL = URLResource.getResource("/resources/esri/tudelft.xml");
105             System.out.println("ESRI-map file: " + gisURL.toString());
106             try
107             {
108                 this.gisMap = new EsriRenderable2d(getSimulator().getReplication(), EsriFileXmlParser.parseMapFile(gisURL));
109             }
110             catch (IOException e)
111             {
112                 throw new SimRuntimeException(e);
113             }
114         }
115 
116         /**
117          * @return gisMap
118          */
119         public GisRenderable2d getGisMap()
120         {
121             return this.gisMap;
122         }
123     }
124 }