This notebook-tool showcases how to use KDTrees to query closest points to a location in miles
{lat_float: latitude} {lng_float: longitude}
target_folder = '.'
lat_float = 40.705735
lng_float = -73.814284
import os
import pandas as pd
import pysal
from pysal.cg.kdtree import KDTree
def get_closest_spots(curr_location, locations):
tree = KDTree(locations.values, distance_metric='Arc', radius=pysal.cg.RADIUS_EARTH_MILES)
distances, indices = tree.query(curr_location, k=10)
return distances, indices
# get all points within half mile
# indices = tree.query_ball_point(curr, 0.5)
# print(data.ix[indices]['ssid'])
if __name__ == '__main__':
url = 'https://data.cityofnewyork.us/resource/jd4g-ks2z.json'
data = pd.read_json(url)
locations = data[['lat', 'lon']]
distances, indices = get_closest_spots((lat_float, lng_float), locations)
columns=['name',
'latitude',
'longitude',
'fill color',
'radius in pixels']
data = [[i[0], i[1], i[2], 'red', 10] for i in data.ix[indices][['location', 'lat', 'lon']].values]
path = os.path.join(target_folder, 'geomap.csv')
pd.DataFrame(data, columns=columns).to_csv(path, index=False)
print("points_geotable_path = {0}".format(path))