NYC Open Data Examples




Pay Notebook Creator: Roy Hyunjin Han0
Set Container: Numerical CPU with TINY Memory for 10 Minutes 0
Total0

Find the Nearest Basketball Court

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.

In [ ]:
# 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'
In [ ]:
import pandas as pd
t = pd.read_json(source_table_url)
t.head()
In [ ]:
# Drop rows that are missing coordinates
t.dropna(subset=['lat', 'lon'], inplace=True)
In [ ]:
t.iloc[0]
In [ ]:
t[['lat', 'lon']][:3]
In [ ]:
source_latlons = t[['lat', 'lon']].values
source_latlons
In [ ]:
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
In [ ]:
import pip
pip.main(['install', 'pysal'])
In [ ]:
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)
In [ ]:
selected_t = t.iloc[indices].copy()
selected_t['Distance'] = distances
selected_t
In [ ]:
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)

Nearest Basketball Courts

{ selected_location_geotable : Nearest Basketball Courts ? Click on a circle to see details }