5 ms·
i was just starting a project and considering the use of s2. i'm going with geohash instead because I would get different values for the s2 cell based on implem
by durkie 5y ago
i was just starting a project and considering the use of s2. i'm going with geohash instead because I would get different values for the s2 cell based on implementation.
ruby:
S2Cells::S2LatLon.new(34.1234, -84.1234).to_s2_id(30)
=> -8577780315821624425
S2Cells::S2LatLon.new(34.1234, -84.1234).to_s2_id(1)
=> -8358680908399640576
https://gojekfarm.github.io/s2-calc/ https://gojekfarm.github.io/s2-calc/ agrees with these values
python's s2sphere (from sidewalk labs, seems like it'd be more of a reference implementation) gives very different numbers:
s2sphere.Cell.from_lat_lng(s2sphere.LatLng.from_degrees(34.1234, -84.1234))
=> Cell: face 4, level 30, orientation 0, id 9868963757887927191
This discrepancy feels like it'd be some kind of signed/unsigned integer issue, but I didn't think that would be a thing in Ruby. It also doesn't seem like the level=1 cell value in Ruby would be such a large, specific number. I'm missing something.
- random4726427 5y agoS2 cell IDs are usually uint64, so the Ruby version looks wrong. If you cast that int64 to uint64 you get the same value as the python version. Level 1 IDs look like large random numbers in decimal, but when viewed in binary they have many trailing zeros, that are used to truncate to make the tokens.
- durkie 5y agothanks!
- panarky 5y ago"The reference implementation of the S2 library is written in C++. Versions in other languages are ported from the C++ code, and may not have the same robustness, performance, or features as the C++ version." From https://s2geometry.io/about/overview https://s2geometry.io/about/overview
- durkie 5y agoyes, that's the true "reference" implementation, i just figured a repo from sidewalk labs (a google company) would be maybe more authoritative.
- kevinventullo 5y agoFor what it’s worth, at least in the case you described, it is indeed a case of signed vs unsigned representations. That is, the numbers -8577780315821624425 and 9868963757887927191 have the exact same underlying 64-bit representation, which is all that matters for S2 (you can verify this by checking their difference is exactly 2^64). The former is just interpreting the 64-bit string as a signed int, while the latter is unsigned. Separately, the level 1 number you mention -8358680908399640576 is fairly simple if you look at its binary representation; almost all of its trailing digits are 0. Basically, you really shouldn’t think of S2 values as integers, but more as their underlying length 64 bit string.
- durkie 5y agothank you!