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
55
56
57
58
59
60
61
62
63 public class OpenStreetMap extends AbstractWrappableAnimation implements UNITS
64 {
65
66 private static final long serialVersionUID = 1L;
67
68
69 private OSMModel model;
70
71
72 private OSMNetwork osmNetwork;
73
74
75 private OTSNetwork otsNetwork;
76
77
78 private ProgressListener progressListener;
79
80
81 private WarningListener warningListener;
82
83
84 Rectangle2D rectangle = null;
85
86
87 public OpenStreetMap()
88 {
89
90 }
91
92
93
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
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
203 this.osmNetwork = net;
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
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
250 @Override
251 public final String shortName()
252 {
253 return "Open Street Map Demonstration";
254 }
255
256
257 @Override
258 public final String description()
259 {
260 return "Load an OpenStreetMap file and show it";
261 }
262
263
264 @Override
265 protected final void addAnimationToggles()
266 {
267 AnimationToggles.setTextAnimationTogglesStandard(this);
268 }
269
270
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
281
282
283
284
285
286
287
288
289 class OSMModel implements OTSModelInterface
290 {
291
292 private static final long serialVersionUID = 20150227L;
293
294
295 private DEVSSimulatorInterface.TimeDoubleUnit simulator;
296
297
298 private OTSNetwork network = new OTSNetwork("network");
299
300
301 private OSMNetwork osmNetwork;
302
303
304 private List<Lane> lanes = new ArrayList<>();
305
306
307 private ProgressListener progressListener;
308
309
310 private WarningListener warningListener;
311
312
313 private final Convert converter;
314
315
316
317
318
319
320
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
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
386
387
388 }
389
390
391 @Override
392 public SimulatorInterface<Time, Duration, SimTimeDoubleUnit> getSimulator()
393 {
394 return this.simulator;
395 }
396
397
398 @Override
399 public OTSNetwork getNetwork()
400 {
401 return this.network;
402 }
403 }