NYC Open Data Examples 20180202-0046




Pay Notebook Creator: Yasin Ehsan0
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 [1]:
# 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 [2]:
import pandas as pd
t = pd.read_json(source_table_url)
t.head()
Out[2]:
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; } </style>
Accessible Location Name Num_of_Courts Prop_ID lat lon
0 N E 174 St. & Bronx River Ave. 174th Street Playground NaN X159 40.8342 -73.8775
1 N Kelly St. & Ave. St. John 52 Playground NaN X179 40.8149 -73.9021
2 N Tinton Ave. & E 156 St. Abigail Adams Playground NaN X216 40.8172 -73.9044
3 N Barnes Ave. & E 215 St. Agnes Haywood Playground NaN X169 40.8804 -73.8619
4 N Bouck & Throop Aves. Allerton Playground NaN X172 40.8659 -73.8503
In [3]:
t.iloc[0]
Out[3]:
Accessible                                  N
Location         E 174 St. & Bronx River Ave.
Name                  174th Street Playground
Num_of_Courts                             NaN
Prop_ID                                  X159
lat                                   40.8342
lon                                  -73.8775
Name: 0, dtype: object
In [4]:
t[['lat', 'lon']][:3]
Out[4]:
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; } </style>
lat lon
0 40.8342 -73.8775
1 40.8149 -73.9021
2 40.8172 -73.9044
In [5]:
source_latlons = t[['lat', 'lon']].values
source_latlons
Out[5]:
array([[ 40.8342, -73.8775],
       [ 40.8149, -73.9021],
       [ 40.8172, -73.9044],
       ...,
       [     nan,      nan],
       [ 40.7772, -73.9031],
       [ 40.6817, -73.9595]])
In [6]:
from scipy.spatial.kdtree import KDTree
tree = KDTree([
    (0, 0),
    (0.7, 0.75),
    (1, 1),
    (1, 0),
])
tree
Out[6]:
<scipy.spatial.kdtree.KDTree at 0x7f23cfe042e8>
In [7]:
tree.query_ball_point([1, 1], r=0.5)
Out[7]:
[1, 2]
In [8]:
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
Out[8]:
(40.8612109, -73.8980113)
In [9]:
import pip
pip.main(['install', 'pysal'])
Collecting pysal
  Downloading PySAL-1.14.3.tar.gz (17.8MB)
Requirement already satisfied: scipy>=0.11 in /usr/lib64/python3.6/site-packages (from pysal)
Requirement already satisfied: numpy>=1.3 in /home/user/.virtualenvs/crosscompute/lib/python3.6/site-packages (from pysal)
Building wheels for collected packages: pysal
  Running setup.py bdist_wheel for pysal: started
  Running setup.py bdist_wheel for pysal: finished with status 'done'
  Stored in directory: /home/user/.cache/pip/wheels/07/1a/85/674d3de7848408b7a8b1b38f1e071128629efeac378e101b44
Successfully built pysal
Installing collected packages: pysal
Successfully installed pysal-1.14.3
Out[9]:
0
In [10]:
from scipy.spatial import kdtree
In [11]:
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)
[0.09111751 0.10428859 0.3151998  0.368613   0.38154487]
[ 84  88  36 100   6]
In [12]:
import numpy as np
x = np.array([
    [0, 1],
    [1, 2],
    [3, 4],
])
x
Out[12]:
array([[0, 1],
       [1, 2],
       [3, 4]])
In [13]:
indices = [0, 1]
x[indices]
Out[13]:
array([[0, 1],
       [1, 2]])
In [14]:
source_tree.query_ball_point?
In [15]:
t.iloc[0]
Out[15]:
Accessible                                  N
Location         E 174 St. & Bronx River Ave.
Name                  174th Street Playground
Num_of_Courts                             NaN
Prop_ID                                  X159
lat                                   40.8342
lon                                  -73.8775
Name: 0, dtype: object
In [16]:
t.head(n=10)
Out[16]:
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; } </style>
Accessible Location Name Num_of_Courts Prop_ID lat lon
0 N E 174 St. & Bronx River Ave. 174th Street Playground NaN X159 40.8342 -73.8775
1 N Kelly St. & Ave. St. John 52 Playground NaN X179 40.8149 -73.9021
2 N Tinton Ave. & E 156 St. Abigail Adams Playground NaN X216 40.8172 -73.9044
3 N Barnes Ave. & E 215 St. Agnes Haywood Playground NaN X169 40.8804 -73.8619
4 N Bouck & Throop Aves. Allerton Playground NaN X172 40.8659 -73.8503
5 N Gun Hill Rd., Eastchester Rd. and O'Neill Pl. Angelo Campanero Playground NaN X187 40.8679 -73.8430
6 N W 183 St., Aqueduct Ave., W Tremont Ave. and U... Aqueduct Lands Playground NaN X001 40.8634 -73.9035
7 N E 164 St. & Teller Ave. Arcilla Playground NaN X219 40.8271 -73.9145
8 N Bailey Ave. & W 234-W 238 Sts. Bailey Playground NaN X150J 40.8811 -73.9008
9 N E 166 St., Union Ave. and Tinton Ave. Behagen Playground NaN X166 40.8254 -73.9015
In [17]:
indices = [1, 3, 5, 7, 9]
t.iloc[indices]
Out[17]:
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; } </style>
Accessible Location Name Num_of_Courts Prop_ID lat lon
1 N Kelly St. & Ave. St. John 52 Playground NaN X179 40.8149 -73.9021
3 N Barnes Ave. & E 215 St. Agnes Haywood Playground NaN X169 40.8804 -73.8619
5 N Gun Hill Rd., Eastchester Rd. and O'Neill Pl. Angelo Campanero Playground NaN X187 40.8679 -73.8430
7 N E 164 St. & Teller Ave. Arcilla Playground NaN X219 40.8271 -73.9145
9 N E 166 St., Union Ave. and Tinton Ave. Behagen Playground NaN X166 40.8254 -73.9015
In [18]:
t.iloc[indices]
Out[18]:
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; } </style>
Accessible Location Name Num_of_Courts Prop_ID lat lon
1 N Kelly St. & Ave. St. John 52 Playground NaN X179 40.8149 -73.9021
3 N Barnes Ave. & E 215 St. Agnes Haywood Playground NaN X169 40.8804 -73.8619
5 N Gun Hill Rd., Eastchester Rd. and O'Neill Pl. Angelo Campanero Playground NaN X187 40.8679 -73.8430
7 N E 164 St. & Teller Ave. Arcilla Playground NaN X219 40.8271 -73.9145
9 N E 166 St., Union Ave. and Tinton Ave. Behagen Playground NaN X166 40.8254 -73.9015
In [19]:
selected_t = t.iloc[indices].copy()
selected_t['Distance'] = distances
selected_t
Out[19]:
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; } </style>
Accessible Location Name Num_of_Courts Prop_ID lat lon Distance
1 N Kelly St. & Ave. St. John 52 Playground NaN X179 40.8149 -73.9021 0.091118
3 N Barnes Ave. & E 215 St. Agnes Haywood Playground NaN X169 40.8804 -73.8619 0.104289
5 N Gun Hill Rd., Eastchester Rd. and O'Neill Pl. Angelo Campanero Playground NaN X187 40.8679 -73.8430 0.315200
7 N E 164 St. & Teller Ave. Arcilla Playground NaN X219 40.8271 -73.9145 0.368613
9 N E 166 St., Union Ave. and Tinton Ave. Behagen Playground NaN X166 40.8254 -73.9015 0.381545
In [20]:
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_path = /tmp/locations.csv

Nearest Basketball Courts

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

In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]: