1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
10 /// @file NBPTLine.cpp
11 /// @author Gregor Laemmel
12 /// @author Nikita Cherednychek
13 /// @date Tue, 20 Mar 2017
14 /// @version $Id$
15 ///
16 // The representation of one direction of a single pt line
17 /****************************************************************************/
18 #include <utils/iodevices/OutputDevice.h>
19
20 #include <utility>
21 #include <utils/common/ToString.h>
22 #include <utils/common/StringUtils.h>
23 #include "NBEdgeCont.h"
24 #include "NBPTLine.h"
25 #include "NBPTStop.h"
26
NBPTLine(const std::string & id,const std::string & name,const std::string & type,const std::string & ref,int interval,const std::string & nightService)27 NBPTLine::NBPTLine(const std::string& id, const std::string& name, const std::string& type, const std::string& ref, int interval, const std::string& nightService) :
28 myName(name),
29 myType(type),
30 myPTLineId(id),
31 myRef(ref != "" ? ref : name),
32 myInterval(interval),
33 myNightService(nightService) {
34 }
35
addPTStop(NBPTStop * pStop)36 void NBPTLine::addPTStop(NBPTStop* pStop) {
37 myPTStops.push_back(pStop);
38
39 }
40
getStops()41 std::vector<NBPTStop*> NBPTLine::getStops() {
42 return myPTStops;
43 }
write(OutputDevice & device,NBEdgeCont & ec)44 void NBPTLine::write(OutputDevice& device, NBEdgeCont& ec) {
45 device.openTag(SUMO_TAG_PT_LINE);
46 device.writeAttr(SUMO_ATTR_ID, myPTLineId);
47 if (!myName.empty()) {
48 device.writeAttr(SUMO_ATTR_NAME, StringUtils::escapeXML(myName));
49 }
50
51 device.writeAttr(SUMO_ATTR_LINE, StringUtils::escapeXML(myRef));
52 device.writeAttr(SUMO_ATTR_TYPE, myType);
53 if (myInterval > 0) {
54 // write seconds
55 device.writeAttr(SUMO_ATTR_PERIOD, 60 * myInterval);
56 }
57 if (myNightService != "") {
58 device.writeAttr("nightService", myNightService);
59 }
60 device.writeAttr("completeness", toString((double)myPTStops.size() / (double)myNumOfStops));
61
62 std::vector<std::string> validEdgeIDs;
63 // filter out edges that have been removed due to joining junctions
64 // (therest of the route is valid)
65 for (NBEdge* e : myRoute) {
66 if (ec.retrieve(e->getID())) {
67 validEdgeIDs.push_back(e->getID());
68 }
69 }
70 if (!myRoute.empty()) {
71 device.openTag(SUMO_TAG_ROUTE);
72 device.writeAttr(SUMO_ATTR_EDGES, validEdgeIDs);
73 device.closeTag();
74 }
75
76 for (auto& myPTStop : myPTStops) {
77 device.openTag(SUMO_TAG_BUS_STOP);
78 device.writeAttr(SUMO_ATTR_ID, myPTStop->getID());
79 device.writeAttr(SUMO_ATTR_NAME, StringUtils::escapeXML(myPTStop->getName()));
80 device.closeTag();
81 }
82 // device.writeAttr(SUMO_ATTR_LANE, myLaneId);
83 // device.writeAttr(SUMO_ATTR_STARTPOS, myStartPos);
84 // device.writeAttr(SUMO_ATTR_ENDPOS, myEndPos);
85 // device.writeAttr(SUMO_ATTR_FRIENDLY_POS, "true");
86 device.closeTag();
87
88 }
89
addWayNode(long long int way,long long int node)90 void NBPTLine::addWayNode(long long int way, long long int node) {
91 std::string wayStr = toString(way);
92 if (wayStr != myCurrentWay) {
93 myCurrentWay = wayStr;
94 myWays.push_back(wayStr);
95 }
96 myWaysNodes[wayStr].push_back(node);
97
98 }
getMyWays() const99 const std::vector<std::string>& NBPTLine::getMyWays() const {
100 return myWays;
101 }
getWaysNodes(std::string wayId)102 std::vector<long long int>* NBPTLine::getWaysNodes(std::string wayId) {
103 if (myWaysNodes.find(wayId) != myWaysNodes.end()) {
104 return &myWaysNodes[wayId];
105 }
106 return nullptr;
107 }
108
addEdgeVector(std::vector<NBEdge * >::iterator fr,std::vector<NBEdge * >::iterator to)109 void NBPTLine::addEdgeVector(std::vector<NBEdge*>::iterator fr, std::vector<NBEdge*>::iterator to) {
110 myRoute.insert(myRoute.end(), fr, to);
111
112 }
setMyNumOfStops(int numStops)113 void NBPTLine::setMyNumOfStops(int numStops) {
114 myNumOfStops = numStops;
115 }
getRoute() const116 const std::vector<NBEdge*>& NBPTLine::getRoute() const {
117 return myRoute;
118 }
119