-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFireRiskPredictorTkinter.py
430 lines (222 loc) · 11.3 KB
/
FireRiskPredictorTkinter.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
from PIL import ImageTk,Image
from numpy import interp
import pandas as pd
import time
import tkinter
from tkinter import*
# def interaction(city, state):
# fireRiskIndex = GetFireRiskFromCity(city, state)
# fireRiskIndexYears = fireRiskIndex + 0.45* years/5
#install library "openpyxl" as well
df = pd.read_excel("uscities.xlsx")
temp_color_to_fire_risk_index= {"dark_red": 8, "red" : 7 , "orange": 6, "yellow": 5, "blue": 4, "green": 3, "purple": 2, "pink": 1}
humidity_to_fire_risk_index = {"blue": 1, "dark_green": 2, "green" : 3, "yellow_green" : 4, "light_green" : 5, "yellow" : 6, "orange" : 7, "red" : 8}
state = input("Enter your state (Ex. Texas): ")
city = input("Enter your city (Ex. Boston): ")
def getLatlongitude(city, state):
cityRowArr = df[df['city'] == city].index.values
cityRowNumber = -1 #-1 means no row number exists
#Check if state is correct as well
for i in range(0, len(cityRowArr)):
if df.loc[cityRowArr[i]][1] == state: #index 0 = city, 1 = state, 2 = lat, 3 = long
cityRowNumber = cityRowArr[i] #note that the row index is two less than the row number in excel
#make sure that city exists
if cityRowNumber != -1:
cityDataArr = df.loc[cityRowNumber]
lat = cityDataArr[2]
longitude = cityDataArr[3]
return [lat,longitude]
else:
print("city not found")
def get_temp(lat, longitude):
temp_img = Image.open("tempreture_map.png")
width, height = temp_img.size
temp_rgb = temp_img.convert("RGB")
#Get x and y positions of pixel representing city
img_y = interp(lat, [24.382257, 50.532143], [1,height])
img_x = interp(longitude, [-125.591848,-66.375709], [1,width])
img_x = img_x.round()
img_y = img_y.round()
img_y = height - img_y
r, g, b = temp_rgb.getpixel((img_x, img_y))
if ((r >= 122 and r <= 128) and (g >= 21 and g <= 27) and (b >= 25 and b <= 31)):
return "dark_red"
elif ((r >= 227 and r <= 233) and (g >= 16 and g <= 22) and (b >= 22 and b <= 28)):
return "red"
elif ((r >= 242 and r <= 248) and (g >= 163 and g <= 167) and (b >= 22 and b <= 28)):
return "orange"
elif ((r >= 237 and r <= 243) and (g >= 232 and g <= 238) and (b >= 47 and b <= 53)):
return "yellow"
elif ((r >= 67 and r <= 73) and (g >= 182 and g <= 188) and (b >= 137 and b <= 143)):
return "blue"
elif ((r >= 32 and r <= 38) and (g >= 80 and g <= 86) and (b >= 162 and b <= 168)):
return "green"
elif ((r >= 107 and r <= 113) and (g >= 62 and g <= 68) and (b >= 147 and b <= 153)):
return "purple"
elif ((r >= 222 and r <= 228) and (g >= 90 and g <= 97) and (b >= 155 and b <= 161)):
return "pink"
elif ((r >= 250 and r <= 255) and (g >= 250 and g <= 255) and (b >= 250 and b <= 255)):
return "water"
else:
return "red"
def get_humidity(lat, longitude):
#Set up images
humidity_img = Image.open("humidities.png")
width, height = humidity_img.size
pixel_rgb = humidity_img.convert("RGB")
img_y = interp(lat, [24.382257, 50.532143], [1,height])
img_x = interp(longitude, [-125.591848,-66.375709], [1,width])
img_x = img_x.round()
img_y = img_y.round()
img_y = height - img_y
#Get color of pixel
try:
r, g, b = pixel_rgb.getpixel((img_x, img_y))
except:
return "pixel does not exist"
if ((r >= 200 and r <= 210) and (g >= 223 and g <= 233) and (b >= 242 and b <= 252)):
return ("pixel part of ocean")
elif ((r >= 224 and r <= 234) and (g >= 213 and g <= 223) and (b >= 199 and b <= 209)):
return ("part of land")
else:
if ((r >= 190 and r <= 200) and (g >= 65 and g <= 75) and (b >= 46 and b <= 56)):
return "red"
elif ((r >= 233 and r <= 243) and (g >= 155 and g <= 165) and (b >= 59 and b <= 69)):
return "orange"
elif ((r >= 248 and r <= 258) and (g >= 236 and g <= 246) and (b >= 76 and b <= 86)):
return "yellow"
elif ((r >= 169 and r <= 179) and (g >= 200 and g <= 210) and (b >= 113 and b <= 123)):
return "light_green"
elif ((r >= 129 and r <= 139) and (g >= 184 and g <= 194) and (b >= 85 and b <= 95)):
return "yellow_green"
elif ((r >= 98 and r <= 108) and (g >= 167 and g <= 177) and (b >= 78 and b <= 88)):
return "green"
elif ((r >= 56 and r <= 66) and (g >= 132 and g <= 142) and (b >= 71 and b <= 81)):
return "dark_green"
elif ((r >= 48 and r <= 58) and (g >= 127 and g <= 137) and (b >= 172 and b <= 182)):
return "blue"
else:
return "red"
def getFireRiskIndex(pixel_temp_color, pixel_humidity_color):
#dictionaries for pixel color, risk index, and temp/humidity numbers
temp_color_to_fire_risk_index= {"dark_red": 8, "red" : 7 , "orange": 6, "yellow": 5, "blue": 4, "green": 3, "purple": 2, "pink": 1}
color_to_temp = {"dark_red": 72.5, "red" : 67.5 , "orange": 62.5, "yellow": 57.5, "blue": 52.5, "green": 47.5, "purple": 42.5, "pink": 37.5}
temp_to_color = {value : key for (key, value) in color_to_temp.items()}
humidity_to_fire_risk_index = {"blue": 1, "dark_green": 2, "green" : 3, "yellow_green" : 4, "light_green" : 5, "yellow" : 6, "orange" : 7, "red" : 8}
#calc fire risk index
fire_risk_index = temp_color_to_fire_risk_index[pixel_temp_color] + humidity_to_fire_risk_index[pixel_humidity_color]
return fire_risk_index
def GetFireRiskFromCity(city, state):
lat = getLatlongitude(city, state)[0]
longitude = getLatlongitude(city, state)[1]
temp = get_temp(lat, longitude)
humidity = get_humidity(lat, longitude)
fire_risk_index = getFireRiskIndex(temp, humidity)
return fire_risk_index
#Start of user interaction
"""try:
try:
years = int(input("How many years into the future would you like to see the impact of climate change? "))
except:
print("That is not a valid number!")
fireRiskIndex = GetFireRiskFromCity(city, state)
fireRiskIndexYears = fireRiskIndex + 0.45*years/5
print("The chance of a fire emerging in your area will increase by ", round(100*fireRiskIndexYears/fireRiskIndex - 100, 0), "% over the next ", years," years")
time.sleep(5)
if (humidity_to_fire_risk_index[get_humidity(getLatlongitude(city, state)[0], getLatlongitude(city, state)[1])] > temp_color_to_fire_risk_index[get_temp(getLatlongitude(city, state)[0], getLatlongitude(city, state)[1])]):
print("The main cause of these fires is humidity")
time.sleep(6)
print("To save planet Earth in the future by preventing changes in humidity, you can start by:")
time.sleep(2)
print("Waste less water. Using too much water can cause inbalances in water concentration in the atmosphere, changing the humidity.")
time.sleep(6)
print("Plant more trees and grow your own food. Plants help maintain the concentration of water in the soil and atmosphere, keeping humidity levels stable.")
else:
print("The main cause of these fires is global warming")
time.sleep(6)
print("To save planet Earth in the future by preventing increases in temperature, you can start by:")
time.sleep(2)
print("Generate less trash, make sure to recycle and compost as much as possible. Pollution traps heat which causes more fires.")
time.sleep(6)
print("Ride a bike or walk next time you need to go somewhere. The exhaust from car engines releases toxic pollutants into the atmosphere that warm the globe.")
time.sleep(4)
def Impact(fire_risk_index):
affected_toll = 100 * pow(10, fire_risk_index/2.5 - 4)
acres = 2000 * pow(10, fire_risk_index/2.5 - 4)
cost = 2000000 * pow(10, fire_risk_index/2.5 - 4)
return round(affected_toll, 0), round(acres, 0), round(cost, 0)
print("The number of people impacted per fire will increase by ", Impact(fireRiskIndexYears)[0] - Impact(fireRiskIndex)[0], " people in ", years, " years")
time.sleep(4)
print("The number of acres destroyed per fire will increase by ", Impact(fireRiskIndexYears)[1] - Impact(fireRiskIndex)[1], " acres in ", years, " years")
time.sleep(4)
print("The repair costs per fire will increase by ", Impact(fireRiskIndexYears)[2] - Impact(fireRiskIndex)[2], " dollars in ", years, " years")
print("Climate change is a serious issue.")
time.sleep(3)
print("Stop.")
time.sleep(1)
print("Climate change.")
time.sleep(1)
print("Now.")
except:
print("Invalid input")"""
def data(city, state, year):
fire_risk_increase(city, state, year)
fire_risk(city, state, year)
fri = Label(window, text = fire_risk_increase(city, state, year))
fri.grid(row=1, column=4)
frl = Label(window, text = "Fire Risk: " +fire_risk_level(city, state, year))
frl.grid(row=2, column=4)
at = Label(window, text = "affected: " + affected_toll(fire_risk(city,state)))
at.grid(row=3, column=4)
acrs = Label(window, text = "acres burned: " +acres(fire_risk(city, state)))
acrs.grid(row=4, column=4)
cost = Label(window, text = "cost: " +cost(fire_risk(city, state)))
cost.grid(row=5, column=4)
def fire_risk_increase(city, state, years):
fireRiskIndex = GetFireRiskFromCity(city, state)
fireRiskIndexYears = fireRiskIndex + 0.45*years/5
return "The chance of a fire emerging in your area will increase by ", round(100*fireRiskIndexYears/fireRiskIndex - 100, 0), "% over the next ", years," years"
def fire_risk_level(city, state, years):
fire_risk_index = GetFireRiskFromCity(city, state)
try:
if (fire_risk_index >= 12 and fire_risk_index <= 16):
return "super high"
if (fire_risk_index >= 8 and fire_risk_index <= 11):
return "very high"
if (fire_risk_index >= 4 and fire_risk_index <= 7):
return "high"
if (fire_risk_index > 0 and fire_risk_index <=3):
return "medium"
if (fire_risk_index == 0):
return "low"
except:
pass
def fire_risk(city, state):
fireRiskIndex = GetFireRiskFromCity(city, state)
return fireRiskIndex
def affected_toll(fire_risk_index):
affected_toll = 100 * pow(10, fire_risk_index/2.5 - 4)
return str(round(affected_toll, 0))
def acres(fire_risk_index):
acres = 2000 * pow(10, fire_risk_index/2.5 - 4)
return str(round(acres, 0))
def cost(fire_risk_index):
cost = 2000000 * pow(10, fire_risk_index/2.5 - 4)
return str(round(cost, 0))
window = Tk()
window.geometry("300x300")
window.title("Fire Prediction Model")
Fount_tuple_title = ("Cambria", 31, "bold")
Font_tuple_important_text = ("Cambria", 23, "bold")
Font_tuple_text = ("Cambria", 23)
canvas = Canvas(window, width = 300, height = 300)
canvas.grid(row = 0, rowspan= 5, column = 0, columnspan = 5)
city = Entry(window)
city.grid(row = 2, column = 0)
state = Entry(window)
state.grid(row = 3, column = 0)
year = Entry(window)
year.grid(row = 4, column = 0)
submit = Button(window, text= "submit fields", command = data(city.get(), state.get(), year.get()))
submit.grid(row= 5, column=0)
window.mainloop()