-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9944677
commit dea0ad1
Showing
3 changed files
with
199 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from . import ChangeEdges | ||
from . import ChangeLanes |