benbovy / spherely

Manipulation and analysis of geometric objects on the sphere.
https://spherely.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
119 stars 8 forks source link

Support pickling Geography objects #54

Open jorisvandenbossche opened 1 month ago

jorisvandenbossche commented 1 month ago

At the moment we didn't implement this, so you will get TypeError: cannot pickle 'spherely.Point' object, but it would be useful to support it.

To provide a full exact roundtrip through pickling, we should maybe pickle the raw unit vector (x, y, z) of the S2Point, instead of going through latitude/longitude like other conversions (eg WKB), because there can always be some floating point difference in this conversion.

benbovy commented 1 month ago

S2geometry provides Encoder and Decoder objects as well as Encode() and Decode() methods from most of its data types (point, polyline, polygon) so we might be able to just reuse that. For a point it serializes the raw unit vector. It is also possible to encode S2ShapeIndex objects with lazy decoding.

Probably cleanest would be to expose this API via s2geography::Geography wrapper classes rather than dealing with it directly in spherely (same for other methods like Clone(), which would be better than this function implemented in #51).

benbovy commented 1 month ago

Actually, I think we could already access Encode() and Decode() via s2geography::Geography::Shape().

benbovy commented 1 week ago

https://github.com/paleolimbot/s2geography/pull/40 is now merged so adding support for pickle should be easier.

That s2geography PR also added the GeographyKind enum tag, which will allow us to clean up some parts of the code in Spherely (e.g., remove some dynamic_cast).