View Javadoc
1   package org.opentrafficsim.demo.carFollowing;
2   
3   import java.awt.Dimension;
4   import java.awt.FileDialog;
5   import java.awt.geom.Rectangle2D;
6   import java.io.File;
7   import java.io.IOException;
8   import java.io.Serializable;
9   import java.net.MalformedURLException;
10  import java.net.URISyntaxException;
11  import java.rmi.RemoteException;
12  import java.util.ArrayList;
13  import java.util.Iterator;
14  import java.util.List;
15  
16  import javax.naming.NamingException;
17  import javax.swing.JFrame;
18  
19  import org.djunits.unit.util.UNITS;
20  import org.djunits.value.vdouble.scalar.Duration;
21  import org.djunits.value.vdouble.scalar.Time;
22  import org.opentrafficsim.core.dsol.AbstractOTSModel;
23  import org.opentrafficsim.core.dsol.OTSAnimator;
24  import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
25  import org.opentrafficsim.core.geometry.OTSGeometryException;
26  import org.opentrafficsim.core.network.Link;
27  import org.opentrafficsim.core.network.NetworkException;
28  import org.opentrafficsim.core.network.Node;
29  import org.opentrafficsim.draw.core.OTSDrawingException;
30  import org.opentrafficsim.road.network.OTSRoadNetwork;
31  import org.opentrafficsim.road.network.factory.osm.OSMLink;
32  import org.opentrafficsim.road.network.factory.osm.OSMNetwork;
33  import org.opentrafficsim.road.network.factory.osm.OSMNode;
34  import org.opentrafficsim.road.network.factory.osm.OSMTag;
35  import org.opentrafficsim.road.network.factory.osm.events.ProgressEvent;
36  import org.opentrafficsim.road.network.factory.osm.events.ProgressListener;
37  import org.opentrafficsim.road.network.factory.osm.events.ProgressListenerImpl;
38  import org.opentrafficsim.road.network.factory.osm.events.WarningListener;
39  import org.opentrafficsim.road.network.factory.osm.events.WarningListenerImpl;
40  import org.opentrafficsim.road.network.factory.osm.input.ReadOSMFile;
41  import org.opentrafficsim.road.network.factory.osm.output.Convert;
42  import org.opentrafficsim.road.network.lane.Lane;
43  import org.opentrafficsim.swing.gui.AnimationToggles;
44  import org.opentrafficsim.swing.gui.OTSAnimationPanel;
45  import org.opentrafficsim.swing.gui.OTSSimulationApplication;
46  
47  import nl.tudelft.simulation.dsol.SimRuntimeException;
48  import nl.tudelft.simulation.language.DSOLException;
49  
50  /**
51   * <p>
52   * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
53   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
54   * <p>
55   * $LastChangedDate: 2020-05-08 11:08:12 +0200 (Fri, 08 May 2020) $, @version $Revision: 6464 $, by $Author: pknoppers $,
56   * initial version Feb 10, 2015 <br>
57   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
58   * @author Moritz Bergmann
59   */
60  public class OpenStreetMap extends OTSSimulationApplication<OSMModel> implements UNITS
61  {
62      /** */
63      private static final long serialVersionUID = 1L;
64  
65      /**
66       * Construct the OpenStreetMap demo.
67       * @param model OSMModel; the model
68       * @param panel OTSAnimationPanel; the Swing panel
69       * @throws OTSDrawingException on animation error
70       */
71      public OpenStreetMap(final OSMModel model, final OTSAnimationPanel panel) throws OTSDrawingException
72      {
73          super(model, panel);
74      }
75  
76      /** {@inheritDoc} */
77      @Override
78      protected void setAnimationToggles()
79      {
80          AnimationToggles.setTextAnimationTogglesFull(getAnimationPanel());
81      }
82  
83      /**
84       * Main program.
85       * @param args String[]; the command line arguments (not used)
86       */
87      public static void main(final String[] args)
88      {
89          demo(true);
90      }
91  
92      /**
93       * Start the demo.
94       * @param exitOnClose boolean; when running stand-alone: true; when running as part of a demo: false
95       */
96      public static void demo(final boolean exitOnClose)
97      {
98          try
99          {
100             String filepath = chooseFile();
101             if (filepath != null)
102             {
103                 OTSAnimator simulator = new OTSAnimator("OpenStreetMap");
104                 final OSMModelwing/OpenStreetMap.html#OSMModel">OSMModel osmModel = new OSMModel(simulator, filepath);
105                 simulator.initialize(Time.ZERO, Duration.ZERO, Duration.instantiateSI(3600.0), osmModel);
106                 OTSAnimationPanel animationPanel = new OTSAnimationPanel(osmModel.getNetwork().getExtent(),
107                         new Dimension(800, 600), simulator, osmModel, DEFAULT_COLORER, osmModel.getNetwork());
108                 OpenStreetMapwing/OpenStreetMap.html#OpenStreetMap">OpenStreetMap app = new OpenStreetMap(osmModel, animationPanel);
109                 app.setExitOnClose(exitOnClose);
110             }
111             else
112             {
113                 if (exitOnClose)
114                 {
115                     System.exit(0);
116                 }
117             }
118         }
119         catch (SimRuntimeException | NamingException | RemoteException | OTSDrawingException | DSOLException exception)
120         {
121             exception.printStackTrace();
122         }
123     }
124 
125     /**
126      * choose a file and construct the model.
127      * @return a model based on a given file
128      */
129     protected static final String chooseFile()
130     {
131         JFrame frame = new JFrame();
132         FileDialog fd = new FileDialog(frame, "Choose a file", FileDialog.LOAD);
133         fd.setFile("*.osm");
134         fd.setVisible(true);
135         File[] file = fd.getFiles();
136         if (file.length == 0)
137         {
138             return null;
139         }
140         String filename = fd.getFile();
141         String filepath = null;
142         try
143         {
144             filepath = file[0].toURI().toURL().toString();
145         }
146         catch (MalformedURLException e)
147         {
148             e.printStackTrace();
149         }
150         if (filename == null)
151         {
152             System.out.println("You cancelled the choice");
153             return null;
154         }
155         return filepath;
156     }
157 }
158 
159 /**
160  * <p>
161  * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
162  * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
163  * <p>
164  * $LastChangedDate: 2020-05-08 11:08:12 +0200 (Fri, 08 May 2020) $, @version $Revision: 6464 $, by $Author: pknoppers $,
165  * initial version Feb 10, 2015 <br>
166  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
167  * @author Moritz Bergmann
168  */
169 class OSMModel extends AbstractOTSModel
170 {
171     /** */
172     private static final long serialVersionUID = 20150227L;
173 
174     /** Provided Network. */
175     private OSMNetwork osmNetwork;
176 
177     /** Provided lanes. */
178     private List<Lane> lanes = new ArrayList<>();
179 
180     /** The OTS network. */
181     private OTSRoadNetwork otsNetwork = new OTSRoadNetwork("network", true, getSimulator());
182 
183     /** The ProgressListener. */
184     private ProgressListener progressListener;
185 
186     /** The WarningListener. */
187     private WarningListener warningListener;
188 
189     /** The coordinate converter. */
190     private Convert converter;
191 
192     /** Bounding rectangle of the loaded map. */
193     private Rectangle2D rectangle = null;
194 
195     /** the file path. */
196     private String filepath;
197 
198     /**
199      * @param simulator OTSSimulatorInterface; the simulator
200      * @param filepath String; the path to the OSM file
201      */
202     OSMModel(final OTSSimulatorInterface simulator, final String filepath)
203     {
204         super(simulator);
205         this.filepath = filepath;
206     }
207 
208     /** {@inheritDoc} */
209     @Override
210     public void constructModel() throws SimRuntimeException
211     {
212         this.converter = new Convert();
213         System.out.println("Opening file " + this.filepath);
214         ArrayList<OSMTag> wantedTags = new ArrayList<>();
215         wantedTags.add(new OSMTag("highway", "primary"));
216         wantedTags.add(new OSMTag("highway", "secondary"));
217         wantedTags.add(new OSMTag("highway", "tertiary"));
218         wantedTags.add(new OSMTag("highway", "cycleway"));
219         wantedTags.add(new OSMTag("highway", "trunk"));
220         wantedTags.add(new OSMTag("highway", "path"));
221         wantedTags.add(new OSMTag("cycleway", "lane"));
222         wantedTags.add(new OSMTag("highway", "residential"));
223         wantedTags.add(new OSMTag("highway", "service"));
224         wantedTags.add(new OSMTag("highway", "motorway"));
225         wantedTags.add(new OSMTag("highway", "bus_stop"));
226         wantedTags.add(new OSMTag("highway", "motorway_link"));
227         wantedTags.add(new OSMTag("highway", "unclassified"));
228         wantedTags.add(new OSMTag("highway", "footway"));
229         wantedTags.add(new OSMTag("cycleway", "track"));
230         wantedTags.add(new OSMTag("highway", "road"));
231         wantedTags.add(new OSMTag("highway", "pedestrian"));
232         wantedTags.add(new OSMTag("highway", "track"));
233         wantedTags.add(new OSMTag("highway", "living_street"));
234         wantedTags.add(new OSMTag("highway", "tertiary_link"));
235         wantedTags.add(new OSMTag("highway", "secondary_link"));
236         wantedTags.add(new OSMTag("highway", "primary_link"));
237         wantedTags.add(new OSMTag("highway", "trunk_link"));
238         ArrayList<String> ft = new ArrayList<>();
239         try
240         {
241             System.out.println(this.filepath);
242             this.progressListener = new ProgressListenerImpl();
243             this.warningListener = new WarningListenerImpl();
244             ReadOSMFile osmf = new ReadOSMFile(this.filepath, wantedTags, ft, this.progressListener);
245             OSMNetwork net = osmf.getNetwork();
246             // net.removeRedundancy(); // Defective; do not call removeRedundancy
247             this.osmNetwork = net; // new OSMNetwork(net); // Why would you make a copy?
248             this.otsNetwork = new OTSRoadNetwork(this.osmNetwork.getName(), true, getSimulator());
249             for (OSMNode osmNode : this.osmNetwork.getNodes().values())
250             {
251                 try
252                 {
253                     this.converter.convertNode(this.otsNetwork, osmNode);
254                 }
255                 catch (NetworkException ne)
256                 {
257                     System.out.println(ne.getMessage());
258                 }
259             }
260             for (OSMLink osmLink : this.osmNetwork.getLinks())
261             {
262                 // TODO OTS-256
263                 Link link = this.converter.convertLink(this.otsNetwork, osmLink, null);
264                 this.otsNetwork.addLink(link);
265             }
266             this.osmNetwork.makeLinks(this.warningListener, this.progressListener);
267         }
268         catch (URISyntaxException | IOException | NetworkException | OTSGeometryException exception)
269         {
270             exception.printStackTrace();
271             return;
272         }
273 
274         Iterator<Node> count = this.otsNetwork.getNodeMap().values().iterator();
275         Rectangle2D area = null;
276         while (count.hasNext())
277         {
278             Node node = count.next();
279             if (null == area)
280             {
281                 area = new Rectangle2D.Double(node.getPoint().x, node.getPoint().y, 0, 0);
282             }
283             else
284             {
285                 area = area.createUnion(new Rectangle2D.Double(node.getPoint().x, node.getPoint().y, 0, 0));
286             }
287         }
288         this.rectangle = area;
289 
290         this.otsNetwork = new OTSRoadNetwork(this.osmNetwork.getName(), true, getSimulator());
291         for (OSMNode osmNode : this.osmNetwork.getNodes().values())
292         {
293             try
294             {
295                 this.converter.convertNode(this.otsNetwork, osmNode);
296             }
297             catch (Exception e)
298             {
299                 System.err.println(e.getMessage());
300             }
301         }
302         for (OSMLink osmLink : this.osmNetwork.getLinks())
303         {
304             try
305             {
306                 this.converter.convertLink(this.otsNetwork, osmLink, this.simulator);
307             }
308             catch (Exception e)
309             {
310                 System.err.println(e.getMessage());
311             }
312         }
313         Convert.findSinksandSources(this.osmNetwork, this.progressListener);
314         this.progressListener.progress(
315                 new ProgressEvent(this.osmNetwork, "Creation the lanes on " + this.osmNetwork.getLinks().size() + " links"));
316         double total = this.osmNetwork.getLinks().size();
317         double counter = 0;
318         double nextPercentage = 5.0;
319         for (OSMLink link : this.osmNetwork.getLinks())
320         {
321             try
322             {
323                 this.lanes.addAll(this.converter.makeLanes(this.otsNetwork, link, this.simulator, this.warningListener));
324             }
325             catch (Exception e)
326             {
327                 e.printStackTrace();
328                 System.err.println(e.getMessage());
329             }
330             counter++;
331             double currentPercentage = counter / total * 100;
332             if (currentPercentage >= nextPercentage)
333             {
334                 this.progressListener.progress(new ProgressEvent(this, nextPercentage + "% Progress"));
335                 nextPercentage += 5.0D;
336             }
337         }
338         System.out.println("Number of Links: " + this.otsNetwork.getLinkMap().size());
339         System.out.println("Number of Nodes: " + this.otsNetwork.getNodeMap().size());
340         System.out.println("Number of Lanes: " + this.lanes.size());
341     }
342 
343     /** {@inheritDoc} */
344     @Override
345     public OTSRoadNetwork getNetwork()
346     {
347         return this.otsNetwork;
348     }
349 
350     /**
351      * @return rectangle
352      */
353     public final Rectangle2D getRectangle()
354     {
355         return this.rectangle;
356     }
357 
358     /** {@inheritDoc} */
359     @Override
360     public Serializable getSourceId()
361     {
362         return "OSMModel";
363     }
364 }