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

When encoding geohashes, there may be an unexpected in the handling of zero points #16

Open
osdakira opened this issue Mar 24, 2023 · 0 comments

Comments

@osdakira
Copy link
Contributor

osdakira commented Mar 24, 2023

Thanks for the excellent library, I use this library when I use python.
However, I noticed that this library and the java library have different geohash results for 0.0 points.

I looked for the specs but couldn't find them, so this is an experimental result, but it seems that only this library is different.

  • this library => 7zupb
>>> import pygeohash as pgh
WARNING:root:Numpy and Numba are soft dependencies to use the numba geohashing functions.
Can only import/use native python functions.
>>> pgh.encode(0.0, -5.6, precision=5)
'7zupb'
  • MySQL => ebh00
mysql> SELECT ST_GeoHash(-5.6,0.0,5);
+------------------------+
| ST_GeoHash(-5.6,0.0,5) |
+------------------------+
| ebh00                  |
+------------------------+
1 row in set (0.00 sec)
  • geohash.org => ebh00

image

Perhaps the cause is that when comparing to mid, it is set to "greater than", where it should be "greater than or equal to"?

@@ -93,14 +93,14 @@ def encode(latitude, longitude, precision=12):
     while len(geohash) < precision:
         if even:
             mid = (lon_interval[0] + lon_interval[1]) / 2
-            if longitude > mid:
+            if longitude >= mid:
                 ch |= bits[bit]
                 lon_interval = (mid, lon_interval[1])
             else:
                 lon_interval = (lon_interval[0], mid)
         else:
             mid = (lat_interval[0] + lat_interval[1]) / 2
-            if latitude > mid:
+            if latitude >= mid:
                 ch |= bits[bit]
                 lat_interval = (mid, lat_interval[1])
             else:

I would like to ask you to fix this if possible, but I think it is risky because of the lack of backward compatibility.
I have created two pull requests, one with the corrected code and one with another function added.

Comparison with mid "greater than equal", not "greater than" by osdakira · Pull Request #18 · wdm0006/pygeohash
Add an encode_strictly function that fixes the handling of the 0 geographical location by osdakira · Pull Request #17 · wdm0006/pygeohash

@osdakira osdakira changed the title When encoding geohashes, there may be an error in the handling of zero points When encoding geohashes, there may be an unexpected in the handling of zero points Mar 24, 2023
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

1 participant