Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for python Encode / Decode #9

Closed
ryanchapman opened this issue Mar 2, 2018 · 4 comments
Closed

Support for python Encode / Decode #9

ryanchapman opened this issue Mar 2, 2018 · 4 comments
Assignees

Comments

@ryanchapman
Copy link

Hi,

I'm trying to encode and decode a polygon using as an example the C++ tests, and the Encoder() class doesn't seem to work the same, or maybe it isn't yet exposed in the SWIG files.

[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pywraps2 as s2
>>> london = s2.S2LatLng.FromDegrees(51.5001525, -0.1262355)
>>> src = s2.S2Polygon(s2.S2Cell(s2.S2CellId(london)))
>>> encoder = s2.Encoder()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'Encoder'

Is there a different way to accomplish this in python?

@jmr jmr self-assigned this Mar 2, 2018
@jmr
Copy link
Member

jmr commented Mar 2, 2018

I'll need to add a coding.i file wrapping the Encoder/Decoder.

@jmr
Copy link
Member

jmr commented Mar 15, 2018

Until I get the coding.i file added, try adding this in the %extend S2Polygon block in s2_common.i.

  string Encode() const {
    Encoder encoder;
    $self->Encode(&encoder);
    return string(encoder.base(), encoder.length());
  }

  bool Decode(const string& encoded) {
    Decoder decoder(encoded.data(), encoded.size());
    return $self->Decode(&decoder);
  }

That should get you started, assuming there aren't too many types you need to encode/decode.

@ryanchapman
Copy link
Author

Thanks! I was able to encode and decode polygons successfully. I'll see if I can extend this to other types.

@jmr
Copy link
Member

jmr commented Mar 16, 2018

I added Encoder/Decoder to the SWIG, so it should work for you without additional effort.

Having the string versions would be more convenient, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants