Skip to content

Commit f4cdd86

Browse files
committed
Python: add repr() methods to geodetic/cell classes
1 parent c59d0ca commit f4cdd86

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

src/python/pywraps2_test.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,33 @@ def testS2EarthMetricRadians(self):
613613
radius_m = s2.S2Earth.RadiansToMeters(angle.radians())
614614
self.assertEqual(radius_m, 12340.0)
615615

616+
def testRepr(self):
617+
ll_null = s2.S2LatLng()
618+
self.assertEqual(repr(ll_null), "<S2LatLng: (0.000000,0.000000)>")
619+
620+
lon1 = s2.S2LatLng.FromDegrees(51.3368602, 0.4931979)
621+
self.assertEqual(repr(lon1), "<S2LatLng: (51.336860,0.493198)>")
622+
623+
london = s2.S2LatLngRect(lon1,
624+
s2.S2LatLng.FromDegrees(51.7323965, 0.1495211))
625+
self.assertEqual(repr(london),
626+
"<S2LatLngRect: (51.3368602,0.4931979,51.7323965,0.1495211)>"
627+
)
628+
629+
llr_null = s2.S2LatLngRect()
630+
self.assertEqual(repr(llr_null),
631+
"<S2LatLngRect: Empty>"
632+
)
633+
634+
cell_id = s2.S2CellId.FromToken("487604c489f841c3")
635+
self.assertEqual(repr(cell_id), "<S2CellId: 2/100323000212021010333002003201>")
636+
637+
cell = s2.S2Cell(cell_id)
638+
self.assertEqual(repr(cell), "<S2Cell: 2/100323000212021010333002003201>")
639+
640+
cell_id = s2.S2CellId.FromToken("this is invalid")
641+
self.assertEqual(repr(cell_id), "<S2CellId: Invalid: 0000000000000000>")
642+
616643

617644
class RegionTermIndexerTest(unittest.TestCase):
618645
def _randomCaps(self, query_type, **indexer_options):

src/python/s2_common.i

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,44 @@ USE_EQUALS_FN_FOR_EQ_AND_NE(S2Loop)
724724
USE_EQUALS_FN_FOR_EQ_AND_NE(S2Polygon)
725725
USE_EQUALS_FN_FOR_EQ_AND_NE(S2Polyline)
726726

727+
// repr() methods for geodetic/cell classes
728+
729+
%extend S2CellId {
730+
%pythoncode %{
731+
def __repr__(self):
732+
return '<S2CellId: %s>' % self.ToString()
733+
%}
734+
};
735+
736+
%extend S2Cell {
737+
%pythoncode %{
738+
def __repr__(self):
739+
return '<S2Cell: %s>' % self.id().ToString()
740+
%}
741+
};
742+
743+
%extend S2LatLng {
744+
%pythoncode %{
745+
def __repr__(self):
746+
return '<S2LatLng: (%s)>' % self.ToStringInDegrees()
747+
%}
748+
};
749+
750+
%extend S2LatLngRect {
751+
%pythoncode %{
752+
def __repr__(self):
753+
if self.is_empty():
754+
return '<S2LatLngRect: Empty>'
755+
else:
756+
return '<S2LatLngRect: (%s,%s,%s,%s)>' % (
757+
self.lat_lo().degrees(),
758+
self.lng_lo().degrees(),
759+
self.lat_hi().degrees(),
760+
self.lng_hi().degrees(),
761+
)
762+
%}
763+
};
764+
727765
// Simple implementation of key S2Testing methods
728766
%pythoncode %{
729767
import random

0 commit comments

Comments
 (0)