points_within_x_miles




Pay Notebook Creator: Salah Ahmed0
Set Container: Numerical CPU with TINY Memory for 10 Minutes 0
Total0
In [1]:
from shapely.wkt import loads as wkt_loads
In [9]:
import pysal
from pysal.lib.cg.kdtree import KDTree
/home/user/.virtualenvs/crosscompute/lib/python3.6/site-packages/pysal/lib/weights/util.py:19: UserWarning: geopandas not available. Some functionality will be disabled.
  warn('geopandas not available. Some functionality will be disabled.')
/home/user/.virtualenvs/crosscompute/lib/python3.6/site-packages/pysal/model/spvcm/abstracts.py:10: UserWarning: The `dill` module is required to use the sqlite backend fully.
  from .sqlite import head_to_sql, start_sql
In [4]:
from shapely.geometry import GeometryCollection
In [2]:
import pandas
In [3]:
points = pandas.read_csv('selected-features.csv')
points
Out[3]:
<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>
Description Population in 2016 RadiusInPixelsRange10-20 FillBlues LongitudeLatitudeWkt
0 New York, NY 8537673.0 8537673.0 1.0 POINT (-74.0059413 40.7127837)
1 Philadelphia, PA 1567872.0 1567872.0 3.0 POINT (-75.1652215 39.9525839)
2 Ann Arbor, MI 120782.0 120782.0 5.0 POINT (-83.7430378 42.2808256)
3 Mansfield, MO 1261.0 1261.0 7.0 POINT (-92.5807231 37.1067189)
4 From Mansfield, MO to Ann Arbor, MI NaN NaN NaN LINESTRING (-92.5807231 37.1067189, -83.743037...
In [5]:
points = [wkt_loads(p) for p in points['LongitudeLatitudeWkt'].values if 'POINT' in p]
GeometryCollection(points)
Out[5]:
<shapely.geometry.collection.GeometryCollection at 0x7f13f10f4fd0>
In [12]:
selected_point = points[0].y, points[0].x
In [10]:
locations = [(p.y, p.x) for p in points[1:]]
locations
Out[10]:
[(39.9525839, -75.1652215),
 (42.2808256, -83.7430378),
 (37.1067189, -92.5807231)]
In [13]:
# new york
selected_point
Out[13]:
(40.7127837, -74.0059413)
In [ ]:
'''
locations = [(40.702566, -73.816859),
         (40.70546, -73.810708),
         (40.709179, -73.820574),
         (40.700486, -73.807969),
         (40.694624, -73.820593),
         (40.695132, -73.820841),
         (40.694095, -73.821334),
         (40.694165, -73.822368),
         (40.695077, -73.822817),
         (40.6747769261, -73.8092618174)] 
'''
In [25]:
tree = KDTree(locations, distance_metric='Arc', radius=pysal.lib.cg.RADIUS_EARTH_MILES)
current_point = (40.709523, -73.802472)
# get all points within 1 mile of 'current_point'
indices = tree.query_ball_point(selected_point,  85)
for i in indices:
    print(locations[i])
(39.9525839, -75.1652215)