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.OTSDEVSSimulatorInterface;
27 import org.opentrafficsim.core.dsol.OTSModelInterface;
28 import org.opentrafficsim.core.dsol.OTSSimTimeDouble;
29 import org.opentrafficsim.core.geometry.OTSGeometryException;
30 import org.opentrafficsim.core.gtu.animation.GTUColorer;
31 import org.opentrafficsim.core.network.Link;
32 import org.opentrafficsim.core.network.NetworkException;
33 import org.opentrafficsim.core.network.Node;
34 import org.opentrafficsim.core.network.OTSNetwork;
35 import org.opentrafficsim.road.animation.AnimationToggles;
36 import org.opentrafficsim.road.modelproperties.IDMPropertySet;
37 import org.opentrafficsim.road.network.factory.osm.OSMLink;
38 import org.opentrafficsim.road.network.factory.osm.OSMNetwork;
39 import org.opentrafficsim.road.network.factory.osm.OSMNode;
40 import org.opentrafficsim.road.network.factory.osm.OSMTag;
41 import org.opentrafficsim.road.network.factory.osm.events.ProgressEvent;
42 import org.opentrafficsim.road.network.factory.osm.events.ProgressListener;
43 import org.opentrafficsim.road.network.factory.osm.events.ProgressListenerImpl;
44 import org.opentrafficsim.road.network.factory.osm.events.WarningListener;
45 import org.opentrafficsim.road.network.factory.osm.events.WarningListenerImpl;
46 import org.opentrafficsim.road.network.factory.osm.input.ReadOSMFile;
47 import org.opentrafficsim.road.network.factory.osm.output.Convert;
48 import org.opentrafficsim.road.network.lane.Lane;
49 import org.opentrafficsim.simulationengine.AbstractWrappableAnimation;
50
51 import nl.tudelft.simulation.dsol.SimRuntimeException;
52 import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
53
54
55
56
57
58
59
60
61
62
63
64 public class OpenStreetMap extends AbstractWrappableAnimation implements UNITS
65 {
66
67 private static final long serialVersionUID = 1L;
68
69
70 private OSMModel model;
71
72
73 private OSMNetwork osmNetwork;
74
75
76 private OTSNetwork otsNetwork;
77
78
79 private ProgressListener progressListener;
80
81
82 private WarningListener warningListener;
83
84
85 Rectangle2D rectangle = null;
86
87
88 public OpenStreetMap()
89 {
90
91 }
92
93
94
95
96 public static void main(final String[] args)
97 {
98 SwingUtilities.invokeLater(new Runnable()
99 {
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),
130 localProperties, null, true);
131 }
132 catch (Exception e)
133 {
134 e.printStackTrace();
135 }
136 }
137 });
138
139 }
140
141
142 @Override
143 protected final OTSModelInterface makeModel(final GTUColorer colorer)
144 {
145 JFrame frame = new JFrame();
146 FileDialog fd = new FileDialog(frame, "Choose a file", FileDialog.LOAD);
147 fd.setFile("*.osm");
148 fd.setVisible(true);
149 File[] file = fd.getFiles();
150 if (file.length == 0)
151 {
152 return null;
153 }
154 String filename = fd.getFile();
155 String filepath = null;
156 try
157 {
158 filepath = file[0].toURI().toURL().toString();
159 }
160 catch (MalformedURLException e)
161 {
162 e.printStackTrace();
163 }
164 if (filename == null)
165 {
166 System.out.println("You cancelled the choice");
167 return null;
168 }
169 Convert converter = new Convert();
170 System.out.println("Opening file " + filename);
171 ArrayList<OSMTag> wantedTags = new ArrayList<OSMTag>();
172 wantedTags.add(new OSMTag("highway", "primary"));
173 wantedTags.add(new OSMTag("highway", "secondary"));
174 wantedTags.add(new OSMTag("highway", "tertiary"));
175 wantedTags.add(new OSMTag("highway", "cycleway"));
176 wantedTags.add(new OSMTag("highway", "trunk"));
177 wantedTags.add(new OSMTag("highway", "path"));
178 wantedTags.add(new OSMTag("cycleway", "lane"));
179 wantedTags.add(new OSMTag("highway", "residential"));
180 wantedTags.add(new OSMTag("highway", "service"));
181 wantedTags.add(new OSMTag("highway", "motorway"));
182 wantedTags.add(new OSMTag("highway", "bus_stop"));
183 wantedTags.add(new OSMTag("highway", "motorway_link"));
184 wantedTags.add(new OSMTag("highway", "unclassified"));
185 wantedTags.add(new OSMTag("highway", "footway"));
186 wantedTags.add(new OSMTag("cycleway", "track"));
187 wantedTags.add(new OSMTag("highway", "road"));
188 wantedTags.add(new OSMTag("highway", "pedestrian"));
189 wantedTags.add(new OSMTag("highway", "track"));
190 wantedTags.add(new OSMTag("highway", "living_street"));
191 wantedTags.add(new OSMTag("highway", "tertiary_link"));
192 wantedTags.add(new OSMTag("highway", "secondary_link"));
193 wantedTags.add(new OSMTag("highway", "primary_link"));
194 wantedTags.add(new OSMTag("highway", "trunk_link"));
195 ArrayList<String> ft = new ArrayList<String>();
196 try
197 {
198 System.out.println(filepath);
199 this.progressListener = new ProgressListenerImpl();
200 this.warningListener = new WarningListenerImpl();
201 ReadOSMFile osmf = new ReadOSMFile(filepath, wantedTags, ft, this.progressListener);
202 OSMNetwork net = osmf.getNetwork();
203
204 this.osmNetwork = net;
205 this.otsNetwork = new OTSNetwork(this.osmNetwork.getName());
206 for (OSMNode osmNode : this.osmNetwork.getNodes().values())
207 {
208 try
209 {
210 this.otsNetwork.addNode(converter.convertNode(this.otsNetwork, osmNode));
211 }
212 catch (NetworkException ne)
213 {
214 System.out.println(ne.getMessage());
215 }
216 }
217 for (OSMLink osmLink : this.osmNetwork.getLinks())
218 {
219
220 Link link = converter.convertLink(this.otsNetwork, osmLink, null);
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
251 @Override
252 public final String shortName()
253 {
254 return "Open Street Map Demonstration";
255 }
256
257
258 @Override
259 public final String description()
260 {
261 return "Load an OpenStreetMap file and show it";
262 }
263
264
265 @Override
266 protected final void addAnimationToggles()
267 {
268 AnimationToggles.setTextAnimationTogglesStandard(this);
269 }
270
271
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
282
283
284
285
286
287
288
289
290 class OSMModel implements OTSModelInterface
291 {
292
293 private static final long serialVersionUID = 20150227L;
294
295
296 private OTSDEVSSimulatorInterface simulator;
297
298
299 private OTSNetwork network = new OTSNetwork("network");
300
301
302 private OSMNetwork osmNetwork;
303
304
305 private List<Lane> lanes = new ArrayList<Lane>();
306
307
308 private ProgressListener progressListener;
309
310
311 private WarningListener warningListener;
312
313
314 private final Convert converter;
315
316
317
318
319
320
321
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
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, simulator));
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
387
388
389 }
390
391
392 @Override
393 public SimulatorInterface<Time, Duration, OTSSimTimeDouble> getSimulator() throws RemoteException
394 {
395 return this.simulator;
396 }
397
398
399 @Override
400 public OTSNetwork getNetwork()
401 {
402 return this.network;
403 }
404 }