-
Notifications
You must be signed in to change notification settings - Fork 0
/
maps.py
172 lines (166 loc) · 8.72 KB
/
maps.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
"""
WFP Libya CO DP Manager.
Mapping functions.
"""
from math import sqrt
from geojson import Feature, FeatureCollection
import json
from flask import request
@app.route('/', methods=['GET', 'POST'])
def maps(month=last_month.month, year=last_month.year, select_activities=["GFD"], figure="hh_reached", aggregation="baladiya", display="circles", monitoring="all"):
month = request.form.get("month") if request.form.get("month") else month
year = request.form.get("year") if request.form.get("year") else year
select_activities = request.form.getlist("activity") if request.form.getlist("activity") else select_activities
figure = request.form.get("reached") if request.form.get("reached") else figure
aggregation = request.form.get("aggregation") if request.form.get("aggregation") else aggregation
monitoring = request.form.get("monitoring") if request.form.get("monitoring") else monitoring
display = request.form.get("display") if request.form.get("display") else display
if aggregation == "mantika":
aggregation_list1 = ["activity", "mantika"]
elif aggregation == "baladiya":
aggregation_list1 = ["activity", "mantika", "baladiya"]
elif aggregation == "distributionpoint":
aggregation_list1 = ["activity", "mantika", "baladiya", "distributionpoint"]
aggregation_var = aggregation_list1[-1]
aggregation_list2 = []
for x in aggregation_list1:
aggregation_list2.append(getattr(distribution_points2, x))
all_activities = Wfp_lists.query.filter_by(type="activity").all()
query_db = query_db = db.session.query(
distribution_points2.geom.st_union().st_centroid().ST_AsGeoJSON(), # Taking the centroid per Baladiya
distribution_points2.activity,
func.min(distribution_points2.distributionpoint).label("distributionpoint"),
func.min(distribution_points2.mantika).label("mantika"),
func.min(distribution_points2.baladiya).label("baladiya"),
func.bool_or(distributionfigures_gfd.moomken).label("moomken"),
func.bool_or(distributionfigures_gfd.wfp).label("wfp"),
func.sum(getattr(distributionfigures_gfd, figure)).label("hh")).join( # Joining disrib figures
distributionfigures_gfd, and_(
distribution_points2.activity.in_(select_activities),
distribution_points2.id == distributionfigures_gfd.distribution_point,
distributionfigures_gfd.month == month,
distributionfigures_gfd.year == year,
getattr(distributionfigures_gfd, figure) > 0)).group_by(*aggregation_list2)
response = query_db.all()
if request.form.get("download"):
return excel.make_response_from_query_sets(response, aggregation_list1 + ["moomken", "wfp", "hh"], "xls", file_name=month + "_" + year + "_" + "_".join(select_activities) + "_" + figure + ".xls")
my_geojson = []
total_str = "0"
if [x.hh for x in response]:
max_hh = max([x.hh for x in response])
total = sum([x.hh for x in response]) * 5
total_str = '{:,}'.format(total)
for x in response:
if display == "circles":
circle_size = sqrt(x.hh / max_hh)
elif display == "points":
circle_size = 0.2
if monitoring == "all":
opacity = 0.7
elif monitoring == "wfp":
opacity = 0.2
if x.wfp is True:
opacity = 0.7
elif monitoring == "moomken":
opacity = 0.2
if x.moomken is True:
opacity = 0.7
feature = Feature(geometry=json.loads(x[0]),
properties={
"mantika": x.mantika,
"baladiya": x.baladiya,
"activity": x.activity,
"moomken": x.moomken,
"wfp": x.wfp,
"distributionpoint": x.distributionpoint,
"circle_size": circle_size,
"circle_color": activity2col(x.activity),
"circle_opacity": opacity,
'ind': (x.hh) * 5,
'ind_str': '{:,}'.format((x.hh) * 5)})
my_geojson.append(feature)
feature_collection = FeatureCollection(my_geojson)
return render_template("map2.html",
geojson=feature_collection,
activities=all_activities,
select_activities=select_activities,
month=month,
year=year,
total=total_str,
figure=figure,
aggregation_var=aggregation_var,
monitoring=monitoring,
display=display)
#return jsonify(feature_collection)
@app.route('/map_RRM/', methods=['GET', 'POST'])
def map_rrm(aggregation="baladiya", display="circles", monitoring="all"):
aggregation = request.form.get("aggregation") if request.form.get("aggregation") else aggregation
monitoring = request.form.get("monitoring") if request.form.get("monitoring") else monitoring
display = request.form.get("display") if request.form.get("display") else display
if aggregation == "mantika":
aggregation_list1 = ["activity", "mantika"]
elif aggregation == "baladiya":
aggregation_list1 = ["activity", "mantika", "baladiya"]
elif aggregation == "distributionpoint":
aggregation_list1 = ["activity", "mantika", "baladiya", "distributionpoint"]
aggregation_var = aggregation_list1[-1]
aggregation_list2 = []
for x in aggregation_list1:
aggregation_list2.append(getattr(distribution_points2, x))
query_db = query_db = db.session.query(
distribution_points2.geom.st_union().st_centroid().ST_AsGeoJSON(), # Taking the centroid per Baladiya
distribution_points2.activity,
func.min(distribution_points2.distributionpoint).label("distributionpoint"),
func.min(distribution_points2.mantika).label("mantika"),
func.min(distribution_points2.baladiya).label("baladiya"),
func.bool_or(distributionfigures_rrm.moomken).label("moomken"),
func.bool_or(distributionfigures_rrm.wfp).label("wfp"),
func.sum(distributionfigures_rrm.hh_reached).label("hh")).join( # Joining disrib figures
distributionfigures_rrm, and_(
distribution_points2.id == distributionfigures_rrm.distribution_point,
distributionfigures_rrm.hh_reached > 0)).group_by(*aggregation_list2)
response = query_db.all()
if request.form.get("download"):
return excel.make_response_from_query_sets(response, aggregation_list1 + ["moomken", "wfp", "hh"], "xls", file_name="RRM_HH_Reached.xls")
my_geojson = []
total_str = "0"
if [x.hh for x in response]:
max_hh = max([x.hh for x in response])
total = sum([x.hh for x in response]) * 5
total_str = '{:,}'.format(total)
for x in response:
if display == "circles":
circle_size = sqrt(x.hh / max_hh)
elif display == "points":
circle_size = 0.2
if monitoring == "all":
opacity = 0.7
elif monitoring == "wfp":
opacity = 0.2
if x.wfp is True:
opacity = 0.7
elif monitoring == "moomken":
opacity = 0.2
if x.moomken is True:
opacity = 0.7
feature = Feature(geometry=json.loads(x[0]),
properties={
"mantika": x.mantika,
"baladiya": x.baladiya,
"activity": x.activity,
"moomken": x.moomken,
"wfp": x.wfp,
"distributionpoint": x.distributionpoint,
"circle_size": circle_size,
"circle_color": "#2A93FC",
"circle_opacity": opacity,
'ind': (x.hh) * 5,
'ind_str': '{:,}'.format((x.hh) * 5)})
my_geojson.append(feature)
feature_collection = FeatureCollection(my_geojson)
return render_template("map.html",
geojson=feature_collection,
total=total_str,
aggregation_var=aggregation_var,
monitoring=monitoring,
display=display)