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