def test_get_linestring_with_s2_cellids(): from_point = CellId(5221390681063996525) to_point = CellId(5221390693823388667) ls = utils.get_linestring(from_point, to_point) assert isinstance(ls, LineString) assert [(round(c[0], 6), round(c[1], 6)) for c in ls.coords] == [(-0.137925, 51.521699), (-0.134456, 51.520027)]
def get_sub_cells(self, cellid, level=1): sub_cells = [] start = CellId(id_=cellid).child_begin() end = CellId(id_=cellid).child_end() while (start.id() < end.id()): if level == 1: sub_cells.append(start.id()) else: sub_cells += self.get_sub_cells(start.id(), level - 1) start = start.next() return sub_cells
def post(self): log.msg("Got the TestKariosHandler req") from_tm = 0 to_tm = 0 #loc = [] topic = "location" try: from_tm = self.request.arguments['start_time'] to_tm = self.request.arguments['end_time'] except Exception: log.msg("No time given, will give all location") if (from_tm != 0 and to_tm != 0): loc = self.get_data.kdb_get_data(topic, from_tm, to_tm) else: log.msg("inside else") r = yield self.conn.read_absolute(self, ["location"], 0) length = r['queries'][0]['sample_size'] if length: if (length > 100): n = 100 else: n = length res = r['queries'][0]['results'][0]['values'] for i in range(0, n): val = CellId(res[i][1]) lat = val.to_lat_lng().lat().degrees longt = val.to_lat_lng().lng().degrees tm = res[i][0] / 1000 v = {"latitude": lat, "longitude": longt, "timestamp": tm} loc.append(v) data = loc self.write({"resp_code": True, "location": loc})
def get_captured_lat_lng(pokemon): lat, lng = 0, 0 if pokemon.get('captured_cell_id'): cell = CellId(pokemon.get('captured_cell_id')) lat, lng = cell.to_lat_lng().lat().degrees, cell.to_lat_lng().lng( ).degrees return {'lat': lat, 'lng': lng}
async def _send_weather_webhook(self, s2cellId, weatherId, severe, warn, day, time): if self.__application_args.weather_webhook: log.debug("Send Weather Webhook") cell = Cell(CellId(s2cellId)) coords = [] for v in range(0, 4): vertex = LatLng.from_point(cell.get_vertex(v)) coords.append([vertex.lat().degrees, vertex.lng().degrees]) data = weather_webhook_payload.format(s2cellId, coords, weatherId, severe, warn, day, time) log.debug(data) payload = json.loads(data) log.info("Sending weather webhook: %s" % str(payload)) try: response = requests.post( self.__application_args.webhook_url, data=json.dumps(payload), headers={'Content-Type': 'application/json'}, timeout=5 ) if response.status_code != 200: log.warning("Go status code other than 200 OK from webhook destination: %s" % str(response.status_code)) else: log.info("Success sending webhook") except Exception as e: log.warning("Exception occured while sending webhook: %s" % str(e)) else: log.debug("Weather Webhook Disabled")
def get_cellid_center_coordinate(self, cellid): """ get one cell's center point's coordinate [longitude, latitude] :param cellid: lnglat_to_cellid生成的cellid :return:tuple(center_longitude, center_latitude) """ center = Cell(cell_id=CellId(cellid)).get_center() return (LatLng.longitude(center).degrees, LatLng.latitude(center).degrees)
def kdb_get_data(self, topic, from_tm, to_tm): loc = [] result = [] temp = 0 j = k = 0 max_speed = 0 avg_speed = 0 dist = 0 if (from_tm != 0): r = yield self.conn.read_absolute(self, [topic], from_tm, to_tm) length = r['queries'][0]['sample_size'] if length: res = r['queries'][0]['results'][0]['values'] for rs in res: val = CellId(rs[1]) lat = val.to_lat_lng().lat().degrees longt = val.to_lat_lng().lng().degrees tm = rs[0] / 1000 v = { "latitude": lat, "longitude": longt, "timestamp": tm, "speed": 0, 'distance': -1 } loc.append(v) es = r['queries'][0]['results'][0]['tags']['speed'] if len(es): for x in es: x = float(x) loc[j]['speed'] = x j = j + 1 temp = temp + x max_speed = max(es) avg_speed = temp / len(es) avg_speed = float(format(avg_speed, '.2f')) result.append(avg_speed) result.append(float(max_speed)) try: ds = r['queries'][0]['results'][0]['tags']['distance'] if len(ds): for x in ds: x = float(x) dist = dist + x k = k + 1 if (dist == 0.0): continue loc[k]['distance'] = dist except Exception, e: log.msg("No distance recorded in DB for this vehicle") result.append(loc) data = result else: data = "NULL"
async def _send_weather_webhook(self, s2cellId, weatherId, severe, warn, day, time): if self.__application_args.weather_webhook: log.debug("Send Weather Webhook") ll = CellId(s2cellId).to_lat_lng() latitude = ll.lat().degrees longitude = ll.lng().degrees cell = Cell(CellId(s2cellId)) coords = [] for v in range(0, 4): vertex = LatLng.from_point(cell.get_vertex(v)) coords.append([vertex.lat().degrees, vertex.lng().degrees]) data = weather_webhook_payload.format(s2cellId, coords, weatherId, severe, warn, day, time, latitude, longitude) log.debug(data) payload = json.loads(data) self.__sendToWebhook(payload) else: log.debug("Weather Webhook Disabled")
def kdb_get_data(self, topic, from_tm, to_tm): loc = [] r = yield self.conn.read_absolute(self, [topic], from_tm, to_tm) log.msg("test_read_absolute_without_tags: ", r) length = r['queries'][0]['sample_size'] if length: res = r['queries'][0]['results'][0]['values'] for rs in res: val = CellId(rs[1]) lat = val.to_lat_lng().lat().degrees longt = val.to_lat_lng().lng().degrees tm = rs[0] / 1000 v = {"latitude": lat, "longitude": longt, "timestamp": tm} loc.append(v) returnValue(loc)
def _get_cell_id_from_latlong(self, radius=1000): # type: (Optional[int]) -> List[str] position_lat, position_lng, _ = self.api_wrapper.get_position() cells = get_cell_ids(position_lat, position_lng, radius) if self.config['debug']: self._log('Cells:', color='yellow') self._log('Origin: {},{}'.format(position_lat, position_lng), color='yellow') for cell in cells: cell_id = CellId(cell) lat_lng = cell_id.to_lat_lng() self._log('Cell : {},{}'.format(lat_lng.lat().degrees, lat_lng.lng().degrees), color='yellow') return cells
def send_weather_webhook(s2cellId, weatherId, severe, warn, day, time): if args.weather_webhook: log.debug("Send Weather Webhook") cell = Cell(CellId(s2cellId)) coords = [] for v in range(0, 4): vertex = LatLng.from_point(cell.get_vertex(v)) coords.append([vertex.lat().degrees, vertex.lng().degrees]) data = weather_webhook_payload.format(s2cellId, coords, weatherId, severe, warn, day, time) log.debug(data) payload = json.loads(data) response = requests.post(args.webhook_url, data=json.dumps(payload), headers={'Content-Type': 'application/json'}) else: log.debug("Weather Webhook Disabled")
def get_search_points(self, cell_id): points = [] # For cell level 15 for c in Cell(CellId(cell_id)).subdivide(): for cc in c.subdivide(): latlng = LatLng.from_point(cc.get_center()) point = (latlng.lat().degrees, latlng.lng().degrees) points.append(point) points[0], points[1] = points[1], points[0] points[14], points[15] = points[15], points[14] point = points.pop(2) points.insert(7, point) point = points.pop(13) points.insert(8, point) closest = min(points, key=lambda p: great_circle(self.bot.position, p).meters) index = points.index(closest) return points[index:] + points[:index]
Ctargets.append(Ptarget[1]) # fine if len(Ptargets) > 0: initsubgrid = hex_spiral(plat, plng, 70, 2) initsubgrid.pop(0) # already scanned in main thread s = 1 Sdone = [] while len(Ctargets) > 0 and len(Ptargets) > 0: tempsubgrid = [] for tmp in initsubgrid: q = 0 for Ctarget in Ctargets: q += circle_in_cell(CellId(Ctarget), tmp[0], tmp[1], 70, 12) if q > 0: tempsubgrid.append([tmp, q]) tempsubgrid.sort(key=lambda q: q[1], reverse=True) subgrid = [] for tmp in tempsubgrid: subgrid.append(tmp[0]) for spos in subgrid: if spos in Sdone: continue slat, slng = spos[0], spos[1] cell_ids = get_cell_ids( cover_circle(slat, slng, 75, 15))
def main(): config = init_config() if not config: return watchlist = get_watchlist('watch.txt') pokenames = get_pokenames('pokes.txt') db = sqlite3.connect(config.ndbfile) dbc = db.cursor() run = 1 while run: _ccnt, y, z = 1, config.limit, (config.step - 1) * config.limit dbc.execute("SELECT cell_id FROM _queue ORDER BY cell_id LIMIT %d,%d" % (z, y)) del y # http://stackoverflow.com/questions/3614277/how-to-strip-from-python-pyodbc-sql-returns scan_queque = [x[0] for x in dbc.fetchall()] if config.regen or len(scan_queque) == 0: log.info('Generating scan queue...') if gen_que(config.ndbfile, config.fdbfile): continue else: return api = api_init(config) if api == None: log.error('Login failed!') return else: log.info('API online! starting Scan...') time.sleep(5) for queq in scan_queque: try: _ecnt = [0, 0] traverse = 0 targets = [] cell = CellId.from_token(queq) lat = CellId.to_lat_lng(cell).lat().degrees lng = CellId.to_lat_lng(cell).lng().degrees cell_ids = [cell.id()] response_dict = get_response(api, cell_ids, lat, lng) log.info('Scanning cell {} of {}.'.format( _ccnt + z, z + (len(scan_queque)))) for _map_cell in response_dict['responses']['GET_MAP_OBJECTS'][ 'map_cells']: if 'nearby_pokemons' in _map_cell: for _poke in _map_cell['nearby_pokemons']: _ecnt[0] += 1 _s = hex(_poke['encounter_id']) _c = CellId(_map_cell['s2_cell_id']).to_token() dbc.execute( "INSERT OR IGNORE INTO encounters (encounter_id, cell_id, pokemon_id, encounter_time) VALUES ('{}','{}',{},{})" "".format( _s.strip('L'), _c, _poke['pokemon_id'], int(_map_cell['current_timestamp_ms'] / 1000))) if _poke['pokemon_id'] in watchlist: traverse = 1 targets.append(_poke['encounter_id']) log.info('{} nearby!'.format( pokenames[_poke['pokemon_id']])) if 'catchable_pokemons' in _map_cell: for _poke in _map_cell['catchable_pokemons']: _ecnt[1] += 1 _s = hex(_poke['encounter_id']) dbc.execute( "INSERT OR REPLACE INTO encounters (spawn_id, encounter_id, pokemon_id, encounter_time, expire_time) VALUES ('{}','{}',{},{},{})" "".format( _poke['spawn_point_id'], _s.strip('L'), _poke['pokemon_id'], int(_map_cell['current_timestamp_ms'] / 1000), int(_poke['expiration_timestamp_ms'] / 1000))) db.commit() if traverse: _remaining = len(targets) log.info('Narrow search for %d Pokemon...' % len(targets)) time.sleep(config.delay) _scnt = 1 subcells = susub_cells(cell) for _sub in subcells: log.debug('Scanning subcell {} of up to 16.'.format( _scnt, (len(scan_queque)))) lat = CellId.to_lat_lng(_sub).lat().degrees lng = CellId.to_lat_lng(_sub).lng().degrees cell_ids = get_cell_ids(cover_circle(lat, lng, 100)) try: response_dict = get_response( api, cell_ids, lat, lng) except NotLoggedInException: api = None api = api_init(config) response_dict = get_response( api, cell_ids, lat, lng) for _map_cell in response_dict['responses'][ 'GET_MAP_OBJECTS']['map_cells']: if 'catchable_pokemons' in _map_cell: for _poke in _map_cell['catchable_pokemons']: _ecnt[1] += 1 _s = hex(_poke['encounter_id']) dbc.execute( "INSERT OR REPLACE INTO encounters (spawn_id, encounter_id, pokemon_id, encounter_time, expire_time) VALUES ('{}','{}',{},{},{})" "".format( _poke['spawn_point_id'], _s.strip('L'), _poke['pokemon_id'], int(_map_cell[ 'current_timestamp_ms'] / 1000), int(_poke[ 'expiration_timestamp_ms'] / 1000))) if _poke['encounter_id'] in targets: log.info('Tracked down {}!'.format( pokenames[_poke['pokemon_id']])) _remaining -= 1 log.info('%d Pokemon remaining...' % _remaining) if _remaining <= 0: break time.sleep(config.delay) _scnt += 1 db.commit() log.info("Encounters: {} coarse, {} fine...".format(*_ecnt)) time.sleep(config.delay) _ccnt += 1 except NotLoggedInException: api = None break log.info("Rinsing 'n' Repeating...")
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))
def main(): config = init_config() if not config: return if not os.path.isfile('db2.sqlite'): log.error('DB not found - please run nestgen.py!') return watchlist = get_watchlist('watch.txt') pokenames = get_pokenames('pokes.txt') log.info("DB ok. Loggin' in...") db = sqlite3.connect('db2.sqlite') db_cur = db.cursor() run = 1 while run: _ccnt = 1 db_cur.execute( "SELECT cell_id FROM queque ORDER BY spawn_count LIMIT %d" % config.limit) # http://stackoverflow.com/questions/3614277/how-to-strip-from-python-pyodbc-sql-returns scan_queque = [x[0] for x in db_cur.fetchall()] 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 queq in scan_queque: try: _ecnt = [0, 0] traverse = 0 targets = [] cell = CellId.from_token(queq) _ll = CellId.to_lat_lng(cell) lat, lng, alt = _ll.lat().degrees, _ll.lng().degrees, 0 cell_ids = [cell.id()] response_dict = get_response(cell_ids, lat, lng, alt, api, config) log.info('Scanning macrocell {} of {}.'.format( _ccnt, (len(scan_queque)))) for _map_cell in response_dict['responses']['GET_MAP_OBJECTS'][ 'map_cells']: if 'nearby_pokemons' in _map_cell: for _poke in _map_cell['nearby_pokemons']: _ecnt[0] += 1 _s = hex(_poke['encounter_id']) _c = CellId(_map_cell['s2_cell_id']).to_token() db_cur.execute( "INSERT OR IGNORE INTO encounters (encounter_id, cell_id, pokemon_id, encounter_time) VALUES ('{}','{}',{},{})" "".format( _s.strip('L'), _c.strip('L'), _poke['pokemon_id'], int(_map_cell['current_timestamp_ms'] / 1000))) if _poke['pokemon_id'] in watchlist: traverse = 1 targets.append(_poke['encounter_id']) log.info('{} nearby!'.format( pokenames[_poke['pokemon_id']])) if 'catchable_pokemons' in _map_cell: for _poke in _map_cell['catchable_pokemons']: _ecnt[1] += 1 _s = hex(_poke['encounter_id']) db_cur.execute( "INSERT OR REPLACE INTO encounters (spawn_id, encounter_id, pokemon_id, encounter_time, expire_time) VALUES ('{}','{}',{},{},{})" "".format( _poke['spawn_point_id'], _s.strip('L'), _poke['pokemon_id'], int(_map_cell['current_timestamp_ms'] / 1000), int(_poke['expiration_timestamp_ms'] / 1000))) db.commit if traverse: _remaining = len(targets) log.info('Narrow search for %d Pokemon...' % len(targets)) time.sleep(config.delay) _scnt = 1 subcells = susub_cells(cell) for _sub in subcells: log.info('Scanning subcell {} of up to 16.'.format( _scnt, (len(scan_queque)))) _ll = CellId.to_lat_lng(_sub) lat, lng, alt = _ll.lat().degrees, _ll.lng().degrees, 0 cell_ids = get_cell_ids(lat, lng, 100) 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']: if 'catchable_pokemons' in _map_cell: for _poke in _map_cell['catchable_pokemons']: _ecnt[1] += 1 _s = hex(_poke['encounter_id']) db_cur.execute( "INSERT OR REPLACE INTO encounters (spawn_id, encounter_id, pokemon_id, encounter_time, expire_time) VALUES ('{}','{}',{},{},{})" "".format( _poke['spawn_point_id'], _s.strip('L'), _poke['pokemon_id'], int(_map_cell[ 'current_timestamp_ms'] / 1000), int(_poke[ 'expiration_timestamp_ms'] / 1000))) if _poke['encounter_id'] in targets: log.info('Tracked down {}!'.format( pokenames[_poke['pokemon_id']])) _remaining -= 1 log.info('%d Pokemon remaining...' % _remaining) if _remaining <= 0: break time.sleep(config.delay) _scnt += 1 db.commit() log.info("Encounters: {} coarse, {} fine...".format(*_ecnt)) time.sleep(config.delay) _ccnt += 1 except NotLoggedInException: del api break log.info("Rinsing 'n' Repeating...")
def get_position_from_cell_id(cellid): cell = CellId(id_=cellid).to_lat_lng() return (math.degrees(cell._LatLng__coords[0]), math.degrees(cell._LatLng__coords[1]), 0)
k = k + 1 if (dist == 0.0): continue loc[k]['distance'] = dist except Exception, e: log.msg("No distance recorded in DB for this vehicle") result.append(loc) data = result else: data = "NULL" else: r = yield self.conn.read_absolute(self, [topic], 0) n = r['queries'][0]['sample_size'] if n: es = r['queries'][0]['results'][0]['values'] val = CellId(es[n - 1][1]) lat = val.to_lat_lng().lat().degrees longt = val.to_lat_lng().lng().degrees tm = es[n - 1][0] / 1000 sp = r['queries'][0]['results'][0]['tags']['speed'] x = len(sp) speed = sp[x - 1] try: sp = r['queries'][0]['results'][0]['tags']['distance'] x = len(sp) dist = sp[x - 1] except Exception, e: log.msg("No distance recorded in DB for this vehicle") dist = -1 v = { "latitude": lat,