'Calculate zoom level and center coordinates in mercator.'

def set_bounds_from_center_and_zoom(self, center_x, center_y, zoom):

    self.center_x = center_x
    self.center_y = center_y
    self.zoom = zoom
    deg_len_equator = (40075160 / 360)
    def get_lon_lat():
        wgs84 = Proj(init='epsg:4326')
        mercator = Proj(init='epsg:3857')
        (lon, lat) = transform(mercator, wgs84, center_x, center_y)
        return (lon, lat)
    def deg_len():
        (lon, lat) = get_lon_lat()
        return (math.cos(lat) * deg_len_equator)
    (lon, lat) = get_lon_lat()
    distance_per_pixel = ((40075160 * math.cos(lat)) / (2 ** (zoom + 8)))
    distance_x_degrees = ((distance_per_pixel * 500) / deg_len())
    distance_y_degrees = ((distance_per_pixel * 350) / deg_len_equator)
    self.bbox_x0 = (lon - distance_x_degrees)
    self.bbox_x1 = (lon + distance_x_degrees)
    self.bbox_y0 = (lat - distance_y_degrees)
    self.bbox_y1 = (lat + distance_y_degrees)
