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