-
Notifications
You must be signed in to change notification settings - Fork 1
/
public_zoning_stats.py
137 lines (131 loc) · 4.01 KB
/
public_zoning_stats.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import json
class PublicZoningStats():
def __init__(self, conn):
self.conn = conn
def get_stats(self):
zones = self.get_zones()
for zone in zones:
zone.update( {"operators": []} )
zone_to_index = {k["zone_id"]: v for v, k in enumerate(zones)}
zone_ids = [zone['zone_id'] for zone in zones]
counts = self.query_stats(zone_ids)
for count in counts:
idx = zone_to_index[count[0]]
zone = zones[idx]["operators"].append(
{
"system_id": count[1],
"number_of_vehicles": count[2]
}
)
zone_geometries = self.query_zones(zone_ids)
for zone_geometry in zone_geometries:
idx = zone_to_index[zone_geometry[0]]
zone = zones[idx]["geojson"] = json.loads(zone_geometry[1])
return zones
def query_stats(self, zone_ids):
cur = self.conn.cursor()
stmt = """
SELECT zone_id, system_id, count(*)
FROM park_events
JOIN zones
ON ST_Within(location, area)
WHERE end_time
is null
AND zone_id in %s
GROUP by zone_id, system_id;
"""
cur.execute(stmt, (tuple(zone_ids),))
return cur.fetchall()
def query_zones(self, zone_ids):
cur = self.conn.cursor()
stmt = """
SELECT zone_id, ST_AsGeoJSON(area) as geometry
FROM zones
WHERE zone_id in %s;
"""
cur.execute(stmt, (tuple(zone_ids),))
return cur.fetchall()
# For now some hardcoding to speedup the development process.
# In the future this should be in a DB.
# and should be depending on the municipality you select.
def get_zones(self):
return [
{
"name": "Eind Zwarte Pad",
"zone_id": 51184,
"capacity": 114
},
{
"name": "Midden Zwarte Pad",
"zone_id": 51186,
"capacity": 84
},
{
"name": "Begin Zwarte Pad",
"zone_id": 51185,
"capacity": 66
},
{
"name": "Korte Zeekant",
"zone_id": 51225,
"capacity": 110
},
{
"name": "Strandweg (Seinpostduin)",
"zone_id": 51200,
"capacity": 84
},
{
"name": "Keerlus Tramlijn 11",
"zone_id": 51199,
"capacity": 22
},
{
"name": "Visserhavenweg",
"zone_id": 51193,
"capacity": 40
},
{
"name": "Biesieklette Noordelijk Havenhoofd",
"zone_id": 51198,
"capacity": 24
},
{
"name": "Strandslag 12",
"zone_id": 51197,
"capacity": 60
},
{
"name": "Biesieklette Kijkduin",
"zone_id": 51215,
"capacity": 40
},
{
"name": "Parking Strand",
"zone_id": 51209
},
{
"name": "Rand servicegebied Keizerstraat",
"zone_id": 51214
},
{
"name": "Rand servicegebied Badhuisweg",
"zone_id": 51213
},
{
"name": "Rand servicegebied Duinstraat",
"zone_id": 51212
},
{
"name": "Rand servicegebied Vissershavenweg",
"zone_id": 51211
},
{
"name": "Den Haag Centraal",
"zone_id": 16810
},
{
"name": "nabij HS",
"zone_id": 16776
}
]