LCOV - code coverage report
Current view: top level - src/netwrite - NWWriter_SUMO.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 98.5 % 599 590
Test Date: 2026-07-25 16:16:11 Functions: 100.0 % 21 21

            Line data    Source code
       1              : /****************************************************************************/
       2              : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3              : // Copyright (C) 2001-2026 German Aerospace Center (DLR) and others.
       4              : // This program and the accompanying materials are made available under the
       5              : // terms of the Eclipse Public License 2.0 which is available at
       6              : // https://www.eclipse.org/legal/epl-2.0/
       7              : // This Source Code may also be made available under the following Secondary
       8              : // Licenses when the conditions for such availability set forth in the Eclipse
       9              : // Public License 2.0 are satisfied: GNU General Public License, version 2
      10              : // or later which is available at
      11              : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
      12              : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
      13              : /****************************************************************************/
      14              : /// @file    NWWriter_SUMO.cpp
      15              : /// @author  Daniel Krajzewicz
      16              : /// @author  Jakob Erdmann
      17              : /// @author  Michael Behrisch
      18              : /// @author  Leonhard Luecken
      19              : /// @date    Tue, 04.05.2011
      20              : ///
      21              : // Exporter writing networks using the SUMO format
      22              : /****************************************************************************/
      23              : #include <config.h>
      24              : #include <cmath>
      25              : #include <algorithm>
      26              : #include <utils/options/OptionsCont.h>
      27              : #include <utils/iodevices/OutputDevice.h>
      28              : #include <utils/geom/GeoConvHelper.h>
      29              : #include <utils/common/ToString.h>
      30              : #include <utils/common/MsgHandler.h>
      31              : #include <utils/common/StringUtils.h>
      32              : #include <utils/common/SUMOVehicleClass.h>
      33              : #include <utils/geom/GeomConvHelper.h>
      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 <netbuild/NBTrafficLightLogic.h>
      40              : #include <netbuild/NBDistrict.h>
      41              : #include <netbuild/NBHelpers.h>
      42              : #include "NWFrame.h"
      43              : #include "NWWriter_SUMO.h"
      44              : 
      45              : 
      46              : //#define DEBUG_OPPOSITE_INTERNAL
      47              : 
      48              : // ===========================================================================
      49              : // method definitions
      50              : // ===========================================================================
      51              : // ---------------------------------------------------------------------------
      52              : // static methods
      53              : // ---------------------------------------------------------------------------
      54              : void
      55         2238 : NWWriter_SUMO::writeNetwork(const OptionsCont& oc, NBNetBuilder& nb) {
      56              :     // check whether a sumo net-file shall be generated
      57         4476 :     if (!oc.isSet("output-file")) {
      58           92 :         return;
      59              :     }
      60         4289 :     OutputDevice& device = OutputDevice::getDevice(oc.getString("output-file"));
      61              :     std::map<SumoXMLAttr, std::string> attrs;
      62         2143 :     attrs[SUMO_ATTR_VERSION] = toString(NETWORK_VERSION);
      63         4286 :     if (oc.getBool("lefthand") != oc.getBool("flip-y-axis")) {
      64           34 :         attrs[SUMO_ATTR_LEFTHAND] = "true";
      65         4252 :     } else if (oc.getBool("lefthand")) {
      66              :         // network was flipped, correct written link directions
      67            1 :         OptionsCont::getOptions().resetWritable();
      68            2 :         OptionsCont::getOptions().set("lefthand", "false");
      69              :     }
      70         2143 :     LaneSpreadFunction defaultSpread = SUMOXMLDefinitions::LaneSpreadFunctions.get(oc.getString("default.spreadtype"));
      71         2143 :     const int cornerDetail = oc.getInt("junctions.corner-detail");
      72         2143 :     if (cornerDetail > 0) {
      73         1868 :         attrs[SUMO_ATTR_CORNERDETAIL] = toString(cornerDetail);
      74              :     }
      75         4286 :     if (!oc.isDefault("junctions.internal-link-detail")) {
      76            2 :         attrs[SUMO_ATTR_LINKDETAIL] = toString(oc.getInt("junctions.internal-link-detail"));
      77              :     }
      78         4286 :     if (oc.getBool("rectangular-lane-cut")) {
      79           54 :         attrs[SUMO_ATTR_RECTANGULAR_LANE_CUT] = "true";
      80              :     }
      81         4236 :     if (oc.getBool("crossings.guess") || oc.getBool("walkingareas")) {
      82          122 :         attrs[SUMO_ATTR_WALKINGAREAS] = "true";
      83              :     }
      84         4286 :     if (oc.getFloat("junctions.limit-turn-speed") > 0) {
      85         3820 :         attrs[SUMO_ATTR_LIMIT_TURN_SPEED] = toString(oc.getFloat("junctions.limit-turn-speed"));
      86              :     }
      87         4286 :     if (!oc.isDefault("check-lane-foes.all")) {
      88           10 :         attrs[SUMO_ATTR_CHECKLANEFOES_ALL] = toString(oc.getBool("check-lane-foes.all"));
      89              :     }
      90         4286 :     if (!oc.isDefault("check-lane-foes.roundabout")) {
      91            0 :         attrs[SUMO_ATTR_CHECKLANEFOES_ROUNDABOUT] = toString(oc.getBool("check-lane-foes.roundabout"));
      92              :     }
      93         4286 :     if (!oc.isDefault("tls.ignore-internal-junction-jam")) {
      94            8 :         attrs[SUMO_ATTR_TLS_IGNORE_INTERNAL_JUNCTION_JAM] = toString(oc.getBool("tls.ignore-internal-junction-jam"));
      95              :     }
      96         2143 :     if (defaultSpread != LaneSpreadFunction::RIGHT) {
      97           10 :         attrs[SUMO_ATTR_SPREADTYPE] = oc.getString("default.spreadtype");
      98              :     }
      99         5768 :     if (oc.exists("geometry.avoid-overlap") && !oc.getBool("geometry.avoid-overlap")) {
     100          982 :         attrs[SUMO_ATTR_AVOID_OVERLAP] = toString(oc.getBool("geometry.avoid-overlap"));
     101              :     }
     102         4286 :     if (oc.exists("junctions.higher-speed") && oc.getBool("junctions.higher-speed")) {
     103            2 :         attrs[SUMO_ATTR_HIGHER_SPEED] = toString(oc.getBool("junctions.higher-speed"));
     104              :     }
     105         6427 :     if (oc.exists("internal-junctions.vehicle-width") && !oc.isDefault("internal-junctions.vehicle-width")) {
     106            4 :         attrs[SUMO_ATTR_INTERNAL_JUNCTIONS_VEHICLE_WIDTH] = toString(oc.getFloat("internal-junctions.vehicle-width"));
     107              :     }
     108         4286 :     if (!oc.isDefault("junctions.minimal-shape")) {
     109            4 :         attrs[SUMO_ATTR_JUNCTIONS_MINIMAL_SHAPE] = toString(oc.getBool("junctions.minimal-shape"));
     110              :     }
     111         4286 :     if (!oc.isDefault("junctions.endpoint-shape")) {
     112            4 :         attrs[SUMO_ATTR_JUNCTIONS_ENDPOINT_SHAPE] = toString(oc.getBool("junctions.endpoint-shape"));
     113              :     }
     114         4286 :     device.writeXMLHeader("net", "net_file.xsd", attrs); // street names may contain non-ascii chars
     115         2143 :     device.lf();
     116              :     // get involved container
     117              :     const NBNodeCont& nc = nb.getNodeCont();
     118              :     const NBEdgeCont& ec = nb.getEdgeCont();
     119              :     const NBDistrictCont& dc = nb.getDistrictCont();
     120              : 
     121              :     // write network offsets and projection
     122         2143 :     GeoConvHelper::writeLocation(device);
     123              : 
     124              :     // write edge types and restrictions
     125         2143 :     std::set<std::string> usedTypes = ec.getUsedTypes();
     126         2143 :     nb.getTypeCont().writeEdgeTypes(device, usedTypes);
     127              : 
     128              :     // write inner lanes
     129         4286 :     if (!oc.getBool("no-internal-links")) {
     130              :         bool hadAny = false;
     131        29210 :         for (std::map<std::string, NBNode*>::const_iterator i = nc.begin(); i != nc.end(); ++i) {
     132        27735 :             hadAny |= writeInternalEdges(device, ec, *(*i).second);
     133              :         }
     134         1475 :         if (hadAny) {
     135         1258 :             device.lf();
     136              :         }
     137              :     }
     138              : 
     139              :     // write edges with lanes and connected edges
     140         2143 :     bool noNames = !oc.getBool("output.street-names");
     141       121367 :     for (std::map<std::string, NBEdge*>::const_iterator i = ec.begin(); i != ec.end(); ++i) {
     142       119224 :         writeEdge(device, *(*i).second, noNames, defaultSpread);
     143              :     }
     144         2143 :     device.lf();
     145              : 
     146              :     // write tls logics
     147         2143 :     writeTrafficLights(device, nb.getTLLogicCont());
     148              : 
     149              :     // write the nodes (junctions)
     150        68192 :     for (std::map<std::string, NBNode*>::const_iterator i = nc.begin(); i != nc.end(); ++i) {
     151        66049 :         writeJunction(device, *(*i).second);
     152              :     }
     153         2143 :     device.lf();
     154         2143 :     const bool includeInternal = !oc.getBool("no-internal-links");
     155         2143 :     if (includeInternal) {
     156              :         // ... internal nodes if not unwanted
     157              :         bool hadAny = false;
     158        29210 :         for (std::map<std::string, NBNode*>::const_iterator i = nc.begin(); i != nc.end(); ++i) {
     159        27735 :             hadAny |= writeInternalNodes(device, *(*i).second);
     160              :         }
     161         1475 :         if (hadAny) {
     162          703 :             device.lf();
     163              :         }
     164              :     }
     165              : 
     166              :     // write the successors of lanes
     167              :     int numConnections = 0;
     168       121367 :     for (std::map<std::string, NBEdge*>::const_iterator it_edge = ec.begin(); it_edge != ec.end(); it_edge++) {
     169       119224 :         NBEdge* from = it_edge->second;
     170              :         const std::vector<NBEdge::Connection>& connections = from->getConnections();
     171       119224 :         numConnections += (int)connections.size();
     172       354868 :         for (const NBEdge::Connection& con : connections) {
     173       235644 :             writeConnection(device, *from, con, includeInternal);
     174              :         }
     175              :     }
     176         2143 :     if (numConnections > 0) {
     177         1888 :         device.lf();
     178              :     }
     179         2143 :     if (includeInternal) {
     180              :         // ... internal successors if not unwanted
     181              :         bool hadAny = false;
     182        29210 :         for (std::map<std::string, NBNode*>::const_iterator i = nc.begin(); i != nc.end(); ++i) {
     183        27735 :             hadAny |= writeInternalConnections(device, *(*i).second);
     184              :         }
     185         1475 :         if (hadAny) {
     186         1258 :             device.lf();
     187              :         }
     188              :     }
     189        68192 :     for (std::map<std::string, NBNode*>::const_iterator i = nc.begin(); i != nc.end(); ++i) {
     190        66049 :         NBNode* node = (*i).second;
     191              :         // write connections from pedestrian crossings
     192        66049 :         std::vector<NBNode::Crossing*> crossings = node->getCrossings();
     193        67630 :         for (auto c : crossings) {
     194         3162 :             NWWriter_SUMO::writeInternalConnection(device, c->id, c->nextWalkingArea, 0, 0, "", LinkDirection::STRAIGHT, c->tlID, c->tlLinkIndex2);
     195              :         }
     196              :         // write connections from pedestrian walking areas
     197        70351 :         for (const NBNode::WalkingArea& wa : node->getWalkingAreas()) {
     198         5883 :             for (const std::string& cID : wa.nextCrossings) {
     199         1581 :                 const NBNode::Crossing& nextCrossing = *node->getCrossing(cID);
     200              :                 // connection to next crossing (may be tls-controlled)
     201         1581 :                 device.openTag(SUMO_TAG_CONNECTION);
     202         1581 :                 device.writeAttr(SUMO_ATTR_FROM, wa.id);
     203         1581 :                 device.writeAttr(SUMO_ATTR_TO, cID);
     204         1581 :                 device.writeAttr(SUMO_ATTR_FROM_LANE, 0);
     205         1581 :                 device.writeAttr(SUMO_ATTR_TO_LANE, 0);
     206         1581 :                 if (nextCrossing.tlID != "") {
     207          600 :                     device.writeAttr(SUMO_ATTR_TLID, nextCrossing.tlID);
     208              :                     assert(nextCrossing.tlLinkIndex >= 0);
     209          600 :                     device.writeAttr(SUMO_ATTR_TLLINKINDEX, nextCrossing.tlLinkIndex);
     210              :                 }
     211         1581 :                 device.writeAttr(SUMO_ATTR_DIR, LinkDirection::STRAIGHT);
     212         2551 :                 device.writeAttr(SUMO_ATTR_STATE, nextCrossing.priority ? LINKSTATE_MAJOR : LINKSTATE_MINOR);
     213         3162 :                 device.closeTag();
     214              :             }
     215              :             // optional connections from/to sidewalk
     216              :             std::string edgeID;
     217              :             int laneIndex;
     218         9559 :             for (const std::string& sw : wa.nextSidewalks) {
     219         5257 :                 NBHelpers::interpretLaneID(sw, edgeID, laneIndex);
     220        10514 :                 NWWriter_SUMO::writeInternalConnection(device, wa.id, edgeID, 0, laneIndex, "");
     221              :             }
     222         9507 :             for (const std::string& sw : wa.prevSidewalks) {
     223         5205 :                 NBHelpers::interpretLaneID(sw, edgeID, laneIndex);
     224        10410 :                 NWWriter_SUMO::writeInternalConnection(device, edgeID, wa.id, laneIndex, 0, "");
     225              :             }
     226              :         }
     227        66049 :     }
     228              : 
     229              :     // write loaded prohibitions
     230        68192 :     for (std::map<std::string, NBNode*>::const_iterator i = nc.begin(); i != nc.end(); ++i) {
     231        66049 :         writeProhibitions(device, i->second->getProhibitions(), ec);
     232              :     }
     233              : 
     234              :     // write roundabout information
     235         2143 :     writeRoundabouts(device, ec.getRoundabouts(), ec);
     236              : 
     237              :     // write the districts
     238         2146 :     if (dc.size() != 0 && oc.isDefault("taz-output")) {
     239            4 :         WRITE_WARNING(TL("Embedding TAZ-data inside the network is deprecated. Use option --taz-output instead"));
     240           44 :         for (std::map<std::string, NBDistrict*>::const_iterator i = dc.begin(); i != dc.end(); i++) {
     241           42 :             writeDistrict(device, *(*i).second);
     242              :         }
     243            2 :         device.lf();
     244              :     }
     245         2143 :     device.close();
     246              : }
     247              : 
     248              : 
     249              : std::string
     250        92285 : NWWriter_SUMO::getOppositeInternalID(const NBEdgeCont& ec, const NBEdge* from, const NBEdge::Connection& con, double& oppositeLength) {
     251        92285 :     const NBEdge::Lane& succ = con.toEdge->getLanes()[con.toLane];
     252        92285 :     const NBEdge::Lane& pred = from->getLanes()[con.fromLane];
     253        92285 :     const bool lefthand = OptionsCont::getOptions().getBool("lefthand");
     254        92285 :     if (succ.oppositeID != "" && succ.oppositeID != "-" && pred.oppositeID != "" && pred.oppositeID != "-") {
     255              : #ifdef DEBUG_OPPOSITE_INTERNAL
     256              :         std::cout << "getOppositeInternalID con=" << con.getDescription(from) << " (" << con.getInternalLaneID() << ")\n";
     257              : #endif
     258              :         // find the connection that connects succ.oppositeID to pred.oppositeID
     259          112 :         const NBEdge* succOpp = ec.retrieve(succ.oppositeID.substr(0, succ.oppositeID.rfind("_")));
     260          112 :         const NBEdge* predOpp = ec.retrieve(pred.oppositeID.substr(0, pred.oppositeID.rfind("_")));
     261              :         assert(succOpp != 0);
     262              :         assert(predOpp != 0);
     263              :         const std::vector<NBEdge::Connection>& connections = succOpp->getConnections();
     264          213 :         for (std::vector<NBEdge::Connection>::const_iterator it_c = connections.begin(); it_c != connections.end(); it_c++) {
     265              :             const NBEdge::Connection& conOpp = *it_c;
     266          167 :             if (succOpp != from // turnaround
     267           84 :                     && predOpp == conOpp.toEdge
     268          199 :                     && succOpp->getLaneID(conOpp.fromLane) == succ.oppositeID
     269          195 :                     && predOpp->getLaneID(conOpp.toLane) == pred.oppositeID
     270           14 :                     && from->getToNode()->getDirection(from, con.toEdge, lefthand) == LinkDirection::STRAIGHT
     271          177 :                     && from->getToNode()->getDirection(succOpp, predOpp, lefthand) == LinkDirection::STRAIGHT
     272              :                ) {
     273              : #ifdef DEBUG_OPPOSITE_INTERNAL
     274              :                 std::cout << "  found " << conOpp.getInternalLaneID() << "\n";
     275              : #endif
     276           10 :                 oppositeLength = conOpp.length;
     277           10 :                 return conOpp.getInternalLaneID();
     278              :             } else {
     279              :                 /*
     280              :                 #ifdef DEBUG_OPPOSITE_INTERNAL
     281              :                 std::cout << "  rejected " << conOpp.getInternalLaneID()
     282              :                     << "\n     succ.oppositeID=" << succ.oppositeID
     283              :                     << "\n         succOppLane=" << succOpp->getLaneID(conOpp.fromLane)
     284              :                     << "\n     pred.oppositeID=" << pred.oppositeID
     285              :                     << "\n         predOppLane=" << predOpp->getLaneID(conOpp.toLane)
     286              :                     << "\n      predOpp=" << predOpp->getID()
     287              :                     << "\n     conOppTo=" << conOpp.toEdge->getID()
     288              :                     << "\n     len1=" << con.shape.length()
     289              :                     << "\n     len2=" << conOpp.shape.length()
     290              :                     << "\n";
     291              :                 #endif
     292              :                 */
     293              :             }
     294              :         }
     295           46 :         return "";
     296              :     } else {
     297        92229 :         return "";
     298              :     }
     299              : }
     300              : 
     301              : 
     302              : bool
     303        27735 : NWWriter_SUMO::writeInternalEdges(OutputDevice& into, const NBEdgeCont& ec, const NBNode& n) {
     304              :     bool ret = false;
     305              :     const EdgeVector& incoming = n.getIncomingEdges();
     306              :     // first pass: determine opposite internal edges and average their length
     307              :     std::map<std::string, std::string> oppositeLaneID;
     308              :     std::map<std::string, double> oppositeLengths;
     309        75240 :     for (NBEdge* e : incoming) {
     310       139790 :         for (const NBEdge::Connection& c : e->getConnections()) {
     311        92285 :             double oppositeLength = 0;
     312        92285 :             const std::string op = getOppositeInternalID(ec, e, c, oppositeLength);
     313       184570 :             oppositeLaneID[c.getInternalLaneID()] = op;
     314        92285 :             if (op != "") {
     315           10 :                 oppositeLengths[c.id] = oppositeLength;
     316              :             }
     317              :         }
     318              :     }
     319        27735 :     if (oppositeLengths.size() > 0) {
     320           13 :         for (NBEdge* e : incoming) {
     321           56 :             for (NBEdge::Connection& c : e->getConnections()) {
     322           46 :                 if (oppositeLengths.count(c.id) > 0) {
     323           19 :                     c.length = (c.length + oppositeLengths[c.id]) / 2;
     324              :                 }
     325              :             }
     326              :         }
     327              :     }
     328              : 
     329        75240 :     for (NBEdge* e : incoming) {
     330              :         const std::vector<NBEdge::Connection>& elv = e->getConnections();
     331        47505 :         if (elv.size() > 0) {
     332              :             bool haveVia = false;
     333        35161 :             std::string edgeID = "";
     334        35161 :             double bidiLength = -1;
     335              :             // second pass: write non-via edges
     336       127446 :             for (const NBEdge::Connection& k : elv) {
     337        92285 :                 if (k.toEdge == nullptr) {
     338              :                     assert(false); // should never happen. tell me when it does
     339            0 :                     continue;
     340              :                 }
     341        92285 :                 if (edgeID != k.id) {
     342        83052 :                     if (edgeID != "") {
     343              :                         // close the previous edge
     344        95782 :                         into.closeTag();
     345              :                     }
     346              :                     edgeID = k.id;
     347        83052 :                     into.openTag(SUMO_TAG_EDGE);
     348        83052 :                     into.writeAttr(SUMO_ATTR_ID, edgeID);
     349        83052 :                     into.writeAttr(SUMO_ATTR_FUNCTION, SumoXMLEdgeFunc::INTERNAL);
     350        83052 :                     if (k.edgeType != "") {
     351            4 :                         into.writeAttr(SUMO_ATTR_TYPE, k.edgeType);
     352              :                     }
     353        83052 :                     bidiLength = -1;
     354        84281 :                     if (e->getBidiEdge() && k.toEdge->getBidiEdge() &&
     355         1229 :                             e != k.toEdge->getTurnDestination(true)) {
     356         1044 :                         const std::string bidiEdge = getInternalBidi(e, k, bidiLength);
     357         1044 :                         if (bidiEdge != "") {
     358         1040 :                             into.writeAttr(SUMO_ATTR_BIDI, bidiEdge);
     359              :                         }
     360              :                     }
     361              :                     // open a new edge
     362              :                 }
     363              :                 // to avoid changing to an internal lane which has a successor
     364              :                 // with the wrong permissions we need to inherit them from the successor
     365        92285 :                 const NBEdge::Lane& successor = k.toEdge->getLanes()[k.toLane];
     366       184423 :                 SVCPermissions permissions = (k.permissions != SVC_UNSPECIFIED) ? k.permissions : (
     367        92138 :                                                  successor.permissions & e->getPermissions(k.fromLane));
     368        92285 :                 SVCPermissions changeLeft = k.changeLeft != SVC_UNSPECIFIED ? k.changeLeft : SVCAll;
     369        92285 :                 SVCPermissions changeRight = k.changeRight != SVC_UNSPECIFIED ? k.changeRight : SVCAll;
     370        92285 :                 const double width = e->getInternalLaneWidth(n, k, successor, false);
     371        92285 :                 const double length = bidiLength > 0 ? bidiLength : k.length;
     372       369140 :                 writeLane(into, k.getInternalLaneID(), k.vmax, k.friction,
     373        92285 :                           permissions, successor.preferred,
     374              :                           changeLeft, changeRight,
     375              :                           NBEdge::UNSPECIFIED_OFFSET, NBEdge::UNSPECIFIED_OFFSET,
     376       184570 :                           StopOffset(), width, k.shape, &k,
     377       184570 :                           length, k.internalLaneIndex, oppositeLaneID[k.getInternalLaneID()], "");
     378        92285 :                 haveVia = haveVia || k.haveVia;
     379              :             }
     380              :             ret = true;
     381        35161 :             into.closeTag(); // close the last edge
     382              :             // third pass: write via edges
     383        35161 :             if (haveVia) {
     384        10370 :                 std::string viaEdgeID = "";
     385        48110 :                 for (const NBEdge::Connection& k : elv) {
     386        37740 :                     if (!k.haveVia) {
     387        20446 :                         continue;
     388              :                     }
     389        17294 :                     if (k.toEdge == nullptr) {
     390              :                         assert(false); // should never happen. tell me when it does
     391            0 :                         continue;
     392              :                     }
     393        17294 :                     if (viaEdgeID != k.viaID) {
     394        17083 :                         if (viaEdgeID != "") {
     395              :                             // close the previous edge
     396        13426 :                             into.closeTag();
     397              :                         }
     398              :                         viaEdgeID = k.viaID;
     399              :                         // open a new edge
     400        17083 :                         into.openTag(SUMO_TAG_EDGE);
     401        17083 :                         into.writeAttr(SUMO_ATTR_ID, viaEdgeID);
     402        17083 :                         into.writeAttr(SUMO_ATTR_FUNCTION, SumoXMLEdgeFunc::INTERNAL);
     403        17083 :                         if (k.edgeType != "") {
     404            4 :                             into.writeAttr(SUMO_ATTR_TYPE, k.edgeType);
     405              :                         }
     406              :                     }
     407        17294 :                     const NBEdge::Lane& successor = k.toEdge->getLanes()[k.toLane];
     408        34500 :                     SVCPermissions permissions = (k.permissions != SVC_UNSPECIFIED) ? k.permissions : (
     409        17206 :                                                      successor.permissions & e->getPermissions(k.fromLane));
     410        17294 :                     const double width = e->getInternalLaneWidth(n, k, successor, true);
     411        69176 :                     writeLane(into, k.getInternalViaLaneID(), k.vmax, k.friction, permissions, successor.preferred,
     412              :                               SVCAll, SVCAll, // #XXX todo
     413              :                               NBEdge::UNSPECIFIED_OFFSET, NBEdge::UNSPECIFIED_OFFSET,
     414        34588 :                               StopOffset(), width, k.viaShape, &k,
     415        17294 :                               MAX2(k.viaLength, POSITION_EPS), // microsim needs positive length
     416              :                               0, "", "");
     417              :                 }
     418        20740 :                 into.closeTag();
     419              :             }
     420              :         }
     421              :     }
     422              :     // write pedestrian crossings
     423        27735 :     const double crossingSpeed = OptionsCont::getOptions().getFloat("default.crossing-speed");
     424        29316 :     for (const NBNode::Crossing* c : n.getCrossings()) {
     425         1581 :         into.openTag(SUMO_TAG_EDGE).writeAttr(SUMO_ATTR_ID, c->id).writeAttr(SUMO_ATTR_FUNCTION, SumoXMLEdgeFunc::CROSSING);
     426         1581 :         into.writeAttr(SUMO_ATTR_CROSSING_EDGES, c->edges);
     427         4743 :         writeLane(into, c->id + "_0", crossingSpeed, NBEdge::UNSPECIFIED_FRICTION, SVC_PEDESTRIAN, 0, SVCAll, SVCAll,
     428              :                   NBEdge::UNSPECIFIED_OFFSET, NBEdge::UNSPECIFIED_OFFSET,
     429         3162 :                   StopOffset(), c->width, c->shape, c,
     430         1581 :                   MAX2(c->shape.length(), POSITION_EPS), 0, "", "", false, c->customShape.size() != 0, c->outlineShape);
     431         3162 :         into.closeTag();
     432        27735 :     }
     433              :     // write pedestrian walking areas
     434        55470 :     const double walkingareaSpeed = OptionsCont::getOptions().getFloat("default.walkingarea-speed");
     435        32037 :     for (const NBNode::WalkingArea& wa : n.getWalkingAreas()) {
     436         4302 :         into.openTag(SUMO_TAG_EDGE).writeAttr(SUMO_ATTR_ID, wa.id).writeAttr(SUMO_ATTR_FUNCTION, SumoXMLEdgeFunc::WALKINGAREA);
     437        17208 :         writeLane(into, wa.id + "_0", walkingareaSpeed, NBEdge::UNSPECIFIED_FRICTION, SVC_PEDESTRIAN, 0, SVCAll, SVCAll,
     438              :                   NBEdge::UNSPECIFIED_OFFSET, NBEdge::UNSPECIFIED_OFFSET,
     439        12906 :                   StopOffset(), wa.width, wa.shape, nullptr, wa.length, 0, "", "", false, wa.hasCustomShape);
     440         8604 :         into.closeTag();
     441              :     }
     442        27735 :     return ret;
     443              : }
     444              : 
     445              : 
     446              : std::string
     447         1044 : NWWriter_SUMO::getInternalBidi(const NBEdge* e, const NBEdge::Connection& k, double& length) {
     448         1044 :     const NBEdge* fromBidi = e->getTurnDestination(true);
     449         1044 :     const NBEdge* toBidi = k.toEdge->getTurnDestination(true);
     450         1044 :     const std::vector<NBEdge::Connection> cons = toBidi->getConnectionsFromLane(-1, fromBidi, -1);
     451         1044 :     if (cons.size() > 0) {
     452         1042 :         if (e->getNumLanes() == 1 && k.toEdge->getNumLanes() == 1 && fromBidi->getNumLanes() == 1 && toBidi->getNumLanes() == 1) {
     453         1026 :             length = (k.length + cons.back().length) / 2;
     454              :             return cons.back().id;
     455              :         }
     456              :         // do a more careful check in case there are parallel internal edges
     457              :         // note: k is the first connection with the new id
     458           43 :         for (const NBEdge::Connection& c : e->getConnections()) {
     459           41 :             if (c.id == k.id) {
     460           26 :                 PositionVector rShape = c.shape.reverse();
     461           62 :                 for (const NBEdge::Connection& k2 : cons) {
     462           50 :                     if (k2.shape.almostSame(rShape, POSITION_EPS)) {
     463           14 :                         length = (c.length + k2.length) / 2;
     464              :                         return k2.id;
     465              :                     }
     466              :                 }
     467           26 :             }
     468              :         }
     469              :     } else {
     470            6 :         WRITE_WARNINGF(TL("Could not find bidi-connection for edge '%'"), k.id)
     471              :     }
     472            4 :     return "";
     473         1044 : }
     474              : 
     475              : void
     476       119224 : NWWriter_SUMO::writeEdge(OutputDevice& into, const NBEdge& e, bool noNames, LaneSpreadFunction defaultSpread) {
     477              :     // write the edge's begin
     478       119224 :     into.openTag(SUMO_TAG_EDGE).writeAttr(SUMO_ATTR_ID, e.getID());
     479       119224 :     into.writeAttr(SUMO_ATTR_FROM, e.getFromNode()->getID());
     480       119224 :     into.writeAttr(SUMO_ATTR_TO, e.getToNode()->getID());
     481       119224 :     if (!noNames && e.getStreetName() != "") {
     482        38955 :         into.writeAttr(SUMO_ATTR_NAME, StringUtils::escapeXML(e.getStreetName()));
     483              :     }
     484       119224 :     into.writeAttr(SUMO_ATTR_PRIORITY, e.getPriority());
     485       119224 :     if (e.getTypeID() != "") {
     486        86601 :         into.writeAttr(SUMO_ATTR_TYPE, e.getTypeID());
     487              :     }
     488       119224 :     if (e.getRoutingType() != "") {
     489         1916 :         into.writeAttr(SUMO_ATTR_ROUTINGTYPE, e.getRoutingType());
     490              :     }
     491       119224 :     if (e.isMacroscopicConnector()) {
     492            0 :         into.writeAttr(SUMO_ATTR_FUNCTION, SumoXMLEdgeFunc::CONNECTOR);
     493              :     }
     494              :     // write the spread type if not default ("right")
     495       119224 :     if (e.getLaneSpreadFunction() != defaultSpread) {
     496        48483 :         into.writeAttr(SUMO_ATTR_SPREADTYPE, e.getLaneSpreadFunction());
     497              :     }
     498       119224 :     if (e.hasLoadedLength()) {
     499         2173 :         into.writeAttr(SUMO_ATTR_LENGTH, e.getLoadedLength());
     500              :     }
     501       119224 :     if (!e.hasDefaultGeometry()) {
     502        63832 :         into.writeAttr(SUMO_ATTR_SHAPE, e.getGeometry());
     503              :     }
     504       119224 :     if (e.getEdgeStopOffset().isDefined()) {
     505           12 :         writeStopOffsets(into, e.getEdgeStopOffset());
     506              :     }
     507       119224 :     if (e.getBidiEdge()) {
     508         6514 :         into.writeAttr(SUMO_ATTR_BIDI, e.getBidiEdge()->getID());
     509              :     }
     510       119224 :     if (e.getDistance() != 0) {
     511         1680 :         into.writeAttr(SUMO_ATTR_DISTANCE, e.getDistance());
     512              :     }
     513              : 
     514              :     // write the lanes
     515              :     const std::vector<NBEdge::Lane>& lanes = e.getLanes();
     516              : 
     517       119224 :     double length = e.getFinalLength();
     518       119224 :     if (e.getBidiEdge() != nullptr) {
     519         6514 :         length = (length + e.getBidiEdge()->getFinalLength()) / 2;
     520              :     }
     521       119224 :     double startOffset = e.isBidiRail() ? e.getTurnDestination(true)->getEndOffset() : 0;
     522       272325 :     for (int i = 0; i < (int) lanes.size(); i++) {
     523       153101 :         const NBEdge::Lane& l = lanes[i];
     524       153101 :         StopOffset stopOffset;
     525       153101 :         if (l.laneStopOffset != e.getEdgeStopOffset()) {
     526           27 :             stopOffset = l.laneStopOffset;
     527              :         }
     528       459303 :         writeLane(into, e.getLaneID(i), l.speed, l.friction,
     529       153101 :                   l.permissions, l.preferred,
     530       153101 :                   l.changeLeft, l.changeRight,
     531       153101 :                   startOffset, l.endOffset,
     532       153101 :                   stopOffset, l.width, l.shape, &l,
     533       153101 :                   length, i, l.oppositeID, l.type, l.accelRamp, l.customShape.size() > 0);
     534              :     }
     535              :     // close the edge
     536       119224 :     e.writeParams(into);
     537       119224 :     into.closeTag();
     538       119224 : }
     539              : 
     540              : 
     541              : void
     542       268563 : NWWriter_SUMO::writeLane(OutputDevice& into, const std::string& lID,
     543              :                          double speed, double friction,
     544              :                          SVCPermissions permissions, SVCPermissions preferred,
     545              :                          SVCPermissions changeLeft, SVCPermissions changeRight,
     546              :                          double startOffset, double endOffset,
     547              :                          const StopOffset& stopOffset, double width, PositionVector shape,
     548              :                          const Parameterised* params, double length, int index,
     549              :                          const std::string& oppositeID,
     550              :                          const std::string& type,
     551              :                          bool accelRamp, bool customShape,
     552              :                          const PositionVector& outlineShape) {
     553              :     // output the lane's attributes
     554       268563 :     into.openTag(SUMO_TAG_LANE).writeAttr(SUMO_ATTR_ID, lID);
     555              :     // the first lane of an edge will be the depart lane
     556       268563 :     into.writeAttr(SUMO_ATTR_INDEX, index);
     557              :     // write the list of allowed/disallowed vehicle classes
     558       268563 :     if (permissions != SVC_UNSPECIFIED) {
     559       268563 :         writePermissions(into, permissions);
     560              :     }
     561       268563 :     writePreferences(into, preferred);
     562              :     // some further information
     563       268563 :     into.writeAttr(SUMO_ATTR_SPEED, MAX2(0.0, speed));
     564       268563 :     if (friction != NBEdge::UNSPECIFIED_FRICTION) {
     565           34 :         into.writeAttr(SUMO_ATTR_FRICTION, friction);
     566              :     }
     567       268563 :     into.writeAttr(SUMO_ATTR_LENGTH, length);
     568       268563 :     if (endOffset != NBEdge::UNSPECIFIED_OFFSET) {
     569           41 :         into.writeAttr(SUMO_ATTR_ENDOFFSET, endOffset);
     570              :     }
     571       268563 :     if (width != NBEdge::UNSPECIFIED_WIDTH) {
     572        34880 :         into.writeAttr(SUMO_ATTR_WIDTH, width);
     573              :     }
     574       268563 :     if (accelRamp) {
     575           74 :         into.writeAttr<bool>(SUMO_ATTR_ACCELERATION, accelRamp);
     576              :     }
     577       268563 :     if (customShape) {
     578           47 :         into.writeAttr(SUMO_ATTR_CUSTOMSHAPE, true);
     579              :     }
     580       268563 :     if (endOffset > 0 || startOffset > 0) {
     581           43 :         startOffset = MIN2(startOffset, shape.length() - POSITION_EPS);
     582           43 :         endOffset = MIN2(endOffset, shape.length() - startOffset - POSITION_EPS);
     583              :         assert(startOffset + endOffset < shape.length());
     584           86 :         shape = shape.getSubpart(startOffset, shape.length() - endOffset);
     585              :     }
     586       268563 :     into.writeAttr(SUMO_ATTR_SHAPE, shape);
     587       268563 :     if (type != "") {
     588          527 :         into.writeAttr(SUMO_ATTR_TYPE, type);
     589              :     }
     590       268563 :     if (changeLeft != SVC_UNSPECIFIED && changeLeft != SVCAll && changeLeft != SVC_IGNORING) {
     591          273 :         into.writeAttr(SUMO_ATTR_CHANGE_LEFT, getVehicleClassNames(changeLeft));
     592              :     }
     593       268563 :     if (changeRight != SVC_UNSPECIFIED && changeRight != SVCAll && changeRight != SVC_IGNORING) {
     594           79 :         into.writeAttr(SUMO_ATTR_CHANGE_RIGHT, getVehicleClassNames(changeRight));
     595              :     }
     596       268563 :     if (stopOffset.isDefined()) {
     597           13 :         writeStopOffsets(into, stopOffset);
     598              :     }
     599       268563 :     if (outlineShape.size() != 0) {
     600         1581 :         into.writeAttr(SUMO_ATTR_OUTLINESHAPE, outlineShape);
     601              :     }
     602              : 
     603       268563 :     if (oppositeID != "" && oppositeID != "-") {
     604           66 :         into.openTag(SUMO_TAG_NEIGH);
     605           66 :         into.writeAttr(SUMO_ATTR_LANE, oppositeID);
     606          132 :         into.closeTag();
     607              :     }
     608              : 
     609       268563 :     if (params != nullptr) {
     610       264261 :         params->writeParams(into);
     611              :     }
     612              : 
     613       268563 :     into.closeTag();
     614       268563 : }
     615              : 
     616              : 
     617              : void
     618        66049 : NWWriter_SUMO::writeJunction(OutputDevice& into, const NBNode& n) {
     619              :     // write the attributes
     620        66049 :     into.openTag(SUMO_TAG_JUNCTION).writeAttr(SUMO_ATTR_ID, n.getID());
     621        66049 :     into.writeAttr(SUMO_ATTR_TYPE, n.getType());
     622        66049 :     NWFrame::writePositionLong(n.getPosition(), into);
     623              :     // write the incoming lanes
     624              :     std::vector<std::string> incLanes;
     625              :     const std::vector<NBEdge*>& incoming = n.getIncomingEdges();
     626       185273 :     for (std::vector<NBEdge*>::const_iterator i = incoming.begin(); i != incoming.end(); ++i) {
     627       119224 :         int noLanes = (*i)->getNumLanes();
     628       272325 :         for (int j = 0; j < noLanes; j++) {
     629       306202 :             incLanes.push_back((*i)->getLaneID(j));
     630              :         }
     631              :     }
     632        66049 :     std::vector<NBNode::Crossing*> crossings = n.getCrossings();
     633              :     std::set<std::string> prevWAs;
     634              :     // avoid duplicates
     635        67630 :     for (auto c : crossings) {
     636         1581 :         if (prevWAs.count(c->prevWalkingArea) == 0) {
     637         3142 :             incLanes.push_back(c->prevWalkingArea + "_0");
     638              :             prevWAs.insert(c->prevWalkingArea);
     639              :         }
     640              :     }
     641        66049 :     into.writeAttr(SUMO_ATTR_INCLANES, incLanes);
     642              :     // write the internal lanes
     643              :     std::vector<std::string> intLanes;
     644       132098 :     if (!OptionsCont::getOptions().getBool("no-internal-links")) {
     645        75240 :         for (EdgeVector::const_iterator i = incoming.begin(); i != incoming.end(); i++) {
     646        47505 :             const std::vector<NBEdge::Connection>& elv = (*i)->getConnections();
     647       139790 :             for (std::vector<NBEdge::Connection>::const_iterator k = elv.begin(); k != elv.end(); ++k) {
     648        92285 :                 if ((*k).toEdge == nullptr) {
     649            0 :                     continue;
     650              :                 }
     651        92285 :                 if (!(*k).haveVia) {
     652       149982 :                     intLanes.push_back((*k).getInternalLaneID());
     653              :                 } else {
     654        34588 :                     intLanes.push_back((*k).getInternalViaLaneID());
     655              :                 }
     656              :             }
     657              :         }
     658              :     }
     659        66049 :     if (n.getType() != SumoXMLNodeType::DEAD_END && n.getType() != SumoXMLNodeType::NOJUNCTION) {
     660        52027 :         for (auto c : crossings) {
     661         3146 :             intLanes.push_back(c->id + "_0");
     662              :         }
     663              :     }
     664        66049 :     into.writeAttr(SUMO_ATTR_INTLANES, intLanes);
     665              :     // close writing
     666        66049 :     into.writeAttr(SUMO_ATTR_SHAPE, n.getShape().simplified());
     667              :     // write optional radius
     668        66049 :     if (n.getRadius() != NBNode::UNSPECIFIED_RADIUS) {
     669           83 :         into.writeAttr(SUMO_ATTR_RADIUS, n.getRadius());
     670              :     }
     671              :     // specify whether a custom shape was used
     672        66049 :     if (n.hasCustomShape()) {
     673           42 :         into.writeAttr(SUMO_ATTR_CUSTOMSHAPE, true);
     674              :     }
     675        66049 :     if (n.getRightOfWay() != RightOfWay::DEFAULT) {
     676           33 :         into.writeAttr<std::string>(SUMO_ATTR_RIGHT_OF_WAY, toString(n.getRightOfWay()));
     677              :     }
     678        66049 :     if (n.getFringeType() != FringeType::DEFAULT) {
     679          338 :         into.writeAttr<std::string>(SUMO_ATTR_FRINGE, toString(n.getFringeType()));
     680              :     }
     681        66049 :     if (n.getRoundaboutType() != RoundaboutType::DEFAULT) {
     682            1 :         into.writeAttr<std::string>(SUMO_ATTR_ROUNDABOUT, toString(n.getRoundaboutType()));
     683              :     }
     684        66049 :     if (n.getName() != "") {
     685            4 :         into.writeAttr<std::string>(SUMO_ATTR_NAME, StringUtils::escapeXML(n.getName()));
     686              :     }
     687        66049 :     if (n.getType() != SumoXMLNodeType::DEAD_END) {
     688              :         // write right-of-way logics
     689        50571 :         n.writeLogic(into);
     690              :     }
     691        66049 :     n.writeParams(into);
     692        66049 :     into.closeTag();
     693       132098 : }
     694              : 
     695              : 
     696              : bool
     697        27735 : NWWriter_SUMO::writeInternalNodes(OutputDevice& into, const NBNode& n) {
     698              :     bool ret = false;
     699              :     const std::vector<NBEdge*>& incoming = n.getIncomingEdges();
     700              :     // build the list of internal lane ids
     701              :     std::vector<std::string> internalLaneIDs;
     702              :     std::map<std::string, std::string> viaIDs;
     703        75240 :     for (const NBEdge* in : incoming) {
     704       139790 :         for (const auto& con : in->getConnections()) {
     705        92285 :             if (con.toEdge != nullptr) {
     706        92285 :                 internalLaneIDs.push_back(con.getInternalLaneID());
     707        92285 :                 if (con.viaID != "") {
     708        34588 :                     viaIDs[con.getInternalLaneID()] = (con.getInternalViaLaneID());
     709              :                 }
     710              :             }
     711              :         }
     712              :     }
     713        29316 :     for (auto c : n.getCrossings()) {
     714         3162 :         internalLaneIDs.push_back(c->id + "_0");
     715        27735 :     }
     716              :     // write the internal nodes
     717        75240 :     for (const NBEdge* in : incoming) {
     718       139790 :         for (const auto& con : in->getConnections()) {
     719        92285 :             if (con.toEdge == nullptr || !con.haveVia) {
     720        74991 :                 continue;
     721              :             }
     722        17294 :             Position pos = con.shape[-1];
     723        17294 :             into.openTag(SUMO_TAG_JUNCTION).writeAttr(SUMO_ATTR_ID, con.getInternalViaLaneID());
     724        17294 :             into.writeAttr(SUMO_ATTR_TYPE, SumoXMLNodeType::INTERNAL);
     725        17294 :             NWFrame::writePositionLong(pos, into);
     726        17294 :             std::string incLanes = con.getInternalLaneID();
     727              :             std::vector<std::string> foeIDs;
     728        52499 :             for (std::string incLane : con.foeIncomingLanes) {
     729        35205 :                 if (incLane[0] == ':') {
     730              :                     // intersecting left turns
     731          160 :                     const int index = StringUtils::toInt(incLane.substr(1));
     732          160 :                     incLane = internalLaneIDs[index];
     733          160 :                     if (viaIDs[incLane] != "") {
     734          160 :                         foeIDs.push_back(viaIDs[incLane]);
     735              :                     }
     736              :                 }
     737        70410 :                 incLanes += " " + incLane;
     738              :             }
     739        17294 :             into.writeAttr(SUMO_ATTR_INCLANES, incLanes);
     740              :             const std::vector<int>& foes = con.foeInternalLinks;
     741       103260 :             for (int foe : foes) {
     742        85966 :                 foeIDs.push_back(internalLaneIDs[foe]);
     743              :             }
     744        17294 :             into.writeAttr(SUMO_ATTR_INTLANES, joinToString(foeIDs, " "));
     745        17294 :             into.closeTag();
     746              :             ret = true;
     747        17294 :         }
     748              :     }
     749        27735 :     return ret;
     750        27735 : }
     751              : 
     752              : 
     753              : void
     754       251819 : NWWriter_SUMO::writeConnection(OutputDevice& into, const NBEdge& from, const NBEdge::Connection& c,
     755              :                                bool includeInternal, ConnectionStyle style, bool useGeo, bool geoAccuracy) {
     756              :     assert(c.toEdge != nullptr);
     757       251819 :     into.openTag(SUMO_TAG_CONNECTION);
     758       251819 :     into.writeAttr(SUMO_ATTR_FROM, from.getID());
     759       251819 :     into.writeAttr(SUMO_ATTR_TO, c.toEdge->getID());
     760       251819 :     into.writeAttr(SUMO_ATTR_FROM_LANE, c.fromLane);
     761       251819 :     into.writeAttr(SUMO_ATTR_TO_LANE, c.toLane);
     762       251819 :     if (style != TLL) {
     763       249550 :         into.writeOptionalAttr(SUMO_ATTR_PASS, c.mayDefinitelyPass, !c.mayDefinitelyPass);
     764       249550 :         into.writeOptionalAttr<bool>(SUMO_ATTR_KEEP_CLEAR, false, c.keepClear != KEEPCLEAR_FALSE);
     765       249550 :         into.writeOptionalAttr(SUMO_ATTR_CONTPOS, c.contPos, c.contPos == NBEdge::UNSPECIFIED_CONTPOS);
     766       249550 :         writePermissions(into, c.permissions);
     767       249550 :         const bool changeLeft = (c.changeLeft != SVC_UNSPECIFIED && c.changeLeft != SVCAll && c.changeLeft != SVC_IGNORING);
     768       249550 :         into.writeOptionalAttr(SUMO_ATTR_CHANGE_LEFT, getVehicleClassNames(c.changeLeft), !changeLeft);
     769       249550 :         const bool changeRight = (c.changeRight != SVC_UNSPECIFIED && c.changeRight != SVCAll && c.changeRight != SVC_IGNORING);
     770       249550 :         into.writeOptionalAttr(SUMO_ATTR_CHANGE_RIGHT, getVehicleClassNames(c.changeRight), !changeRight);
     771       249550 :         into.writeOptionalAttr(SUMO_ATTR_SPEED, c.speed, c.speed == NBEdge::UNSPECIFIED_SPEED);
     772       499100 :         into.writeOptionalAttr(SUMO_ATTR_LENGTH, c.customLength, c.customLength == NBEdge::UNSPECIFIED_LOADED_LENGTH);
     773       249550 :         writeShape(into, GeoConvHelper::getFinal(), c.customShape, SUMO_ATTR_SHAPE, useGeo, geoAccuracy, c.customShape.size() != 0);
     774       249550 :         into.writeOptionalAttr(SUMO_ATTR_UNCONTROLLED, c.uncontrolled, !c.uncontrolled);
     775       249550 :         into.writeOptionalAttr(SUMO_ATTR_INDIRECT, c.indirectLeft, !c.indirectLeft);
     776       249550 :         into.writeOptionalAttr(SUMO_ATTR_TYPE, c.edgeType, c.edgeType == "");
     777              :     }
     778       249550 :     if (style != PLAIN) {
     779       237913 :         if (includeInternal) {
     780        92285 :             into.writeAttr(SUMO_ATTR_VIA, c.getInternalLaneID());
     781              :         }
     782              :         // set information about the controlling tl if any
     783       237913 :         if (c.tlID != "") {
     784        29010 :             into.writeAttr(SUMO_ATTR_TLID, c.tlID);
     785        29010 :             into.writeAttr(SUMO_ATTR_TLLINKINDEX, c.tlLinkIndex);
     786        29010 :             if (c.tlLinkIndex2 >= 0) {
     787           13 :                 into.writeAttr(SUMO_ATTR_TLLINKINDEX2, c.tlLinkIndex2);
     788              :             }
     789              :         }
     790              :     }
     791       237913 :     if (style != TLL) {
     792       249550 :         if (style == SUMONET) {
     793              :             // write the direction information
     794       235644 :             LinkDirection dir = from.getToNode()->getDirection(&from, c.toEdge, OptionsCont::getOptions().getBool("lefthand"));
     795              :             assert(dir != LinkDirection::NODIR);
     796       235644 :             into.writeAttr(SUMO_ATTR_DIR, toString(dir));
     797              :             // write the state information
     798       235644 :             const LinkState linkState = from.getToNode()->getLinkState(
     799       235644 :                                             &from, c.toEdge, c.fromLane, c.toLane, c.mayDefinitelyPass, c.tlID);
     800       235644 :             into.writeAttr(SUMO_ATTR_STATE, linkState);
     801              :             if (linkState == LINKSTATE_MINOR
     802        80579 :                     && c.visibility == NBEdge::UNSPECIFIED_VISIBILITY_DISTANCE
     803       316139 :                     && c.toEdge->getJunctionPriority(c.toEdge->getToNode()) == NBEdge::JunctionPriority::ROUNDABOUT) {
     804          200 :                 const double visibilityDistance = OptionsCont::getOptions().getFloat("roundabouts.visibility-distance");
     805          200 :                 if (visibilityDistance != NBEdge::UNSPECIFIED_VISIBILITY_DISTANCE) {
     806          197 :                     into.writeAttr(SUMO_ATTR_VISIBILITY_DISTANCE, visibilityDistance);
     807              :                 }
     808              :             }
     809              :         }
     810       249550 :         if (c.visibility != NBEdge::UNSPECIFIED_VISIBILITY_DISTANCE) {
     811          102 :             into.writeAttr(SUMO_ATTR_VISIBILITY_DISTANCE, c.visibility);
     812              :         }
     813              :     }
     814       251819 :     c.writeParams(into);
     815       251819 :     into.closeTag();
     816       251819 : }
     817              : 
     818              : 
     819              : bool
     820        27735 : NWWriter_SUMO::writeInternalConnections(OutputDevice& into, const NBNode& n) {
     821              :     bool ret = false;
     822        55470 :     const bool lefthand = OptionsCont::getOptions().getBool("lefthand");
     823        75240 :     for (const NBEdge* const from : n.getIncomingEdges()) {
     824       139790 :         for (const NBEdge::Connection& c : from->getConnections()) {
     825        92285 :             LinkDirection dir = n.getDirection(from, c.toEdge, lefthand);
     826              :             assert(c.toEdge != 0);
     827        92285 :             if (c.haveVia) {
     828              :                 // internal split with optional signal
     829        17294 :                 std::string tlID = "";
     830        17294 :                 int linkIndex2 = NBConnection::InvalidTlIndex;
     831        17294 :                 if (c.tlLinkIndex2 != NBConnection::InvalidTlIndex) {
     832              :                     linkIndex2 = c.tlLinkIndex2;
     833            9 :                     tlID = c.tlID;
     834              :                 }
     835        17294 :                 writeInternalConnection(into, c.id, c.toEdge->getID(), c.internalLaneIndex, c.toLane, c.getInternalViaLaneID(), dir, tlID, linkIndex2, false, c.visibility);
     836        34588 :                 writeInternalConnection(into, c.viaID, c.toEdge->getID(), c.internalViaLaneIndex, c.toLane, "", dir, "", NBConnection::InvalidTlIndex,
     837        17294 :                                         n.brakeForCrossingOnExit(c.toEdge, dir, c.indirectLeft));
     838              :             } else {
     839              :                 // no internal split
     840       149982 :                 writeInternalConnection(into, c.id, c.toEdge->getID(), c.internalLaneIndex, c.toLane, "", dir);
     841              :             }
     842              :             ret = true;
     843              :         }
     844              :     }
     845        27735 :     return ret;
     846              : }
     847              : 
     848              : 
     849              : void
     850       121622 : NWWriter_SUMO::writeInternalConnection(OutputDevice& into,
     851              :                                        const std::string& from, const std::string& to,
     852              :                                        int fromLane, int toLane, const std::string& via,
     853              :                                        LinkDirection dir,
     854              :                                        const std::string& tlID, int linkIndex,
     855              :                                        bool minor,
     856              :                                        double visibility) {
     857       121622 :     into.openTag(SUMO_TAG_CONNECTION);
     858       121622 :     into.writeAttr(SUMO_ATTR_FROM, from);
     859       121622 :     into.writeAttr(SUMO_ATTR_TO, to);
     860       121622 :     into.writeAttr(SUMO_ATTR_FROM_LANE, fromLane);
     861       121622 :     into.writeAttr(SUMO_ATTR_TO_LANE, toLane);
     862       121622 :     if (via != "") {
     863        17294 :         into.writeAttr(SUMO_ATTR_VIA, via);
     864              :     }
     865       121622 :     if (tlID != "" && linkIndex != NBConnection::InvalidTlIndex) {
     866              :         // used for the reverse direction of pedestrian crossings
     867           38 :         into.writeAttr(SUMO_ATTR_TLID, tlID);
     868           38 :         into.writeAttr(SUMO_ATTR_TLLINKINDEX, linkIndex);
     869              :     }
     870       121622 :     into.writeAttr(SUMO_ATTR_DIR, dir);
     871       140331 :     into.writeAttr(SUMO_ATTR_STATE, ((via != "" || minor) ? "m" : "M"));
     872       121622 :     if (visibility != NBEdge::UNSPECIFIED_VISIBILITY_DISTANCE) {
     873            1 :         into.writeAttr(SUMO_ATTR_VISIBILITY_DISTANCE, visibility);
     874              :     }
     875       121622 :     into.closeTag();
     876       121622 : }
     877              : 
     878              : 
     879              : void
     880         2151 : NWWriter_SUMO::writeRoundabouts(OutputDevice& into, const std::set<EdgeSet>& roundabouts,
     881              :                                 const NBEdgeCont& ec) {
     882              :     //  make output deterministic
     883              :     std::vector<std::vector<std::string> > edgeIDs;
     884         2276 :     for (std::set<EdgeSet>::const_iterator i = roundabouts.begin(); i != roundabouts.end(); ++i) {
     885              :         std::vector<std::string> tEdgeIDs;
     886          730 :         for (EdgeSet::const_iterator j = (*i).begin(); j != (*i).end(); ++j) {
     887              :             // the edges may have been erased from NBEdgeCont but their pointers are still valid
     888              :             // we verify their existance in writeRoundabout()
     889          605 :             tEdgeIDs.push_back((*j)->getID());
     890              :         }
     891          125 :         std::sort(tEdgeIDs.begin(), tEdgeIDs.end());
     892          125 :         edgeIDs.push_back(tEdgeIDs);
     893          125 :     }
     894         2151 :     std::sort(edgeIDs.begin(), edgeIDs.end());
     895              :     //  write
     896         2276 :     for (std::vector<std::vector<std::string> >::const_iterator i = edgeIDs.begin(); i != edgeIDs.end(); ++i) {
     897          125 :         writeRoundabout(into, *i, ec);
     898              :     }
     899         2151 :     if (roundabouts.size() != 0) {
     900           90 :         into.lf();
     901              :     }
     902         2151 : }
     903              : 
     904              : 
     905              : void
     906          125 : NWWriter_SUMO::writeRoundabout(OutputDevice& into, const std::vector<std::string>& edgeIDs,
     907              :                                const NBEdgeCont& ec) {
     908              :     std::vector<std::string> validEdgeIDs;
     909              :     std::vector<std::string> invalidEdgeIDs;
     910              :     std::vector<std::string> nodeIDs;
     911          730 :     for (std::vector<std::string>::const_iterator i = edgeIDs.begin(); i != edgeIDs.end(); ++i) {
     912          605 :         const NBEdge* edge = ec.retrieve(*i);
     913          605 :         if (edge != nullptr) {
     914          602 :             nodeIDs.push_back(edge->getToNode()->getID());
     915          602 :             validEdgeIDs.push_back(edge->getID());
     916              :         } else {
     917            3 :             invalidEdgeIDs.push_back(*i);
     918              :         }
     919              :     }
     920          125 :     std::sort(nodeIDs.begin(), nodeIDs.end());
     921          125 :     if (validEdgeIDs.size() > 0) {
     922          125 :         into.openTag(SUMO_TAG_ROUNDABOUT);
     923          125 :         into.writeAttr(SUMO_ATTR_NODES, joinToString(nodeIDs, " "));
     924          125 :         into.writeAttr(SUMO_ATTR_EDGES, joinToString(validEdgeIDs, " "));
     925          250 :         into.closeTag();
     926          125 :         if (invalidEdgeIDs.size() > 0) {
     927            6 :             WRITE_WARNING("Writing incomplete roundabout. Edges: '"
     928              :                           + joinToString(invalidEdgeIDs, " ") + "' no longer exist'");
     929              :         }
     930              :     }
     931          125 : }
     932              : 
     933              : 
     934              : void
     935           71 : NWWriter_SUMO::writeDistrict(OutputDevice& into, const NBDistrict& d) {
     936           71 :     std::vector<double> sourceW = d.getSourceWeights();
     937           71 :     VectorHelper<double>::normaliseSum(sourceW, 1.0);
     938           71 :     std::vector<double> sinkW = d.getSinkWeights();
     939           71 :     VectorHelper<double>::normaliseSum(sinkW, 1.0);
     940              :     // write the head and the id of the district
     941           71 :     into.openTag(SUMO_TAG_TAZ).writeAttr(SUMO_ATTR_ID, d.getID());
     942           71 :     if (d.getShape().size() > 0) {
     943           43 :         into.writeAttr(SUMO_ATTR_SHAPE, d.getShape());
     944              :     }
     945              :     // write all sources
     946              :     const std::vector<NBEdge*>& sources = d.getSourceEdges();
     947           71 :     for (int i = 0; i < (int)sources.size(); i++) {
     948              :         // write the head and the id of the source
     949            0 :         into.openTag(SUMO_TAG_TAZSOURCE).writeAttr(SUMO_ATTR_ID, sources[i]->getID()).writeAttr(SUMO_ATTR_WEIGHT, sourceW[i]);
     950            0 :         into.closeTag();
     951              :     }
     952              :     // write all sinks
     953              :     const std::vector<NBEdge*>& sinks = d.getSinkEdges();
     954           71 :     for (int i = 0; i < (int)sinks.size(); i++) {
     955              :         // write the head and the id of the sink
     956            0 :         into.openTag(SUMO_TAG_TAZSINK).writeAttr(SUMO_ATTR_ID, sinks[i]->getID()).writeAttr(SUMO_ATTR_WEIGHT, sinkW[i]);
     957            0 :         into.closeTag();
     958              :     }
     959              :     // write the tail
     960           71 :     into.closeTag();
     961           71 : }
     962              : 
     963              : 
     964              : std::string
     965       112789 : NWWriter_SUMO::writeSUMOTime(SUMOTime steps) {
     966       112789 :     double time = STEPS2TIME(steps);
     967       112789 :     if (time == std::floor(time)) {
     968        24784 :         return toString(int(time));
     969              :     } else {
     970        88005 :         return toString(time);
     971              :     }
     972              : }
     973              : 
     974              : void
     975        70767 : NWWriter_SUMO::writeProhibitions(OutputDevice& into, const NBConnectionProhibits& prohibitions, const NBEdgeCont& ec) {
     976              :     // the edges may have been erased from NBEdgeCont but their pointers are still valid so we need to check
     977        70869 :     for (NBConnectionProhibits::const_iterator j = prohibitions.begin(); j != prohibitions.end(); j++) {
     978          102 :         NBConnection prohibited = (*j).first;
     979          102 :         if (ec.retrieve(prohibited.getFrom()->getID()) == nullptr
     980          102 :                 || ec.retrieve(prohibited.getTo()->getID()) == nullptr) {
     981              :             continue;
     982              :         }
     983              :         const NBConnectionVector& prohibiting = (*j).second;
     984          301 :         for (NBConnectionVector::const_iterator k = prohibiting.begin(); k != prohibiting.end(); k++) {
     985          199 :             NBConnection prohibitor = *k;
     986          199 :             if (ec.retrieve(prohibitor.getFrom()->getID()) == nullptr
     987          199 :                     || ec.retrieve(prohibitor.getTo()->getID()) == nullptr) {
     988              :                 continue;
     989              :             }
     990          199 :             into.openTag(SUMO_TAG_PROHIBITION);
     991          199 :             into.writeAttr(SUMO_ATTR_PROHIBITOR, prohibitionConnection(prohibitor));
     992          199 :             into.writeAttr(SUMO_ATTR_PROHIBITED, prohibitionConnection(prohibited));
     993          199 :             into.closeTag();
     994          199 :         }
     995          102 :     }
     996        70767 : }
     997              : 
     998              : 
     999              : std::string
    1000          398 : NWWriter_SUMO::prohibitionConnection(const NBConnection& c) {
    1001          796 :     return c.getFrom()->getID() + "->" + c.getTo()->getID();
    1002              : }
    1003              : 
    1004              : 
    1005              : void
    1006         2326 : NWWriter_SUMO::writeTrafficLights(OutputDevice& into, const NBTrafficLightLogicCont& tllCont) {
    1007         2326 :     std::vector<NBTrafficLightLogic*> logics = tllCont.getComputed();
    1008         5069 :     for (NBTrafficLightLogic* logic : logics) {
    1009         2743 :         writeTrafficLight(into, logic);
    1010              :         // only raise warnings on write instead of on compute (to avoid cluttering netedit)
    1011         2743 :         NBTrafficLightDefinition* def = tllCont.getDefinition(logic->getID(), logic->getProgramID());
    1012              :         assert(def != nullptr);
    1013         2743 :         def->finalChecks();
    1014              :     }
    1015         2326 :     if (logics.size() > 0) {
    1016          735 :         into.lf();
    1017              :     }
    1018         2326 : }
    1019              : 
    1020              : 
    1021              : void
    1022         2743 : NWWriter_SUMO::writeTrafficLight(OutputDevice& into, const NBTrafficLightLogic* logic) {
    1023         2743 :     into.openTag(SUMO_TAG_TLLOGIC);
    1024         2743 :     into.writeAttr(SUMO_ATTR_ID, logic->getID());
    1025         2743 :     into.writeAttr(SUMO_ATTR_TYPE, logic->getType());
    1026         2743 :     into.writeAttr(SUMO_ATTR_PROGRAMID, logic->getProgramID());
    1027         2743 :     into.writeAttr(SUMO_ATTR_OFFSET, logic->getOffset() == SUMOTime_MAX ? "begin" : writeSUMOTime(logic->getOffset()));
    1028              :     // write the phases
    1029              :     const bool varPhaseLength = logic->getType() != TrafficLightType::STATIC;
    1030        16499 :     for (const NBTrafficLightLogic::PhaseDefinition& phase : logic->getPhases()) {
    1031        13756 :         into.openTag(SUMO_TAG_PHASE);
    1032        13756 :         into.writeAttr(SUMO_ATTR_DURATION, writeSUMOTime(phase.duration));
    1033        13756 :         if (phase.duration < TIME2STEPS(10)) {
    1034        17074 :             into.writePadding(" ");
    1035              :         }
    1036        13756 :         into.writeAttr(SUMO_ATTR_STATE, phase.state);
    1037        38674 :         into.writeOptionalAttr(SUMO_ATTR_MINDURATION, writeSUMOTime(phase.minDur), !varPhaseLength || phase.minDur == NBTrafficLightDefinition::UNSPECIFIED_DURATION);
    1038        38674 :         into.writeOptionalAttr(SUMO_ATTR_MAXDURATION, writeSUMOTime(phase.maxDur), !varPhaseLength || phase.maxDur == NBTrafficLightDefinition::UNSPECIFIED_DURATION);
    1039        41267 :         into.writeOptionalAttr(SUMO_ATTR_EARLIEST_END, writeSUMOTime(phase.earliestEnd), !varPhaseLength || phase.earliestEnd == NBTrafficLightDefinition::UNSPECIFIED_DURATION);
    1040        41267 :         into.writeOptionalAttr(SUMO_ATTR_LATEST_END, writeSUMOTime(phase.latestEnd), !varPhaseLength || phase.latestEnd == NBTrafficLightDefinition::UNSPECIFIED_DURATION);
    1041              :         // NEMA attributes
    1042        41179 :         into.writeOptionalAttr(SUMO_ATTR_VEHICLEEXTENSION, writeSUMOTime(phase.vehExt), !varPhaseLength || phase.vehExt == NBTrafficLightDefinition::UNSPECIFIED_DURATION);
    1043        41179 :         into.writeOptionalAttr(SUMO_ATTR_YELLOW, writeSUMOTime(phase.yellow), !varPhaseLength || phase.yellow == NBTrafficLightDefinition::UNSPECIFIED_DURATION);
    1044        41179 :         into.writeOptionalAttr(SUMO_ATTR_RED, writeSUMOTime(phase.red), !varPhaseLength || phase.red == NBTrafficLightDefinition::UNSPECIFIED_DURATION);
    1045              : 
    1046        27512 :         into.writeOptionalAttr(SUMO_ATTR_NAME, phase.name, phase.name == "", true);
    1047        13756 :         into.writeOptionalAttr(SUMO_ATTR_NEXT, phase.next, phase.next.empty());
    1048        27512 :         into.closeTag();
    1049              :     }
    1050              :     // write params
    1051         2743 :     logic->writeParams(into);
    1052         2743 :     into.closeTag();
    1053         2743 : }
    1054              : 
    1055              : 
    1056              : void
    1057         9023 : NWWriter_SUMO::writeStopOffsets(OutputDevice& into, const StopOffset& stopOffset) {
    1058         9023 :     if (stopOffset.isDefined()) {
    1059           25 :         const std::string ss_vclasses = getVehicleClassNames(stopOffset.getPermissions());
    1060           25 :         if (ss_vclasses.length() == 0) {
    1061              :             // This stopOffset would have no effect...
    1062              :             return;
    1063              :         }
    1064           22 :         into.openTag(SUMO_TAG_STOPOFFSET);
    1065           22 :         const std::string ss_exceptions = getVehicleClassNames(~stopOffset.getPermissions());
    1066           22 :         if (ss_vclasses.length() <= ss_exceptions.length()) {
    1067            8 :             into.writeAttr(SUMO_ATTR_VCLASSES, ss_vclasses);
    1068              :         } else {
    1069           14 :             if (ss_exceptions.length() == 0) {
    1070            4 :                 into.writeAttr(SUMO_ATTR_VCLASSES, "all");
    1071              :             } else {
    1072           10 :                 into.writeAttr(SUMO_ATTR_EXCEPTIONS, ss_exceptions);
    1073              :             }
    1074              :         }
    1075           22 :         into.writeAttr(SUMO_ATTR_VALUE, stopOffset.getOffset());
    1076           44 :         into.closeTag();
    1077              :     }
    1078              : }
    1079              : 
    1080              : 
    1081              : void
    1082       274095 : NWWriter_SUMO::writeShape(OutputDevice& out, const GeoConvHelper& gch, PositionVector shape, SumoXMLAttr attr, bool useGeo, bool geoAccuracy, bool needsWrite) {
    1083       274095 :     if (!needsWrite) {
    1084       268812 :         out.writeOptionalAttr(attr, shape, true);
    1085       268812 :         return;
    1086              :     }
    1087         5283 :     if (useGeo) {
    1088          326 :         for (int i = 0; i < (int) shape.size(); i++) {
    1089          262 :             gch.cartesian2geo(shape[i]);
    1090              :         }
    1091              :     }
    1092         5283 :     if (geoAccuracy) {
    1093           64 :         out.setPrecision(gPrecisionGeo);
    1094              :     }
    1095         5283 :     out.writeAttr(attr, shape);
    1096         5283 :     if (geoAccuracy) {
    1097           64 :         out.setPrecision();
    1098              :     }
    1099              : }
    1100              : 
    1101              : 
    1102              : /****************************************************************************/
        

Generated by: LCOV version 2.0-1