Skip to content

Commit

Permalink
add: class implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
vijay-jaisankar committed Apr 24, 2022
1 parent 9944677 commit dea0ad1
Show file tree
Hide file tree
Showing 3 changed files with 199 additions and 0 deletions.
97 changes: 97 additions & 0 deletions trafficinterventions/ChangeEdges.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import xml.etree.ElementTree as ET

"""
@Class : To perform edge change interventions
"""

class ChangeEdges():
def __init__(self, fileName):
self._fileName = fileName
self._tree = ET.parse(self._fileName)
self._root = self._tree.getroot()

"""
Get Head of XML Tree
"""
def getRootElementTag(self):
return self._root.tag

"""
Get different attribute types
"""
def getUniqueParentTags(self):
d = {}
for child in self._root:
if (child.tag) not in d:
d[child.tag] = 1
else:
d[child.tag] += 1

return d.keys()

"""
Get all Edge IDs
"""
def getUniqueEdgeIDs(self):
l = []
for child in self._root:
if child.tag == "edge":
l.append(child.attrib["id"])

return list(set(l))

"""
Get Edge Information
"""
def getEdgeInformation(self, edgeIdList):
for child in self._root:
if child.tag == "edge":
if child.attrib["id"] in edgeIdList:
for lanes in child:
print(lanes.attrib)

"""
Disallow certain vehicle types at edges
@Link: https://sumo.dlr.de/docs/Vehicle_Type_Parameter_Defaults.html
"""
def disallowAppendTypes(self, vehicleTypes, edgeIdList, newFileName = None):
for child in self._root:
if child.tag == "edge":
if child.attrib["id"] in edgeIdList:
for lanes in child:
attributesDict = lanes.attrib
if "disallow" in attributesDict:
s = attributesDict["disallow"]
for v in vehicleTypes:
if v not in s:
s += " "
s += str(v)
lanes.set("disallow", s)

if newFileName is None:
self._tree.write(self._fileName)
else:
self._tree.write(str(newFileName))

"""
Allow certain vehicle types at edges
@Link: https://sumo.dlr.de/docs/Vehicle_Type_Parameter_Defaults.html
"""
def allowAppendTypes(self, vehicleTypes, edgeIdList, newFileName = None):
for child in self._root:
if child.tag == "edge":
if child.attrib["id"] in edgeIdList:
for lanes in child:
attributesDict = lanes.attrib
if "allow" in attributesDict:
s = attributesDict["allow"]
for v in vehicleTypes:
if v not in s:
s += " "
s += str(v)
lanes.set("allow", s)

if newFileName is None:
self._tree.write(self._fileName)
else:
self._tree.write(str(newFileName))
100 changes: 100 additions & 0 deletions trafficinterventions/ChangeLanes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import xml.etree.ElementTree as ET

"""
@Class : To perform lane change interventions
"""
class ChangeLanes():
def __init__(self, fileName):
self._fileName = fileName
self._tree = ET.parse(self._fileName)
self._root = self._tree.getroot()

"""
Get Head of XML Tree
"""
def getRootElementTag(self):
return self._root.tag

"""
Get different attribute types
"""
def getUniqueParentTags(self):
d = {}
for child in self._root:
if (child.tag) not in d:
d[child.tag] = 1
else:
d[child.tag] += 1

return d.keys()

"""
Get all Lane IDs
"""
def getLaneTypes(self):
l = []
for child in self._root:
if child.tag == "type":
l.append(child.attrib["id"])

return l

"""
Change priority attributes of certain lanes
"""
def changePriorityLanes(self, laneTypeList, newPriorityValue, newFileName = None):
for child in self._root:
if child.tag == "type":
attributesDict = child.attrib
if attributesDict["id"] in laneTypeList:
child.set("priority", str(newPriorityValue))

if newFileName is None:
self._tree.write(self._fileName)
else:
self._tree.write(str(newFileName))

"""
Change the number of lanes with given IDs
"""
def changeNumLanes(self, laneTypeList, newNumberLanes, newFileName = None):
for child in self._root:
if child.tag == "type":
attributesDict = child.attrib
if attributesDict["id"] in laneTypeList:
child.set("numLanes", str(newNumberLanes))

if newFileName is None:
self._tree.write(self._fileName)
else:
self._tree.write(str(newFileName))

"""
Make certain lanes one-way, or reverse their states
"""
def toggleOneWay(self, laneTypeList, newFileName = None):
for child in self._root:
if child.tag == "type":
attributesDict = child.attrib
if attributesDict["id"] in laneTypeList:
if str(attributesDict["oneway"]) == 0:
child.set("oneway", "1")
else:
child.set("oneway", "0")
if newFileName is None:
self._tree.write(self._fileName)
else:
self._tree.write(str(newFileName))

"""
Get Lane information
"""
def getLaneInformation(self):
l = []
for child in self._root:
if child.tag == "type":
l.append(
(child.attrib["id"],
child.attrib)
)
return l
2 changes: 2 additions & 0 deletions trafficinterventions/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import ChangeEdges
from . import ChangeLanes

0 comments on commit dea0ad1

Please sign in to comment.