async def s2_cap(self): coords = await self.center_coords() if not coords: return None point = s2.S2LatLng.FromDegrees(*coords).ToPoint() radius = await self.radius() angle = radius / 6371.0 cap = s2.S2Cap(point, s2.S1Angle.Radians(angle)) return cap
def testS2CapRegion(self): center = s2.S2LatLng.FromDegrees(2.0, 3.0).ToPoint() cap = s2.S2Cap(center, s2.S1Angle.Degrees(1.0)) inside = s2.S2LatLng.FromDegrees(2.1, 2.9).ToPoint() outside = s2.S2LatLng.FromDegrees(0.0, 0.0).ToPoint() self.assertTrue(cap.Contains(inside)) self.assertFalse(cap.Contains(outside)) self.assertTrue(cap.Contains(s2.S2Cell(inside))) self.assertFalse(cap.Contains(s2.S2Cell(outside))) self.assertTrue(cap.MayIntersect(s2.S2Cell(inside))) self.assertFalse(cap.MayIntersect(s2.S2Cell(outside))) self.assertTrue(cap.ApproxEquals(cap.GetCapBound())) rect_bound = cap.GetRectBound() self.assertTrue(rect_bound.Contains(inside)) self.assertFalse(rect_bound.Contains(outside))
def main(): parser = argparse.ArgumentParser( description=( "This example shows how to convert spatial data into index terms, " "which can then be indexed along with the other document " "information." ) ) parser.add_argument( '--num_documents', type=int, default=10000, help="Number of documents" ) parser.add_argument( '--num_queries', type=int, default=10000, help="Number of queries" ) parser.add_argument( '--query_radius_km', type=float, default=100, help="Query radius in kilometers" ) args = parser.parse_args() # A prefix added to spatial terms to distinguish them from other index terms # (e.g. representing words or phrases). PREFIX = "s2:" # Create a set of "documents" to be indexed. Each document consists of a # single point. (You can easily substitute any S2Region type here, or even # index a mixture of region types using S2Region. Other # region types include polygons, polylines, rectangles, discs, buffered # geometry, etc.) documents = [] for i in range(args.num_documents): documents.append(s2.S2Testing.RandomPoint()) # We use a dict as our inverted index. The key is an index term, and # the value is the set of "document ids" where this index term is present. index = defaultdict(set) # Create an indexer suitable for an index that contains points only. # (You may also want to adjust min_level() or max_level() if you plan # on querying very large or very small regions.) indexer = s2.S2RegionTermIndexer() indexer.set_index_contains_points_only(True) # Add the documents to the index. for docid, index_region in enumerate(documents): for term in indexer.GetIndexTerms(index_region, PREFIX): index[term].add(docid) # Convert the query radius to an angle representation. radius = s2.S1Angle.Radians(s2.S2Earth.KmToRadians(args.query_radius_km)) # Count the number of documents (points) found in all queries. num_found = 0 for i in range(args.num_queries): # Choose a random center for query. query_region = s2.S2Cap(s2.S2Testing.RandomPoint(), radius) # Convert the query region to a set of terms, and compute the union of # the document ids associated with those terms. (An actual information # retrieval system would do something more sophisticated.) candidates = set() for term in indexer.GetQueryTerms(query_region, PREFIX): candidates |= index[term] # "candidates" now contains all documents that intersect the query # region, along with some documents that nearly intersect it. We can # prune the results by retrieving the original "document" and checking # the distance more precisely. result = [] for docid in candidates: if query_region.Contains(documents[docid]): result.append(docid) # Now do something with the results (in this example we just count # them). num_found += len(result) print("Found %d points in %d queries" % (num_found, args.num_queries))