NYC Open Data Examples 20180202-0046




Pay Notebook Creator: Yasin Ehsan0
Set Container: Numerical CPU with TINY Memory for 10 Minutes 0
Total0
In [16]:
#crosscompute
#copy the prevoius code
#get dataset nyc data: nyc hotspots
#show map of ten hotspots near you

searches = 10
address = '85-52 168th street'
url = 'https://data.cityofnewyork.us/api/views/yjub-udmw/rows.csv?accessType=DOWNLOAD'
target_folder = '/tmp'
In [17]:
import pandas as pd
In [18]:
table = pd.read_csv(url)
In [20]:
import geopy

# Convert address to latitude and longitude
geocode = geopy.GoogleV3('AIzaSyDNqc0tWzXHx_wIp1w75-XTcCk4BSphB5w').geocode
search_location = geocode(address)
search_latlon = search_location.latitude, search_location.longitude
search_latlon
Out[20]:
(40.7120757, -73.7960246)
In [21]:
import pip
pip.main(['install', 'pysal'])
Requirement already satisfied: pysal in /home/user/.virtualenvs/crosscompute/lib/python3.6/site-packages
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)
Out[21]:
0
In [22]:
table.iloc[0]
Out[22]:
OBJECTID                                                             998
Borough                                                               MN
Type                                                                Free
Provider                                            LinkNYC - Citybridge
Name                                                        mn-05-123662
Location                                              179 WEST 26 STREET
Latitude                                                          40.746
Longitude                                                        -73.994
X                                                                 985902
Y                                                                 211053
Location_T                                                 Outdoor Kiosk
Remarks                 Tablet Internet -phone , Free 1 GB Wi-FI Service
City                                                            New York
SSID                                                  LinkNYC Free Wi-Fi
SourceID                                                     LINK-008695
Activated                                                     01/18/2017
BoroCode                                                               1
BoroName                                                       Manhattan
NTACode                                                             MN17
NTAName                                            Midtown-Midtown South
CounDist                                                               3
Postcode                                                           10001
BoroCD                                                               105
CT2010                                                                95
BCTCB2010                                                        1009500
BIN                                                                    0
BBL                                                                    0
DOITT_ID                                                            1425
Location (Lat, Long)         New York\n(40.74596800000, -73.99403900000)
Name: 0, dtype: object
In [23]:
source_latlons = table[['Latitude', 'Longitude']].values
In [24]:
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=searches)
print(distances)
print(indices)
[0.12337947 0.13396052 0.14828002 0.15363211 0.1624178  0.17008907
 0.19250907 0.19431536 0.19577347 0.19899113]
[1469  871  854  872  495 1860  870  315  869  865]
In [25]:
var = table[['Latitude', 'Longitude','Borough']]
var[:3]
Out[25]:
<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>
Latitude Longitude Borough
0 40.745968 -73.994039 MN
1 40.744614 -73.985069 MN
2 40.757666 -73.985878 MN
In [26]:
location = table.iloc[indices].copy()

from os.path import join
target_path = join(target_folder, 'locations.csv')
location.to_csv(target_path, index=False)
print('selected_location_geotable_path = %s' % target_path)
selected_location_geotable_path = /tmp/locations.csv
In [ ]: