def _indexGeohash(self, redisConn): """ Save the key of the object into the goehash index for this object type """ redisConn.zadd(self.getGeohashIndexKey(), self.getId(), hasher.encode(self.lat, self.lon))
def getByLatLon(cls, lat, lon, redisConn=None): if not redisConn: redisConn = redis.Redis() geoKey = hasher.encode(lat, lon) return cls.getByGeohash(geoKey, redisConn)
def __init__(self, rangeMin, rangeMax, lat, lon, zipcode = ''): self.rangeMin = rangeMin self.rangeMax = rangeMax self.lat = lat self.lon = lon self.zipcode = zipcode #encode a numeric geohash key self.geoKey = hasher.encode(lat, lon) self.key = '%s:%s:%s' % (self.rangeMin, self.rangeMax, self.zipcode)
def save(self, obj, redisConn): p = redisConn.pipeline() for r in self.resolutions: cell = self.getGeocell(obj.lat, obj.lon, self.BIT_RESOLUTIONS[r]) k = self.getKey(r, cell) #print _hash p.zadd(k, **{obj.getId(): hasher.encode(obj.lat, obj.lon)}) p.execute()
def getCity(ip, redisConn): """ Get location object by resolving an IP address @param ip IPv4 address string (e.g. 127.0.0.1) @oaram redisConn redis connection to the database @return a Location object if we can resolve this ip, else None """ range = IPRange.get(ip, redisConn) if not range: return None # load a location by the return City.getByGeohash(hasher.encode(range.lat, range.lon), redisConn)
def getCity(ip, redisConn): """ Get location object by resolving an IP address @param ip IPv4 address string (e.g. 127.0.0.1) @oaram redisConn redis connection to the database @return a Location object if we can resolve this ip, else None """ range = IPRange.get(ip, redisConn) if not range: return None #load a location by the return City.getByGeohash(hasher.encode(range.lat, range.lon), redisConn)
def getByLatLon(cls, lat, lon, redisConn): geoKey = hasher.encode(lat, lon) return cls.getByGeohash(geoKey, redisConn)
def getGeocell(self, lat, lon, bitres): return (hasher.encode(lat, lon) & int('1'*(64 - bitres) + '0'*bitres, 2))
def getGeocell(self, lat, lon, bitres): return (hasher.encode(lat, lon) & int('1' * (64 - bitres) + '0' * bitres, 2))
def get_geo_hash(lat, lon): ''' Returns the geo-hashed value for give latitude and longitude. ''' return hasher.encode(lat, lon)