-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
82 lines (65 loc) · 2.33 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
from flask import Flask,request,render_template
import pickle as pkl
from math import radians, cos, sin, asin, sqrt
from shapely.geometry import Point
from shapely.geometry.polygon import Polygon
from geopy.geocoders import Nominatim
import json
geolocator = Nominatim()
app = Flask(__name__)
@app.route('/')
def my_form():
return render_template('input.html')
@app.route("/detect",methods=["GET"])
def getPlaceName():
lat = float(request.args["lat"])
long = float(request.args["long"])
flag = 'null'
with open("data.pkl","rb") as f:
df = pkl.load(f)
for place in df.columns:
polygon = Polygon(df[place].values[0]) # create polygon
point = Point(long,lat) # create point
print(point.within(polygon))
if(point.within(polygon)):
flag = place
if flag != 'null':
return flag
else:
return 'No place found'
#calculating the distance between two Point
def distance(lon1, lat1, lon2, lat2):
"""
Calculate the great circle distance between two points
on the earth (specified in decimal degrees)
"""
# convert decimal degrees to radians
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * asin(sqrt(a))
r = 6371 # Radius of earth in kilometers. Use 3956 for miles
return c * r
@app.route("/get_using_self")
def postcode():
long, lat = [float(request.args.get('long')), float(request.args.get('lat'))]
with open("data.pkl","rb") as f:
df = pkl.load(f)
details = {}
for city_belong in df.columns:
test_point = df[city_belong][0]
# print(city_belong,test_point)
for i in test_point:
distance_in_km = distance(long,lat,i[0],i[1])
# print('Distance (km):',a)
if distance_in_km <= 5:
location = geolocator.reverse([i[1],i[0]])
if location != None:
details[location.raw['address']['postcode']] = location.raw['display_name']
print(location.raw['address']['postcode'])
print(location.raw['display_name'])
return json.dumps(details)
if __name__ == '__main__':
app.run(debug=True)