Skip to content

Instantly share code, notes, and snippets.

@wbills
Last active July 19, 2016 04:57
Show Gist options
  • Save wbills/2fdb19e4cf279652c045e3ca4c07d0e8 to your computer and use it in GitHub Desktop.
Save wbills/2fdb19e4cf279652c045e3ca4c07d0e8 to your computer and use it in GitHub Desktop.
test script for determining neighbor cell ids to send to pokemon go api for location requests
import sys
import math
from s2sphere import CellId, LatLng, LatLngRect, Cell
def getNeighbors():
origin = CellId.from_lat_lng(LatLng.from_degrees(FLOAT_LAT, FLOAT_LONG)).parent(15)
walk = [origin.id()]
#get the 8 neighboring cells
path = [90,180,270,270,0,0,90,90]
origin_lat = FLOAT_LAT
origin_lng = FLOAT_LONG
R = 6379.1 #earth
for direction in path:
bearing = math.radians(direction)
#choose a distance based on direction to get us into the adjoining cell
if direction in [0, 180]:
distance = 0.305 #approx distance in km from centroid of cell to next NS cell centroid
elif direction in [90,270]:
distance = 0.213 #approx distance in km from centroid of cell to next EW cell centroid
lat1 = math.radians(origin_lat)
lng1 = math.radians(origin_lng)
lat2 = math.asin( math.sin(lat1)*math.cos(distance/R) +
math.cos(lat1)*math.sin(distance/R)*math.cos(bearing))
lng2 = lng1 + math.atan2(math.sin(bearing)*math.sin(distance/R)*math.cos(lat1),
math.cos(distance/R)-math.sin(lat1)*math.sin(lat2))
lat2 = math.degrees(lat2)
lng2 = math.degrees(lng2)
new_cell = CellId.from_lat_lng(LatLng.from_degrees(lat2, lng2)).parent(15)
walk.append(new_cell.id())
origin_lat = lat2
origin_lng = lng2
#be sure that whatever walk length is here that you set the length of f2 in the request to be the same
return walk
if __name__ == '__main__':
FLOAT_LAT, FLOAT_LONG = map(float, sys.argv[1:])
print "id,latitude,longitude"
count = 0
for c in getNeighbors():
cell = CellId(c)
cell_ll = cell.to_lat_lng()
lat = math.degrees(cell_ll.lat().radians)
lng = math.degrees(cell_ll.lng().radians)
print ','.join(map(str, [count, lat, lng]))
count += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment