View Javadoc
1   package org.opentrafficsim.demo.carFollowing;
2   
3   import java.awt.FileDialog;
4   import java.awt.geom.Rectangle2D;
5   import java.io.File;
6   import java.io.IOException;
7   import java.net.MalformedURLException;
8   import java.net.URISyntaxException;
9   import java.util.ArrayList;
10  import java.util.Iterator;
11  import java.util.List;
12  
13  import javax.swing.JFrame;
14  import javax.swing.SwingUtilities;
15  
16  import nl.tudelft.simulation.dsol.SimRuntimeException;
17  import nl.tudelft.simulation.dsol.simtime.SimTimeDoubleUnit;
18  import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
19  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
20  
21  import org.djunits.unit.UNITS;
22  import org.djunits.value.vdouble.scalar.Acceleration;
23  import org.djunits.value.vdouble.scalar.Duration;
24  import org.djunits.value.vdouble.scalar.Length;
25  import org.djunits.value.vdouble.scalar.Time;
26  import org.opentrafficsim.base.modelproperties.ProbabilityDistributionProperty;
27  import org.opentrafficsim.base.modelproperties.Property;
28  import org.opentrafficsim.base.modelproperties.PropertyException;
29  import org.opentrafficsim.base.modelproperties.SelectionProperty;
30  import org.opentrafficsim.core.dsol.OTSModelInterface;
31  import org.opentrafficsim.core.geometry.OTSGeometryException;
32  import org.opentrafficsim.core.network.Link;
33  import org.opentrafficsim.core.network.NetworkException;
34  import org.opentrafficsim.core.network.Node;
35  import org.opentrafficsim.core.network.OTSNetwork;
36  import org.opentrafficsim.road.animation.AnimationToggles;
37  import org.opentrafficsim.road.modelproperties.IDMPropertySet;
38  import org.opentrafficsim.road.network.factory.osm.OSMLink;
39  import org.opentrafficsim.road.network.factory.osm.OSMNetwork;
40  import org.opentrafficsim.road.network.factory.osm.OSMNode;
41  import org.opentrafficsim.road.network.factory.osm.OSMTag;
42  import org.opentrafficsim.road.network.factory.osm.events.ProgressEvent;
43  import org.opentrafficsim.road.network.factory.osm.events.ProgressListener;
44  import org.opentrafficsim.road.network.factory.osm.events.ProgressListenerImpl;
45  import org.opentrafficsim.road.network.factory.osm.events.WarningListener;
46  import org.opentrafficsim.road.network.factory.osm.events.WarningListenerImpl;
47  import org.opentrafficsim.road.network.factory.osm.input.ReadOSMFile;
48  import org.opentrafficsim.road.network.factory.osm.output.Convert;
49  import org.opentrafficsim.road.network.lane.Lane;
50  import org.opentrafficsim.simulationengine.AbstractWrappableAnimation;
51  
52  /**
53   * <p>
54   * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
55   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
56   * <p>
57   * $LastChangedDate: 2018-09-20 09:23:47 +0200 (Thu, 20 Sep 2018) $, @version $Revision: 4173 $, by $Author: pknoppers $,
58   * initial version Feb 10, 2015 <br>
59   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
60   * @author Moritz Bergmann
61   */
62  public class OpenStreetMap extends AbstractWrappableAnimation implements UNITS
63  {
64      /** */
65      private static final long serialVersionUID = 1L;
66  
67      /** The model. */
68      private OSMModel model;
69  
70      /** The OSMNetwork. */
71      private OSMNetwork osmNetwork;
72  
73      /** The OTS network. */
74      private OTSNetwork otsNetwork;
75  
76      /** The ProgressListener. */
77      private ProgressListener progressListener;
78  
79      /** The WarningListener. */
80      private WarningListener warningListener;
81  
82      /** Bounding rectangle of the loaded map. */
83      Rectangle2D rectangle = null;
84  
85      /** Construct the OpenStreetMap demo. */
86      public OpenStreetMap()
87      {
88          // The work is done in buildSimulator which, in turn, calls makeModel.
89      }
90  
91      /**
92       * @param args String[]; the command line arguments (not used)
93       */
94      public static void main(final String[] args)
95      {
96          SwingUtilities.invokeLater(new Runnable()
97          {
98              @Override
99              public void run()
100             {
101                 try
102                 {
103                     OpenStreetMap osm = new OpenStreetMap();
104                     List<Property<?>> localProperties = osm.getProperties();
105                     try
106                     {
107                         localProperties.add(new ProbabilityDistributionProperty("TrafficComposition", "Traffic composition",
108                                 "<html>Mix of passenger cars and trucks</html>", new String[] { "passenger car", "truck" },
109                                 new Double[] { 0.8, 0.2 }, false, 10));
110                     }
111                     catch (PropertyException exception)
112                     {
113                         exception.printStackTrace();
114                     }
115                     localProperties.add(new SelectionProperty("CarFollowingModel", "Car following model",
116                             "<html>The car following model determines "
117                                     + "the acceleration that a vehicle will make taking into account "
118                                     + "nearby vehicles, infrastructural restrictions (e.g. speed limit, "
119                                     + "curvature of the road) capabilities of the vehicle and personality "
120                                     + "of the driver.</html>", new String[] { "IDM", "IDM+" }, 1, false, 1));
121                     localProperties.add(IDMPropertySet.makeIDMPropertySet("IDMCar", "Car", new Acceleration(1.0,
122                             METER_PER_SECOND_2), new Acceleration(1.5, METER_PER_SECOND_2), new Length(2.0, METER),
123                             new Duration(1.0, SECOND), 2));
124                     localProperties.add(IDMPropertySet.makeIDMPropertySet("IDMTruck", "Truck", new Acceleration(0.5,
125                             METER_PER_SECOND_2), new Acceleration(1.25, METER_PER_SECOND_2), new Length(2.0, METER),
126                             new Duration(1.0, SECOND), 3));
127                     osm.buildAnimator(Time.ZERO, Duration.ZERO, new Duration(3600.0, SECOND), localProperties, null, true);
128                 }
129                 catch (Exception e)
130                 {
131                     e.printStackTrace();
132                 }
133             }
134         });
135 
136     }
137 
138     /** {@inheritDoc} */
139     @Override
140     protected final OTSModelInterface makeModel()
141     {
142         JFrame frame = new JFrame();
143         FileDialog fd = new FileDialog(frame, "Choose a file", FileDialog.LOAD);
144         fd.setFile("*.osm");
145         fd.setVisible(true);
146         File[] file = fd.getFiles();
147         if (file.length == 0)
148         {
149             return null;
150         }
151         String filename = fd.getFile();
152         String filepath = null;
153         try
154         {
155             filepath = file[0].toURI().toURL().toString();
156         }
157         catch (MalformedURLException e)
158         {
159             e.printStackTrace();
160         }
161         if (filename == null)
162         {
163             System.out.println("You cancelled the choice");
164             return null;
165         }
166         Convert converter = new Convert();
167         System.out.println("Opening file " + filename);
168         ArrayList<OSMTag> wantedTags = new ArrayList<>();
169         wantedTags.add(new OSMTag("highway", "primary"));
170         wantedTags.add(new OSMTag("highway", "secondary"));
171         wantedTags.add(new OSMTag("highway", "tertiary"));
172         wantedTags.add(new OSMTag("highway", "cycleway"));
173         wantedTags.add(new OSMTag("highway", "trunk"));
174         wantedTags.add(new OSMTag("highway", "path"));
175         wantedTags.add(new OSMTag("cycleway", "lane"));
176         wantedTags.add(new OSMTag("highway", "residential"));
177         wantedTags.add(new OSMTag("highway", "service"));
178         wantedTags.add(new OSMTag("highway", "motorway"));
179         wantedTags.add(new OSMTag("highway", "bus_stop"));
180         wantedTags.add(new OSMTag("highway", "motorway_link"));
181         wantedTags.add(new OSMTag("highway", "unclassified"));
182         wantedTags.add(new OSMTag("highway", "footway"));
183         wantedTags.add(new OSMTag("cycleway", "track"));
184         wantedTags.add(new OSMTag("highway", "road"));
185         wantedTags.add(new OSMTag("highway", "pedestrian"));
186         wantedTags.add(new OSMTag("highway", "track"));
187         wantedTags.add(new OSMTag("highway", "living_street"));
188         wantedTags.add(new OSMTag("highway", "tertiary_link"));
189         wantedTags.add(new OSMTag("highway", "secondary_link"));
190         wantedTags.add(new OSMTag("highway", "primary_link"));
191         wantedTags.add(new OSMTag("highway", "trunk_link"));
192         ArrayList<String> ft = new ArrayList<>();
193         try
194         {
195             System.out.println(filepath);
196             this.progressListener = new ProgressListenerImpl();
197             this.warningListener = new WarningListenerImpl();
198             ReadOSMFile osmf = new ReadOSMFile(filepath, wantedTags, ft, this.progressListener);
199             OSMNetwork net = osmf.getNetwork();
200             // net.removeRedundancy(); // Defective; do not call removeRedundancy
201             this.osmNetwork = net; // new OSMNetwork(net); // Why would you make a copy?
202             this.otsNetwork = new OTSNetwork(this.osmNetwork.getName());
203             for (OSMNode osmNode : this.osmNetwork.getNodes().values())
204             {
205                 try
206                 {
207                     converter.convertNode(this.otsNetwork, osmNode);
208                 }
209                 catch (NetworkException ne)
210                 {
211                     System.out.println(ne.getMessage());
212                 }
213             }
214             for (OSMLink osmLink : this.osmNetwork.getLinks())
215             {
216                 // TODO OTS-256
217                 Link link = converter.convertLink(this.otsNetwork, osmLink, null);
218                 this.otsNetwork.addLink(link);
219             }
220             this.osmNetwork.makeLinks(this.warningListener, this.progressListener);
221         }
222         catch (URISyntaxException | IOException | NetworkException | OTSGeometryException exception)
223         {
224             exception.printStackTrace();
225             return null;
226         }
227         this.model =
228                 new OSMModel(getUserModifiedProperties(), this.osmNetwork, this.warningListener, this.progressListener,
229                         converter);
230         Iterator<Node> count = this.otsNetwork.getNodeMap().values().iterator();
231         Rectangle2D area = null;
232         while (count.hasNext())
233         {
234             Node node = count.next();
235             if (null == area)
236             {
237                 area = new Rectangle2D.Double(node.getPoint().x, node.getPoint().y, 0, 0);
238             }
239             else
240             {
241                 area = area.createUnion(new Rectangle2D.Double(node.getPoint().x, node.getPoint().y, 0, 0));
242             }
243         }
244         this.rectangle = area;
245         return this.model;
246     }
247 
248     /** {@inheritDoc} */
249     @Override
250     public final String shortName()
251     {
252         return "Open Street Map Demonstration";
253     }
254 
255     /** {@inheritDoc} */
256     @Override
257     public final String description()
258     {
259         return "Load an OpenStreetMap file and show it";
260     }
261 
262     /** {@inheritDoc} */
263     @Override
264     protected final void addAnimationToggles()
265     {
266         AnimationToggles.setTextAnimationTogglesStandard(this);
267     }
268 
269     /** {@inheritDoc} */
270     @Override
271     protected final java.awt.geom.Rectangle2D.Double makeAnimationRectangle()
272     {
273         return new Rectangle2D.Double(this.rectangle.getX(), this.rectangle.getY(), this.rectangle.getWidth(),
274                 this.rectangle.getHeight());
275     }
276 }
277 
278 /**
279  * <p>
280  * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
281  * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
282  * <p>
283  * $LastChangedDate: 2018-09-20 09:23:47 +0200 (Thu, 20 Sep 2018) $, @version $Revision: 4173 $, by $Author: pknoppers $,
284  * initial version Feb 10, 2015 <br>
285  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
286  * @author Moritz Bergmann
287  */
288 class OSMModel implements OTSModelInterface
289 {
290     /** */
291     private static final long serialVersionUID = 20150227L;
292 
293     /** The simulator. */
294     private DEVSSimulatorInterface.TimeDoubleUnit simulator;
295 
296     /** The network. */
297     private OTSNetwork network = new OTSNetwork("network");
298 
299     /** Provided Network. */
300     private OSMNetwork osmNetwork;
301 
302     /** Provided lanes. */
303     private List<Lane> lanes = new ArrayList<>();
304 
305     /** */
306     private ProgressListener progressListener;
307 
308     /** */
309     private WarningListener warningListener;
310 
311     /** The coordinate converter. */
312     private final Convert converter;
313 
314     /**
315      * @param properties ArrayList&lt;AbstractProperty&lt;?&gt;&gt;; the properties (not used)
316      * @param osmNetwork OSMNetwork; the OSM network structure
317      * @param wL WarningListener; the receiver of warning events
318      * @param pL ProgressListener; the receiver of progress events
319      * @param converter Convert; the output converter
320      */
321     OSMModel(final List<Property<?>> properties, final OSMNetwork osmNetwork, final WarningListener wL,
322             final ProgressListener pL, final Convert converter)
323     {
324         this.osmNetwork = osmNetwork;
325         this.warningListener = wL;
326         this.progressListener = pL;
327         this.converter = converter;
328     }
329 
330     /** {@inheritDoc} */
331     @Override
332     public void constructModel(final SimulatorInterface<Time, Duration, SimTimeDoubleUnit> theSimulator)
333             throws SimRuntimeException
334     {
335         this.simulator = (DEVSSimulatorInterface.TimeDoubleUnit) theSimulator;
336         OTSNetwork otsNetwork = new OTSNetwork(this.osmNetwork.getName());
337         for (OSMNode osmNode : this.osmNetwork.getNodes().values())
338         {
339             try
340             {
341                 this.converter.convertNode(otsNetwork, osmNode);
342             }
343             catch (Exception e)
344             {
345                 System.err.println(e.getMessage());
346             }
347         }
348         for (OSMLink osmLink : this.osmNetwork.getLinks())
349         {
350             try
351             {
352                 this.converter.convertLink(otsNetwork, osmLink, this.simulator);
353             }
354             catch (Exception e)
355             {
356                 System.err.println(e.getMessage());
357             }
358         }
359         Convert.findSinksandSources(this.osmNetwork, this.progressListener);
360         this.progressListener.progress(new ProgressEvent(this.osmNetwork, "Creation the lanes on "
361                 + this.osmNetwork.getLinks().size() + " links"));
362         double total = this.osmNetwork.getLinks().size();
363         double counter = 0;
364         double nextPercentage = 5.0;
365         for (OSMLink link : this.osmNetwork.getLinks())
366         {
367             try
368             {
369                 this.lanes.addAll(this.converter.makeLanes(otsNetwork, link, (DEVSSimulatorInterface.TimeDoubleUnit) theSimulator,
370                         this.warningListener));
371             }
372             catch (Exception e)
373             {
374                 e.printStackTrace();
375                 System.err.println(e.getMessage());
376             }
377             counter++;
378             double currentPercentage = counter / total * 100;
379             if (currentPercentage >= nextPercentage)
380             {
381                 this.progressListener.progress(new ProgressEvent(this, nextPercentage + "% Progress"));
382                 nextPercentage += 5.0D;
383             }
384         }
385         System.out.println("Number of Links: " + this.network.getLinkMap().size());
386         System.out.println("Number of Nodes: " + this.network.getNodeMap().size());
387         System.out.println("Number of Lanes: " + this.lanes.size());
388     }
389 
390     /** {@inheritDoc} */
391     @Override
392     public SimulatorInterface<Time, Duration, SimTimeDoubleUnit> getSimulator()
393     {
394         return this.simulator;
395     }
396 
397     /** {@inheritDoc} */
398     @Override
399     public OTSNetwork getNetwork()
400     {
401         return this.network;
402     }
403 }