Skip to content

Commit 5e56a5a

Browse files
add: trip modification intervention
1 parent fed3f5f commit 5e56a5a

File tree

9 files changed

+129
-4
lines changed

9 files changed

+129
-4
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ ce = trafficinterventions.ChangeEdges.ChangeEdges(fileName="sample.xml")
2020

2121
# Sample Intervention
2222
ce.disallowAppendTypes(["bus"], ["-100"], "new_file.xml")
23-
2423
```
2524
---
2625

@@ -32,8 +31,21 @@ cl = trafficinterventions.ChangeLanes.ChangeLanes(fileName="sample.xml")
3231

3332
# Sample Intervention
3433
ce.changePriorityLanes(["highway.cycleway"], 100, "new_file.xml")
34+
```
35+
---
36+
37+
### Sample Usage : Trip Manipulation
38+
```py
39+
import trafficinterventions
3540

41+
ct = trafficinterventions.ChangeTrips.ChangeTrips(fileName="sample.xml")
42+
43+
# Sample Intervention
44+
ct.changeTripStartTime([3], 1.00, "new_file.xml")
3645
```
46+
---
47+
48+
3749
## Simulations
3850

3951
### Sample Usage: Speed Camera Placement
12.2 KB
Binary file not shown.
8.97 KB
Binary file not shown.

docs/change_trips.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import trafficinterventions
2+
3+
"""
4+
Note: If you wish to not rewrite the XML file, please add another parameter to the functions.
5+
"""
6+
7+
"""
8+
Change Trips Functionality
9+
"""
10+
11+
# Instantiating an object of the XML Parser for Trip Modification
12+
fileName = "sample3.xml" # Change the path of your file accordingly.
13+
ct = trafficinterventions.ChangeTrips.ChangeTrips(fileName=fileName)
14+
15+
# Get Root Tag
16+
print(ct.getRootElementTag())
17+
18+
# Get Unique Parent Tags
19+
print(ct.getUniqueParentTags())
20+
21+
# Get all Trip IDs
22+
print(ct.getLaneTypes())
23+
24+
# Get all Trip Information
25+
print(ct.getTripInformation())
26+
27+
# Changing the start time of trip 3 to 1.00
28+
ct.changeTripStartTime(
29+
[3],
30+
1.00
31+
)

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = trafficinterventions
3-
version = 2.0.0
3+
version = 2.0.1
44
author = WSL, IIITB
55
author_email = WSL1@iiitb.ac.in
66
description = Python Package to perform simple Traffic Interventions and run traffic simulations.

src/trafficinterventions.egg-info/PKG-INFO

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Metadata-Version: 2.1
22
Name: trafficinterventions
3-
Version: 2.0.0
3+
Version: 2.0.1
44
Summary: Python Package to perform simple Traffic Interventions and run traffic simulations.
55
Home-page: https://github.com/WSL-IIITB/Traffic-Interventions
66
Author: WSL, IIITB
@@ -37,7 +37,6 @@ ce = trafficinterventions.ChangeEdges.ChangeEdges(fileName="sample.xml")
3737

3838
# Sample Intervention
3939
ce.disallowAppendTypes(["bus"], ["-100"], "new_file.xml")
40-
4140
```
4241
---
4342

@@ -49,8 +48,21 @@ cl = trafficinterventions.ChangeLanes.ChangeLanes(fileName="sample.xml")
4948

5049
# Sample Intervention
5150
ce.changePriorityLanes(["highway.cycleway"], 100, "new_file.xml")
51+
```
52+
---
53+
54+
### Sample Usage : Trip Manipulation
55+
```py
56+
import trafficinterventions
5257

58+
ct = trafficinterventions.ChangeTrips.ChangeTrips(fileName="sample.xml")
59+
60+
# Sample Intervention
61+
ct.changeTripStartTime([3], 1.00, "new_file.xml")
5362
```
63+
---
64+
65+
5466
## Simulations
5567

5668
### Sample Usage: Speed Camera Placement

src/trafficinterventions.egg-info/SOURCES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pyproject.toml
44
setup.cfg
55
src/trafficinterventions/ChangeEdges.py
66
src/trafficinterventions/ChangeLanes.py
7+
src/trafficinterventions/ChangeTrips.py
78
src/trafficinterventions/EmissionJunction.py
89
src/trafficinterventions/SpeedCamera.py
910
src/trafficinterventions/StressJunction.py
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import xml.etree.ElementTree as ET
2+
3+
"""
4+
@Class : To perform trip interventions
5+
"""
6+
class ChangeTrips():
7+
def __init__(self, fileName):
8+
self._fileName = fileName
9+
self._tree = ET.parse(self._fileName)
10+
self._root = self._tree.getroot()
11+
12+
"""
13+
Get Head of XML Tree
14+
"""
15+
def getRootElementTag(self):
16+
return self._root.tag
17+
18+
"""
19+
Get different attribute types
20+
"""
21+
def getUniqueParentTags(self):
22+
d = {}
23+
for child in self._root:
24+
if (child.tag) not in d:
25+
d[child.tag] = 1
26+
else:
27+
d[child.tag] += 1
28+
29+
return d.keys()
30+
31+
"""
32+
Get all Trip IDs
33+
"""
34+
def getLaneTypes(self):
35+
l = []
36+
for child in self._root:
37+
if child.tag == "trip":
38+
l.append(child.attrib["id"])
39+
40+
return l
41+
42+
"""
43+
Change the start time of a trip
44+
"""
45+
def changeTripStartTime(self, tripIDList, newStartTime, newFileName = None):
46+
for child in self._root:
47+
if child.tag == "trip" and newStartTime >= 0:
48+
attributesDict = child.attrib
49+
if int(attributesDict["id"]) in tripIDList:
50+
child.set("depart", str(newStartTime))
51+
52+
if newFileName is None:
53+
self._tree.write(self._fileName)
54+
else:
55+
self._tree.write(str(newFileName))
56+
57+
"""
58+
Get Trip information
59+
"""
60+
def getTripInformation(self):
61+
l = []
62+
for child in self._root:
63+
if child.tag == "trip":
64+
l.append(
65+
(child.attrib["id"],
66+
child.attrib)
67+
)
68+
return l

src/trafficinterventions/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from . import ChangeEdges
22
from . import ChangeLanes
3+
from . import ChangeTrips
34
from . import SpeedCamera
45
from . import StressJunction
56
from . import EmissionJunction

0 commit comments

Comments
 (0)