At the outset I would like to say I am not interested in using redis or any other spatial DB. I am trying to do a very simplistic in memory geohash range query and I am using the following software to calculate geohash- geohash-int C package and I have a Cython wrapper to call these APIs in Python 3.6. I am using a SortedList to store the geohashes and my goal is to do a simple geohash range query in memory.
#GeoHash is a Cython wrapper of external C geohash library (link provided)
from geo import GeoHash
from sortedcontainers import SortedList
import numpy as np
import time
minLat = 27.401436
maxLat = 62.54858
minLo = -180.0
maxLo = 179.95000000000002
latGrid = np.arange(minLat,maxLat,0.05)
lonGrid = np.arange(minLo,maxLo,0.05)
geoHash = GeoHash()
print(latGrid.shape,lonGrid.shape)
gridLon,gridLat = np.meshgrid(lonGrid,latGrid)
grid_points = np.c_[gridLon.ravel(),gridLat.ravel()]
sl = SortedList()
geohash1 = {}
t0 = time.time()
for grid_point in grid_points:
lon = grid_point[0]
lat = grid_point[1]
geohash = geoHash.encode(lon,lat,26)
bitsOriginal = geohash["bits"]
sl.add(bitsOriginal)
neighbors = geoHash.get_neighbors(geohash)
for k,v in neighbors.items():
bits1 = v["bits"]
sl.add(bits1)
t1 = time.time()
print(t1-t0)
lonTest = 172.76843
latTest = 61.560745
geohashTest = geoHash.encode(lonTest,latTest,26)
bitsTest = geohashTest["bits"]
What I want to do is the following
it = sl.irange(bitsTest-window,bitsTest+window)
My question is how do I about calculating the window ? I want the window to be within 0.1 degrees or whatever window I specify. I have no idea on how to calculate the window. The whole geohash package is very fast and I am only interested in approximate matches for my query. I believe my test point should be within "range" of the input data set for which I have calculated geohashes but I have no idea on how to obtain the range of the geohashes for my query point. Can somebody help ?
UPDATE The proposed answer is fine but has a complexity of O( N).If there is a complexity of the order of O(log N) that would be acceptable.