Skip to content

Commit

Permalink
SWIG-wrap only S2CellId::FromToken(const string&) (google#123)
Browse files Browse the repository at this point in the history
* Add tests for SWIG S2CellId.FromToken/ToToken

* SWIG-wrap only S2CellId::FromToken(const string&)

* Declare S2CellId::FromToken with std::string instead of string to please SWIG.

Fixes google#109
  • Loading branch information
jmr authored Jul 29, 2020
1 parent 1382d0c commit e97eb1e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/python/pywraps2_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ def testS2CellIdComparsionIsWrappedCorrectly(self):
self.assertLess(cell, cell.next())
self.assertGreater(cell.next(), cell)

def testS2CellIdFromToTokenIsWrappedCorrectly(self):
cell = s2.S2CellId.FromToken("487604c489f841c3")
self.assertEqual(cell.ToToken(), "487604c489f841c3")
self.assertEqual(cell.id(), 0x487604c489f841c3)

cell = s2.S2CellId.FromToken("487")
self.assertEqual(cell.ToToken(), "487")
self.assertEqual(cell.id(), 0x4870000000000000)

cell = s2.S2CellId.FromToken("this is invalid")
self.assertEqual(cell.ToToken(), "X")
self.assertEqual(cell.id(), 0)

def testS2CellIdGetEdgeNeighborsIsWrappedCorrectly(self):
cell = s2.S2CellId(0x466d319000000000)
expected_neighbors = [s2.S2CellId(0x466d31b000000000),
Expand Down
3 changes: 2 additions & 1 deletion src/python/s2_common.i
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

%{
#include <sstream>
#include <string>

#include "s2/s2cell_id.h"
#include "s2/s2region.h"
Expand Down Expand Up @@ -307,7 +308,7 @@ class S2Point {
%unignore S2CellId::FromFacePosLevel(int, uint64, int);
%unignore S2CellId::FromLatLng;
%unignore S2CellId::FromPoint;
%unignore S2CellId::FromToken;
%unignore S2CellId::FromToken(const std::string&);
%unignore S2CellId::GetCenterSiTi(int*, int*) const;
%unignore S2CellId::GetEdgeNeighbors;
%unignore S2CellId::ToFaceIJOrientation(int*, int*, int*) const;
Expand Down
2 changes: 1 addition & 1 deletion src/s2/s2cell_id.h
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ class S2CellId {
// FromToken() returns S2CellId::None() for malformed inputs.
string ToToken() const;
static S2CellId FromToken(const char* token, size_t length);
static S2CellId FromToken(const string& token);
static S2CellId FromToken(const std::string& token);

// Use encoder to generate a serialized representation of this cell id.
// Can also encode an invalid cell.
Expand Down

0 comments on commit e97eb1e

Please sign in to comment.