Mercurial Hosting > traffic-intelligence
annotate trafficintelligence/iframework.py @ 1272:785c86013d2c
added moving average smoothing for kitti loading
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Mon, 17 Jun 2024 22:49:22 -0400 |
parents | c4c50678c856 |
children |
rev | line source |
---|---|
1172
f9ab0cbb92da
updates to iframework, to better reflect transport modes (suggestion)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1164
diff
changeset
|
1 ''' Framework to record data to measure the three street functions: access, transit and place ''' |
f9ab0cbb92da
updates to iframework, to better reflect transport modes (suggestion)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1164
diff
changeset
|
2 |
1162
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
3 from enum import Enum |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
4 from pathlib import Path |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
5 from datetime import datetime |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
6 |
1248
c4c50678c856
clean api update in sqlalchemy
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1175
diff
changeset
|
7 from sqlalchemy.ext.declarative import declared_attr |
1162
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
8 from sqlalchemy import Table, Column, Integer, Boolean, String, Float, DateTime, Enum as SQLEnum, ForeignKey, CheckConstraint, create_engine |
1248
c4c50678c856
clean api update in sqlalchemy
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1175
diff
changeset
|
9 from sqlalchemy.orm import relationship, backref, sessionmaker, declarative_base |
1162
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
10 |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
11 Base = declarative_base() |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
12 |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
13 GenderEnum = Enum('GenderEnum', 'male female unknown') |
1172
f9ab0cbb92da
updates to iframework, to better reflect transport modes (suggestion)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1164
diff
changeset
|
14 ModeEnum = Enum('ModeEnum', 'cardriver carpassenger transit taxi motorcycle cycling walking other') # the idea is that the mode could be sufficient to record all events (line and zone crossings), whether the actual, more precise vehicle, is |
f9ab0cbb92da
updates to iframework, to better reflect transport modes (suggestion)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1164
diff
changeset
|
15 VehicleEnum = Enum('VehicleEnum', 'car suv van truck motorcycle bus bike scooter skate rollers') |
1162
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
16 |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
17 # should there be a survey object for site info, observer, etc? |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
18 |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
19 class Mode(Base): |
1172
f9ab0cbb92da
updates to iframework, to better reflect transport modes (suggestion)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1164
diff
changeset
|
20 '''A mode is personal, because in a group (family), some might have a scooter or rollers''' |
1162
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
21 __tablename__ = 'modes' |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
22 idx = Column(Integer, primary_key=True) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
23 personIdx = Column(Integer, ForeignKey('persons.idx')) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
24 vehicleIdx = Column(Integer, ForeignKey('vehicles.idx')) |
1173
c1766bb5076c
udpated the iframework metadata to allow adding it to an existing database, and creating only a subset of tables for group/person attributes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1172
diff
changeset
|
25 name = Column(SQLEnum(ModeEnum), nullable=False) |
1172
f9ab0cbb92da
updates to iframework, to better reflect transport modes (suggestion)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1164
diff
changeset
|
26 startTime = Column(DateTime) # None first time if only one mode |
1162
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
27 pointIdx = Column(Integer, ForeignKey('points.idx')) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
28 |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
29 person = relationship('Person', backref = backref('modes')) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
30 vehicle = relationship('Vehicle') |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
31 point = relationship('Point') |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
32 |
1173
c1766bb5076c
udpated the iframework metadata to allow adding it to an existing database, and creating only a subset of tables for group/person attributes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1172
diff
changeset
|
33 def __init__(self, name, person, vehicle = None, startTime = None, p = None): |
1162
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
34 self.person = person |
1173
c1766bb5076c
udpated the iframework metadata to allow adding it to an existing database, and creating only a subset of tables for group/person attributes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1172
diff
changeset
|
35 self.name = name |
1162
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
36 self.vehicle = vehicle |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
37 self.startTime = startTime |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
38 self.point = p |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
39 |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
40 @staticmethod |
1173
c1766bb5076c
udpated the iframework metadata to allow adding it to an existing database, and creating only a subset of tables for group/person attributes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1172
diff
changeset
|
41 def initGroup(name, group, vehicle = None, startTime = None): |
c1766bb5076c
udpated the iframework metadata to allow adding it to an existing database, and creating only a subset of tables for group/person attributes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1172
diff
changeset
|
42 return [Mode(name, p, startTime) for p in group.getPersons()] |
1162
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
43 |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
44 class Group(Base): |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
45 __tablename__ = 'groups' |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
46 idx = Column(Integer, primary_key=True) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
47 |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
48 def __init__(self, persons): |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
49 for p in persons: |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
50 GroupBelonging(p, self) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
51 |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
52 def getPersons(self): |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
53 return [gb.person for gb in self.groupBelongings] |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
54 |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
55 class GroupBelonging(Base): |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
56 __tablename__ = 'groupbelongings' |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
57 groupIdx = Column(Integer, ForeignKey('groups.idx'), primary_key=True) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
58 personIdx = Column(Integer, ForeignKey('persons.idx'), primary_key=True) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
59 pointIdx = Column(Integer, ForeignKey('points.idx')) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
60 startTime = Column(DateTime) # None first time if only one group |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
61 |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
62 person = relationship('Person', backref = backref('groupBelongings')) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
63 group = relationship('Group', backref = backref('groupBelongings')) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
64 point = relationship('Point') |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
65 |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
66 def __init__(self, person, group, startTime = None, p = None): |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
67 self.person = person |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
68 self.group = group |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
69 self.startTime = startTime |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
70 self.point = p |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
71 |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
72 # in aggregated form, there is a total number of observations for a given time interval, a number for each binary variable and k-1 variables for a categorical variable with k categories |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
73 class Person(Base): |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
74 __tablename__ = 'persons' |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
75 idx = Column(Integer, primary_key=True) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
76 #groupIdx = Column(Integer, ForeignKey('groups.idx')) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
77 age = Column(String) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
78 gender = Column(SQLEnum(GenderEnum), nullable=False) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
79 disability = Column(String) # could be enum |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
80 stroller = Column(Boolean) # the booleans could be strings or enum to have more information |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
81 bag = Column(Boolean) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
82 animal = Column(Boolean) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
83 |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
84 #group = relationship('Group', backref = backref('persons')) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
85 |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
86 def __init__(self, age = 'unknown', gender = 'unknown', disability = False, stroller = False, bag = False, animal = False): |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
87 self.age = age |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
88 self.gender = gender |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
89 self.disability = disability |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
90 self.stroller = stroller |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
91 self.bag = bag |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
92 self.animal = animal |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
93 |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
94 def getAgeNum(self): |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
95 if str.isnumeric(self.age): |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
96 return int(self.age) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
97 elif '.' in self.age: |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
98 try: |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
99 return float(self.age) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
100 except ValueError: |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
101 pass |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
102 else: |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
103 return self.age |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
104 |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
105 def getGroups(self): |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
106 if len(self.groupBelongings) > 0: |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
107 return [gb.group for gb in self.groupBelongings] |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
108 else: |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
109 return None |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
110 |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
111 class Vehicle(Base): |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
112 __tablename__ = 'vehicles' |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
113 idx = Column(Integer, primary_key=True) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
114 category = Column(SQLEnum(VehicleEnum), nullable=False) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
115 trailer = Column(Boolean) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
116 |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
117 def __init__(self, category, trailer = False): |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
118 self.category = category |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
119 self.trailer = trailer |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
120 |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
121 class Point(Base): |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
122 __tablename__ = 'points' |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
123 idx = Column(Integer, primary_key=True) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
124 x = Column(Float) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
125 y = Column(Float) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
126 |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
127 def __init__(self, x, y): |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
128 self.x = x |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
129 self.y = y |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
130 |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
131 pointLineAssociation = Table('pointlines', Base.metadata, |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
132 Column('pointIdx', Integer, ForeignKey('points.idx')), |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
133 Column('lineIdx', Integer, ForeignKey('lines.idx'))) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
134 |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
135 class Line(Base): |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
136 __tablename__ = 'lines' |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
137 idx = Column(Integer, primary_key=True) |
1163
fa9c358789ac
bug correction
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1162
diff
changeset
|
138 name = Column(String) |
fa9c358789ac
bug correction
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1162
diff
changeset
|
139 # todo define lines for access counting: add type? - AccessLine? |
1162
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
140 |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
141 points = relationship('Point', secondary=pointLineAssociation) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
142 |
1163
fa9c358789ac
bug correction
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1162
diff
changeset
|
143 def __init__(self, name, x1, y1, x2, y2): |
fa9c358789ac
bug correction
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1162
diff
changeset
|
144 self.name = name |
1162
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
145 self.points = [Point(x1, y1), Point(x2, y2)] |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
146 |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
147 pointZoneAssociation = Table('pointzones', Base.metadata, |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
148 Column('pointIdx', Integer, ForeignKey('points.idx')), |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
149 Column('zoneIdx', Integer, ForeignKey('zones.idx'))) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
150 |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
151 class Zone(Base): |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
152 __tablename__ = 'zones' |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
153 idx = Column(Integer, primary_key=True) |
1163
fa9c358789ac
bug correction
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1162
diff
changeset
|
154 name = Column(String) |
1162
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
155 |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
156 points = relationship('Point', secondary=pointZoneAssociation) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
157 |
1163
fa9c358789ac
bug correction
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1162
diff
changeset
|
158 def __init__(self, name, xs = None, ys = None): |
1162
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
159 'xs and ys are the list of x and y coordinates' |
1163
fa9c358789ac
bug correction
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1162
diff
changeset
|
160 self.name = name |
1162
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
161 if xs is not None and ys is not None: |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
162 for x,y in zip(xs, ys): |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
163 self.addPoint(x,y) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
164 |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
165 def addPoint(self, x, y): |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
166 self.points.append(Point(x, y)) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
167 |
1172
f9ab0cbb92da
updates to iframework, to better reflect transport modes (suggestion)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1164
diff
changeset
|
168 class AbstractCrossing: |
1173
c1766bb5076c
udpated the iframework metadata to allow adding it to an existing database, and creating only a subset of tables for group/person attributes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1172
diff
changeset
|
169 def initPersonGroupCrossing(self, group, person, modeName, vehicle): |
1175
35725db5e83f
naming changed for framework
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1173
diff
changeset
|
170 ''' initiates with the crossing the group or person |
1163
fa9c358789ac
bug correction
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1162
diff
changeset
|
171 |
fa9c358789ac
bug correction
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1162
diff
changeset
|
172 design question: what should be done about simple line counting, |
fa9c358789ac
bug correction
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1162
diff
changeset
|
173 without information about persons''' |
1162
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
174 if person is None and group is not None: # create group |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
175 self.group = group |
1173
c1766bb5076c
udpated the iframework metadata to allow adding it to an existing database, and creating only a subset of tables for group/person attributes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1172
diff
changeset
|
176 if modeName is not None: |
c1766bb5076c
udpated the iframework metadata to allow adding it to an existing database, and creating only a subset of tables for group/person attributes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1172
diff
changeset
|
177 Mode.initGroup(modeName, group, vehicle) |
1162
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
178 elif person is not None and group is None: # create person |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
179 self.group = Group([person]) |
1173
c1766bb5076c
udpated the iframework metadata to allow adding it to an existing database, and creating only a subset of tables for group/person attributes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1172
diff
changeset
|
180 if modeName is not None: |
c1766bb5076c
udpated the iframework metadata to allow adding it to an existing database, and creating only a subset of tables for group/person attributes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1172
diff
changeset
|
181 Mode(modeName, person, vehicle) |
1162
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
182 else: |
1175
35725db5e83f
naming changed for framework
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1173
diff
changeset
|
183 print('Warning: crossing person and group or both None') |
1162
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
184 |
1172
f9ab0cbb92da
updates to iframework, to better reflect transport modes (suggestion)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1164
diff
changeset
|
185 class LineCrossing(AbstractCrossing,Base): |
1175
35725db5e83f
naming changed for framework
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1173
diff
changeset
|
186 __tablename__ = 'linecrossings' # formerly 'linepassings' |
1162
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
187 idx = Column(Integer, primary_key=True) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
188 lineIdx = Column(Integer, ForeignKey('lines.idx')) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
189 groupIdx = Column(Integer, ForeignKey('groups.idx')) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
190 pointIdx = Column(Integer, ForeignKey('points.idx')) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
191 instant = Column(DateTime) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
192 speed = Column(Float) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
193 wrongDirection = Column(Boolean) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
194 |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
195 line = relationship('Line') |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
196 group = relationship('Group') |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
197 point = relationship('Point') |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
198 |
1173
c1766bb5076c
udpated the iframework metadata to allow adding it to an existing database, and creating only a subset of tables for group/person attributes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1172
diff
changeset
|
199 def __init__(self, line, instant, speed = None, wrongDirection = None, p = None, group = None, person = None, modeName = None, vehicle = None): |
1162
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
200 # makes it possible to create person and mode for just counting |
1173
c1766bb5076c
udpated the iframework metadata to allow adding it to an existing database, and creating only a subset of tables for group/person attributes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1172
diff
changeset
|
201 # pass modeName as string to instantiate after |
1162
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
202 self.line = line |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
203 self.instant = instant |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
204 self.speed = speed |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
205 self.wrongDirection = wrongDirection |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
206 self.point = p |
1173
c1766bb5076c
udpated the iframework metadata to allow adding it to an existing database, and creating only a subset of tables for group/person attributes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1172
diff
changeset
|
207 self.initPersonGroupCrossing(group, person, modeName, vehicle) |
1162
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
208 |
1172
f9ab0cbb92da
updates to iframework, to better reflect transport modes (suggestion)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1164
diff
changeset
|
209 class ZoneCrossing(AbstractCrossing,Base): |
1164
f1a33f458d7e
changing ZonePassing to ZoneCrossing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1163
diff
changeset
|
210 __tablename__ = 'zonecrossings' |
1162
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
211 idx = Column(Integer, primary_key=True) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
212 zoneIdx = Column(Integer, ForeignKey('zones.idx')) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
213 groupIdx = Column(Integer, ForeignKey('groups.idx')) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
214 pointIdx = Column(Integer, ForeignKey('points.idx')) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
215 instant = Column(DateTime) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
216 entering = Column(Boolean) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
217 |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
218 zone = relationship('Zone') |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
219 group = relationship('Group') |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
220 point = relationship('Point') |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
221 |
1173
c1766bb5076c
udpated the iframework metadata to allow adding it to an existing database, and creating only a subset of tables for group/person attributes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1172
diff
changeset
|
222 def __init__(self, zone, instant, entering, p = None, group = None, person = None, modeName = None, vehicle = None): |
1162
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
223 self.zone = zone |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
224 self.instant = instant |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
225 self.entering = entering |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
226 self.point = p |
1173
c1766bb5076c
udpated the iframework metadata to allow adding it to an existing database, and creating only a subset of tables for group/person attributes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1172
diff
changeset
|
227 self.initPersonGroupCrossing(group, person, modeName, vehicle) |
1162
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
228 |
1172
f9ab0cbb92da
updates to iframework, to better reflect transport modes (suggestion)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1164
diff
changeset
|
229 class Activity(AbstractCrossing,Base): |
1162
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
230 __tablename__ = 'activities' |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
231 idx = Column(Integer, primary_key=True) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
232 activity = Column(String) # could be enum |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
233 groupIdx = Column(Integer, ForeignKey('groups.idx')) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
234 # can an activity be done in a vehicle? Is it relevant? Can it be unambiguously identified? |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
235 startTime = Column(DateTime) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
236 endTime = Column(DateTime) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
237 zoneIdx = Column(Integer, ForeignKey('zones.idx')) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
238 pointIdx = Column(Integer, ForeignKey('points.idx')) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
239 |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
240 group = relationship('Group') |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
241 zone = relationship('Zone') |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
242 point = relationship('Point') |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
243 |
1173
c1766bb5076c
udpated the iframework metadata to allow adding it to an existing database, and creating only a subset of tables for group/person attributes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1172
diff
changeset
|
244 def __init__(self, activity, startTime, endTime, zone, p = None, group = None, person = None, modeName = None, vehicle = None): |
1162
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
245 self.activity = activity |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
246 self.startTime = startTime |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
247 self.endTime = endTime |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
248 self.zone = zone |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
249 self.point = p |
1173
c1766bb5076c
udpated the iframework metadata to allow adding it to an existing database, and creating only a subset of tables for group/person attributes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1172
diff
changeset
|
250 self.initPersonGroupCrossing(group, person, modeName, vehicle) |
1162
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
251 |
1173
c1766bb5076c
udpated the iframework metadata to allow adding it to an existing database, and creating only a subset of tables for group/person attributes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1172
diff
changeset
|
252 def createDatabase(filename, insertInExisting = False, createOnlyGroupTables = False): |
1162
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
253 'creates a session to query the filename' |
1173
c1766bb5076c
udpated the iframework metadata to allow adding it to an existing database, and creating only a subset of tables for group/person attributes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1172
diff
changeset
|
254 if Path(filename).is_file() and not insertInExisting: |
1162
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
255 print('The file '+filename+' exists') |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
256 return None |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
257 else: |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
258 engine = create_engine('sqlite:///'+filename) |
1173
c1766bb5076c
udpated the iframework metadata to allow adding it to an existing database, and creating only a subset of tables for group/person attributes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1172
diff
changeset
|
259 if createOnlyGroupTables: |
c1766bb5076c
udpated the iframework metadata to allow adding it to an existing database, and creating only a subset of tables for group/person attributes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1172
diff
changeset
|
260 Base.metadata.create_all(engine, tables = [Base.metadata.tables['modes'], Base.metadata.tables['groups'], Base.metadata.tables['groupbelongings'], Base.metadata.tables['persons'], Base.metadata.tables['vehicles'], Base.metadata.tables['points']]) |
c1766bb5076c
udpated the iframework metadata to allow adding it to an existing database, and creating only a subset of tables for group/person attributes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1172
diff
changeset
|
261 else: |
c1766bb5076c
udpated the iframework metadata to allow adding it to an existing database, and creating only a subset of tables for group/person attributes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1172
diff
changeset
|
262 Base.metadata.create_all(engine) |
1162
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
263 Session = sessionmaker(bind=engine) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
264 return Session() |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
265 |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
266 def connectDatabase(filename): |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
267 'creates a session to query the filename' |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
268 if Path(filename).is_file(): |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
269 engine = create_engine('sqlite:///'+filename) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
270 Session = sessionmaker(bind=engine) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
271 return Session() |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
272 else: |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
273 print('The file '+filename+' does not exist') |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
274 return None |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
275 |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
276 if __name__ == '__main__': # demo code |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
277 session = createDatabase('test.sqlite') |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
278 if session is None: |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
279 session = connectDatabase('test.sqlite') |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
280 # count example |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
281 p = Person(6, 'female', bag = True) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
282 veh1 = Vehicle('car') |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
283 modes = [Mode('cardriver', p, veh1), Mode('walking', p, startTime = datetime(2020,7,7,11,20))] |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
284 |
1163
fa9c358789ac
bug correction
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1162
diff
changeset
|
285 line = Line('line1', 0.,0.,0.,10.) |
fa9c358789ac
bug correction
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1162
diff
changeset
|
286 zone = Zone('zone1', [0., 0., 1., 1.], [0., 1., 1., 0.]) |
fa9c358789ac
bug correction
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1162
diff
changeset
|
287 destination = Zone('destination1', [10., 10., 11., 11.], [10., 11., 11., 10.]) |
1173
c1766bb5076c
udpated the iframework metadata to allow adding it to an existing database, and creating only a subset of tables for group/person attributes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1172
diff
changeset
|
288 counts = [LineCrossing(line, datetime(2020,7,2,23,20+i), person = Person(20+i, 'female', disability = True), modeName = 'walking') for i in range(5)] |
1162
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
289 group1 = Group([Person(13+i,'female', False, False, True, False) for i in range(3)]) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
290 groupMode1 = Mode.initGroup('walking', group1) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
291 activities = [Activity('walking', datetime(2020,7,2,23,0), datetime(2020,7,2,23,10), zone, person = Person(40, 'male', True, False, True, False)), |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
292 Activity('eating', datetime(2020,7,2,23,10), datetime(2020,7,2,23,12), zone, person = Person(40, 'male', True, False, True, False)), |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
293 Activity('playing', datetime(2020,7,2,22,0), datetime(2020,7,2,23,0), zone, group = group1)] |
1172
f9ab0cbb92da
updates to iframework, to better reflect transport modes (suggestion)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1164
diff
changeset
|
294 counts.append(LineCrossing(line, datetime(2020,7,2,23,5), group = group1)) |
1173
c1766bb5076c
udpated the iframework metadata to allow adding it to an existing database, and creating only a subset of tables for group/person attributes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1172
diff
changeset
|
295 counts.append(LineCrossing(line, datetime(2020,7,2,23,7), person = Person(23, 'unknown'), modeName = 'cardriver', vehicle = Vehicle('car'))) |
c1766bb5076c
udpated the iframework metadata to allow adding it to an existing database, and creating only a subset of tables for group/person attributes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1172
diff
changeset
|
296 counts.append(LineCrossing(line, datetime(2020,7,2,23,9), person = Person('teen', 'unknown'), modeName = 'other', vehicle = Vehicle('scooter'))) |
c1766bb5076c
udpated the iframework metadata to allow adding it to an existing database, and creating only a subset of tables for group/person attributes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1172
diff
changeset
|
297 counts.append(LineCrossing(line, datetime(2020,7,2,23,11), person = Person(12, 'female'), modeName = 'cycling')) |
c1766bb5076c
udpated the iframework metadata to allow adding it to an existing database, and creating only a subset of tables for group/person attributes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1172
diff
changeset
|
298 counts.append(LineCrossing(line, datetime(2020,7,2,23,13), person = Person(), modeName = 'cardriver')) # example of counting cars without knowing the driver and passenger's attributes |
c1766bb5076c
udpated the iframework metadata to allow adding it to an existing database, and creating only a subset of tables for group/person attributes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1172
diff
changeset
|
299 counts.append(LineCrossing(line, datetime(2020,7,2,23,15), group = Group([Person(34+i) for i in range(3)]), modeName = 'carpassenger')) |
1162
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
300 |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
301 |
1164
f1a33f458d7e
changing ZonePassing to ZoneCrossing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1163
diff
changeset
|
302 counts.append(ZoneCrossing(zone, datetime(2020,7,7,9,5), True, person = Person(33, 'male', False, False, True, False))) |
1162
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
303 |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
304 session.add_all([line, p, zone, group1, destination]+modes+groupMode1+counts+activities) |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
305 |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
306 session.commit() |
efd52c55a72b
added indicator framework file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
307 session.close() |