#CrossCompute
search_address = '11356'
search_count = 7
source_table_path = 'courts.csv'
target_folder = '/tmp'
import pandas as pd
t = pd.read_csv(source_table_path)
t.head()
t = t.dropna(subset=['lat', 'lon'])
t= t[t['Accessible']== 'Y']
t.head()
t.iloc[0]
t[['lat', 'lon']][:5]
source_latlons = t[['lat', 'lon']].values
source_latlons
import geopy
# Convert address to latitude and longitude
geocode = geopy.GoogleV3('AIzaSyDNqc0tWzXHx_wIp1w75-XTcCk4BSphB5w').geocode
search_location = geocode(search_address)
search_latlon = search_location.latitude, search_location.longitude
search_latlon
import pip
pip.main(['install', 'pysal'])
from pysal.cg import RADIUS_EARTH_MILES
from pysal.cg.kdtree import KDTree
source_tree = KDTree(source_latlons, distance_metric='Arc', radius=RADIUS_EARTH_MILES)
distances, indices = source_tree.query(search_latlon, k=search_count)
print(distances)
print(indices)
selected_t = t.iloc[indices].copy()
selected_t['Distance'] = distances
selected_t
target_path = target_folder + '/map.csv'
selected_t.to_csv(target_path, index=False)
print('map_geotable_path = %s' % target_path)
%matplotlib inline
fig = t.plot()
ax = t.plot() # s is an instance of Series
fig = ax.get_figure()
ax = selected_t[['Name', 'Distance']].plot(kind = 'bar')
fig = ax.get_figure()
target_path = target_folder + '/x.png'
fig.savefig(target_path)
print('abc_image_path = %s' % target_path)