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