def getDistance(geoHash1, geoHash2): """ Estimate the distance between 2 geohashes in uint64 format """ # return abs(geoHash1 - geoHash2) try: coords1 = hasher.decode(geoHash1) coords2 = hasher.decode(geoHash2) return math.sqrt(math.pow(coords1[0] - coords2[0], 2) + math.pow(coords1[1] - coords2[1], 2)) except Exception, e: print e return None
def get(ip, redisConn): """ Get a range and all its data by ip """ ipnum = IPRange.ip2long(ip) #get the location record from redis record = redisConn.zrangebyscore(IPRange._indexKey, ipnum ,'+inf', 0, 1, True) if not record: #not found? k! return None #extract location id try: geoKey,rng = record[0][0].split('@') lat,lon = hasher.decode(long(geoKey)) rngMin, rngMax, zipcode = rng.split(':') rngMin = int(rngMin) rngMax = int(rngMax) except IndexError: return None #address not in any range if not rngMin <= ipnum <= rngMax: return None return IPRange(rngMin, rngMax, lat, lon, zipcode)
def getIds(self, redisConn, lat, lon, radius, text=''): if not text: ids = self.geoIndex.getIds(redisConn, lat, lon, radius, False) #nodes = filter(lambda c: c and Location.getLatLonDistance((lat, lon), c[1]) <= radius, ids) return [id[0] for id in ids] else: #store matching elements from text key nameKey = self.textIndex.getIds(redisConn, text, True) geoKeys = self.geoIndex.getIds(redisConn, lat, lon, radius, True) if nameKey and geoKeys: tmpKey = 'tk:%s::%%s' % (hash( '%s' % [lat, lon, radius, text or ''])) with TimeSampler(None, 'Getting shit done'): p = redisConn.pipeline(False) for id, gk in enumerate(geoKeys): p.zinterstore(tmpKey % id, {gk: 1, nameKey: 0}, 'SUM') for id, gk in enumerate(geoKeys): p.zrevrange(tmpKey % id, 0, -1, True) rx = p.execute() with TimeSampler(None, 'Filtering shit out'): ids = filter( lambda x: Location.getLatLonDistance( (lat, lon), x[1]) <= radius, ((x[0], hasher.decode(long(x[1]))) for x in itertools.chain(*(rx[len(geoKeys):])))) return [id[0] for id in ids] else: return []
def getDistance(geoHash1, geoHash2): """ Estimate the distance between 2 geohashes in uint64 format """ # return abs(geoHash1 - geoHash2) try: coords1 = hasher.decode(geoHash1) coords2 = hasher.decode(geoHash2) return Location.getLatLonDistance(coords1, coords2) #return math.sqrt(math.pow(coords1[0] - coords2[0], 2) + #math.pow(coords1[1] - coords2[1], 2)) except Exception, e: print e return None
def getIds(self, redisConn, lat, lon, radius, text = ''): if not text: ids = self.geoIndex.getIds(redisConn, lat, lon, radius, False) #nodes = filter(lambda c: c and Location.getLatLonDistance((lat, lon), c[1]) <= radius, ids) return [id[0] for id in ids] else: #store matching elements from text key nameKey = self.textIndex.getIds(redisConn, text, True) geoKeys = self.geoIndex.getIds(redisConn, lat, lon, radius, True) if nameKey and geoKeys: tmpKey = 'tk:%s::%%s' % (hash('%s' % [lat,lon,radius,text or ''])) with TimeSampler(None, 'Getting shit done'): p = redisConn.pipeline(False) for id, gk in enumerate(geoKeys): p.zinterstore(tmpKey % id, {gk: 1, nameKey: 0}, 'SUM') for id, gk in enumerate(geoKeys): p.zrevrange(tmpKey % id, 0, -1, True) rx = p.execute() with TimeSampler(None, 'Filtering shit out'): ids = filter(lambda x: Location.getLatLonDistance((lat, lon), x[1]) <= radius, ((x[0], hasher.decode(long(x[1]))) for x in itertools.chain(*(rx[len(geoKeys):])))) return [id[0] for id in ids] else: return []
def get_lat_lon_by_geohash(geohash): ''' Returns the tuple with lat and lon elements. ''' return hasher.decode(long(geohash))