SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NWWriter_XML.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // Exporter writing networks using XML (native input) format
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
12 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 #include <algorithm>
34 #include <netbuild/NBEdge.h>
35 #include <netbuild/NBEdgeCont.h>
36 #include <netbuild/NBNode.h>
37 #include <netbuild/NBNodeCont.h>
38 #include <netbuild/NBNetBuilder.h>
39 #include <utils/common/ToString.h>
44 #include "NWFrame.h"
45 #include "NWWriter_SUMO.h"
46 #include "NWWriter_XML.h"
47 
48 #ifdef CHECK_MEMORY_LEAKS
49 #include <foreign/nvwa/debug_new.h>
50 #endif // CHECK_MEMORY_LEAKS
51 
52 
53 
54 // ===========================================================================
55 // method definitions
56 // ===========================================================================
57 // ---------------------------------------------------------------------------
58 // static methods
59 // ---------------------------------------------------------------------------
60 void
62  // check whether plain-output files shall be generated
63  if (oc.isSet("plain-output-prefix")) {
64  writeNodes(oc, nb.getNodeCont());
67  }
68  if (oc.isSet("junctions.join-output")) {
70  }
71  if (oc.isSet("street-sign-output")) {
72  writeStreetSigns(oc, nb.getEdgeCont());
73  }
74 }
75 
76 
77 void
80  bool useGeo = oc.exists("proj.plain-geo") && oc.getBool("proj.plain-geo");
81  if (useGeo && !gch.usingGeoProjection()) {
82  WRITE_WARNING("Ignoring option \"proj.plain-geo\" because no geo-conversion has been defined");
83  useGeo = false;
84  }
85  const bool geoAccuracy = useGeo || gch.usingInverseGeoProjection();
86 
87  OutputDevice& device = OutputDevice::getDevice(oc.getString("plain-output-prefix") + ".nod.xml");
88  device.writeXMLHeader("nodes", SUMOSAXAttributes::ENCODING, NWFrame::MAJOR_VERSION + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://sumo.sf.net/xsd/nodes_file.xsd\"");
89 
90  // write network offsets and projection to allow reconstruction of original coordinates
91  if (!useGeo) {
93  }
94 
95  // write nodes
96  for (std::map<std::string, NBNode*>::const_iterator i = nc.begin(); i != nc.end(); ++i) {
97  NBNode* n = (*i).second;
98  device.openTag(SUMO_TAG_NODE);
99  device.writeAttr(SUMO_ATTR_ID, n->getID());
100  // write position
101  Position pos = n->getPosition();
102  if (useGeo) {
103  gch.cartesian2geo(pos);
104  }
105  if (geoAccuracy) {
107  }
108  NWFrame::writePositionLong(pos, device);
109  if (geoAccuracy) {
110  device.setPrecision();
111  }
112 
113  device.writeAttr(SUMO_ATTR_TYPE, toString(n->getType()));
114  if (n->isTLControlled()) {
115  const std::set<NBTrafficLightDefinition*>& tlss = n->getControllingTLS();
116  // set may contain multiple programs for the same id.
117  // make sure ids are unique and sorted
118  std::set<std::string> tlsIDs;
119  for (std::set<NBTrafficLightDefinition*>::const_iterator it_tl = tlss.begin(); it_tl != tlss.end(); it_tl++) {
120  tlsIDs.insert((*it_tl)->getID());
121  }
122  std::vector<std::string> sortedIDs(tlsIDs.begin(), tlsIDs.end());
123  sort(sortedIDs.begin(), sortedIDs.end());
124  device.writeAttr(SUMO_ATTR_TLID, sortedIDs);
125  }
126  device.closeTag(true);
127  }
128  device.close();
129 }
130 
131 
132 void
134  const GeoConvHelper& gch = GeoConvHelper::getFinal();
135  bool useGeo = oc.exists("proj.plain-geo") && oc.getBool("proj.plain-geo");
136  const bool geoAccuracy = useGeo || gch.usingInverseGeoProjection();
137 
138  OutputDevice& edevice = OutputDevice::getDevice(oc.getString("plain-output-prefix") + ".edg.xml");
139  edevice.writeXMLHeader("edges", SUMOSAXAttributes::ENCODING, NWFrame::MAJOR_VERSION + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://sumo.sf.net/xsd/edges_file.xsd\"");
140  OutputDevice& cdevice = OutputDevice::getDevice(oc.getString("plain-output-prefix") + ".con.xml");
141  cdevice.writeXMLHeader("connections", SUMOSAXAttributes::ENCODING, NWFrame::MAJOR_VERSION + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://sumo.sf.net/xsd/connections_file.xsd\"");
142  bool noNames = !oc.getBool("output.street-names");
143  for (std::map<std::string, NBEdge*>::const_iterator i = ec.begin(); i != ec.end(); ++i) {
144  // write the edge itself to the edges-files
145  NBEdge* e = (*i).second;
146  edevice.openTag(SUMO_TAG_EDGE);
147  edevice.writeAttr(SUMO_ATTR_ID, e->getID());
148  edevice.writeAttr(SUMO_ATTR_FROM, e->getFromNode()->getID());
149  edevice.writeAttr(SUMO_ATTR_TO, e->getToNode()->getID());
150  if (!noNames && e->getStreetName() != "") {
152  }
154  // write the type if given
155  if (e->getTypeID() != "") {
156  edevice.writeAttr(SUMO_ATTR_TYPE, e->getTypeID());
157  }
159  if (!e->hasLaneSpecificSpeed()) {
160  edevice.writeAttr(SUMO_ATTR_SPEED, e->getSpeed());
161  }
162  // write non-default geometry
163  if (!e->hasDefaultGeometry()) {
164  PositionVector geom = e->getGeometry();
165  if (useGeo) {
166  for (int i = 0; i < (int) geom.size(); i++) {
167  gch.cartesian2geo(geom[i]);
168  }
169  }
170  if (geoAccuracy) {
172  }
173  edevice.writeAttr(SUMO_ATTR_SHAPE, geom);
174  if (geoAccuracy) {
175  edevice.setPrecision();
176  }
177  }
178  // write the spread type if not default ("right")
181  }
182  // write the length if it was specified
183  if (e->hasLoadedLength()) {
185  }
186  // some attributes can be set by edge default or per lane. Write as default if possible (efficiency)
188  edevice.writeAttr(SUMO_ATTR_WIDTH, e->getWidth());
189  }
191  edevice.writeAttr(SUMO_ATTR_OFFSET, e->getOffset());
192  }
193  if (!e->needsLaneSpecificOutput()) {
194  edevice.closeTag(true);
195  } else {
196  edevice << ">\n";
197  for (unsigned int i = 0; i < e->getLanes().size(); ++i) {
198  const NBEdge::Lane& lane = e->getLanes()[i];
199  edevice.openTag(SUMO_TAG_LANE);
200  edevice.writeAttr(SUMO_ATTR_INDEX, i);
201  // write allowed lanes
204  // write other attributes
206  edevice.writeAttr(SUMO_ATTR_WIDTH, lane.width);
207  }
209  edevice.writeAttr(SUMO_ATTR_OFFSET, lane.offset);
210  }
211  if (e->hasLaneSpecificSpeed()) {
212  edevice.writeAttr(SUMO_ATTR_SPEED, lane.speed);
213  }
214  edevice.closeTag(true);
215  }
216  edevice.closeTag();
217  }
218  // write this edge's connections to the connections-files
220  const std::vector<NBEdge::Connection> connections = e->getConnections();
221  for (std::vector<NBEdge::Connection>::const_iterator c = connections.begin(); c != connections.end(); ++c) {
222  NWWriter_SUMO::writeConnection(cdevice, *e, *c, false, NWWriter_SUMO::PLAIN);
223  }
224  if (connections.size() > 0) {
225  cdevice << "\n";
226  }
227  }
228 
229  // write loaded prohibitions to the connections-file
230  for (std::map<std::string, NBNode*>::const_iterator i = nc.begin(); i != nc.end(); ++i) {
231  NWWriter_SUMO::writeProhibitions(cdevice, i->second->getProhibitions());
232  }
233  edevice.close();
234  cdevice.close();
235 }
236 
237 
238 void
240  OutputDevice& device = OutputDevice::getDevice(oc.getString("plain-output-prefix") + ".tll.xml");
241  device.writeXMLHeader("tlLogics", SUMOSAXAttributes::ENCODING, NWFrame::MAJOR_VERSION + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://sumo.sf.net/xsd/tllogic_file.xsd\"");
243  // we also need to remember the associations between tlLogics and connections
244  // since the information in con.xml is insufficient
245  for (std::map<std::string, NBEdge*>::const_iterator i = ec.begin(); i != ec.end(); ++i) {
246  NBEdge* e = (*i).second;
247  // write this edge's tl-controlled connections
248  const std::vector<NBEdge::Connection> connections = e->getConnections();
249  for (std::vector<NBEdge::Connection>::const_iterator c = connections.begin(); c != connections.end(); ++c) {
250  if (c->tlID != "") {
251  NWWriter_SUMO::writeConnection(device, *e, *c, false, NWWriter_SUMO::TLL);
252  }
253  }
254  }
255  device.close();
256 }
257 
258 
259 void
261  OutputDevice& device = OutputDevice::getDevice(oc.getString("junctions.join-output"));
262  device.writeXMLHeader("nodes", SUMOSAXAttributes::ENCODING, NWFrame::MAJOR_VERSION + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://sumo.sf.net/xsd/nodes_file.xsd\"");
263  const std::vector<std::set<std::string> >& clusters = nc.getJoinedClusters();
264  for (std::vector<std::set<std::string> >::const_iterator it = clusters.begin(); it != clusters.end(); it++) {
265  assert((*it).size() > 0);
266  device.openTag(SUMO_TAG_JOIN);
267  // prepare string
268  std::ostringstream oss;
269  for (std::set<std::string>::const_iterator it_id = it->begin(); it_id != it->end(); it_id++) {
270  oss << *it_id << " ";
271  }
272  // remove final space
273  std::string ids = oss.str();
274  device.writeAttr(SUMO_ATTR_NODES, ids.substr(0, ids.size() - 1));
275  device.closeTag(true);
276  }
277  device.close();
278 }
279 
280 
281 void
283  OutputDevice& device = OutputDevice::getDevice(oc.getString("street-sign-output"));
284  device.writeXMLHeader("pois", SUMOSAXAttributes::ENCODING, NWFrame::MAJOR_VERSION + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://sumo.sf.net/xsd/poi_file.xsd\"");
285  for (std::map<std::string, NBEdge*>::const_iterator i = ec.begin(); i != ec.end(); ++i) {
286  NBEdge* e = (*i).second;
287  const std::vector<NBSign>& signs = e->getSigns();
288  for (std::vector<NBSign>::const_iterator it = signs.begin(); it != signs.end(); ++it) {
289  it->writeAsPOI(device, e);
290  }
291  }
292  device.close();
293 }
294 /****************************************************************************/
295