def testEncodeDecode(self): london = s2.S2LatLng.FromDegrees(51.5001525, -0.1262355) polygon = s2.S2Polygon(s2.S2Cell(s2.S2CellId(london).parent(15))) self.assertEqual(polygon.num_loops(), 1) encoder = s2.Encoder() polygon.Encode(encoder) encoded = encoder.buffer() decoder = s2.Decoder(encoded) decoded_polygon = s2.S2Polygon() self.assertTrue(decoded_polygon.Decode(decoder)) self.assertEqual(decoded_polygon.num_loops(), 1) self.assertTrue(decoded_polygon.Equals(polygon))
def testS2PolygonIntersectsWithPolyline(self): london = s2.S2LatLng.FromDegrees(51.5001525, -0.1262355) polygon = s2.S2Polygon(s2.S2Cell(s2.S2CellId(london).parent(15))) line = s2.S2Polyline() line.InitFromS2LatLngs([s2.S2LatLng.FromDegrees(51.5, -0.128), s2.S2LatLng.FromDegrees(51.5, -0.125)]) intersections = polygon.IntersectWithPolyline(line) self.assertEqual(1, len(intersections))
def testS2LoopIsWrappedCorrectly(self): london = s2.S2LatLng.FromDegrees(51.5001525, -0.1262355) polygon = s2.S2Polygon(s2.S2Cell(s2.S2CellId(london))) loop = polygon.loop(0) self.assertTrue(loop.IsValid()) self.assertEqual(0, loop.depth()) self.assertFalse(loop.is_hole()) self.assertEqual(4, loop.num_vertices()) point = london.ToPoint() self.assertTrue(loop.Contains(point))
def testGetS2LatLngVertexIsWrappedCorrectly(self): london = s2.S2LatLng.FromDegrees(51.5001525, -0.1262355) polygon = s2.S2Polygon(s2.S2Cell(s2.S2CellId(london))) loop = polygon.loop(0) first_vertex = loop.GetS2LatLngVertex(0) self.assertIsInstance(first_vertex, s2.S2LatLng) self.assertEqual("51.500152,-0.126235", first_vertex.ToStringInDegrees()) second_vertex = loop.GetS2LatLngVertex(1) self.assertIsInstance(second_vertex, s2.S2LatLng) self.assertEqual("51.500153,-0.126235", second_vertex.ToStringInDegrees())
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)
def testS2PolygonGetOverlapFractions(self): # Matches S2Polygon, OverlapFractions test from cs/s2polygon_test.cc a = s2.S2Polygon() b = s2.S2Polygon() r1, r2 = s2.S2Polygon.GetOverlapFractions(a, b) self.assertAlmostEqual(1.0, r1) self.assertAlmostEqual(1.0, r2) def verts2loop(vs): loop = s2.S2Loop() loop.Init([s2.S2LatLng.FromDegrees(*v).ToPoint() for v in vs]) return loop loop1verts = [(-10, 10), (0, 10), (0, -10), (-10, -10), (-10, 0)] b = s2.S2Polygon(verts2loop(loop1verts)) r1, r2 = s2.S2Polygon.GetOverlapFractions(a, b) self.assertAlmostEqual(1.0, r1) self.assertAlmostEqual(0.0, r2) loop2verts = [(-10, 0), (10, 0), (10, -10), (-10, -10)] a = s2.S2Polygon(verts2loop(loop2verts)) r1, r2 = s2.S2Polygon.GetOverlapFractions(a, b) self.assertAlmostEqual(0.5, r1) self.assertAlmostEqual(0.5, r2)
def testS2PolygonRegion(self): cell = s2.S2Cell(s2.S2CellId(s2.S2LatLng.FromDegrees(3.0, 4.0)).parent(8)) polygon = s2.S2Polygon(cell) inside = s2.S2LatLng.FromDegrees(3.0, 4.0).ToPoint() outside = s2.S2LatLng.FromDegrees(30.0, 40.0).ToPoint() self.assertTrue(polygon.Contains(inside)) self.assertFalse(polygon.Contains(outside)) self.assertTrue(polygon.Contains(s2.S2Cell(inside))) self.assertFalse(polygon.Contains(s2.S2Cell(outside))) self.assertTrue(polygon.MayIntersect(s2.S2Cell(inside))) self.assertFalse(polygon.MayIntersect(s2.S2Cell(outside))) cap_bound = polygon.GetCapBound() self.assertTrue(cap_bound.Contains(inside)) self.assertFalse(cap_bound.Contains(outside)) rect_bound = polygon.GetRectBound() self.assertTrue(rect_bound.Contains(inside)) self.assertFalse(rect_bound.Contains(outside))
def testS2PolygonUsesValueEquality(self): self.assertEqual(s2.S2Polygon(), s2.S2Polygon())
def testS2PolygonInitNestedWithIncorrectTypeIsWrappedCorrectly(self): london = s2.S2LatLng.FromDegrees(51.5001525, -0.1262355) loop = s2.S2Loop(s2.S2Cell(s2.S2CellId(london))) polygon = s2.S2Polygon() with self.assertRaises(TypeError): polygon.InitNested([loop, s2.S2CellId()])
def testS2PolygonInitNestedIsWrappedCorrectly(self): london = s2.S2LatLng.FromDegrees(51.5001525, -0.1262355) small_loop = s2.S2Loop(s2.S2Cell(s2.S2CellId(london))) big_loop = s2.S2Loop(s2.S2Cell(s2.S2CellId(london).parent(1))) polygon = s2.S2Polygon() polygon.InitNested([big_loop, small_loop])
def testS2PolygonCopiesLoopInConstructorBecauseItTakesOwnership(self): london = s2.S2LatLng.FromDegrees(51.5001525, -0.1262355) loop = s2.S2Loop(s2.S2Cell(s2.S2CellId(london))) s2.S2Polygon(loop)
def testS2PolygonIsWrappedCorrectly(self): london = s2.S2LatLng.FromDegrees(51.5001525, -0.1262355) polygon = s2.S2Polygon(s2.S2Cell(s2.S2CellId(london))) self.assertEqual(polygon.num_loops(), 1) point = london.ToPoint() self.assertTrue(polygon.Contains(point))