Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prakharcode/dd1a4398948d0ae438fe9e8cc59d2de7 to your computer and use it in GitHub Desktop.
Save prakharcode/dd1a4398948d0ae438fe9e8cc59d2de7 to your computer and use it in GitHub Desktop.
def calc_poi_opr_in_radius(poi, entity, radius, **kwargs):
entity = entity.copy()
KDT = KDTree(poi[['lat', 'long']])
deg_to_m = 1/108000.0
query = entity.progress_apply(lambda g: KDT.query_radius([[g.lat, g.long1]], r= deg_to_m * radius), axis=1)
name = kwargs['name'] if kwargs['name'] else 'default'
pos_item = list(poi.columns).index(kwargs['avg_by'])
col = kwargs['avg_by']
entity.loc[:, kwargs['opr'] + "_" + name] = [ eval("poi.iloc[ x[0], pos_item].{0}()".format(kwargs['opr']), {'poi': poi, 'x':x,'pos_item': pos_item, 'col':col }) if len(x[0]) > 0 else 0 for x in tqdm(query)]
return entity
@prakharcode
Copy link
Author

pois = {'parlours': [parlours, 'count', 100, 'name'],
'doctors':[doctors, 'count', 100, 'link'],
'salons': [salons, 'count', 100, 'name'],
'schools': [schools, 'count', 100, 'objectid'],
'family': [family_size, 'sum', 1000, 'FamilyCounts'],
'NF': [family_size, 'sum', 1000, 'NuclearFamilyCounts']
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment