pygeohash icon indicating copy to clipboard operation
pygeohash copied to clipboard

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

Open osdakira opened this issue 1 year ago • 0 comments

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 avatar Mar 24 '23 04:03 osdakira