* Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
* BSD-style license. See OpenTrafficSim License.
*
* $LastChangedDate$, @version $Revision$, by $Author$,
* initial version Feb 10, 2015
* @author Peter Knoppers
* @author Moritz Bergmann
*/
public class OpenStreetMap extends AbstractWrappableAnimation implements UNITS
{
/** */
private static final long serialVersionUID = 1L;
/** The model. */
private OSMModel model;
/** The OSMNetwork. */
private OSMNetwork osmNetwork;
/** The OTS network. */
private OTSNetwork otsNetwork;
/** The ProgressListener. */
private ProgressListener progressListener;
/** The WarningListener. */
private WarningListener warningListener;
/** Bounding rectangle of the loaded map. */
Rectangle2D rectangle = null;
/** Construct the OpenStreetMap demo. */
public OpenStreetMap()
{
// The work is done in buildSimulator which, in turn, calls makeModel.
}
/**
* @param args String[]; the command line arguments (not used)
*/
public static void main(final String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
try
{
OpenStreetMap osm = new OpenStreetMap();
List> localProperties = osm.getProperties();
try
{
localProperties.add(new ProbabilityDistributionProperty("TrafficComposition", "Traffic composition",
"Mix of passenger cars and trucks", new String[] { "passenger car", "truck" },
new Double[] { 0.8, 0.2 }, false, 10));
}
catch (PropertyException exception)
{
exception.printStackTrace();
}
localProperties.add(new SelectionProperty("CarFollowingModel", "Car following model",
"The car following model determines "
+ "the acceleration that a vehicle will make taking into account "
+ "nearby vehicles, infrastructural restrictions (e.g. speed limit, "
+ "curvature of the road) capabilities of the vehicle and personality "
+ "of the driver.",
new String[] { "IDM", "IDM+" }, 1, false, 1));
localProperties.add(IDMPropertySet.makeIDMPropertySet("IDMCar", "Car",
new Acceleration(1.0, METER_PER_SECOND_2), new Acceleration(1.5, METER_PER_SECOND_2),
new Length(2.0, METER), new Duration(1.0, SECOND), 2));
localProperties.add(IDMPropertySet.makeIDMPropertySet("IDMTruck", "Truck",
new Acceleration(0.5, METER_PER_SECOND_2), new Acceleration(1.25, METER_PER_SECOND_2),
new Length(2.0, METER), new Duration(1.0, SECOND), 3));
osm.buildAnimator(new Time(0.0, SECOND), new Duration(0.0, SECOND), new Duration(3600.0, SECOND),
localProperties, null, true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
/** {@inheritDoc} */
@Override
protected final OTSModelInterface makeModel(final GTUColorer colorer)
{
JFrame frame = new JFrame();
FileDialog fd = new FileDialog(frame, "Choose a file", FileDialog.LOAD);
fd.setFile("*.osm");
fd.setVisible(true);
File[] file = fd.getFiles();
if (file.length == 0)
{
return null;
}
String filename = fd.getFile();
String filepath = null;
try
{
filepath = file[0].toURI().toURL().toString();
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
if (filename == null)
{
System.out.println("You cancelled the choice");
return null;
}
Convert converter = new Convert();
System.out.println("Opening file " + filename);
ArrayList wantedTags = new ArrayList();
wantedTags.add(new OSMTag("highway", "primary"));
wantedTags.add(new OSMTag("highway", "secondary"));
wantedTags.add(new OSMTag("highway", "tertiary"));
wantedTags.add(new OSMTag("highway", "cycleway"));
wantedTags.add(new OSMTag("highway", "trunk"));
wantedTags.add(new OSMTag("highway", "path"));
wantedTags.add(new OSMTag("cycleway", "lane"));
wantedTags.add(new OSMTag("highway", "residential"));
wantedTags.add(new OSMTag("highway", "service"));
wantedTags.add(new OSMTag("highway", "motorway"));
wantedTags.add(new OSMTag("highway", "bus_stop"));
wantedTags.add(new OSMTag("highway", "motorway_link"));
wantedTags.add(new OSMTag("highway", "unclassified"));
wantedTags.add(new OSMTag("highway", "footway"));
wantedTags.add(new OSMTag("cycleway", "track"));
wantedTags.add(new OSMTag("highway", "road"));
wantedTags.add(new OSMTag("highway", "pedestrian"));
wantedTags.add(new OSMTag("highway", "track"));
wantedTags.add(new OSMTag("highway", "living_street"));
wantedTags.add(new OSMTag("highway", "tertiary_link"));
wantedTags.add(new OSMTag("highway", "secondary_link"));
wantedTags.add(new OSMTag("highway", "primary_link"));
wantedTags.add(new OSMTag("highway", "trunk_link"));
ArrayList ft = new ArrayList();
try
{
System.out.println(filepath);
this.progressListener = new ProgressListenerImpl();
this.warningListener = new WarningListenerImpl();
ReadOSMFile osmf = new ReadOSMFile(filepath, wantedTags, ft, this.progressListener);
OSMNetwork net = osmf.getNetwork();
// net.removeRedundancy(); // Defective; do not call removeRedundancy
this.osmNetwork = net; // new OSMNetwork(net); // Why would you make a copy?
this.otsNetwork = new OTSNetwork(this.osmNetwork.getName());
for (OSMNode osmNode : this.osmNetwork.getNodes().values())
{
try
{
this.otsNetwork.addNode(converter.convertNode(this.otsNetwork, osmNode));
}
catch (NetworkException ne)
{
System.out.println(ne.getMessage());
}
}
for (OSMLink osmLink : this.osmNetwork.getLinks())
{
// TODO OTS-256
Link link = converter.convertLink(this.otsNetwork, osmLink, null);
this.otsNetwork.addLink(link);
}
this.osmNetwork.makeLinks(this.warningListener, this.progressListener);
}
catch (URISyntaxException | IOException | NetworkException | OTSGeometryException exception)
{
exception.printStackTrace();
return null;
}
this.model = new OSMModel(getUserModifiedProperties(), this.osmNetwork, this.warningListener, this.progressListener,
converter);
Iterator count = this.otsNetwork.getNodeMap().values().iterator();
Rectangle2D area = null;
while (count.hasNext())
{
Node node = count.next();
if (null == area)
{
area = new Rectangle2D.Double(node.getPoint().x, node.getPoint().y, 0, 0);
}
else
{
area = area.createUnion(new Rectangle2D.Double(node.getPoint().x, node.getPoint().y, 0, 0));
}
}
this.rectangle = area;
return this.model;
}
/** {@inheritDoc} */
@Override
public final String shortName()
{
return "Open Street Map Demonstration";
}
/** {@inheritDoc} */
@Override
public final String description()
{
return "Load an OpenStreetMap file and show it";
}
/** {@inheritDoc} */
@Override
protected final void addAnimationToggles()
{
AnimationToggles.setTextAnimationTogglesStandard(this);
}
/** {@inheritDoc} */
@Override
protected final java.awt.geom.Rectangle2D.Double makeAnimationRectangle()
{
return new Rectangle2D.Double(this.rectangle.getX(), this.rectangle.getY(), this.rectangle.getWidth(),
this.rectangle.getHeight());
}
}
/**
*
* Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
* BSD-style license. See OpenTrafficSim License.
*
* $LastChangedDate$, @version $Revision$, by $Author$,
* initial version Feb 10, 2015
* @author Peter Knoppers
* @author Moritz Bergmann
*/
class OSMModel implements OTSModelInterface
{
/** */
private static final long serialVersionUID = 20150227L;
/** The simulator. */
private OTSDEVSSimulatorInterface simulator;
/** The network. */
private OTSNetwork network = new OTSNetwork("network");
/** Provided Network. */
private OSMNetwork osmNetwork;
/** Provided lanes. */
private List lanes = new ArrayList();
/** */
private ProgressListener progressListener;
/** */
private WarningListener warningListener;
/** The coordinate converter. */
private final Convert converter;
/**
* @param properties ArrayList<AbstractProperty<?>>; the properties (not used)
* @param osmNetwork OSMNetwork; the OSM network structure
* @param wL WarningListener; the receiver of warning events
* @param pL ProgressListener; the receiver of progress events
* @param converter Convert; the output converter
*/
OSMModel(final List> properties, final OSMNetwork osmNetwork, final WarningListener wL,
final ProgressListener pL, final Convert converter)
{
this.osmNetwork = osmNetwork;
this.warningListener = wL;
this.progressListener = pL;
this.converter = converter;
}
/** {@inheritDoc} */
@Override
public void constructModel(final SimulatorInterface