diff --git a/src/python/pywraps2_test.py b/src/python/pywraps2_test.py index 3c06c717..93ce0f5e 100644 --- a/src/python/pywraps2_test.py +++ b/src/python/pywraps2_test.py @@ -613,6 +613,33 @@ def testS2EarthMetricRadians(self): radius_m = s2.S2Earth.RadiansToMeters(angle.radians()) self.assertEqual(radius_m, 12340.0) + def testRepr(self): + ll_null = s2.S2LatLng() + self.assertEqual(repr(ll_null), "") + + lon1 = s2.S2LatLng.FromDegrees(51.3368602, 0.4931979) + self.assertEqual(repr(lon1), "") + + london = s2.S2LatLngRect(lon1, + s2.S2LatLng.FromDegrees(51.7323965, 0.1495211)) + self.assertEqual(repr(london), + "" + ) + + llr_null = s2.S2LatLngRect() + self.assertEqual(repr(llr_null), + "" + ) + + cell_id = s2.S2CellId.FromToken("487604c489f841c3") + self.assertEqual(repr(cell_id), "") + + cell = s2.S2Cell(cell_id) + self.assertEqual(repr(cell), "") + + cell_id = s2.S2CellId.FromToken("this is invalid") + self.assertEqual(repr(cell_id), "") + class RegionTermIndexerTest(unittest.TestCase): def _randomCaps(self, query_type, **indexer_options): diff --git a/src/python/s2_common.i b/src/python/s2_common.i index 6feba3ad..d9b61d45 100644 --- a/src/python/s2_common.i +++ b/src/python/s2_common.i @@ -724,6 +724,44 @@ USE_EQUALS_FN_FOR_EQ_AND_NE(S2Loop) USE_EQUALS_FN_FOR_EQ_AND_NE(S2Polygon) USE_EQUALS_FN_FOR_EQ_AND_NE(S2Polyline) +// repr() methods for geodetic/cell classes + +%extend S2CellId { + %pythoncode %{ + def __repr__(self): + return '' % self.ToString() + %} +}; + +%extend S2Cell { + %pythoncode %{ + def __repr__(self): + return '' % self.id().ToString() + %} +}; + +%extend S2LatLng { + %pythoncode %{ + def __repr__(self): + return '' % self.ToStringInDegrees() + %} +}; + +%extend S2LatLngRect { + %pythoncode %{ + def __repr__(self): + if self.is_empty(): + return '' + else: + return '' % ( + self.lat_lo().degrees(), + self.lng_lo().degrees(), + self.lat_hi().degrees(), + self.lng_hi().degrees(), + ) + %} +}; + // Simple implementation of key S2Testing methods %pythoncode %{ import random