Skip to content

Commit

Permalink
Python: add repr() methods to geodetic/cell classes
Browse files Browse the repository at this point in the history
  • Loading branch information
rcoup committed Dec 14, 2020
1 parent 340f569 commit a5f5518
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/python/pywraps2_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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), "<S2LatLng: (0.000000,0.000000)>")

lon1 = s2.S2LatLng.FromDegrees(51.3368602, 0.4931979)
self.assertEqual(repr(lon1), "<S2LatLng: (51.336860,0.493198)>")

london = s2.S2LatLngRect(lon1,
s2.S2LatLng.FromDegrees(51.7323965, 0.1495211))
self.assertEqual(repr(london),
"<S2LatLngRect: (51.3368602,0.4931979,51.7323965,0.1495211)>"
)

llr_null = s2.S2LatLngRect()
self.assertEqual(repr(llr_null),
"<S2LatLngRect: Empty>"
)

cell_id = s2.S2CellId.FromToken("487604c489f841c3")
self.assertEqual(repr(cell_id), "<S2CellId: 2/100323000212021010333002003201>")

cell = s2.S2Cell(cell_id)
self.assertEqual(repr(cell), "<S2Cell: 2/100323000212021010333002003201>")

cell_id = s2.S2CellId.FromToken("this is invalid")
self.assertEqual(repr(cell_id), "<S2CellId: Invalid: 0000000000000000>")


class RegionTermIndexerTest(unittest.TestCase):
def _randomCaps(self, query_type, **indexer_options):
Expand Down
38 changes: 38 additions & 0 deletions src/python/s2_common.i
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<S2CellId: %s>' % self.ToString()
%}
};

%extend S2Cell {
%pythoncode %{
def __repr__(self):
return '<S2Cell: %s>' % self.id().ToString()
%}
};

%extend S2LatLng {
%pythoncode %{
def __repr__(self):
return '<S2LatLng: (%s)>' % self.ToStringInDegrees()
%}
};

%extend S2LatLngRect {
%pythoncode %{
def __repr__(self):
if self.is_empty():
return '<S2LatLngRect: Empty>'
else:
return '<S2LatLngRect: (%s,%s,%s,%s)>' % (
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
Expand Down

0 comments on commit a5f5518

Please sign in to comment.