-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
108 lines (100 loc) · 4.14 KB
/
main.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
import os
import sys
import subprocess
from flask import Flask, render_template, session
from flask import request, escape, redirect, url_for
import configparser
import json
from matplotlib.figure import Figure
import base64
from io import BytesIO
import numpy as np
import plotly
import pandas as pd
import plotly.express as px
app = Flask(__name__)
app.secret_key = 'Plots'
def get_json_image(df, x= 'year', color = 'month'):
if(x=='month'):
color = 'year'
fig = px.line(df, x = x, y = 'areas', color = color)
graphJSON = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
return graphJSON
def get_json_image_xy(x, y):
fig = px.line(x=x, y=y)
graphJSON = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
return graphJSON
bands = ['built', 'crops', 'flooded_vegetation', 'trees', 'water']
df_list = []
for band in bands:
feature_list = json.load(open(f'data/Bangalore Urban/{band}/areas.json', 'r'))
areas = (np.array([i.get('properties').get('label') for i in feature_list])*20)**2
month_data = np.array([i.get('properties').get('month') for i in feature_list])
year_data = np.array([i.get('properties').get('year') for i in feature_list])
df = pd.DataFrame({"year": year_data, "month": month_data, "areas": areas})
df['date'] = pd.to_datetime(dict(year=df.year, month=df.month, day = 1))
df.set_index('date', inplace=True)
df = df.resample('MS').asfreq()
df.fillna(method='ffill', inplace=True)
df.month = df.index.month
df.year = df.index.year
df_list.append(df)
unique_year = np.sort(np.unique(year_data))[1:]
month_names = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
def get_months_box_bool(checkboxes):
bool_aray = []
for month in month_names:
if month in checkboxes:
bool_aray.append(True)
else:
bool_aray.append(False)
return bool_aray
@app.route("/", methods=['GET', 'POST'])
def index():
months_box = [True, True, True, True, True, True, False, False, False, False, False, False]
image_data = []
selected_option = 'year'
kwargs = {
"months_list": month_names,
"bands": bands
}
is_run_files = request.args.get("view","")
print(is_run_files)
selected_option = request.args.get('x_axis', selected_option, type=str)
month_req = get_months_box_bool(request.args.getlist("month", None))
if month_req is not None:
months_box = month_req
if(any(key.startswith("chart_") for key in request.form.keys())):
keys = request.form.keys()
print("******")
for key in keys:
save_path = request.form.get(key, "")
print(save_path)
if(save_path != ""):
band_idx = bands.index(key.replace("chart_", ""))
os.makedirs(os.path.dirname(save_path), exist_ok=True)
print(bands[band_idx])
if(is_run_files=="Advanced"):
for idx, band in enumerate(bands):
df = df_list[idx]
df = df[df.month.isin([i+1 for i, x in enumerate(months_box) if x])]
image_buff = get_json_image(df, x = selected_option)
image_data.append(image_buff)
return render_template('index.html', image_data = image_data, months_box = months_box, x_axis = selected_option, **kwargs)
for idx, band in enumerate(bands):
df = df_list[idx]
yearly_data = [np.mean(df[df.year==i].areas[months_box[0:(df.year==i).sum()]]) for i in unique_year]
image_buff = get_json_image_xy(unique_year, yearly_data)
image_data.append(image_buff)
return render_template('index.html', image_data = image_data, months_box = months_box, x_axis = selected_option, **kwargs)
@app.route("/advanced", methods=['GET', 'POST'])
def advanced_view():
image_data = []
months_box = session['months_box']
for idx, band in enumerate(bands):
df = df_list[idx]
image_buff = get_json_image(df)
image_data.append(image_buff)
return render_template('index.html', image_data = image_data, bands = bands, months_box = months_box)
if __name__ == "__main__":
app.run(host="127.0.0.1", port=8080)