-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutages.py
109 lines (92 loc) · 3.29 KB
/
outages.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
import csv
import json
import requests
"""
This program retrieves data from the Electric Power Board (EPB)'s Power Outage and Restore map.
Link: https://epb.com/outage-and-storm-center/energy-outages/
The location of each outage and restore retrieved on each run is written to an outage and restore CSV,
respectively. This data will then be used in conjunction with other emergency- and service-related data
to track the (potential) effects of the snowstorm in the Chattanooga, TN area on January 10, 2025.
"""
"""
Retrieves the outages from the webpage and writes their data to outages.csv.
"""
def get_outages():
# Grab the JSON data
r = requests.get(url="https://api.epb.com/web/api/v1/outages/power/incidents")
# Convert to dictionary
data = json.loads(r.content)
# "data" is a Dict with keys "district_metrics", "outage_points", and "summary"
# Add each outage from data["outage_points"] to the "outages" List
outages = []
for outage in data["outage_points"]:
outages.append(outage)
# Write to CSV
fieldnames = ["crew_qty", "customer_qty", "incident_status", "latitude", "longitude"]
with open("outages.csv", "w", newline="") as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(outages)
features = []
for outage in outages:
features.append({
"type": "Feature",
"properties": {
"incident_status": outage['incident_status']
},
"geometry": {
"type": "Point",
"coordinates": [
outage['longitude'],
outage['latitude']
]
}
})
geojson = {
"type": "FeatureCollection",
"features": features
}
with open("outages.geojson", "w") as f:
json.dump(geojson, f)
"""
Retrieves the restores from the webpage and writes their data to restores.csv.
"""
def get_restores():
# Grab the JSON data
r = requests.get(url="https://api.epb.com/web/api/v1/outages/power/restores")
# Convert to dictionary
data = json.loads(r.content)
# "data" is a Dict with keys "district_metrics", "restore_points", "summary", and "timespan_label"
# Add each restore from data["restore_points"] to the "restores" List
restores = []
for restore in data["restore_points"]:
restores.append(restore)
# Write to CSV
fieldnames = ["customer_qty", "incident_status", "latitude", "longitude"]
with open("restores.csv", "w", newline="") as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(restores)
features = []
for restore in restores:
features.append({
"type": "Feature",
"properties": {
"incident_status": restore['incident_status']
},
"geometry": {
"type": "Point",
"coordinates": [
restore['longitude'],
restore['latitude']
]
}
})
geojson = {
"type": "FeatureCollection",
"features": features
}
with open("restores.geojson", "w") as f:
json.dump(geojson, f)
get_outages()
get_restores()