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