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.Length;
27 import org.djunits.value.vdouble.scalar.Time;
28 import org.opentrafficsim.core.dsol.OTSDEVSSimulatorInterface;
29 import org.opentrafficsim.core.dsol.OTSModelInterface;
30 import org.opentrafficsim.core.dsol.OTSSimTimeDouble;
31 import org.opentrafficsim.core.geometry.OTSGeometryException;
32 import org.opentrafficsim.core.gtu.animation.GTUColorer;
33 import org.opentrafficsim.core.network.Link;
34 import org.opentrafficsim.core.network.NetworkException;
35 import org.opentrafficsim.core.network.Node;
36 import org.opentrafficsim.core.network.OTSNetwork;
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 import org.opentrafficsim.simulationengine.properties.AbstractProperty;
51 import org.opentrafficsim.simulationengine.properties.IDMPropertySet;
52 import org.opentrafficsim.simulationengine.properties.ProbabilityDistributionProperty;
53 import org.opentrafficsim.simulationengine.properties.PropertyException;
54 import org.opentrafficsim.simulationengine.properties.SelectionProperty;
55
56
57
58
59
60
61
62
63
64
65
66 public class OpenStreetMap extends AbstractWrappableAnimation implements UNITS
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 public void run()
100 {
101 try
102 {
103 OpenStreetMap osm = new OpenStreetMap();
104 ArrayList<AbstractProperty<?>> localProperties = osm.getProperties();
105 try
106 {
107 localProperties.add(new ProbabilityDistributionProperty("Traffic composition",
108 "<html>Mix of passenger cars and trucks</html>", new String[]{"passenger car", "truck"},
109 new Double[]{0.8, 0.2}, false, 10));
110 }
111 catch (PropertyException exception)
112 {
113 exception.printStackTrace();
114 }
115 localProperties.add(new SelectionProperty("Car following model",
116 "<html>The car following model determines "
117 + "the acceleration that a vehicle will make taking into account "
118 + "nearby vehicles, infrastructural restrictions (e.g. speed limit, "
119 + "curvature of the road) capabilities of the vehicle and personality "
120 + "of the driver.</html>", new String[]{"IDM", "IDM+"}, 1, false, 1));
121 localProperties.add(IDMPropertySet.makeIDMPropertySet("Car", new Acceleration(1.0,
122 METER_PER_SECOND_2), new Acceleration(1.5, METER_PER_SECOND_2), new Length.Rel(2.0, METER),
123 new Time.Rel(1.0, SECOND), 2));
124 localProperties.add(IDMPropertySet.makeIDMPropertySet("Truck", new Acceleration(0.5,
125 METER_PER_SECOND_2), new Acceleration(1.25, METER_PER_SECOND_2), new Length.Rel(2.0, METER),
126 new Time.Rel(1.0, SECOND), 3));
127 osm.buildAnimator(new Time.Abs(0.0, SECOND), new Time.Rel(0.0, SECOND),
128 new Time.Rel(3600.0, SECOND), localProperties, null, true);
129 }
130 catch (Exception e)
131 {
132 e.printStackTrace();
133 }
134 }
135 });
136
137 }
138
139
140 @Override
141 protected OTSModelInterface makeModel(GTUColorer colorer)
142 {
143 JFrame frame = new JFrame();
144 FileDialog fd = new FileDialog(frame, "Choose a file", FileDialog.LOAD);
145 fd.setFile("*.osm");
146 fd.setVisible(true);
147 File[] file = fd.getFiles();
148 if (file.length == 0)
149 {
150 return null;
151 }
152 String filename = fd.getFile();
153 String filepath = null;
154 try
155 {
156 filepath = file[0].toURI().toURL().toString();
157 }
158 catch (MalformedURLException e)
159 {
160 e.printStackTrace();
161 }
162 if (filename == null)
163 {
164 System.out.println("You cancelled the choice");
165 return null;
166 }
167 Convert converter = new Convert();
168 System.out.println("Opening file " + filename);
169 ArrayList<OSMTag> wantedTags = new ArrayList<OSMTag>();
170 wantedTags.add(new OSMTag("highway", "primary"));
171 wantedTags.add(new OSMTag("highway", "secondary"));
172 wantedTags.add(new OSMTag("highway", "tertiary"));
173 wantedTags.add(new OSMTag("highway", "cycleway"));
174 wantedTags.add(new OSMTag("highway", "trunk"));
175 wantedTags.add(new OSMTag("highway", "path"));
176 wantedTags.add(new OSMTag("cycleway", "lane"));
177 wantedTags.add(new OSMTag("highway", "residential"));
178 wantedTags.add(new OSMTag("highway", "service"));
179 wantedTags.add(new OSMTag("highway", "motorway"));
180 wantedTags.add(new OSMTag("highway", "bus_stop"));
181 wantedTags.add(new OSMTag("highway", "motorway_link"));
182 wantedTags.add(new OSMTag("highway", "unclassified"));
183 wantedTags.add(new OSMTag("highway", "footway"));
184 wantedTags.add(new OSMTag("cycleway", "track"));
185 wantedTags.add(new OSMTag("highway", "road"));
186 wantedTags.add(new OSMTag("highway", "pedestrian"));
187 wantedTags.add(new OSMTag("highway", "track"));
188 wantedTags.add(new OSMTag("highway", "living_street"));
189 wantedTags.add(new OSMTag("highway", "tertiary_link"));
190 wantedTags.add(new OSMTag("highway", "secondary_link"));
191 wantedTags.add(new OSMTag("highway", "primary_link"));
192 wantedTags.add(new OSMTag("highway", "trunk_link"));
193 ArrayList<String> ft = new ArrayList<String>();
194 try
195 {
196 System.out.println(filepath);
197 this.progressListener = new ProgressListenerImpl();
198 this.warningListener = new WarningListenerImpl();
199 ReadOSMFile osmf = new ReadOSMFile(filepath, wantedTags, ft, this.progressListener);
200 OSMNetwork net = osmf.getNetwork();
201
202 this.osmNetwork = net;
203 this.otsNetwork = new OTSNetwork(this.osmNetwork.getName());
204 for (OSMNode osmNode : this.osmNetwork.getNodes().values())
205 {
206 try
207 {
208 this.otsNetwork.addNode(converter.convertNode(osmNode));
209 }
210 catch (NetworkException ne)
211 {
212 System.out.println(ne.getMessage());
213 }
214 }
215 for (OSMLink osmLink : this.osmNetwork.getLinks())
216 {
217 Link link = converter.convertLink(osmLink);
218 this.otsNetwork.addLink(link);
219 }
220 this.osmNetwork.makeLinks(this.warningListener, this.progressListener);
221 }
222 catch (URISyntaxException | IOException | NetworkException | OTSGeometryException exception)
223 {
224 exception.printStackTrace();
225 return null;
226 }
227 this.model =
228 new OSMModel(getUserModifiedProperties(), this.osmNetwork, this.warningListener, this.progressListener,
229 converter);
230 Iterator<Node> count = this.otsNetwork.getNodeMap().values().iterator();
231 Rectangle2D area = null;
232 while (count.hasNext())
233 {
234 Node node = count.next();
235 if (null == area)
236 {
237 area = new Rectangle2D.Double(node.getPoint().x, node.getPoint().y, 0, 0);
238 }
239 else
240 {
241 area = area.createUnion(new Rectangle2D.Double(node.getPoint().x, node.getPoint().y, 0, 0));
242 }
243 }
244 this.rectangle = area;
245 return this.model;
246 }
247
248
249 @Override
250 public final String shortName()
251 {
252 return "Open Street Map Demonstration";
253 }
254
255
256 @Override
257 public final String description()
258 {
259 return "Load an OpenStreetMap file and show it";
260 }
261
262
263 @Override
264 protected JPanel makeCharts()
265 {
266 return null;
267 }
268
269
270 @Override
271 protected java.awt.geom.Rectangle2D.Double makeAnimationRectangle()
272 {
273 return new Rectangle2D.Double(this.rectangle.getX(), this.rectangle.getY(), this.rectangle.getWidth(),
274 this.rectangle.getHeight());
275 }
276 }
277
278
279
280
281
282
283
284
285
286
287
288 class OSMModel implements OTSModelInterface
289 {
290
291 private static final long serialVersionUID = 20150227L;
292
293
294 private OTSDEVSSimulatorInterface simulator;
295
296
297 private OTSNetwork network = new OTSNetwork("network");
298
299
300 private OSMNetwork osmNetwork;
301
302
303 private List<Lane> lanes = new ArrayList<Lane>();
304
305
306 private ProgressListener progressListener;
307
308
309 private WarningListener warningListener;
310
311
312 private final Convert converter;
313
314
315
316
317
318
319
320
321 public OSMModel(final ArrayList<AbstractProperty<?>> properties, final OSMNetwork osmNetwork,
322 final WarningListener wL, final ProgressListener pL, final Convert converter)
323 {
324 this.osmNetwork = osmNetwork;
325 this.warningListener = wL;
326 this.progressListener = pL;
327 this.converter = converter;
328 }
329
330
331 @Override
332 public void constructModel(final SimulatorInterface<Abs<TimeUnit>, Rel<TimeUnit>, OTSSimTimeDouble> theSimulator)
333 throws SimRuntimeException, RemoteException
334 {
335 OTSNetwork otsNetwork = new OTSNetwork(this.osmNetwork.getName());
336 for (OSMNode osmNode : this.osmNetwork.getNodes().values())
337 {
338 try
339 {
340 otsNetwork.addNode(this.converter.convertNode(osmNode));
341 }
342 catch (Exception e)
343 {
344 System.err.println(e.getMessage());
345 }
346 }
347 for (OSMLink osmLink : this.osmNetwork.getLinks())
348 {
349 try
350 {
351 otsNetwork.addLink(this.converter.convertLink(osmLink));
352 }
353 catch (Exception e)
354 {
355 System.err.println(e.getMessage());
356 }
357 }
358 Convert.findSinksandSources(this.osmNetwork, this.progressListener);
359 this.progressListener.progress(new ProgressEvent(this.osmNetwork, "Creation the lanes on "
360 + this.osmNetwork.getLinks().size() + " links"));
361 double total = this.osmNetwork.getLinks().size();
362 double counter = 0;
363 double nextPercentage = 5.0;
364 for (OSMLink link : this.osmNetwork.getLinks())
365 {
366 try
367 {
368 this.lanes.addAll(this.converter.makeLanes(link, (OTSDEVSSimulatorInterface) theSimulator,
369 this.warningListener));
370 }
371 catch (Exception e)
372 {
373 System.err.println(e.getMessage());
374 }
375 counter++;
376 double currentPercentage = counter / total * 100;
377 if (currentPercentage >= nextPercentage)
378 {
379 this.progressListener.progress(new ProgressEvent(this, nextPercentage + "% Progress"));
380 nextPercentage += 5.0D;
381 }
382 }
383
384
385
386
387 }
388
389
390 @Override
391 public SimulatorInterface<Abs<TimeUnit>, Rel<TimeUnit>, OTSSimTimeDouble> getSimulator() throws RemoteException
392 {
393 return this.simulator;
394 }
395
396 }