def get_s2_coverage(swLat, swLng, neLat, neLng): geoms = [] r = s2sphere.RegionCoverer() r.min_level = 10 r.max_level = 10 r.max_cells = 40 p1 = s2sphere.LatLng.from_degrees(float(swLat), float(swLng)) p2 = s2sphere.LatLng.from_degrees(float(neLat), float(neLng)) covering = r.get_covering(s2sphere.LatLngRect.from_point_pair(p1, p2)) for cell_id in covering: cell_to_render = {} rect_bound = s2sphere.Cell(cell_id) center = s2sphere.LatLng.from_point(rect_bound.get_center()) cell_to_render['s2_cell_id'] = str(cell_id.id()) cell_to_render['center'] = { 'lat': center.lat().degrees, 'lng': center.lng().degrees } cell_to_render['vertices'] = get_vertices_from_s2cell(rect_bound) # log.info(rect_bound.approx_area()) del rect_bound geoms.append(cell_to_render) return geoms
def search_map(request): print(request) params = {} params["north"] = 1 params["south"] = 1 params["east"] = 1 params["west"] = 1 json_result = [] # 1. Check redis results # redis_cached = check_redis(params) # if len(redis_cached) != 0: # json_result.append(redis_cached) tic = time.gmtime(0) # # 2. Check database results region = s2sphere.RegionCoverer() p1 = s2sphere.LatLng.from_degrees(params["north"], params["west"]) p2 = s2sphere.LatLng.from_degrees(params["south"], params["east"]) cell_ids = region.get_covering(s2sphere.LatLngRect.from_point_pair(p1, p2)) db_result = search_database(cell_ids) toc = time.gmtime(0) # if toc - tic > COMMON_TIME_LAG: # return json_result if len(db_result) == 0: data_fetcher.fetch(cell_ids) db_result =search_database(cell_ids) else: return HttpResponse(json.dupms(json_result))
def get_cell_ids(lat, lng, angle): region = s2sphere.Cap.from_axis_angle(s2sphere.LatLng.from_degrees(lat, lng).to_point(), s2sphere.Angle.from_radians(angle)) coverer = s2sphere.RegionCoverer() coverer.min_level = 9 coverer.max_level = 9 cells = coverer.get_covering(region) return cells
def main(): r = s2sphere.RegionCoverer() p1 = s2sphere.LatLng.from_degrees(48.831776, 2.222639) p2 = s2sphere.LatLng.from_degrees(48.902839, 2.406) cell_ids = r.get_covering(s2sphere.LatLngRect.from_point_pair(p1, p2)) # print(cell_ids) # create a map map_osm = folium.Map(location=[48.86, 2.3], zoom_start=12, tiles='Stamen Toner') # # get vertices from rect to draw them on map # rect_vertices = [] # for i in [0, 1, 2, 3, 0]: # rect_vertices.append([vertex.lat().degrees(), vertex.lng().degrees()]) # draw the cells style_function = lambda x: {'weight': 1, 'fillColor': '#eea500'} for cellid in cell_ids: cell = s2sphere.Cell(cellid) vertices = [] for i in range(0, 4): vertex = cell.get_vertex(i) latlng = s2sphere.LatLng.from_point(vertex) # currently the angle is in radians vertices.append((math.degrees(latlng.lat().radians), math.degrees(latlng.lng().radians))) gj = folium.GeoJson({"type": "Polygon", "coordinates": [vertices]}, style_function=style_function) gj.add_child(folium.Popup(cellid.to_token())) gj.add_to(map_osm)
def get_s2cells_from_circle(lat, lng, radius, level=15): earth = 6371000 region = s2sphere.Cap.from_axis_angle(s2sphere.LatLng.from_degrees(lat, lng).to_point(), s2sphere.Angle.from_degrees(360 * radius / (2 * math.pi * earth))) coverer = s2sphere.RegionCoverer() coverer.min_level = level coverer.max_level = level cells = coverer.get_covering(region) return cells
def lat_lng_to_cell_id(lat, lng, level=10): region_cover = s2sphere.RegionCoverer() region_cover.min_level = level region_cover.max_level = level region_cover.max_cells = 1 p1 = s2sphere.LatLng.from_degrees(lat, lng) p2 = s2sphere.LatLng.from_degrees(lat, lng) covering = region_cover.get_covering(s2sphere.LatLngRect.from_point_pair(p1, p2)) # we will only get our desired cell ;) return covering[0].id()
def break_down_area_to_cell(north, south, west, east): """Return a list of s2 cell id""" result = [] region = s2sphere.RegionCoverer() region.min_level = 15 region.max_level = 15 p1 = s2sphere.LatLng.from_degrees(north, west) p2 = s2sphere.LatLng.from_degrees(south, east) cell_ids = region.get_covering(s2sphere.LatLngRect.from_point_pair(p1, p2)) result += [cell_id.id() for cell_id in cell_ids] return result
def test_covering(self): r = s2sphere.RegionCoverer() p1 = s2sphere.LatLng.from_degrees(33, -122) p2 = s2sphere.LatLng.from_degrees(33.1, -122.1) cell_ids = r.get_covering(s2sphere.LatLngRect.from_point_pair(p1, p2)) ids = sorted([c.id() for c in cell_ids]) target = [ 9291041754864156672, 9291043953887412224, 9291044503643226112, 9291045878032760832, 9291047252422295552, 9291047802178109440, 9291051650468806656, 9291052200224620544 ] self.assertEqual(ids, target)
def getNeighbors(loc, level=15, spread=700): distance = VincentyDistance(meters=spread) center = (loc[0], loc[1], 0) p1 = distance.destination(point=center, bearing=45) p2 = distance.destination(point=center, bearing=225) p1 = s2sphere.LatLng.from_degrees(p1[0], p1[1]) p2 = s2sphere.LatLng.from_degrees(p2[0], p2[1]) rect = s2sphere.LatLngRect.from_point_pair(p1, p2) region = s2sphere.RegionCoverer() region.min_level = level region.max_level = level cells = region.get_covering(rect) return sorted([c.id() for c in cells])
def get_s2_cells(n=north, w=west, s=south, e=east, level=12): region_covered = s2sphere.LatLngRect.from_point_pair( s2sphere.LatLng.from_degrees(n, w), s2sphere.LatLng.from_degrees(s, e)) coverer = s2sphere.RegionCoverer() coverer.min_level = level coverer.max_level = level coverer.max_cells = 50 covering = coverer.get_covering(region_covered) markers = [] for cellid in covering: cell = s2sphere.Cell(cellid) markers.append({ 'id': 'cell-' + str(cellid.id()), 'coords': [(get_vertex(cell, v)) for v in range(0, 4)] }) return markers
def calc_s2_cells(north, south, west, east, cell_size=16): centers_in_area = [] region = s2sphere.RegionCoverer() region.min_level = cell_size region.max_level = cell_size p1 = s2sphere.LatLng.from_degrees(north, west) p2 = s2sphere.LatLng.from_degrees(south, east) cell_ids = region.get_covering( s2sphere.LatLngRect.from_point_pair(p1, p2)) log.debug('Detecting ' + str(len(cell_ids)) + ' L{} Cells in Area'.format(str(cell_size))) for cell_id in cell_ids: split_cell_id = str(cell_id).split(' ') position = S2Helper.get_position_from_cell(int(split_cell_id[1], 16)) centers_in_area.append([position[0], position[1]]) # calc_route_data.append(str(position[0]) + ', ' + str(position[1])) return centers_in_area
def generate_cells(nLat, nLng, sLat, sLng, cell_size=13): points = [] area = s2sphere.LatLngRect.from_point_pair( s2sphere.LatLng.from_degrees(nLat, nLng), s2sphere.LatLng.from_degrees(sLat, sLng)) r = s2sphere.RegionCoverer() r.min_level = cell_size r.max_level = cell_size cells = r.get_covering(area) for cell in cells: c = s2sphere.Cell(cell.parent(13)) ll = s2sphere.LatLng.from_point(c.get_center()) points.append([ll.lat().degrees, ll.lng().degrees]) return points
def get_s2_cells_from_fence(geofence, cell_size=16): _geofence = geofence south, east, north, west = _geofence.get_polygon_from_fence() calc_route_data = [] region = s2sphere.RegionCoverer() region.min_level = cell_size region.max_level = cell_size p1 = s2sphere.LatLng.from_degrees(north, west) p2 = s2sphere.LatLng.from_degrees(south, east) cell_ids = region.get_covering( s2sphere.LatLngRect.from_point_pair(p1, p2)) for cell_id in cell_ids: split_cell_id = str(cell_id).split(' ') position = S2Helper.middle_of_cell(int(split_cell_id[1], 16)) calc_route_data.append([position[0], position[1]]) log.debug('Detecting ' + str(len(calc_route_data)) + ' L{} Cells in Area'.format(str(cell_size))) return calc_route_data
def break_down_area_to_cell(north, south, west, east): result = [] region = s2sphere.RegionCoverer() region.min_level = 15 region.max_level = 15 p1 = s2sphere.LatLng.from_degrees(north, west) p2 = s2sphere.LatLng.from_degrees(south, east) rect = s2sphere.LatLngRect.from_point_pair(p1, p2) area = rect.area() if (area * 1000 * 1000 * 100 > 7): print DBG + "The area is too big, return ..." #logger.info(DBG + "The area is too big, return ...") return cell_ids = region.get_covering(rect) result += [cell_id.id() for cell_id in cell_ids] return result
def get_cellids_in_range(latitude, longitude, range_in_meters, min_level, max_level, max_cells, level_mod=1): ll = s2sphere.LatLng.from_degrees(latitude, longitude) cap = s2sphere.Cap(ll.to_point(), (meters_to_radians(range_in_meters)**2) / 2) cap_rect_bound = cap.get_rect_bound() coverer = s2sphere.RegionCoverer() coverer.min_level = min_level coverer.max_level = max_level coverer.max_cells = max_cells coverer.level_mod = level_mod cell_ids = coverer.get_covering(cap) return cell_ids
def getCircleCoveringRect(lat, lng, radius, parent_level): radius_radians = earthMetersToRadians(radius) latlng = s2sphere.LatLng.from_degrees(float(lat), float(lng)).normalized().to_point() region = s2sphere.Cap.from_axis_height( latlng, (radius_radians * radius_radians) / 2) coverer = s2sphere.RegionCoverer() coverer.min_level = int(parent_level) coverer.max_level = int(parent_level) coverer.max_cells = MAX_S2_CELLS covering = coverer.get_covering(region) s2_rect = [] for cell_id in covering: new_cell = s2sphere.Cell(cell_id) vertices = [] for i in range(4): vertex = new_cell.get_vertex(i) latlng = s2sphere.LatLng.from_point(vertex) vertices.append((math.degrees(latlng.lat().radians), math.degrees(latlng.lng().radians))) s2_rect.append(vertices) return s2_rect
import socket from s2sphere import CellId, math, Cap, LatLng, Angle from s2sphere import RegionCoverer HOST = '' PORT = 50007 MAX_NUMBER_OF_SIMULATION = 100 POKEMON_NUMBER = 493 EXPIRATION_DURATION = 60 * 20 * 1000 client = MongoClient('localhost', 27017) db = client.pokemon_golazzy_data posts = db.posts region = s2sphere.RegionCoverer() class Pokemon: def __init__(self, pokemon_id, lng, lat, expire, cell_id): self.pokemon_id = pokemon_id self.lng = lng self.lat = lat self.expire = expire self.cell_id = cell_id def __str__(self): return "id: {0}, lng: {1}, lat: {2}, expire: {3}".format( self.pokemon_id, self.lng, self.lat, self.expire)
def getRectCoveringRect(max_lon, min_lon, max_lat, min_lat): r = s2sphere.RegionCoverer() p1 = s2sphere.LatLng.from_degrees(min_lat, min_lon) p2 = s2sphere.LatLng.from_degrees(max_lat, max_lon) cell_ids = r.get_covering(s2sphere.LatLngRect.from_point_pair(p1, p2)) return cell_ids