-
Notifications
You must be signed in to change notification settings - Fork 0
/
potential-locations.py
233 lines (175 loc) · 8.02 KB
/
potential-locations.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
import numpy as np
import pandas as pd
import math
from scipy.spatial.distance import cdist
from sklearn.cluster import KMeans
# Functions for clustering nearby locations -----------------------------------
def distance(orig, dest):
lat1, lon1 = orig[0], orig[1]
lat2, lon2 = dest[0], dest[1]
radius = 6371 # km
d_lat = math.radians(lat2 - lat1)
d_lon = math.radians(lon2 - lon1)
a = math.sin(d_lat/2) * math.sin(d_lat/2) + math.cos(math.radians(lat1)) \
* math.cos(math.radians(lat2)) * math.sin(d_lon/2) * math.sin(d_lon/2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
d = radius * c
return d
def create_clusters(num_clusts, locs):
kmeans = KMeans(n_clusters=num_clusts, random_state=2022).fit(locs)
l_array = np.array([[label] for label in kmeans.labels_])
clusts = np.append(locs, l_array, axis=1)
return clusts
def validate_solution(max_dist, clusts):
_, __, num_clusts = clusts.max(axis=0)
num_clusts = int(num_clusts)
for i in range(num_clusts):
clust_2d = clusts[clusts[:, 2] == i][:, np.array([True, True, False])]
if not validate_cluster(max_dist=max_dist, clust=clust_2d):
return False
else:
continue
return True
def validate_cluster(max_dist, clust):
distances = cdist(
XA=clust,
XB=clust,
metric=lambda orig, dest: distance(orig=orig, dest=dest))
for item in distances.flatten():
if item > max_dist:
return False
return True
def cluster_nearby_locations(max_dist, locs):
for i in range(2, len(locs)):
print(str(i) + '/' + str(len(locs)))
clusts = create_clusters(num_clusts=i, locs=locs)
if validate_solution(max_dist=max_dist, clusts=clusts):
break
clusts = pd.DataFrame(data=clusts, columns=['lat', 'lon', 'clust'])
clusts = clusts.groupby(by='clust').agg({'lat': 'mean', 'lon': 'mean'})
clusts = clusts.reset_index().drop(columns='clust')
return clusts
all_locs = pd.DataFrame(columns=['name', 'lat', 'lon'])
# Bank of Ceylon --------------------------------------------------------------
locs = pd.read_json('data/bank/bank_of_ceylon.json')
locs = np.array(object=locs[['lat', 'lng']], dtype='float')
locs = np.unique(ar=locs, axis=0)
locs = cluster_nearby_locations(max_dist=0.1, locs=locs)
locs['name'] = 'Bank of Ceylon'
locs = locs.astype({'name': 'string', 'lat': 'float', 'lon': 'float'})
all_locs = pd.concat(objs=[all_locs, locs])
del locs
# Cargills Bank ---------------------------------------------------------------
locs = pd.read_json('data/bank/cargills_bank.json')
locs = np.array(object=locs[['lat', 'lng']], dtype='float')
locs = np.unique(ar=locs, axis=0)
locs = cluster_nearby_locations(max_dist=0.1, locs=locs)
locs['name'] = 'Cargills Bank'
locs = locs.astype({'name': 'string', 'lat': 'float', 'lon': 'float'})
all_locs = pd.concat(objs=[all_locs, locs])
del locs
# Commercial Bank of Ceylon ---------------------------------------------------
locs = pd.concat(objs=[
pd.read_json('data/bank/commercial_bank_atm.json'),
pd.read_json('data/bank/commercial_bank_branch.json')])
locs = locs.pop('children')
locs = [loc['location'] for loc_group in locs for loc in loc_group]
locs = pd.DataFrame(data=locs)
locs = np.array(object=locs[['latitude', 'longitude']], dtype='float')
locs = np.unique(ar=locs, axis=0)
locs = cluster_nearby_locations(max_dist=0.1, locs=locs)
locs['name'] = 'Commercial Bank of Ceylon'
locs = locs.astype({'name': 'string', 'lat': 'float', 'lon': 'float'})
all_locs = pd.concat(objs=[all_locs, locs])
del locs
# DFCC Bank -------------------------------------------------------------------
locs = pd.read_json('data/bank/dfcc_bank.json')
locs = np.array(object=locs[['lat', 'lng']], dtype='float')
locs = np.unique(ar=locs, axis=0)
locs = cluster_nearby_locations(max_dist=0.1, locs=locs)
locs['name'] = 'DFCC Bank'
locs = locs.astype({'name': 'string', 'lat': 'float', 'lon': 'float'})
all_locs = pd.concat(objs=[all_locs, locs])
del locs
# Hatton National Bank --------------------------------------------------------
locs = pd.read_json('data/bank/hatton_national_bank.json')
locs = locs[locs['latitude'].astype(bool)]
locs = locs[locs['longitude'].astype(bool)]
locs = np.array(object=locs[['latitude', 'longitude']], dtype='float')
locs = np.unique(ar=locs, axis=0)
locs = cluster_nearby_locations(max_dist=0.1, locs=locs)
locs['name'] = 'Hatton National Bank'
locs = locs.astype({'name': 'string', 'lat': 'float', 'lon': 'float'})
all_locs = pd.concat(objs=[all_locs, locs])
del locs
# National Development Bank ---------------------------------------------------
locs = pd.read_json('data/bank/national_development_bank.json')
locs = locs[locs['geo_code'].astype(bool)]
locs['lat'] = [loc.split(',')[0] for loc in locs['geo_code']]
locs['lon'] = [loc.split(',')[1] for loc in locs['geo_code']]
locs = np.array(object=locs[['lat', 'lon']], dtype='float')
locs = np.unique(ar=locs, axis=0)
locs = cluster_nearby_locations(max_dist=0.1, locs=locs)
locs['name'] = 'National Development Bank'
locs = locs.astype({'name': 'string', 'lat': 'float', 'lon': 'float'})
all_locs = pd.concat(objs=[all_locs, locs])
del locs
# Nations Trust Bank ----------------------------------------------------------
locs = pd.concat(objs=[
pd.read_json('data/bank/nations_trust_bank_atm.json'),
pd.read_json('data/bank/nations_trust_bank_branch.json')])
locs = np.array(object=locs[[1, 2]], dtype='float')
locs = np.unique(ar=locs, axis=0)
locs = cluster_nearby_locations(max_dist=0.1, locs=locs)
locs['name'] = 'Nations Trust Bank'
locs = locs.astype({'name': 'string', 'lat': 'float', 'lon': 'float'})
all_locs = pd.concat(objs=[all_locs, locs])
del locs
# People's Bank ---------------------------------------------------------------
locs_atm = pd.read_json('data/bank/peoples_bank_atm.json')
locs_atm['lat'] = locs_atm['lat']
locs_atm['lon'] = locs_atm['lng']
locs_branch = pd.read_json('data/bank/peoples_bank_branch.json')
locs_branch['lat'] = [loc['lat'] for loc in locs_branch['position']]
locs_branch['lon'] = [loc['lng'] for loc in locs_branch['position']]
locs = pd.concat(objs=[locs_atm, locs_branch])
del locs_atm, locs_branch
locs = np.array(object=locs[['lat', 'lon']], dtype='float')
locs = np.unique(ar=locs, axis=0)
locs = cluster_nearby_locations(max_dist=0.1, locs=locs)
locs['name'] = 'People\'s Bank'
locs = locs.astype({'name': 'string', 'lat': 'float', 'lon': 'float'})
all_locs = pd.concat(objs=[all_locs, locs])
del locs
# Sampath Bank ----------------------------------------------------------------
locs = pd.read_xml('data/bank/sampath_bank.xml')
locs = locs[locs['lat'].notna()]
locs = locs[locs['lng'].notna()]
locs = np.array(object=locs[['lat', 'lng']], dtype='float')
locs = np.unique(ar=locs, axis=0)
locs = cluster_nearby_locations(max_dist=0.1, locs=locs)
locs['name'] = 'Sampath Bank'
locs = locs.astype({'name': 'string', 'lat': 'float', 'lon': 'float'})
all_locs = pd.concat(objs=[all_locs, locs])
del locs
# Seylan Bank -----------------------------------------------------------------
locs = pd.read_json('data/bank/seylan_bank.geojson')
locs['lat'] = [loc['geometry']['coordinates'][1] for loc in locs['features']]
locs['lon'] = [loc['geometry']['coordinates'][0] for loc in locs['features']]
locs = np.array(object=locs[['lat', 'lon']], dtype='float')
locs = np.unique(ar=locs, axis=0)
locs = cluster_nearby_locations(max_dist=0.1, locs=locs)
locs['name'] = 'Seylan Bank'
locs = locs.astype({'name': 'string', 'lat': 'float', 'lon': 'float'})
all_locs = pd.concat(objs=[all_locs, locs])
del locs
# Existing Locations ----------------------------------------------------------
locs = pd.read_csv('results/mobile_money_locations.csv')
locs = locs[~locs['name'].isin(['Commercial Bank ATM', 'Sampath Bank ATM'])]
locs = locs.drop(columns='id')
all_locs = pd.concat(objs=[locs, all_locs])
del locs
# Write to disk ---------------------------------------------------------------
all_locs['id'] = range(1, len(all_locs) + 1)
all_locs = all_locs.set_index(keys='id')
all_locs.to_csv('results/potential_locations.csv')