def getLatLongIndex(latitude, longitude): return CellId.from_lat_lng( LatLng.from_degrees( latitude, longitude ) ).id()
def locate_step(self, location): (lat, lon) = location origin = LatLng.from_degrees(lat, lon) parent = CellId.from_lat_lng(origin).parent(15) h = self.niantic.heartbit(location) hs = [h] for child in parent.children(): latlng = LatLng.from_point(Cell(child).get_center()) child_location = (latlng.lat().degrees, latlng.lng().degrees) hs.append(self.niantic.heartbit(child_location)) visible = self.__parse_pokemons(hs) current_time_ms = int(round(time.time() * 1000)) for poke in visible.values(): pokeid = str(poke.pokemon.PokemonId) pokename = POKEMON_NAMES[pokeid] expires = poke.LastModifiedMs + poke.TimeTillHiddenMs self.data.pokemons[poke.SpawnPointId] = { "lat": poke.Latitude, "lng": poke.Longitude, "expires": expires, 'expiresAfter': (expires - current_time_ms) / 1000, "id": poke.pokemon.PokemonId, "name": pokename } logger.debug('-- step {} {}: found {} pokemons, {} pokestops, {} gyms'.format( lat, lon, len(self.data.pokemons), len(self.data.pokestops), len(self.data.gyms)))
def getCellId(self): return CellId.from_lat_lng( LatLng.from_degrees( self.latitude, self.longitude ) ).parent(15)
def makeWildPokemon(location): # Cause the randomness to only shift every N minutes (thus new pokes every N minutes) offset = int(time() % 3600) / 10 seedid = str(location[0]) + str(location[1]) + str(offset) seed(seedid) # Now, collect the pokes for this can point pokes = [] for i in range(randint(0, 2)): coords = getRandomPoint(location) ll = LatLng.from_degrees(coords[0], coords[1]) cellId = CellId.from_lat_lng(ll).parent(20).to_token() pokes.append({ 'encounter_id': 'pkm' + seedid + str(i), 'last_modified_timestamp_ms': int((time() - 10) * 1000), 'latitude': coords[0], 'longitude': coords[1], 'pokemon_data': { 'pokemon_id': randint(1, 140), 'iv_attack': 999999 }, 'spawn_point_id': cellId, 'time_till_hidden_ms': randint(60, 600) * 1000 }) return pokes
def getNeighbors(location): level = 15 origin = CellId.from_lat_lng(LatLng.from_degrees(location[0], location[1])).parent(level) max_size = 1 << 30 size = origin.get_size_ij(level) face, i, j = origin.to_face_ij_orientation()[0:3] walk = [origin.id(), origin.from_face_ij_same(face, i, j - size, j - size >= 0).parent(level).id(), origin.from_face_ij_same(face, i, j + size, j + size < max_size).parent(level).id(), origin.from_face_ij_same(face, i - size, j, i - size >= 0).parent(level).id(), origin.from_face_ij_same(face, i + size, j, i + size < max_size).parent(level).id(), origin.from_face_ij_same(face, i - size, j - size, j - size >= 0 and i - size >= 0).parent(level).id(), origin.from_face_ij_same(face, i + size, j - size, j - size >= 0 and i + size < max_size).parent(level).id(), origin.from_face_ij_same(face, i - size, j + size, j + size < max_size and i - size >= 0).parent(level).id(), origin.from_face_ij_same(face, i + size, j + size, j + size < max_size and i + size < max_size).parent(level).id()] #origin.from_face_ij_same(face, i, j - 2*size, j - 2*size >= 0).parent(level).id(), #origin.from_face_ij_same(face, i - size, j - 2*size, j - 2*size >= 0 and i - size >=0).parent(level).id(), #origin.from_face_ij_same(face, i + size, j - 2*size, j - 2*size >= 0 and i + size < max_size).parent(level).id(), #origin.from_face_ij_same(face, i, j + 2*size, j + 2*size < max_size).parent(level).id(), #origin.from_face_ij_same(face, i - size, j + 2*size, j + 2*size < max_size and i - size >=0).parent(level).id(), #origin.from_face_ij_same(face, i + size, j + 2*size, j + 2*size < max_size and i + size < max_size).parent(level).id(), #origin.from_face_ij_same(face, i + 2*size, j, i + 2*size < max_size).parent(level).id(), #origin.from_face_ij_same(face, i + 2*size, j - size, j - size >= 0 and i + 2*size < max_size).parent(level).id(), #origin.from_face_ij_same(face, i + 2*size, j + size, j + size < max_size and i + 2*size < max_size).parent(level).id(), #origin.from_face_ij_same(face, i - 2*size, j, i - 2*size >= 0).parent(level).id(), #origin.from_face_ij_same(face, i - 2*size, j - size, j - size >= 0 and i - 2*size >=0).parent(level).id(), #origin.from_face_ij_same(face, i - 2*size, j + size, j + size < max_size and i - 2*size >=0).parent(level).id()] return walk
def neighbor_s2_circle( location, i_dir=0.0, j_dir=0.0): # input location can be list, tuple or Point if type(location) in (list, tuple): ll_location = LatLng.from_degrees(location[0], location[1]) elif type(location) is Point: ll_location = LatLng.from_point(location) elif type(location) is LatLng: ll_location = location else: return None cid_large = CellId.from_lat_lng(ll_location).parent(lvl_big) cid_small = cid_large.child_begin(lvl_small) vec_to_j = (Cell(ij_offs(cid_small, 0, 1)).get_center() - Cell(cid_small).get_center()).normalize() vec_to_i = (Cell(ij_offs(cid_small, 1, 0)).get_center() - Cell(cid_small).get_center()).normalize() vec_newlocation = ll_location.to_point() + safety * HEX_R / earth_Rrect * ( i_dir * 3**0.5 * vec_to_i + j_dir * 1.5 * vec_to_j) return vec_newlocation # output is Point
def getCells(self, radius=10, bothDirections=True): origin = CellId.from_lat_lng( LatLng.from_degrees( self.latitude, self.longitude ) ).parent(15) # Create walk around area walk = [origin.id()] right = origin.next() left = origin.prev() # Double the radius if we're only walking one way if not bothDirections: radius *= 2 # Search around provided radius for _ in range(radius): walk.append(right.id()) right = right.next() if bothDirections: walk.append(left.id()) left = left.prev() # Return everything return sorted(walk)
def get_s2_cell_center(lat, lng, level): lat_lng = LatLng.from_degrees(lat, lng) cell_id = CellId.from_lat_lng(lat_lng).parent(level) center = cell_id.to_lat_lng() return { 'lat': float(center.lat().degrees), 'lon': float(center.lng().degrees) }
def update_missing_s2_ids(): log.info("Looking for spawn points with missing s2 coordinates") for spawnpoint in db_load_spawn_points_missing_s2(): latlng = LatLng.from_degrees(spawnpoint["latitude"], spawnpoint["longitude"]) cell = CellId.from_lat_lng(latlng).parent(15) db_set_s2_cellid(spawnpoint["id"], cell.id()) log.info("Done establishing s2 points")
def main(): origin_lat_i, origin_lng_i = f2i(origin_lat), f2i(origin_lng) api_endpoint, access_token, profile_response = login(origin_lat_i, origin_lng_i) with sqlite3.connect('database.db') as db: create_tables(db) while True: pos = 1 x = 0 y = 0 dx = 0 dy = -1 steplimit2 = steplimit**2 for step in range(steplimit2): #print('looping: step {} of {}'.format(step + 1, steplimit**2)) # Scan location math if -steplimit2 / 2 < x <= steplimit2 / 2 and -steplimit2 / 2 < y <= steplimit2 / 2: step_lat = x * 0.0025 + origin_lat step_lng = y * 0.0025 + origin_lng step_lat_i = f2i(step_lat) step_lng_i = f2i(step_lng) if x == y or x < 0 and x == -y or x > 0 and x == 1 - y: (dx, dy) = (-dy, dx) (x, y) = (x + dx, y + dy) #print('[+] Searching for Pokemon at location {} {}'.format(step_lat, step_lng)) origin = LatLng.from_degrees(step_lat, step_lng) parent = CellId.from_lat_lng(origin).parent(15) h = get_heartbeat(api_endpoint, access_token, profile_response, step_lat, step_lng, step_lat_i, step_lng_i) hs = [h] for child in parent.children(): latlng = LatLng.from_point(Cell(child).get_center()) child_lat, child_lng = latlng.lat().degrees, latlng.lng().degrees child_lat_i, child_lng_i = f2i(child_lat), f2i(child_lng) hs.append(get_heartbeat(api_endpoint, access_token, profile_response, child_lat, child_lng, child_lat_i, child_lng_i)) visible = [] data = [] for hh in hs: for cell in hh.cells: for poke in cell.WildPokemon: disappear_ms = cell.AsOfTimeMs + poke.TimeTillHiddenMs data.append(( poke.SpawnPointId, poke.pokemon.PokemonId, poke.Latitude, poke.Longitude, disappear_ms, )) if data: print('Upserting {} pokemon'.format(len(data))) with sqlite3.connect('database.db') as db: insert_data(db, data)
def remove_plan(): location = (request.args.get('lat', type=float), request.args.get('lng', type=float)) cid = CellId.from_lat_lng(LatLng.from_degrees(location[0],location[1])).parent(mapl.lvl_big) token = cid.to_token() lock_plans.acquire() if token in list_plans: list_plans.pop(list_plans.index(token)) lock_plans.release() return jsonify("")
def sub_cell(cell,i=0,dist=25): g = Geodesic.WGS84 # @UndefinedVariable olat = CellId.to_lat_lng(cell).lat().degrees olng = CellId.to_lat_lng(cell).lng().degrees p = g.Direct(olat, olng,(45+(90*i)),dist) c = CellId.from_lat_lng(LatLng.from_degrees(p['lat2'],p['lon2'])) return c.parent(cell.level()+1)
def get_cell_walk(lat, lng, radius, level=15): origin = CellId.from_lat_lng(LatLng.from_degrees(lat, lng)).parent(level) walk = [origin] right = origin.next() left = origin.prev() for dummy in range(radius): walk.append(right) walk.append(left) right = right.next() left = left.prev() return sorted(walk)
def get_cellid(lat, long): origin = CellId.from_lat_lng(LatLng.from_degrees(lat, long)).parent(15) walk = [origin.id()] next = origin.next() prev = origin.prev() for i in range(10): walk.append(prev.id()) walk.append(next.id()) next = next.next() prev = prev.prev() return sorted(walk)
def get_neighbors(lat, lon): origin = CellId.from_lat_lng(LatLng.from_degrees(lat, lon)).parent(15) walk = [origin.id()] next = origin.next() prev = origin.prev() for i in range(10): walk.append(prev.id()) walk.append(next.id()) next = next.next() prev = prev.prev() return walk
def remove_plan(): location = (request.args.get('lat', type=float), request.args.get('lng', type=float)) cid = CellId.from_lat_lng(LatLng.from_degrees( location[0], location[1])).parent(mapl.lvl_big) token = cid.to_token() lock_plans.acquire() if token in list_plans: list_plans.pop(list_plans.index(token)) lock_plans.release() return jsonify("")
def cell_spiral(lat, lng, dist, level=15, step=100, res=3.6): cells = [] g = Geodesic.WGS84 # @UndefinedVariable for i in xrange(0, dist, step): for rad in xrange(int(360 / res)): p = g.Direct(lat, lng, rad * res, i) c = CellId.from_lat_lng(LatLng.from_degrees(p['lat2'], p['lon2'])) c = c.parent(level) if c not in cells: cells.append(c) return cells
def cell_spiral(lat, lng, dist, level=15, step=100, res=3.6): cells = [] g = Geodesic.WGS84 # @UndefinedVariable for i in xrange(0,dist,step): for rad in xrange(int(360/res)): p = g.Direct(lat, lng, rad*res, i) c = CellId.from_lat_lng(LatLng.from_degrees(p['lat2'],p['lon2'])) c = c.parent(level) if c not in cells: cells.append(c) return cells
def _get_cellid(self, lat, long, radius=10): origin = CellId.from_lat_lng(LatLng.from_degrees(lat, long)).parent(15) walk = [origin.id()] # 10 before and 10 after next = origin.next() prev = origin.prev() for i in range(radius): walk.append(prev.id()) walk.append(next.id()) next = next.next() prev = prev.prev() return sorted(walk)
def get_cellid(lat, long): origin = CellId.from_lat_lng(LatLng.from_degrees(lat, long)).parent(15) walk = [origin.id()] # 10 before and 10 after next = origin.next() prev = origin.prev() for i in range(10): walk.append(prev.id()) walk.append(next.id()) next = next.next() prev = prev.prev() return ''.join(map(encode, sorted(walk)))
def get_cellid(lat, lng, level=15): origin = CellId.from_lat_lng(LatLng.from_degrees(lat, lng)).parent(level) walk = [origin.id()] # 10 before and 10 after next = origin.next() prev = origin.prev() for i in range(10): walk.append(prev.id()) walk.append(next.id()) next = next.next() prev = prev.prev() return sorted(walk)
def get_neighbors(lat, lng): origin = CellId.from_lat_lng(LatLng.from_degrees(lat, lng)).parent(15) walk = [origin.id()] # 10 before and 10 after next = origin.next() prev = origin.prev() for i in range(10): walk.append(prev.id()) walk.append(next.id()) next = next.next() prev = prev.prev() return walk
def get_area_cell(location, unfilled=False): border = [] locs = [] cid_large = CellId.from_lat_lng( LatLng.from_degrees(location[0], location[1])).parent(lvl_big) border.append(get_border_cell(cid_large)) if unfilled: return [], border, cid_large corner = neighbor_s2_circle( LatLng.from_degrees(border[-1][0][0], border[-1][0][1]), safety_border * 0.5, safety_border / 3.0) j_maxpoint = LatLng.from_point( neighbor_s2_circle( LatLng.from_degrees(border[-1][1][0], border[-1][1][1]), safety_border * 0.5, (1 - safety_border) / 3.0)) i_maxpoint = LatLng.from_point( neighbor_s2_circle( LatLng.from_degrees(border[-1][3][0], border[-1][3][1]), (1 - safety_border) * 0.5, safety_border / 3.0)) base = corner p_start = base dist_j = j_maxpoint.get_distance(LatLng.from_point(p_start)) last_dist_j = None j = 0 while last_dist_j is None or dist_j < last_dist_j: dist_i = i_maxpoint.get_distance(LatLng.from_point(p_start)) last_dist_i = None while last_dist_i is None or dist_i < last_dist_i: locs.append(LatLng.from_point(p_start)) p_start = neighbor_s2_circle(p_start, 1.0, 0.0) last_dist_i = dist_i dist_i = i_maxpoint.get_distance(LatLng.from_point(p_start)) base = neighbor_s2_circle(base, 0.0, 1.0) last_dist_j = dist_j dist_j = j_maxpoint.get_distance(LatLng.from_point(base)) if j % 2 == 1: p_start = base else: p_start = neighbor_s2_circle(base, -0.5, 0.0) j += 1 all_loc = [] for loc in locs: all_loc.append([loc.lat().degrees, loc.lng().degrees]) return all_loc, border, cid_large
def load_spawn_points(): points = {} spawn_points = db_load_spawn_points() print("Calculating map") for spawnpoint in spawn_points: latlng = LatLng.from_degrees(spawnpoint["latitude"], spawnpoint["longitude"]) cell = CellId.from_lat_lng(latlng) while cell.level() != 15: cell = cell.parent() current = points.get(cell.id(), []) current.append(spawnpoint) points[cell.id()] = current return points
def lnglat_to_cellid(self, longitude, latitude): """坐标转s2 cellid""" LEVEL = ConfManage.getInt("S2_LEVEL") if latitude > 90 or latitude < -90: raise ValueError('4002:latitude out of range (-90,90)') elif longitude > 180 or longitude < -180: raise ValueError('4002:latitude out of range (-180,180)') elif LEVEL > 30: raise ValueError('4009:level must be litter than 30') else: latlng = LatLng.from_degrees(latitude, longitude) cell_id = CellId.from_lat_lng(latlng) level_cell_id = cell_id.parent(LEVEL) return level_cell_id.id()
def get_cell_ids(lat, long, radius = 10): origin = CellId.from_lat_lng(LatLng.from_degrees(lat, long)).parent(15) walk = [origin.id()] right = origin.next() left = origin.prev() # Search around provided radius for i in range(radius): walk.append(right.id()) walk.append(left.id()) right = right.next() left = left.prev() # Return everything return sorted(walk)
def _get_cell_id_from_latlong(self, radius=10): position_lat, position_lng, _ = self.api.get_position() origin = CellId.from_lat_lng(LatLng.from_degrees(i2f(position_lat), i2f(position_lng))).parent(15) walk = [origin.id()] # 10 before and 10 after next_cell = origin.next() prev_cell = origin.prev() for _ in range(radius): walk.append(prev_cell.id()) walk.append(next_cell.id()) next_cell = next_cell.next() prev_cell = prev_cell.prev() return sorted(walk)
def get_cell_ids(lat, long, radius=10): origin = CellId.from_lat_lng(LatLng.from_degrees(lat, long)).parent(15) walk = [origin.id()] right = origin.next() left = origin.prev() # Search around provided radius for i in range(radius): walk.append(right.id()) walk.append(left.id()) right = right.next() left = left.prev() # Return everything return sorted(walk)
def _get_cell_id_from_latlong(self, radius=10): # type: (Optional[int]) -> List[str] position_lat, position_lng, _ = self.api_wrapper.get_position() origin = CellId.from_lat_lng( LatLng.from_degrees(position_lat, position_lng)).parent(15) walk = [origin.id()] # 10 before and 10 after next_cell = origin.next() prev_cell = origin.prev() for _ in range(radius): walk.append(prev_cell.id()) walk.append(next_cell.id()) next_cell = next_cell.next() prev_cell = prev_cell.prev() return sorted(walk)
def cover_cell(self, cid): lats = [] lngs = [] output = [] s2_cell = Cell(cid) lvl = s2_cell.level() for i in [0, 1]: for j in [0, 1]: lats.append(s2_cell.get_latitude(i, j)/pi*180) lngs.append(s2_cell.get_longitude(i, j)/pi*180) locations = self.cover_region((min(lats),min(lngs)),(max(lats),max(lngs))) for location in locations: testid = CellId.from_lat_lng(LatLng.from_degrees(location[0],location[1])).parent(lvl) if testid == cid: output.append(location) return output
def getCells(self, radius=10): origin = CellId.from_lat_lng( LatLng.from_degrees(self.latitude, self.longitude)).parent(15) # Create walk around area walk = [origin.id()] right = origin.next() left = origin.prev() # Search around provided radius for _ in range(radius): walk.append(right.id()) walk.append(left.id()) right = right.next() left = left.prev() # Return everything return sorted(walk)
def geo_to_s2(lat, lon, res): """Get s2 given a point and resolution Parameters ---------- lat : float latitude lon : float longitude res : int s2 square resolution, from 0 to 30 Returns ------- string s2 unique token """ return CellId.from_lat_lng(LatLng.from_degrees( lat, lon)).parent(res).to_token()
def neighbor_s2_circle(location, i_dir=0.0, j_dir=0.0): # input location can be list, tuple or Point if type(location) in (list, tuple): ll_location = LatLng.from_degrees(location[0], location[1]) elif type(location) is Point: ll_location = LatLng.from_point(location) elif type(location) is LatLng: ll_location = location else: return None cid_large = CellId.from_lat_lng(ll_location).parent(lvl_big) cid_small = cid_large.child_begin(lvl_small) vec_to_j = (Cell(ij_offs(cid_small, 0, 1)).get_center() - Cell(cid_small).get_center()).normalize() vec_to_i = (Cell(ij_offs(cid_small, 1, 0)).get_center() - Cell(cid_small).get_center()).normalize() vec_newlocation = ll_location.to_point() + safety * HEX_R / earth_Rrect * (i_dir * 3 ** 0.5 * vec_to_i + j_dir * 1.5 * vec_to_j) return vec_newlocation # output is Point
def cover_cell(self, cid): lats = [] lngs = [] output = [] s2_cell = Cell(cid) lvl = s2_cell.level() for i in [0, 1]: for j in [0, 1]: lats.append(s2_cell.get_latitude(i, j) / pi * 180) lngs.append(s2_cell.get_longitude(i, j) / pi * 180) locations = self.cover_region((min(lats), min(lngs)), (max(lats), max(lngs))) for location in locations: testid = CellId.from_lat_lng( LatLng.from_degrees(location[0], location[1])).parent(lvl) if testid == cid: output.append(location) return output
def get_area_cell(location,unfilled=False): border = [] locs = [] cid_large = CellId.from_lat_lng(LatLng.from_degrees(location[0], location[1])).parent(lvl_big) border.append(get_border_cell(cid_large)) if unfilled: return [], border, cid_large corner = neighbor_s2_circle(LatLng.from_degrees(border[-1][0][0], border[-1][0][1]), safety_border*0.5, safety_border/3.0) j_maxpoint = LatLng.from_point(neighbor_s2_circle(LatLng.from_degrees(border[-1][1][0], border[-1][1][1]), safety_border*0.5, (1-safety_border)/3.0)) i_maxpoint = LatLng.from_point(neighbor_s2_circle(LatLng.from_degrees(border[-1][3][0], border[-1][3][1]), (1-safety_border)*0.5, safety_border/3.0)) base = corner p_start = base dist_j = j_maxpoint.get_distance(LatLng.from_point(p_start)) last_dist_j = None j = 0 while last_dist_j is None or dist_j < last_dist_j: dist_i = i_maxpoint.get_distance(LatLng.from_point(p_start)) last_dist_i = None while last_dist_i is None or dist_i < last_dist_i: locs.append(LatLng.from_point(p_start)) p_start = neighbor_s2_circle(p_start, 1.0, 0.0) last_dist_i = dist_i dist_i = i_maxpoint.get_distance(LatLng.from_point(p_start)) base = neighbor_s2_circle(base, 0.0, 1.0) last_dist_j = dist_j dist_j = j_maxpoint.get_distance(LatLng.from_point(base)) if j % 2 == 1: p_start = base else: p_start = neighbor_s2_circle(base, -0.5, 0.0) j += 1 all_loc = [] for loc in locs: all_loc.append([loc.lat().degrees, loc.lng().degrees]) return all_loc, border,cid_large
def getNeighbors(location): level = 15 origin = CellId.from_lat_lng(LatLng.from_degrees( location[0], location[1])).parent(level) max_size = 1 << 30 size = origin.get_size_ij(level) face, i, j = origin.to_face_ij_orientation()[0:3] walk = [ origin.id(), origin.from_face_ij_same(face, i, j - size, j - size >= 0).parent(level).id(), origin.from_face_ij_same(face, i, j + size, j + size < max_size).parent(level).id(), origin.from_face_ij_same(face, i - size, j, i - size >= 0).parent(level).id(), origin.from_face_ij_same(face, i + size, j, i + size < max_size).parent(level).id(), origin.from_face_ij_same(face, i - size, j - size, j - size >= 0 and i - size >= 0).parent(level).id(), origin.from_face_ij_same(face, i + size, j - size, j - size >= 0 and i + size < max_size).parent(level).id(), origin.from_face_ij_same(face, i - size, j + size, j + size < max_size and i - size >= 0).parent(level).id(), origin.from_face_ij_same(face, i + size, j + size, j + size < max_size and i + size < max_size).parent(level).id() ] #origin.from_face_ij_same(face, i, j - 2*size, j - 2*size >= 0).parent(level).id(), #origin.from_face_ij_same(face, i - size, j - 2*size, j - 2*size >= 0 and i - size >=0).parent(level).id(), #origin.from_face_ij_same(face, i + size, j - 2*size, j - 2*size >= 0 and i + size < max_size).parent(level).id(), #origin.from_face_ij_same(face, i, j + 2*size, j + 2*size < max_size).parent(level).id(), #origin.from_face_ij_same(face, i - size, j + 2*size, j + 2*size < max_size and i - size >=0).parent(level).id(), #origin.from_face_ij_same(face, i + size, j + 2*size, j + 2*size < max_size and i + size < max_size).parent(level).id(), #origin.from_face_ij_same(face, i + 2*size, j, i + 2*size < max_size).parent(level).id(), #origin.from_face_ij_same(face, i + 2*size, j - size, j - size >= 0 and i + 2*size < max_size).parent(level).id(), #origin.from_face_ij_same(face, i + 2*size, j + size, j + size < max_size and i + 2*size < max_size).parent(level).id(), #origin.from_face_ij_same(face, i - 2*size, j, i - 2*size >= 0).parent(level).id(), #origin.from_face_ij_same(face, i - 2*size, j - size, j - size >= 0 and i - 2*size >=0).parent(level).id(), #origin.from_face_ij_same(face, i - 2*size, j + size, j + size < max_size and i - 2*size >=0).parent(level).id()] return walk
def getCells(self, radius=10): origin = CellId.from_lat_lng( LatLng.from_degrees( self.latitude, self.longitude ) ).parent(15) # Create walk around area walk = [origin.id()] right = origin.next() left = origin.prev() # Search around provided radius for _ in range(radius): walk.append(right.id()) walk.append(left.id()) right = right.next() left = left.prev() # Return everything return sorted(walk)
def makeWildPokemon(location): # Cause the randomness to only shift every N minutes (thus new pokes every N minutes) offset = int(time() % 3600) / 10 seedid = str(location[0]) + str(location[1]) + str(offset) seed(seedid) # Now, collect the pokes for this can point pokes = [] for i in range(randint(0, 2)): coords = getRandomPoint(location) ll = LatLng.from_degrees(coords[0], coords[1]) cellId = CellId.from_lat_lng(ll).parent(20).to_token() pokes.append( { "encounter_id": "pkm" + seedid + str(i), "last_modified_timestamp_ms": int((time() - 10) * 1000), "latitude": coords[0], "longitude": coords[1], "pokemon_data": {"pokemon_id": randint(1, 140)}, "spawn_point_id": cellId, "time_till_hidden_ms": randint(60, 600) * 1000, } ) return pokes
try: cells = response_dict['responses']['GET_MAP_OBJECTS']['map_cells'] except TypeError, KeyError: print ('error getting map data for {}, {}'.format(sLat, sLng)) return for cell in cells: curTime = cell['current_timestamp_ms'] if 'wild_pokemons' in cell: for wild in cell['wild_pokemons']: if wild['time_till_hidden_ms']>0: timeSpawn = (curTime+(wild['time_till_hidden_ms']))-900000 gmSpawn = time.gmtime(int(timeSpawn/1000)) secSpawn = (gmSpawn.tm_min*60)+(gmSpawn.tm_sec) phash = '{},{}'.format(timeSpawn,wild['spawnpoint_id']) shash = '{},{}'.format(secSpawn,wild['spawnpoint_id']) pokeLog = {'time':timeSpawn, 'sid':wild['spawnpoint_id'], 'lat':wild['latitude'], 'lng':wild['longitude'], 'pid':wild['pokemon_data']['pokemon_id'], 'cell':CellId.from_lat_lng(LatLng.from_degrees(wild['latitude'], wild['longitude'])).to_token()} spawnLog = {'time':secSpawn, 'sid':wild['spawnpoint_id'], 'lat':wild['latitude'], 'lng':wild['longitude'], 'cell':CellId.from_lat_lng(LatLng.from_degrees(wild['latitude'], wild['longitude'])).to_token()} pokes[phash] = pokeLog spawns[shash] = spawnLog if 'forts' in cell: for fort in cell['forts']: if fort['enabled'] == True: if 'type' in fort: #got a pokestop stopLog = {'id':fort['id'],'lat':fort['latitude'],'lng':fort['longitude'],'lure':-1} if 'lure_info' in fort: stopLog['lure'] = fort['lure_info']['lure_expires_timestamp_ms'] stops[fort['id']] = stopLog if 'gym_points' in fort: gymLog = {'id':fort['id'],'lat':fort['latitude'],'lng':fort['longitude'],'team':0} if 'owned_by_team' in fort:
def cellid(loc): return CellId.from_lat_lng(LatLng.from_degrees(loc[0], loc[1])).to_token()
def doScan(wid, sLat, sLng, api): #print ('scanning ({}, {})'.format(sLat, sLng)) api.set_position(sLat, sLng, 0) cell_ids = util.get_cell_ids(lat=sLat, long=sLng, radius=80) timestamps = [ 0, ] * len(cell_ids) while True: try: response_dict = api.get_map_objects(latitude=sLat, longitude=sLng, since_timestamp_ms=timestamps, cell_id=cell_ids) except ServerSideRequestThrottlingException: config['scanDelay'] += 0.5 print('Request throttled, increasing sleep by 0.5 to {}').format( config['scanDelay']) time.sleep(config['scanDelay']) continue except: time.sleep(config['scanDelay']) api.set_position(sLat, sLng, 0) time.sleep(config['scanDelay']) continue break try: cells = response_dict['responses']['GET_MAP_OBJECTS']['map_cells'] except TypeError: print('thread {} error getting map data for {}, {}'.format( wid, sLat, sLng)) raise except KeyError: print('thread {} error getting map data for {}, {}'.format( wid, sLat, sLng)) raise return for cell in cells: curTime = cell['current_timestamp_ms'] if 'wild_pokemons' in cell: for wild in cell['wild_pokemons']: if wild['time_till_hidden_ms'] > 0: timeSpawn = (curTime + (wild['time_till_hidden_ms'])) - 900000 gmSpawn = time.gmtime(int(timeSpawn / 1000)) secSpawn = (gmSpawn.tm_min * 60) + (gmSpawn.tm_sec) phash = '{},{}'.format(timeSpawn, wild['spawn_point_id']) shash = '{},{}'.format(secSpawn, wild['spawn_point_id']) pokeLog = { 'time': timeSpawn, 'sid': wild['spawn_point_id'], 'lat': wild['latitude'], 'lng': wild['longitude'], 'pid': wild['pokemon_data']['pokemon_id'], 'cell': CellId.from_lat_lng( LatLng.from_degrees(wild['latitude'], wild['longitude'])).to_token() } spawnLog = { 'time': secSpawn, 'sid': wild['spawn_point_id'], 'lat': wild['latitude'], 'lng': wild['longitude'], 'cell': CellId.from_lat_lng( LatLng.from_degrees(wild['latitude'], wild['longitude'])).to_token() } pokes[phash] = pokeLog spawns[shash] = spawnLog if 'forts' in cell: for fort in cell['forts']: if fort['enabled'] == True: if 'type' in fort: #got a pokestop stopLog = { 'id': fort['id'], 'lat': fort['latitude'], 'lng': fort['longitude'], 'lure': -1 } if 'lure_info' in fort: stopLog['lure'] = fort['lure_info'][ 'lure_expires_timestamp_ms'] stops[fort['id']] = stopLog if 'gym_points' in fort: gymLog = { 'id': fort['id'], 'lat': fort['latitude'], 'lng': fort['longitude'], 'team': 0 } if 'owned_by_team' in fort: gymLog['team'] = fort['owned_by_team'] gyms[fort['id']] = gymLog time.sleep(config['scanDelay'])
def get_s2_cell_as_polygon(lat, lon, level=12): cell = S2Cell( S2CellId.from_lat_lng(LatLng.from_degrees(lat, lon)).parent(level)) return [(get_vertex(cell, v)) for v in range(0, 4)]
def doScan(wid, sLat, sLng, api): #print ('scanning ({}, {})'.format(sLat, sLng)) api.set_position(sLat,sLng,0) cell_ids = util.get_cell_ids(lat=sLat, long=sLng, radius=80) timestamps = [0,] * len(cell_ids) while True: try: response_dict = api.get_map_objects(latitude = sLat, longitude = sLng, since_timestamp_ms = timestamps, cell_id = cell_ids) except ServerSideRequestThrottlingException: config['scanDelay'] += 0.5 print ('Request throttled, increasing sleep by 0.5 to {}').format(config['scanDelay']) time.sleep(config['scanDelay']) continue except: time.sleep(config['scanDelay']) api.set_position(sLat,sLng,0) time.sleep(config['scanDelay']) continue break try: cells = response_dict['responses']['GET_MAP_OBJECTS']['map_cells'] except TypeError: print ('thread {} error getting map data for {}, {}'.format(wid,sLat, sLng)) raise except KeyError: print ('thread {} error getting map data for {}, {}'.format(wid,sLat, sLng)) raise return for cell in cells: curTime = cell['current_timestamp_ms'] if 'wild_pokemons' in cell: for wild in cell['wild_pokemons']: if wild['time_till_hidden_ms']>0: timeSpawn = (curTime+(wild['time_till_hidden_ms']))-900000 gmSpawn = time.gmtime(int(timeSpawn/1000)) secSpawn = (gmSpawn.tm_min*60)+(gmSpawn.tm_sec) phash = '{},{}'.format(timeSpawn,wild['spawn_point_id']) shash = '{},{}'.format(secSpawn,wild['spawn_point_id']) pokeLog = {'time':timeSpawn, 'sid':wild['spawn_point_id'], 'lat':wild['latitude'], 'lng':wild['longitude'], 'pid':wild['pokemon_data']['pokemon_id'], 'cell':CellId.from_lat_lng(LatLng.from_degrees(wild['latitude'], wild['longitude'])).to_token()} spawnLog = {'time':secSpawn, 'sid':wild['spawn_point_id'], 'lat':wild['latitude'], 'lng':wild['longitude'], 'cell':CellId.from_lat_lng(LatLng.from_degrees(wild['latitude'], wild['longitude'])).to_token()} pokes[phash] = pokeLog spawns[shash] = spawnLog if 'forts' in cell: for fort in cell['forts']: if fort['enabled'] == True: if 'type' in fort: #got a pokestop stopLog = {'id':fort['id'],'lat':fort['latitude'],'lng':fort['longitude'],'lure':-1} if 'lure_info' in fort: stopLog['lure'] = fort['lure_info']['lure_expires_timestamp_ms'] stops[fort['id']] = stopLog if 'gym_points' in fort: gymLog = {'id':fort['id'],'lat':fort['latitude'],'lng':fort['longitude'],'team':0} if 'owned_by_team' in fort: gymLog['team'] = fort['owned_by_team'] gyms[fort['id']] = gymLog time.sleep(config['scanDelay'])
def doScan(sLat, sLng, api): api.set_position(sLat, sLng, 0) cellids = get_cellid(sLat, sLng) api.get_map_objects( latitude=f2i(sLat), longitude=f2i(sLng), since_timestamp_ms=[TIMESTAMP] * len(cellids), cell_id=cellids ) response_dict = api.call() try: cells = response_dict['responses']['GET_MAP_OBJECTS']['map_cells'] except (TypeError, KeyError): print('error getting map data for {}, {}'.format(sLat, sLng)) return for cell in cells: # print(cell['s2_cell_id']) curTime = cell['current_timestamp_ms'] if 'wild_pokemons' in cell: for wild in cell['wild_pokemons']: if wild['time_till_hidden_ms'] > 0: timeSpawn = (curTime + (wild['time_till_hidden_ms'])) - 900000 gmSpawn = time.gmtime(int(timeSpawn / 1000)) secSpawn = (gmSpawn.tm_min * 60) + (gmSpawn.tm_sec) phash = '{},{}'.format(timeSpawn, wild['spawn_point_id']) shash = '{},{}'.format(secSpawn, wild['spawn_point_id']) pokeLog = { 'time': timeSpawn, 'sid': wild['spawn_point_id'], 'lat': wild['latitude'], 'lng': wild['longitude'], 'pid': wild['pokemon_data']['pokemon_id'], 'cell': CellId.from_lat_lng(LatLng.from_degrees(wild['latitude'], wild['longitude'])).to_token() } spawnLog = { 'time': secSpawn, 'sid': wild['spawn_point_id'], 'lat': wild['latitude'], 'lng': wild['longitude'], 'cell': CellId.from_lat_lng(LatLng.from_degrees(wild['latitude'], wild['longitude'])).to_token() } map_objects[POKES][phash] = pokeLog map_objects[SPAWNS][shash] = spawnLog if 'forts' in cell: for fort in cell['forts']: if fort['enabled']: if 'type' in fort: # Got a pokestop stopLog = { 'id': fort['id'], 'lat': fort['latitude'], 'lng': fort['longitude'], 'lure': -1 } if 'lure_info' in fort: stopLog['lure'] = fort['lure_info']['lure_expires_timestamp_ms'] map_objects[STOPS][fort['id']] = stopLog if 'gym_points' in fort: gymLog = { 'id': fort['id'], 'lat': fort['latitude'], 'lng': fort['longitude'], 'team': 0 } if 'owned_by_team' in fort: gymLog['team'] = fort['owned_by_team'] map_objects[GYMS][fort['id']] = gymLog
def path(start_lat, start_lng): cell_id = CellId.from_lat_lng( LatLng.from_degrees(float(start_lat), float(start_lng))) token = cell_id.to_token() jump = {} jump[token] = {'layer': 0, 'pre': ''} queue = [token] total = 0 start = token mxdis = 0 mxpor = None prelayer = -1 while len(queue) > 0: total += 1 token = queue[0] queue = queue[1:] cell_id = CellId.from_token(token) load(cell_id) if jump[token]['layer'] != prelayer: prelayer = jump[token]['layer'] print(jump[token]['layer'], total, mxdis) if geo.haversine(pos[start], pos[token]) > mxdis: mxdis = geo.haversine(pos[start], pos[token]) mxpor = token cells = getCoveringRect(pos[token][0], pos[token][1], 500) for cell in cells: load(cell) if cell.level() == 16: if cell.to_token() in lv16.keys(): for pp in lv16[cell.to_token()]: if pp['cellid'] not in jump.keys(): jump[pp['cellid']] = { 'layer': jump[token]['layer'] + 1, 'pre': token } queue.append(pp['cellid']) elif cell.level() == 15: if cell.to_token() in lv15.keys(): for pp in lv15[cell.to_token()]: if pp['cellid'] not in jump.keys(): jump[pp['cellid']] = { 'layer': jump[token]['layer'] + 1, 'pre': token } queue.append(pp['cellid']) elif cell.level() == 14: if cell.to_token() in lv14.keys(): for pp in lv14[cell.to_token()]: if pp['cellid'] not in jump.keys(): jump[pp['cellid']] = { 'layer': jump[token]['layer'] + 1, 'pre': token } queue.append(pp['cellid']) print(mxdis) token = mxpor draw = [{ "type": "polyline", "latLngs": [{ "lat": pos[token][0], "lng": pos[token][1] }], "color": "#a24ac3" }] while jump[token]['layer'] > 0: token = jump[token]['pre'] draw.append({ "type": "circle", "latLng": { "lat": pos[token][0], "lng": pos[token][1] }, "radius": 500, "color": "#a24ac3" }) draw[0]['latLngs'].append({"lat": pos[token][0], "lng": pos[token][1]}) with open(f'path_{start_lat}_{start_lng}.json', 'w') as f: json.dump(draw, f)
def point_in_cell(cell,lat,lng): ll = LatLng.from_degrees(lat, lng) if CellId.from_lat_lng(ll).parent(cell.level()) == cell: return True else: return False
def main(): config = init_config() if not config: log.error('Configuration Error!'); return db = sqlite3.connect(config.dbfile) db_cur = db.cursor() db_cur.execute("SELECT cell_id FROM '_queue' WHERE cell_level = %d ORDER BY cell_id" % config.level) _tstats = [0, 0, 0, 0] scan_queque = [x[0] for x in db_cur.fetchall()] # http://stackoverflow.com/questions/3614277/how-to-strip-from-python-pyodbc-sql-returns if len(scan_queque) == 0: log.info('Nothing to scan!'); return api = api_init(config) if api == None: log.error('Login failed!'); return else: log.info('API online! Scan starts in 5sec...') time.sleep(5) for que in scan_queque: cell_ids = [] _content = 0 _tstats[0] += 1 _cstats = [0, 0, 0] log.info('Scan {} of {}.'.format(_tstats[0],(len(scan_queque)))) cell = CellId.from_token(que) _ll = CellId.to_lat_lng(cell) lat, lng, alt = _ll.lat().degrees, _ll.lng().degrees, 0 if config.test: cell_ids = get_cell_ids(lat, lng, 1500) else: cells = susub_cells(cell) cell_ids = sorted([x.id() for x in cells]) try: response_dict = get_response(cell_ids, lat, lng, alt, api,config) except NotLoggedInException: del api; api = api_init(config) response_dict = get_response(cell_ids, lat, lng, alt, api,config) for _map_cell in response_dict['responses']['GET_MAP_OBJECTS']['map_cells']: _cell = CellId(_map_cell['s2_cell_id']).to_token() if 'forts' in _map_cell: for _frt in _map_cell['forts']: if 'gym_points' in _frt: _cstats[0]+=1 _type = 0 _content = set_bit(_content, 2) db_cur.execute("INSERT OR IGNORE INTO forts (fort_id, cell_id, pos_lat, pos_lng, fort_enabled, fort_type, last_scan) " "VALUES ('{}','{}',{},{},{},{},{})".format(_frt['id'],_cell,_frt['latitude'],_frt['longitude'], \ int(_frt['enabled']),0,int(_map_cell['current_timestamp_ms']/1000))) else: _type = 1; _cstats[1]+=1 _content = set_bit(_content, 1) db_cur.execute("INSERT OR IGNORE INTO forts (fort_id, cell_id, pos_lat, pos_lng, fort_enabled, fort_type, last_scan) " "VALUES ('{}','{}',{},{},{},{},{})".format(_frt['id'],_cell,_frt['latitude'],_frt['longitude'], \ int(_frt['enabled']),1,int(_map_cell['current_timestamp_ms']/1000))) if 'spawn_points' in _map_cell: _content = set_bit(_content, 0) for _spwn in _map_cell['spawn_points']: _cstats[2]+=1; spwn_id = CellId.from_lat_lng(LatLng.from_degrees(_spwn['latitude'],_spwn['longitude'])).parent(20).to_token() db_cur.execute("INSERT OR IGNORE INTO spawns (spawn_id, cell_id, pos_lat, pos_lng, last_scan) " "VALUES ('{}','{}',{},{},{})".format(spwn_id,_cell,_spwn['latitude'],_spwn['longitude'],int(_map_cell['current_timestamp_ms']/1000))) if 'decimated_spawn_points' in _map_cell: _content = set_bit(_content, 0) for _spwn in _map_cell['decimated_spawn_points']: _cstats[2]+=1; spwn_id = CellId.from_lat_lng(LatLng.from_degrees(_spwn['latitude'],_spwn['longitude'])).parent(20).to_token() db_cur.execute("INSERT OR IGNORE INTO spawns (spawn_id, cell_id, pos_lat, pos_lng, last_scan) " "VALUES ('{}','{}',{},{},{})".format(spwn_id,_cell,_spwn['latitude'],_spwn['longitude'],int(_map_cell['current_timestamp_ms']/1000))) db_cur.execute("INSERT OR IGNORE INTO cells (cell_id, content, last_scan) " "VALUES ('{}', {}, {})".format(_cell,_content,int(_map_cell['current_timestamp_ms']/1000))) _tstats[1] += _cstats[0]; _tstats[2] += _cstats[1]; _tstats[3] += _cstats[2] db_cur.execute("DELETE FROM _queue WHERE cell_id='{}'".format(cell.to_token())) log.info("Found {} Gyms, {} Pokestops, {} Spawns. Sleeping...".format(*_cstats)) db.commit() time.sleep(int(config.delay)) log.info('Scanned {} cells; got {} Gyms, {} Pokestops, {} Spawns.'.format(*_tstats))
PORTAL_NUM = 11475124 if __name__ == '__main__': p = progressbar.ProgressBar() # data_raw -> data_fc p.start(PORTAL_NUM) total = 0 for x in range(90, -90, -10): for y in range(180, -180, -10): data = np.load(f"data_raw/{x}_{y}.npy") areas = {} for i in range(len(data)): cell_id = CellId.from_lat_lng(LatLng.from_degrees(float(data[i]['lat']), float(data[i]['lng']))) parent = cell_id.parent(5).to_token() if parent not in areas.keys(): if os.path.exists(f"data_fc/{parent}.npy"): areas[parent] = list(np.load(f"data_fc/{parent}.npy")) else: areas[parent] = [] data[i]['cellid'] = cell_id.to_token() data[i]['id'] = total areas[parent].append(data[i]) total += 1 p.update(total) for key, value in areas.items(): np.save(f"data_fc/{key}.npy", value) p.finish()