Thanks to Kashfi Fahim for suggesting this tool.
{ search_address : Search Address ? Find the basketball courts closest to this address }
{ search_count : Search Count ? Limit the number of basketball courts shown }
{ source_table_url : NYC Open Data JSON URL ? Specify the data source }
The Table of Basketball Courts in NYC is from the NYC Open Data Website.
The Tech Incubator at CUNY Queens College is hosting a Student Showcase of Tools Made with NYC Open Data Using Python on Sunday, March 4, 2018 from 3pm to 4:30pm.
Visit NYC Open Data Week for more events featuring NYC Open Data.
# CrossCompute
search_address = '2435 Grand Concourse, Bronx, NY 10468'
search_count = 5
source_table_url = 'https://www.nycgovparks.org/bigapps/DPR_Basketball_001.json'
target_folder = '/tmp'
import pandas as pd
t = pd.read_json(source_table_url)
t.head()
t.iloc[0]
t[['lat', 'lon']][:3]
source_latlons = t[['lat', 'lon']].values
source_latlons
from scipy.spatial.kdtree import KDTree
tree = KDTree([
(0, 0),
(0.7, 0.75),
(1, 1),
(1, 0),
])
tree
tree.query_ball_point([1, 1], r=0.5)
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 scipy.spatial import kdtree
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)
import numpy as np
x = np.array([
[0, 1],
[1, 2],
[3, 4],
])
x
indices = [0, 1]
x[indices]
source_tree.query_ball_point?
t.iloc[0]
t.head(n=10)
indices = [1, 3, 5, 7, 9]
t.iloc[indices]
t.iloc[indices]
selected_t = t.iloc[indices].copy()
selected_t['Distance'] = distances
selected_t
from os.path import join
target_path = join(target_folder, 'locations.csv')
selected_t.to_csv(target_path, index=False)
print('selected_location_geotable_path = %s' % target_path)
{ selected_location_geotable : Nearest Basketball Courts ? Click on a circle to see details }