Пример #1
0
    def testS2CellUnionEmpty(self):
        empty_cell_union = s2.S2CellUnion()
        self.assertTrue(empty_cell_union.empty())

        cell_id = s2.S2CellId(s2.S2LatLng.FromDegrees(3.0, 4.0)).parent(8)
        cell_union = s2.S2CellUnion()
        cell_union.Init([cell_id.id()])
        self.assertFalse(cell_union.empty())
Пример #2
0
    def testS2CellUnionIsNormalized(self):
        empty_cell_union = s2.S2CellUnion()
        self.assertTrue(empty_cell_union.IsNormalized())

        london = s2.S2LatLng.FromDegrees(51.5001525, -0.1262355)
        london_cell_id = s2.S2CellId(london)
        normalized_union = s2.S2CellUnion()
        normalized_union.Init([london_cell_id.id()])
        self.assertTrue(normalized_union.IsNormalized())
Пример #3
0
    def testS2CellUnionNormalizeS2CellUnion(self):
        empty_cell_union = s2.S2CellUnion()
        empty_cell_union.NormalizeS2CellUnion()
        self.assertTrue(empty_cell_union.IsNormalized())

        cell_id = s2.S2CellId(s2.S2LatLng.FromDegrees(3.0, 4.0)).parent(8)
        cell_union = s2.S2CellUnion()
        cell_union.Init([cell_id.id()])
        cell_union.NormalizeS2CellUnion()
        self.assertTrue(cell_union.IsNormalized())
Пример #4
0
    def testS2CellUnionIntersectionWithS2CellId(self):
        cell_id = s2.S2CellId(s2.S2LatLng.FromDegrees(3.0, 4.0))
        cell_union = s2.S2CellUnion()
        cell_union.Init([cell_id.id()])

        # No intersection.
        outside_cell_id = s2.S2CellId(s2.S2LatLng.FromDegrees(4.0, 5.0))
        empty_intersection = cell_union.Intersection(outside_cell_id)
        self.assertTrue(empty_intersection.empty())

        # Complete overlap.
        intersection = cell_union.Intersection(cell_id)
        self.assertTrue(intersection.Contains(cell_id))

        # Some intersection.
        joint_cell_union = s2.S2CellUnion()
        joint_cell_union.Init([cell_id.id(), outside_cell_id.id()])
        outside_intersection = joint_cell_union.Intersection(outside_cell_id)
        self.assertTrue(outside_intersection.Contains(outside_cell_id))
        self.assertFalse(outside_intersection.Contains(cell_id))
Пример #5
0
  def testS2PolygonGetAreaIsWrappedCorrectly(self):
    # Cell at level 10 containing central London.

    london_level_10 = s2.S2CellId(
        s2.S2LatLng.FromDegrees(51.5001525, -0.1262355)).parent(10)
    polygon = s2.S2Polygon(s2.S2Cell(london_level_10))
    # Because S2Cell.ExactArea() isn't swigged, compare S2Polygon.GetArea() with
    # S2CellUnion.ExactArea().
    cell_union = s2.S2CellUnion()
    cell_union.Init([london_level_10.id()])
    self.assertAlmostEqual(cell_union.ExactArea(), polygon.GetArea(), places=10)
Пример #6
0
    def testS2CellUnionIsWrappedCorrectly(self):
        cell_union = s2.S2CellUnion()
        cell_union.Init([0x466d319000000000, 0x466d31b000000000])
        self.assertEqual(cell_union.num_cells(), 2)
        trondheim = s2.S2LatLng.FromDegrees(63.431052, 10.395083)
        self.assertTrue(cell_union.Contains(s2.S2CellId(trondheim)))

        # Init() calls Normalized, so cell_ids() are normalized.
        cell_union2 = s2.S2CellUnion.FromNormalized(cell_union.cell_ids())
        # There is no S2CellUnion::Equals, and cell_ids is a non-iterable
        # SWIG object, so just perform the same checks again.
        self.assertEqual(cell_union2.num_cells(), 2)
        self.assertTrue(cell_union2.Contains(s2.S2CellId(trondheim)))
Пример #7
0
    def testS2CellUnionRegion(self):
        cell_id = s2.S2CellId(s2.S2LatLng.FromDegrees(3.0, 4.0)).parent(8)
        cell_union = s2.S2CellUnion()
        cell_union.Init([cell_id.id()])

        inside = s2.S2LatLng.FromDegrees(3.0, 4.0).ToPoint()
        outside = s2.S2LatLng.FromDegrees(30.0, 40.0).ToPoint()

        self.assertTrue(cell_union.Contains(inside))
        self.assertFalse(cell_union.Contains(outside))
        self.assertTrue(cell_union.Contains(s2.S2Cell(inside)))
        self.assertFalse(cell_union.Contains(s2.S2Cell(outside)))
        self.assertTrue(cell_union.MayIntersect(s2.S2Cell(inside)))
        self.assertFalse(cell_union.MayIntersect(s2.S2Cell(outside)))

        cap_bound = cell_union.GetCapBound()
        self.assertTrue(cap_bound.Contains(inside))
        self.assertFalse(cap_bound.Contains(outside))

        rect_bound = cell_union.GetRectBound()
        self.assertTrue(rect_bound.Contains(inside))
        self.assertFalse(rect_bound.Contains(outside))
Пример #8
0
    def _randomCaps(self, query_type, **indexer_options):
        # This function creates an index consisting either of points (if
        # options.index_contains_points_only() is true) or S2Caps of random size.
        # It then executes queries consisting of points (if query_type == POINT)
        # or S2Caps of random size (if query_type == CAP).
        #
        # indexer_options are set on both the indexer & coverer (if relevant)
        # eg. _randomCaps('cap', min_level=0) calls indexer.set_min_level(0)
        ITERATIONS = 400

        indexer = s2.S2RegionTermIndexer()
        coverer = s2.S2RegionCoverer()

        # set indexer options
        for opt_key, opt_value in indexer_options.items():
            setter = "set_%s" % opt_key
            getattr(indexer, setter)(opt_value)
            if hasattr(coverer, setter):
                getattr(coverer, setter)(opt_value)

        caps = []
        coverings = []
        index = defaultdict(set)

        index_terms = 0
        query_terms = 0
        for i in range(ITERATIONS):
            # Choose the region to be indexed: either a single point or a cap
            # of random size (up to a full sphere).
            terms = []
            if indexer.index_contains_points_only():
                cap = s2.S2Cap.FromPoint(s2.S2Testing.RandomPoint())
                terms = indexer.GetIndexTerms(cap.center(), "")
            else:
                cap = s2.S2Testing.GetRandomCap(
                    0.3 * s2.S2Cell.AverageArea(indexer.max_level()),
                    4.0 * s2.S2Cell.AverageArea(indexer.min_level()))
                terms = indexer.GetIndexTerms(cap, "")

            caps.append(cap)
            coverings.append(s2.S2CellUnion(coverer.GetCovering(cap)))
            for term in terms:
                index[term].add(i)

            index_terms += len(terms)

        for i in range(ITERATIONS):
            # Choose the region to be queried: either a random point or a cap of
            # random size.
            terms = []

            if query_type == 'cap':
                cap = s2.S2Cap.FromPoint(s2.S2Testing.RandomPoint())
                terms = indexer.GetQueryTerms(cap.center(), "")
            else:
                cap = s2.S2Testing.GetRandomCap(
                    0.3 * s2.S2Cell.AverageArea(indexer.max_level()),
                    4.0 * s2.S2Cell.AverageArea(indexer.min_level()))
                terms = indexer.GetQueryTerms(cap, "")

            # Compute the expected results of the S2Cell query by brute force.
            covering = s2.S2CellUnion(coverer.GetCovering(cap))
            expected, actual = set(), set()
            for j in range(len(caps)):
                if covering.Intersects(coverings[j]):
                    expected.add(j)

            for term in terms:
                actual |= index[term]

            self.assertEqual(expected, actual)
            query_terms += len(terms)

            print("Index terms/doc: %0.2f, Query terms/doc: %0.2f" %
                  (float(index_terms) / ITERATIONS,
                   float(query_terms) / ITERATIONS))
 def testS2CellUnionIsWrappedCorrectly(self):
     cell_union = s2.S2CellUnion()
     cell_union.Init([0x466d319000000000, 0x466d31b000000000])
     self.assertEqual(cell_union.num_cells(), 2)
     trondheim = s2.S2LatLng.FromDegrees(63.431052, 10.395083)
     self.assertTrue(cell_union.Contains(s2.S2CellId(trondheim)))