Skip to content

Instantly share code, notes, and snippets.

@flibbertigibbet
Created December 14, 2013 06:03
Show Gist options
  • Save flibbertigibbet/7956133 to your computer and use it in GitHub Desktop.
Save flibbertigibbet/7956133 to your computer and use it in GitHub Desktop.
Finds the centroids of 50km hexagons to tile a bounding box and writes the coordinates to a file. Then, creates a bash script to query Google Places for each point and write the responses to files. (There's some overlap between the queried areas, but it should be minimal.) Here, it's searching for grocery stores in New Jersey.
#!/usr/bin/env python
import csv
from math import cos, pi
# API key for Google Places
api_key= 'YOUR_KEY_GOES_HERE'
outf = open('njpoints.csv','w')
w = csv.writer(outf)
# bounding box for NJ
minlon = -75.563586
maxlon = -73.88506 # 1.678526 diff
minlat = 38.788657
maxlat = 41.357423 # 2.568766 diff
inr = 43301.3 # inradius
circumr = 50000.0 # circumradius (50km)
circumOffset = circumr * 1.5 # displacement to tile
earthr = 6378137.0 # Earth's radius, sphere
lat = minlat
lon = minlon
# coordinate offsets in radians
dlat = (inr*2)/earthr
dlon = circumOffset/(earthr*cos(pi*lat/180))
print("finding points to query...")
coords = [] # [lat, lon] pairs
isOffset = False
while lon < maxlon:
print(str(lat) + ", " + str(lon))
w.writerow([lon, lat])
while lat < maxlat:
lat += dlat * 180.0/pi
print('\t' + str(lat) + ", " + str(lon))
coords.append([lat, lon])
w.writerow([lat, lon])
lat = minlat
lon += (circumOffset/(earthr*cos(pi*lat/180))) * 180.0/pi
isOffset = not isOffset
if isOffset:
lat -= (inr/earthr) * 180.0/pi
outf.close()
print("Done finding points.")
print("Writing script...")
#inf = open('njpoints.csv', 'r')
#r = csv.reader(inf)
outf = open('google_supermarkets.sh', 'w')
outf.write("#!/bin/bash\n\n")
qryStart = 'curl --location --globoff "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location='
ct = 0
#for ln in r:
for c in coords:
ct += 1
outf.write(qryStart + str(c[0]) + ',' + str(c[1]) +
'&radius=50000&types=grocery_or_supermarket&sensor=false&key=' + api_key +
'" -o googleloc' + str(ct) + '.json\n')
outf.write('sleep 5\n')
#inf.close()
outf.close()
print("Done writing script to make " + str(ct) + " Google Places queries.")
print("All done!")
@flibbertigibbet
Copy link
Author

Does not check the next_page_token to see if there are more than 20 results for a query, or if the maximum of 60 results for the query were returned.

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