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>
43 #include "NWFrame.h"
44 #include "NWWriter_SUMO.h"
45 #include "NWWriter_XML.h"
46 
47 #ifdef CHECK_MEMORY_LEAKS
48 #include <foreign/nvwa/debug_new.h>
49 #endif // CHECK_MEMORY_LEAKS
50 
51 
52 
53 // ===========================================================================
54 // method definitions
55 // ===========================================================================
56 // ---------------------------------------------------------------------------
57 // static methods
58 // ---------------------------------------------------------------------------
59 void
61  // check whether a matsim-file shall be generated
62  if (oc.isSet("plain-output-prefix")) {
63  writeNodes(oc, nb.getNodeCont());
66  }
67  if (oc.isSet("junctions.join-output")) {
69  }
70 }
71 
72 
73 void
76  bool useGeo = oc.exists("proj.plain-geo") && oc.getBool("proj.plain-geo");
77  if (useGeo && !gch.usingGeoProjection()) {
78  WRITE_WARNING("Ignoring option \"proj.plain-geo\" because no geo-conversion has been defined");
79  useGeo = false;
80  }
81  const bool geoAccuracy = useGeo || gch.usingInverseGeoProjection();
82 
83  OutputDevice& device = OutputDevice::getDevice(oc.getString("plain-output-prefix") + ".nod.xml");
84  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\"");
85 
86  // write network offsets and projection to allow reconstruction of original coordinates
87  if (!useGeo) {
89  }
90 
91  // write nodes
92  for (std::map<std::string, NBNode*>::const_iterator i = nc.begin(); i != nc.end(); ++i) {
93  NBNode* n = (*i).second;
94  device.openTag(SUMO_TAG_NODE);
95  device.writeAttr(SUMO_ATTR_ID, n->getID());
96  // write position
97  Position pos = n->getPosition();
98  if (useGeo) {
99  gch.cartesian2geo(pos);
100  }
101  if (geoAccuracy) {
103  }
104  NWFrame::writePositionLong(pos, device);
105  if (geoAccuracy) {
106  device.setPrecision();
107  }
108 
109  device.writeAttr(SUMO_ATTR_TYPE, toString(n->getType()));
110  if (n->isTLControlled()) {
111  const std::set<NBTrafficLightDefinition*> &tlss = n->getControllingTLS();
112  // set may contain multiple programs for the same id.
113  // make sure ids are unique and sorted
114  std::set<std::string> tlsIDs;
115  for (std::set<NBTrafficLightDefinition*>::const_iterator it_tl = tlss.begin(); it_tl != tlss.end(); it_tl++) {
116  tlsIDs.insert((*it_tl)->getID());
117  }
118  std::vector<std::string> sortedIDs(tlsIDs.begin(), tlsIDs.end());
119  sort(sortedIDs.begin(), sortedIDs.end());
120  device.writeAttr(SUMO_ATTR_TLID, sortedIDs);
121  }
122  device.closeTag(true);
123  }
124  device.close();
125 }
126 
127 
128 void
130  const GeoConvHelper& gch = GeoConvHelper::getFinal();
131  bool useGeo = oc.exists("proj.plain-geo") && oc.getBool("proj.plain-geo");
132  const bool geoAccuracy = useGeo || gch.usingInverseGeoProjection();
133 
134  OutputDevice& edevice = OutputDevice::getDevice(oc.getString("plain-output-prefix") + ".edg.xml");
135  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\"");
136  OutputDevice& cdevice = OutputDevice::getDevice(oc.getString("plain-output-prefix") + ".con.xml");
137  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\"");
138  bool noNames = !oc.getBool("output.street-names");
139  for (std::map<std::string, NBEdge*>::const_iterator i = ec.begin(); i != ec.end(); ++i) {
140  // write the edge itself to the edges-files
141  NBEdge* e = (*i).second;
142  edevice.openTag(SUMO_TAG_EDGE);
143  edevice.writeAttr(SUMO_ATTR_ID, e->getID());
144  edevice.writeAttr(SUMO_ATTR_FROM, e->getFromNode()->getID());
145  edevice.writeAttr(SUMO_ATTR_TO, e->getToNode()->getID());
146  if (!noNames && e->getStreetName() != "") {
147  edevice.writeAttr(SUMO_ATTR_NAME, e->getStreetName());
148  }
150  // write the type if given
151  if (e->getTypeID() != "") {
152  edevice.writeAttr(SUMO_ATTR_TYPE, e->getTypeID());
153  }
155  if (!e->hasLaneSpecificSpeed()) {
156  edevice.writeAttr(SUMO_ATTR_SPEED, e->getSpeed());
157  }
158  // write non-default geometry
159  if (!e->hasDefaultGeometry()) {
160  PositionVector geom = e->getGeometry();
161  if (useGeo) {
162  for (int i = 0; i < (int) geom.size(); i++) {
163  gch.cartesian2geo(geom[i]);
164  }
165  }
166  if (geoAccuracy) {
168  }
169  edevice.writeAttr(SUMO_ATTR_SHAPE, geom);
170  if (geoAccuracy) {
171  edevice.setPrecision();
172  }
173  }
174  // write the spread type if not default ("right")
177  }
178  // write the length if it was specified
179  if (e->hasLoadedLength()) {
181  }
182  // some attributes can be set by edge default or per lane. Write as default if possible (efficiency)
184  edevice.writeAttr(SUMO_ATTR_WIDTH, e->getWidth());
185  }
187  edevice.writeAttr(SUMO_ATTR_OFFSET, e->getOffset());
188  }
189  if (!e->needsLaneSpecificOutput()) {
190  edevice.closeTag(true);
191  } else {
192  edevice << ">\n";
193  for (unsigned int i = 0; i < e->getLanes().size(); ++i) {
194  const NBEdge::Lane& lane = e->getLanes()[i];
195  edevice.openTag(SUMO_TAG_LANE);
196  edevice.writeAttr(SUMO_ATTR_INDEX, i);
197  // write allowed lanes
200  // write other attributes
202  edevice.writeAttr(SUMO_ATTR_WIDTH, lane.width);
203  }
205  edevice.writeAttr(SUMO_ATTR_OFFSET, lane.offset);
206  }
207  if (e->hasLaneSpecificSpeed()) {
208  edevice.writeAttr(SUMO_ATTR_SPEED, lane.speed);
209  }
210  edevice.closeTag(true);
211  }
212  edevice.closeTag();
213  }
214  // write this edge's connections to the connections-files
216  const std::vector<NBEdge::Connection> connections = e->getConnections();
217  for (std::vector<NBEdge::Connection>::const_iterator c = connections.begin(); c != connections.end(); ++c) {
218  NWWriter_SUMO::writeConnection(cdevice, *e, *c, false, NWWriter_SUMO::PLAIN);
219  }
220  if (connections.size() > 0) {
221  cdevice << "\n";
222  }
223  }
224 
225  // write loaded prohibitions to the connections-file
226  for (std::map<std::string, NBNode*>::const_iterator i = nc.begin(); i != nc.end(); ++i) {
227  NWWriter_SUMO::writeProhibitions(cdevice, i->second->getProhibitions());
228  }
229  edevice.close();
230  cdevice.close();
231 }
232 
233 
234 void
236  OutputDevice& device = OutputDevice::getDevice(oc.getString("plain-output-prefix") + ".tll.xml");
237  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\"");
239  // we also need to remember the associations between tlLogics and connections
240  // since the information in con.xml is insufficient
241  for (std::map<std::string, NBEdge*>::const_iterator i = ec.begin(); i != ec.end(); ++i) {
242  NBEdge* e = (*i).second;
243  // write this edge's tl-controlled connections
244  const std::vector<NBEdge::Connection> connections = e->getConnections();
245  for (std::vector<NBEdge::Connection>::const_iterator c = connections.begin(); c != connections.end(); ++c) {
246  if (c->tlID != "") {
247  NWWriter_SUMO::writeConnection(device, *e, *c, false, NWWriter_SUMO::TLL);
248  }
249  }
250  }
251  device.close();
252 }
253 
254 
255 void
257  OutputDevice& device = OutputDevice::getDevice(oc.getString("junctions.join-output"));
258  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\"");
259  const std::vector<std::set<std::string> >& clusters = nc.getJoinedClusters();
260  for (std::vector<std::set<std::string> >::const_iterator it = clusters.begin(); it != clusters.end(); it++) {
261  assert((*it).size() > 0);
262  device.openTag(SUMO_TAG_JOIN);
263  // prepare string
264  std::ostringstream oss;
265  for (std::set<std::string>::iterator it_id = it->begin(); it_id != it->end(); it_id++) {
266  oss << *it_id << " ";
267  }
268  // remove final space
269  std::string ids = oss.str();
270  device.writeAttr(SUMO_ATTR_NODES, ids.substr(0, ids.size() - 1));
271  device.closeTag(true);
272  }
273  device.close();
274 }
275 /****************************************************************************/
276