This repository has been archived by the owner on Jul 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Jimut_Cluster.py
216 lines (198 loc) · 7.35 KB
/
Jimut_Cluster.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
from tkinter import Tk, Label, Button, Entry, StringVar, DISABLED, NORMAL, END, W, E, N, S
# tranforming json file into a pandas dataframe library
from http.server import BaseHTTPRequestHandler, HTTPServer
from math import sin, cos, sqrt, atan2, radians
from pandas.io.json import json_normalize
from folium.plugins import MarkerCluster
from tempfile import NamedTemporaryFile
from geopy.geocoders import Nominatim # module to convert an address into latitude and longitude values
from IPython.core.display import HTML
from IPython.display import Image
from datetime import datetime
from tkinter import *
import tkinter as tk
import pandas as pd # library for data analsysis
import numpy as np # library to handle data in a vectorized manner
import subprocess
import webbrowser
import requests # library to handle requests
import argparse
import random # library for random number generation
import folium # plotting library
import json
import os
def Jimut_cluster(map_data):
import folium # plotting library
"""
Takes in :=>
map_data = [("Meat","Khasi",22.569098,88.366418),
("Meat","Chasi",22.562298,88.376218),
("Tea","My Tea",22.582298,88.367218),
("Museum","museum1",22.570298,88.352218),
("Museum","museum2",22.492298,88.362218),
("Museum","museum3",22.592298,88.307218),
("Chinese Res","Indo China",22.542298,88.397218),
("Thai Res","My Thai",22.535298,88.397218),
("Thai Res","Thaism",22.535298,88.387218),
("Thai Res","Nilu Thai",22.538298,88.396218),
("Airport","NSCB",22.338298,88.333218),
("Fishery","DODO Fishery",22.738298,88.696218),
("Motel","Baloo's Dhaba",22.608298,88.437218)
]
Outputs=> Map
"""
# approximate radius of earth in km
R = 6373.0
def ret_dist(lat1,lon1,lat2,lon2):
lat1 = radians(lat1)
lon1 = radians(lon1)
lat2 = radians(lat2)
lon2 = radians(lon2)
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2
c = 2 * atan2(sqrt(a), sqrt(1 - a))
distance = R * c
#print("Result:", distance)
#print("Should be:", 278.546, "km")
return distance
string_gen = "0123456789abcdef"
def get_random_col():
# unnecessary stuffs to make the visualization cool
ret_str = "#"
for i in range(6):
ret_str += random.choice(string_gen)
return ret_str
latitude = 22.569098
longitude = 88.366418
MAP = folium.Map(location=[latitude, longitude],tiles="Stamen Toner", zoom_start=11)
name=map_data[0][0]
full_tree = []
data_cate = []
OVER_COL = str(get_random_col())
FILL_COL = str(get_random_col())
for item in map_data:
lat = item[2]
lng = item[3]
#print(item," => ")
if(item[0]!=name):
OVER_COL = str(get_random_col())
FILL_COL = str(get_random_col())
full_tree.append([name,data_cate])
name=item[0]
data_cate=[]
#print("got into first check")
if(item[0]==name):
data_cate.append([item[1],lat,lng])
#print("got into 2nd check")
label = "cat : {}, Name : {}".format(item[0],item[1])
folium.CircleMarker(
[lat, lng],
radius=5,
popup=label,
color=OVER_COL,
fill=True,
fill_color=FILL_COL,
fill_opacity=0.7).add_to(MAP)
full_tree.append((name,data_cate))
#MAP
import pandas as pd
distance_api = []
# for i in map_data:
# print(i[0],end=" ")
# print()
index_mat = []
for i in map_data:
lis = []
index_mat.append(str(i[0]+"_"+i[1]))
for j in map_data:
#print(i[2],i[3]," ",j[2],j[3],end="")
#distance_api.append([[i[2],i[3]],[j[2],j[3]],[ret_dist(i[2],i[3],j[2],j[3])]])
lis.append(ret_dist(i[2],i[3],j[2],j[3]))
#distance_api[([i[2],i[3])][([j[2],j[3]])] = ret_dist(i[2],i[3],j[2],j[3])
#distance_api[i[1]][j[1]] = ret_dist(i[2],i[3],j[2],j[3])
p1=[i[2],i[3]]
p2=[j[2],j[3]]
#print(DataFrame(ret_dist(i[2],i[3],j[2],j[3]),end=" "))
folium.PolyLine(locations=[p1, p2], color='blue',weight=0.5,opacity=1).add_to(MAP)
distance_api.append(lis)
#print()
#print(distance_api)
#MAP
list_dist_final = []
for item in full_tree:
add_dist = 0
#print(item[0])
for k in item[1:]:
for var in k:
name_ = var[0]
lat_ = var[1]
lon_ = var[2]
#print("POI => ",name_," ",lat_," ",lon_)
# sub
for item_ in full_tree:
if(item_[0]!=item[0]):
for k_ in item_[1:]:
for var_ in k_:
name__ = var_[0]
lat__ = var_[1]
lon__ = var_[2]
#print(name__," ",lat__," ",lon__,end="")
dis = ret_dist(lat_,lon_,lat__,lon__)
#print(" dist => ",dis)
add_dist += dis
#print("ADD DIST =====> ",add_dist)
list_dist_final.append([item[0],name_,add_dist,[lat_,lon_]])
#print(list_dist_final)
# getting unique categories
name_it = list_dist_final[0][0]
min_ = list_dist_final[0][2]
fin_opt_list = []
for item in list_dist_final:
if(name_it==item[0]):
if(min_>=item[2]):
cat_op = item[0]
min_ = item[2]
op_name = item[1]
lat_lon = item[3:]
if(name_it!=item[0]):
fin_opt_list.append([cat_op,min_,op_name,lat_lon])
min_ = item[2]
name_it = item[0]
cat_op = item[0]
op_name = item[1]
lat_lon = item[3:]
fin_opt_list.append([cat_op,min_,op_name,lat_lon])
#print(fin_opt_list)
import pandas as pd
latitude = 22.569098
longitude = 88.366418
F_MAP = folium.Map(location=[latitude, longitude],tiles="Stamen Toner", zoom_start=11)
for i in fin_opt_list:
for j in fin_opt_list:
p1 = i[3]
p2 = j[3]
#print(p1[0])
#print(p2[0])
folium.PolyLine(locations=[p1[0], p2[0]], color='red',weight=1.5,opacity=1).add_to(F_MAP)
#F_MAP
name=map_data[0][0]
for item in map_data:
lat = item[2]
lng = item[3]
#print(item," => ")
if(item[0]!=name):
OVER_COL = str(get_random_col())
FILL_COL = str(get_random_col())
name = item[0]
label = "cat : {}, Name : {}".format(item[0],item[1])
folium.CircleMarker(
[lat, lng],
radius=5,
popup=label,
color=OVER_COL,
fill=True,
fill_color=FILL_COL,
fill_opacity=0.7).add_to(F_MAP)
print("DONE!")
return F_MAP