Exemplo n.º 1
0
def encode(latitude, longitude, precision=12):
    if latitude >= 90.0 or latitude < -90.0:
        raise Exception("invalid latitude.")
    while longitude < -180.0:
        longitude += 360.0
    while longitude >= 180.0:
        longitude -= 360.0

    if _geohash:
        basecode = _geohash.encode(latitude, longitude)
        if len(basecode) > precision:
            return basecode[0:precision]
        return basecode + '0' * (precision - len(basecode))

    lat = latitude / 180.0
    lon = longitude / 360.0

    xprecision = precision + 1
    lat_length = lon_length = xprecision * 5 / 2
    if xprecision % 2 == 1:
        lon_length += 1

    if lat > 0:
        lat = int((1 << lat_length) * lat) + (1 << (lat_length - 1))
    else:
        lat = (1 << lat_length - 1) - int((1 << lat_length) * (-lat))

    if lon > 0:
        lon = int((1 << lon_length) * lon) + (1 << (lon_length - 1))
    else:
        lon = (1 << lon_length - 1) - int((1 << lon_length) * (-lon))

    return _encode_i2c(lat, lon, lat_length, lon_length)[:precision]
Exemplo n.º 2
0
def encode(latitude, longitude, precision=12):
	if latitude >= 90.0 or latitude < -90.0:
		raise Exception("invalid latitude.")
	while longitude < -180.0:
		longitude += 360.0
	while longitude >= 180.0:
		longitude -= 360.0
	
	if _geohash:
		basecode=_geohash.encode(latitude,longitude)
		if len(basecode)>precision:
			return basecode[0:precision]
		return basecode+'0'*(precision-len(basecode))
	
	lat = latitude/180.0
	lon = longitude/360.0
	
	xprecision=precision+1
	lat_length = lon_length = int(xprecision*5/2)
	if xprecision%2==1:
		lon_length+=1
	
	if lat>0:
		lat = int((1<<lat_length)*lat)+(1<<(lat_length-1))
	else:
		lat = (1<<lat_length-1)-int((1<<lat_length)*(-lat))
	
	if lon>0:
		lon = int((1<<lon_length)*lon)+(1<<(lon_length-1))
	else:
		lon = (1<<lon_length-1)-int((1<<lon_length)*(-lon))
	
	return _encode_i2c(lat,lon,lat_length,lon_length)[:precision]
Exemplo n.º 3
0
def encode(latitude, longitude, precision=12):
    if latitude > 90.0 or latitude < -90.0:
        raise Exception("invalid latitude.")
    while longitude < -180.0:
        longitude += 360.0
    while longitude >= 180.0:
        longitude -= 360.0

    if _geohash:
        basecode = _geohash.encode(latitude, longitude)
        if len(basecode) > precision:
            return basecode[0:precision]
        return basecode + "0" * (precision - len(basecode))

    lat = (latitude + 90.0) / 180.0
    lon = (longitude + 180.0) / 360.0

    lat_length = lon_length = precision * 5 / 2
    if precision % 2 == 1:
        lon_length += 1

    lat = int((1 << lat_length) * lat)
    lon = int((1 << lon_length) * lon)

    return _encode_i2c(lat, lon, lat_length, lon_length)
Exemplo n.º 4
0
 def _get_cache_key(self, **kwargs):
     """
         Generates a valid cache key with a geohash.
         the geohash square is approximately 20 m
     """
     key = 'cartodb_%s_' % _geohash.encode(
         kwargs.pop('lat'), kwargs.pop('lon'))[:8]
     key += '_'.join([
         '%s=%s' % (k, kwargs[k]) for k in sorted(kwargs.iterkeys())])
     return key
Exemplo n.º 5
0
 def _get_cache_key(self, **kwargs):
     """
         Generates a valid cache key with a geohash.
         the geohash square is approximately 20 m
     """
     key = 'cartodb_%s_' % _geohash.encode(kwargs.pop('lat'),
                                           kwargs.pop('lon'))[:8]
     key += '_'.join(
         ['%s=%s' % (k, kwargs[k]) for k in sorted(kwargs.iterkeys())])
     return key
Exemplo n.º 6
0
def encode(latitude, longitude, precision=12):
	# Check Latitude
	if latitude > 90.0 or latitude < -90.0:
		raise Exception("invalid latitude.")

	# Wrap Longitude
	while longitude < -180.0:
		longitude += 360.0
	while longitude >= 180.0:
		longitude -= 360.0
	
	if _geohash:
		basecode=_geohash.encode(latitude,longitude)
		if len(basecode)>precision:
			return basecode[0:precision]
		return basecode+'0'*(precision-len(basecode))
	
	xprecision=precision+1
	lat_length = lon_length = int(xprecision*5/2)
	if xprecision%2==1:
		lon_length+=1
	
	if hasattr(float, "fromhex"):
		a = _float_hex_to_int(latitude/90.0)
		o = _float_hex_to_int(longitude/180.0)
		if a[1] > lat_length:
			ai = a[0]>>(a[1]-lat_length)
		else:
			ai = a[0]<<(lat_length-a[1])
		
		if o[1] > lon_length:
			oi = o[0]>>(o[1]-lon_length)
		else:
			oi = o[0]<<(lon_length-o[1])
		
		return _encode_i2c(ai, oi, lat_length, lon_length)[:precision]
	
	lat = latitude/180.0
	lon = longitude/360.0
	
	if lat>0:
		lat = int((1<<lat_length)*lat)+(1<<(lat_length-1))
	else:
		lat = (1<<lat_length-1)-int((1<<lat_length)*(-lat))
	
	if lon>0:
		lon = int((1<<lon_length)*lon)+(1<<(lon_length-1))
	else:
		lon = (1<<lon_length-1)-int((1<<lon_length)*(-lon))
	
	return _encode_i2c(lat,lon,lat_length,lon_length)[:precision]
Exemplo n.º 7
0
def encode(latitude, longitude, precision=12):
    if latitude >= 90.0 or latitude < -90.0:
        raise Exception("invalid latitude.")
    while longitude < -180.0:
        longitude += 360.0
    while longitude >= 180.0:
        longitude -= 360.0

    if _geohash:
        basecode = _geohash.encode(latitude, longitude)
        if len(basecode) > precision:
            return basecode[0:precision]
        return basecode + '0' * (precision - len(basecode))

    xprecision = precision + 1
    lat_length = lon_length = int(xprecision * 5 / 2)
    if xprecision % 2 == 1:
        lon_length += 1

    if hasattr(float, "fromhex"):
        a = _float_hex_to_int(latitude / 90.0)
        o = _float_hex_to_int(longitude / 180.0)
        if a[1] > lat_length:
            ai = a[0] >> (a[1] - lat_length)
        else:
            ai = a[0] << (lat_length - a[1])

        if o[1] > lon_length:
            oi = o[0] >> (o[1] - lon_length)
        else:
            oi = o[0] << (lon_length - o[1])

        return _encode_i2c(ai, oi, lat_length, lon_length)[:precision]

    lat = latitude / 180.0
    lon = longitude / 360.0

    if lat > 0:
        lat = int((1 << lat_length) * lat) + (1 << (lat_length - 1))
    else:
        lat = (1 << lat_length - 1) - int((1 << lat_length) * (-lat))

    if lon > 0:
        lon = int((1 << lon_length) * lon) + (1 << (lon_length - 1))
    else:
        lon = (1 << lon_length - 1) - int((1 << lon_length) * (-lon))

    return _encode_i2c(lat, lon, lat_length, lon_length)[:precision]
Exemplo n.º 8
0
def encode(*args):
  return _geohash.encode(*args)