def stops_from_db_unvisited(self, geofence_helper: GeofenceHelper, origin: str): logger.debug("DbWrapper::stops_from_db_unvisited called") minLat, minLon, maxLat, maxLon = geofence_helper.get_polygon_from_fence( ) query = ( "SELECT pokestop.latitude, pokestop.longitude " "FROM pokestop " "LEFT JOIN trs_visited ON (pokestop.pokestop_id = trs_visited.pokestop_id AND trs_visited.origin='{}') " "WHERE pokestop.latitude >= {} AND pokestop.longitude >= {} " "AND pokestop.latitude <= {} AND pokestop.longitude <= {} " "AND trs_visited.origin IS NULL").format(origin, minLat, minLon, maxLat, maxLon) res = self.execute(query) unvisited: List[Location] = [] for (latitude, longitude) in res: unvisited.append(Location(latitude, longitude)) if geofence_helper is not None: geofenced_coords = geofence_helper.get_geofenced_coordinates( unvisited) return geofenced_coords else: return unvisited
def get_geofences(mapping_manager, data_manager, fence_type=None, area_id_req=None): areas = mapping_manager.get_areas() geofences = {} for area_id, area in areas.items(): if area_id_req is not None and int(area_id) is not int(area_id_req): continue geo_include = data_manager.get_resource('geofence', identifier=area["geofence_included"]) geo_exclude_id = area.get("geofence_excluded", None) geo_exclude = None if geo_exclude_id is not None: geo_exclude = data_manager.get_resource('geofence', identifier=geo_exclude_id) if fence_type is not None and area['mode'] != fence_type: continue area_geofences = GeofenceHelper(geo_include, geo_exclude, area['name']) include = {} exclude = {} for fences in area_geofences.geofenced_areas: include[fences['name']] = [] for fence in fences['polygon']: include[fences['name']].append([get_coord_float(fence['lat']), get_coord_float(fence['lon'])]) for fences in area_geofences.excluded_areas: exclude[fences['name']] = [] for fence in fences['polygon']: exclude[fences['name']].append([get_coord_float(fence['lat']), get_coord_float(fence['lon'])]) geofences[area_id] = { 'include': include, 'exclude': exclude, 'mode': area['mode'], 'area_id': area_id, 'name': area['name'] } return geofences
def _generate_locations(distance: float, geofence_helper: GeofenceHelper): south, east, north, west = geofence_helper.get_polygon_from_fence() corners = [ Location(south, east), Location(south, west), Location(north, east), Location(north, west) ] # get the center center = get_middle_of_coord_list(corners) # get the farthest to the center... farthest_dist = 0 for corner in corners: dist_temp = get_distance_of_two_points_in_meters( center.lat, center.lng, corner.lat, corner.lng) if dist_temp > farthest_dist: farthest_dist = dist_temp # calculate step_limit, round up to reduce risk of losing stuff step_limit = math.ceil(farthest_dist / distance) # This will loop thorugh all the rings in the hex from the centre # moving outwards logger.info("Calculating positions for init scan") num_cores = multiprocessing.cpu_count() with multiprocessing.Pool(processes=num_cores) as pool: temp = [pool.apply(S2Helper._generate_star_locs, args=( center, distance, i)) for i in range(1, step_limit)] results = [item for sublist in temp for item in sublist] results.append(Location(center.lat, center.lng)) logger.info("Filtering positions for init scan") # Geofence results. if geofence_helper is not None and geofence_helper.is_enabled(): results = geofence_helper.get_geofenced_coordinates(results) if not results: logger.error('No cells regarded as valid for desired scan area. Check your provided geofences. ' 'Aborting.') else: logger.info("Ordering location") results = S2Helper.order_location_list_rows(results) return results
def validate_custom(self) -> Dict[str, List[Tuple[str, str]]]: issues = {} try: GeofenceHelper(self, None) except Exception as err: issues = { 'invalid': [('fence_data', 'Must be one coord set per line (float,float)')] } logger.error("Invalid geofence detected for {}: {}", self.identifier, err) return issues
def get_geofences(self): geofences = self._data_manager.get_root_resource("geofence") export = [] for geofence_id, geofence in geofences.items(): geofence_helper = GeofenceHelper(geofence, None, geofence["name"]) if len(geofence_helper.geofenced_areas) == 1: geofenced_area = geofence_helper.geofenced_areas[0] if "polygon" in geofenced_area: export.append({ "id": geofence_id, "name": geofence["name"], "coordinates": geofenced_area["polygon"] }) return jsonify(export)
def get_all_fences(self): self.logger.debug("::get_all_fences") fences = {} query = "select fence_data, name, fence_type from settings_geofence" geofences = self._mad['db_wrapper'].autofetch_all(query) self.logger.debug(f"got geofences: {geofences}") for geofence in geofences: geofence['fence_data'] = ast.literal_eval(geofence['fence_data']) self.logger.debug(f"passing geofence: {geofence}") geofence_helper = GeofenceHelper(geofence, None, geofence["name"]) for geofenced_area in geofence_helper.geofenced_areas: if "polygon" in geofenced_area: if len(geofence_helper.geofenced_areas) == 1: name = geofenced_area["name"] else: name = f'{geofence["name"]}_{geofenced_area["name"]}' if name not in fences: fences[name] = geofenced_area["polygon"] return fences
def __get_latest_routemanagers(self) -> Optional[Dict[str, dict]]: global mode_mapping areas: Optional[Dict[str, dict]] = {} if self.__configmode: return areas raw_areas = self.__data_manager.get_root_resource('area') thread_pool = ThreadPool(processes=4) areas_procs = {} for area_id, area_true in raw_areas.items(): area = area_true.get_resource() if area["geofence_included"] is None: raise RuntimeError("Cannot work without geofence_included") try: geofence_included = self.__data_manager.get_resource( 'geofence', identifier=area["geofence_included"]) except: raise RuntimeError( "geofence_included for area '{}' is specified but does not exist ('{}')." .format(area["name"], geofence_included)) geofence_excluded_raw_path = area.get("geofence_excluded", None) try: if geofence_excluded_raw_path is not None: geofence_excluded = self.__data_manager.get_resource( 'geofence', identifier=geofence_excluded_raw_path) else: geofence_excluded = None except: raise RuntimeError( "geofence_excluded for area '{}' is specified but file does not exist ('{}')." .format(area["name"], geofence_excluded_raw_path)) area_dict = { "mode": area_true.area_type, "geofence_included": geofence_included, "geofence_excluded": geofence_excluded, "routecalc": area["routecalc"], "name": area['name'] } # also build a routemanager for each area... # grab coords # first check if init is false, if so, grab the coords from DB # coords = np.loadtxt(area["coords"], delimiter=',') geofence_helper = GeofenceHelper(geofence_included, geofence_excluded) mode = area_true.area_type # build routemanagers # map iv list to ids if area.get('settings', None) is not None and 'mon_ids_iv' in area['settings']: # replace list name area['settings']['mon_ids_iv_raw'] = \ self.get_monlist(area['settings'].get('mon_ids_iv', None), area.get("name", "unknown")) route_resource = self.__data_manager.get_resource( 'routecalc', identifier=area["routecalc"]) route_manager = RouteManagerFactory.get_routemanager( self.__db_wrapper, self.__data_manager, area_id, None, mode_mapping.get(mode, {}).get("range", 0), mode_mapping.get(mode, {}).get("max_count", 99999999), geofence_included, path_to_exclude_geofence=geofence_excluded, mode=mode, settings=area.get("settings", None), init=area.get("init", False), name=area.get("name", "unknown"), level=area.get("level", False), coords_spawns_known=area.get("coords_spawns_known", False), routefile=route_resource, calctype=area.get("route_calc_algorithm", "optimized"), joinqueue=self.join_routes_queue, S2level=mode_mapping.get(mode, {}).get("s2_cell_level", 30), include_event_id=area.get("settings", {}).get("include_event_id", None)) if mode not in ("iv_mitm", "idle"): coords = self.__fetch_coords( mode, geofence_helper, coords_spawns_known=area.get("coords_spawns_known", False), init=area.get("init", False), range_init=mode_mapping.get(area_true.area_type, {}).get("range_init", 630), including_stops=area.get("including_stops", False), include_event_id=area.get("settings", {}).get("include_event_id", None)) route_manager.add_coords_list(coords) max_radius = mode_mapping[area_true.area_type]["range"] max_count_in_radius = mode_mapping[ area_true.area_type]["max_count"] if not area.get("init", False): logger.info("Initializing area {}", area["name"]) proc = thread_pool.apply_async( route_manager.initial_calculation, args=(max_radius, max_count_in_radius, 0, False)) areas_procs[area_id] = proc else: logger.info("Init mode enabled. Going row-based for {}", str(area.get("name", "unknown"))) # we are in init, let's write the init route to file to make it visible in madmin calc_coords = [] if area["routecalc"] is not None: for loc in coords: calc_coord = '%s,%s' % (str(loc.lat), str(loc.lng)) calc_coords.append(calc_coord) route_resource['routefile'] = calc_coords route_resource.save() # gotta feed the route to routemanager... TODO: without recalc... proc = thread_pool.apply_async(route_manager.recalc_route, args=(1, 99999999, 0, False)) areas_procs[area_id] = proc area_dict["routemanager"] = route_manager areas[area_id] = area_dict for area in areas_procs.keys(): to_be_checked = areas_procs[area] to_be_checked.get() thread_pool.close() thread_pool.join() return areas
def main(): args = Args() initLogging(args) if len(sys.argv) != 2: logger.error("usage: remove_all_spawns_within_geofence.py GEOFENCE_FILENAME") sys.exit(1) LocationWithID = collections.namedtuple('Location', ['lat', 'lng', 'spawnpoint']) geofence_filename = sys.argv[1] # print("Argument: '%s'" % (geofence_filename)) # no .txt, add it if ".txt" not in geofence_filename: geofence_filename = geofence_filename + ".txt" # no / in filename, probably not an absolute path, append standard MAD path if "/" not in geofence_filename: geofence_filename = "../configs/geofences/" + geofence_filename logger.info("Trying to use file: {}", geofence_filename) if not os.path.isfile(geofence_filename): logger.error("Geofence file {} not found, exit", geofence_filename) sys.exit(1) geofence_helper = GeofenceHelper(geofence_filename, None) minLat, minLon, maxLat, maxLon = geofence_helper.get_polygon_from_fence() query = ( "SELECT latitude, longitude, spawnpoint " "FROM trs_spawn " "WHERE (latitude >= {} AND longitude >= {} " "AND latitude <= {} AND longitude <= {}) " ).format(minLat, minLon, maxLat, maxLon) delete_query = ( "DELETE FROM trs_spawn " "WHERE spawnpoint = {} " ) list_of_coords: List[LocationWithID] = [] dbip = get_value_for(r'\s+dbip:\s+([^\s]+)') dbport = get_value_for(r'\s+dbport:\s+([^.\s]*)', False) if dbport is None: # if dbport is not set, use default dbport = '3306' dbusername = get_value_for(r'\s+dbusername:\s+([^.\s]*)') dbpassword = get_value_for(r'\s+dbpassword:\s+([^.\s]*)') dbname = get_value_for(r'\s+dbname:\s+([^.\s]*)') # print("Successfully parsed config.ini, using values:") # print("dbport: %s" % dbport) # print("dbusername: %s" % dbusername) # print("dbname: %s" % dbname) # print("dbip: %s" % dbip) connection = mysql.connector.connect( host=dbip, port=dbport, user=dbusername, passwd=dbpassword, database=dbname) cursor = connection.cursor() cursor.execute(query) res = cursor.fetchall() for (latitude, longitude, spawnpoint) in res: list_of_coords.append(LocationWithID(latitude, longitude, spawnpoint)) geofenced_coords = geofence_helper.get_geofenced_coordinates(list_of_coords) spawnpointcount = len(geofenced_coords) for coords in geofenced_coords: sql = delete_query.format(coords.spawnpoint) cursor.execute(sql) # print(sql) connection.commit() cursor.close() connection.close() logger.success("Done, deleted {} spawnpoints", spawnpointcount)
def _generate_locations(distance: float, geofence_helper: GeofenceHelper): south, east, north, west = geofence_helper.get_polygon_from_fence() corners = [ Location(south, east), Location(south, west), Location(north, east), Location(north, west) ] # get the center center = get_middle_of_coord_list(corners) # get the farthest to the center... farthest_dist = 0 for corner in corners: dist_temp = get_distance_of_two_points_in_meters( center.lat, center.lng, corner.lat, corner.lng) if dist_temp > farthest_dist: farthest_dist = dist_temp # calculate step_limit, round up to reduce risk of losing stuff step_limit = math.ceil(farthest_dist / distance) # This will loop thorugh all the rings in the hex from the centre # moving outwards logger.info("Calculating positions for init scan") num_cores = multiprocessing.cpu_count() with multiprocessing.Pool(processes=num_cores) as pool: temp = [ pool.apply(S2Helper._generate_star_locs, args=(center, distance, i)) for i in range(1, step_limit) ] results = [item for sublist in temp for item in sublist] results.append(Location(center.lat, center.lng)) # for ring in range(1, step_limit): # for i in range(0, 6): # # Star_locs will contain the locations of the 6 vertices of # # the current ring (90,150,210,270,330 and 30 degrees from # # origin) to form a star # star_loc = S2Helper.get_new_coords(center, distance * ring, # 90 + 60 * i) # for j in range(0, ring): # # Then from each point on the star, create locations # # towards the next point of star along the edge of the # # current ring # loc = S2Helper.get_new_coords(star_loc, distance * j, 210 + 60 * i) # results.append(loc) logger.info("Filtering positions for init scan") # Geofence results. if geofence_helper is not None and geofence_helper.is_enabled(): results = geofence_helper.get_geofenced_coordinates(results) if not results: logger.error( 'No cells regarded as valid for desired scan area. ' 'Check your provided geofences. Aborting.') else: logger.info("Ordering location") results = S2Helper.order_location_list_rows(results) return results