-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
262 lines (205 loc) · 7.52 KB
/
app.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
from flask import Flask
from flask import render_template,request,jsonify
from flask_bootstrap import Bootstrap
from pymongo import MongoClient
from bson import json_util
from bson.json_util import dumps
from threading import Thread
from flask import Response
import json
import os
import csv
import os.path
import time
app = Flask(__name__)
Bootstrap(app)
MONGODB_HOST = 'localhost'
MONGODB_PORT = 27017
DBS_NAME = 'first1'
COLLECTION_NAME = 'projects'
FIELDS = {'date': True,
'state': True,
'city_or_county': True,
'address': True,
'n_killed': True,
'n_injured': True,
'congressional_district': True ,
'latitude': True ,
'longitude': True ,
'n_guns_involved': True ,
'state_house_district': True ,
'state_senate_district': True ,
'zip_code': True ,
'n_child': True ,
'n_teen': True ,
'n_adult': True ,
'n_child_victim': True ,
'n_teen_victim': True ,
'n_adult_victim': True ,
'n_male': True ,
'n_female': True ,
'n_male_victim': True ,
'n_female_victim': True ,
'n_male_victim':True,
'n_female_victim':True,
'state_ab': True,
'_id': False,
'population':True,
'n_killed_normalized_state':True,
'county_population':True,
'zipcode_population':True
}
# # ---------zipcode DB init--------------
#
MONGODB_HOST2 = 'localhost'
DBS_NAME2 = 'second'
MONGODB_PORT2 = 27018
COLLECTION_NAME2 = 'projects2'
FIELDS2 = {
'GEOID':True,
'Average Household Income':True,
'Average Household Income':True,
'Median House Value':True,
'_id':False}
#
#
#
#
#
#
# # ---------------------------------------
static_zip = 90001
@app.route("/")
def home():
return render_template("home.html")
@app.route("/trends")
def trends():
print("trends begin")
return render_template("trends.html")
@app.route("/first1/projects")
def firset_projects():
connection = MongoClient(MONGODB_HOST,MONGODB_PORT)
collection = connection[DBS_NAME][COLLECTION_NAME]
projects = collection.find(projection=FIELDS,limit = 240000)
json_projects = []
for project in projects:
json_projects.append(project)
json_projects = json.dumps(json_projects, default=json_util.default)
connection.close()
return json_projects
@app.route("/test2")
def second_projects():
connection = MongoClient(MONGODB_HOST2,MONGODB_PORT2)
collection = connection[DBS_NAME2][COLLECTION_NAME2]
test =list( collection.find({'GEOID':8817}))
print('I am HERE!!!!',test[0].get('loc'))
arr = test[0].get('loc')
print('hahahahha',arr)
projects = collection.find({"loc":{"$near":arr,"$maxDistance":0.1}},{"GEOID":1,"_id":0});
arr2 = []
for doc in projects:
print(doc)
arr2.append(doc.get('GEOID'))
print(arr2)
connection2 = MongoClient(MONGODB_HOST,MONGODB_PORT)
collection2 = connection2[DBS_NAME][COLLECTION_NAME]
projects = collection2.find({"zip_code":{"$in":arr2}},{"_id":0})
json_projects = []
for project in projects:
json_projects.append(project)
json_projects = json.dumps(json_projects, default=json_util.default)
connection.close()
return json_projects
@app.route("/test")
def test():
connection = MongoClient(MONGODB_HOST,MONGODB_PORT)
collection = connection[DBS_NAME][COLLECTION_NAME]
projects = collection.find({'zip_code':10024})
json_projects = []
for project in projects:
json_projects.append(project)
json_projects = json.dumps(json_projects, default=json_util.default)
connection.close()
return json_projects
# test aggragation
@app.route("/first1/zipfilter",methods = ['POST'])
def zipfilter():
print('you are in filter!!!!!!')
result = request.form
dynamic_zip = result['Area']
dynamic_zip=int(dynamic_zip)
print('zipcode is',dynamic_zip)
connection = MongoClient(MONGODB_HOST2,MONGODB_PORT2)
collection = connection[DBS_NAME2][COLLECTION_NAME2]
test = list(collection.find({'GEOID':dynamic_zip}))
arr = test[0].get('loc')
print('I am HERE!!!!',test[0].get('loc'))
projects = collection.find({"loc":{"$near":arr,"$maxDistance":0.08}},{"GEOID":1,"_id":0}).limit(10);
arr2 = [] #list of nearby zipcode
for doc in projects:
arr2.append(doc.get('GEOID'))
with open('./static/second/onlynearzip.json','w') as fout:
json.dump(arr2,fout)
connection2 = MongoClient(MONGODB_HOST,MONGODB_PORT)
collection2 = connection2[DBS_NAME][COLLECTION_NAME]
projects3 = collection2.find({"zip_code":{"$in":arr2}},{"_id":0})
json_projects3 = []
for project in projects3:
json_projects3.append(project)
with open('./static/second/data_new.json','w') as fout:
json.dump(json_projects3,fout)
projects2 = collection2.find({"zip_code":{"$in":arr2}},{'latitude':1,'longitude':1,'source_url':1,"_id":0})
json_projects2 = []
for project in projects2:
json_projects2.append(project)
with open('./static/second/location.json','w') as fout:
json.dump(json_projects2,fout)
#utlize mongodb aggregate function to find the toall num of accident per zipcode for a specific array of zipcode
projects5 = collection2.aggregate([{"$match":{"zip_code":{"$in":arr2}}},{"$group":{"_id":"$zip_code","count":{"$sum":1}}},{"$sort":{"count":1}}])
json_projects5 = []
for project in projects5:
json_projects5.append(project)
with open('./static/second/aggregat_ecount_foreach_zipcode.json','w') as fout:
json.dump(json_projects5,fout)
#finished aggreage file creation
project4 = collection.find({"GEOID":{"$in":arr2}},{'Median Age':1,'Average Household Income':1,'Total Population':1,'Total Housing Units':1,'Median Household Income' : 1,'Median House Value':1,'GEOID':1,'latitude':1,'longitude':1,'loc':1,"_id":0})
json_projects4 = []
for project in project4:
json_projects4.append(project)
print('what is that !!!',json_projects4)
with open('./static/second/zip_loc.json','w') as fout:
json.dump(json_projects4,fout)
connection.close()
# return json_projects
# test aggragation
@app.route("/about")
def about():
print("delete begin")
if os.path.exists("./static/second/data_new.json"):
os.remove("./static/second/data_new.json")
if os.path.exists("./static/second/location.json"):
os.remove("./static/second/location.json")
if os.path.exists("./static/second/onlynearzip.json"):
os.remove("./static/second/onlynearzip.json")
if os.path.exists("./static/second/zip_loc.json"):
os.remove("./static/second/zip_loc.json")
if os.path.exists("./static/second/aggregat_ecount_foreach_zipcode.json"):
os.remove("./static/second/aggregat_ecount_foreach_zipcode.json")
print("delete stop")
return render_template("about.html")
@app.route("/result",methods = ['POST','GET'])
def result():
while not os.path.exists("./static/second/data_new.json") or not os.path.exists("./static/second/aggregat_ecount_foreach_zipcode.json") or not os.path.exists("./static/second/zip_loc.json"):
time.sleep(0.001)
# while not os.path.exists("./static/second/data_new.json"):
# time.sleep(0.1)
# while not os.path.exists("./static/second/data_new.json"):
# time.sleep(0.1)
if os.path.isfile("./static/second/data_new.json"):
if request.method == 'POST':
result = request.form
print('test')
print('test')
return render_template("result.html",result = result)
if __name__ == "__main__":
app.run(debug=True)