-
Notifications
You must be signed in to change notification settings - Fork 0
/
db1.py
170 lines (101 loc) · 3.85 KB
/
db1.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
import sys
import math
import pandas as pd
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask import render_template
from sqlalchemy.ext.hybrid import hybrid_method, hybrid_property
from sqlalchemy import func
df = pd.read_csv('IN.csv')
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:12345@localhost/mydatabase'
db = SQLAlchemy(app)
class Pincode(db.Model):
__tablename__ = 'pincode'
key = db.Column(db.String(100), primary_key = True)
place_name = db.Column(db.String(150))
admin_name1 = db.Column(db.String(100))
latitude = db.Column(db.Float)
longitude = db.Column(db.Float)
accuracy = db.Column(db.Float)
def __init__(self, key, place_name, admin_name1, latitude, longitude, accuracy):
self.key = key
self.place_name = place_name
self.admin_name1 = admin_name1
self.latitude = latitude
self.longitude = longitude
self.accuracy = accuracy
@hybrid_method
def great_circle_distance(self, p, l):
radius = 6371 # Radius of earth
p1 = self.latitude
l1 = self.longitude
print(p1, l1)
p2 = p
l2 = l
dl = math.radians(abs(l1 - l2))
p1 = math.radians(p1)
p2 = math.radians(p2)
l1 = math.radians(l1)
l2 = math.radians(l2)
ds = math.acos((math.sin(p1) * math.sin(p2)) + (math.cos(p1) * math.cos(p2) * math.cos(dl)))
dist = radius * ds
return dist
@great_circle_distance.expression
def great_circle_distance(cls, p, l):
radius = 6371 # Radius of earth
p1 = cls.latitude
l1 = cls.longitude
print(p1, l1)
p2 = p
l2 = l
dl = func.radians(func.abs(l1 - l2))
p1 = func.radians(p1)
p2 = func.radians(p2)
l1 = func.radians(l1)
l2 = func.radians(l2)
ds = func.acos((func.sin(p1) * func.sin(p2)) + (func.cos(p1) * func.cos(p2) * func.cos(dl)))
dist = radius * ds
return dist
@app.route('/post_location', methods = ['POST'])
def post_location():
data = request.get_json()
if 'key' not in data:
return 'Error! Incomplete data.\n', 400
key_value = data['key']
if Pincode.query.get(key_value):
print('Record already exists! \n', file = sys.stderr)
return 'Error! Already present.\n', 400
pin = Pincode(data['key'], data['place_name'], data['admin_name1'], data['latitude'], data['longitude'], data['accuracy'])
db.session.add(pin)
print('Success!\n', file = sys.stderr)
return 'Success! Record added.\n', 200
@app.route('/get_using_self', methods = ['GET', 'POST'])
def get_using_self():
data = request.get_json()
lat = data['latitude']
lon = data['longitude']
out = Pincode.query.filter(Pincode.great_circle_distance(lat, lon) <= 5).all()
tem = []
for a in out:
tem.append(str(a.key).strip(',').strip(')').strip('('))
tems = ''.join(tem)
return tems, 200
@app.route('/get_using_postgres', methods = ['GET', 'POST'])
def get_using_postgres():
data = request.get_json()
lat = data['latitude']
lon = data['longitude']
query = f'SELECT key FROM pincode WHERE earth_distance(ll_to_earth({lat}, {lon}), ll_to_earth(pincode.latitude, pincode.longitude)) <= 5000'
out = db.engine.execute(query)
near = []
for i in out:
near.append(str(i).strip(',').strip('(').strip(')'))
outm = ''.join(near)
return outm, 200
if __name__ == '__main__':
db.create_all()
print('start')
df.to_sql(name = 'pincode', con = db.engine, schema = 'public', index = False, if_exists = 'append', chunksize = 100)
print('end')
app.run(debug = True, use_reloader = False)