# Path: modules/params.py
# class Params:
#     def __init__(self, default_limit = 100, max_limit = 500):
#         self.bbox       = request.query.getunicode('bbox', default=None)
#         self.item       = request.query.getunicode('item')
#         self.source     = request.query.getunicode('source', default='')
#         self.classs     = request.query.getunicode('class', default='')
#         self.users      = request.query.getunicode('username', default='')
#         self.level      = request.query.getunicode('level', default='1,2,3')
#         self.full       = request.query.getunicode('full', default=False)
#         self.zoom       = request.query.get('zoom', type=int, default=10)
#         self.limit      = request.query.get('limit', type=int, default=default_limit)
#         self.country    = request.query.getunicode('country', default=None)
#         self.useDevItem = request.query.getunicode('useDevItem', default=False)
#         self.status     = request.query.getunicode('status', default="open")
#         self.start_date = request.query.getunicode('start_date', default=None)
#         self.end_date   = request.query.getunicode('end_date', default=None)
#         self.tags       = request.query.getunicode('tags', default=None)
#         self.fixable    = request.query.getunicode('fixable', default=None)
#         self.osm_type   = request.query.getunicode('osm_type', default=None)
#         self.osm_id     = request.query.get('osm_id', type=int, default=None)
#         self.tilex      = request.query.getunicode('tilex', default=None)
#         self.tiley      = request.query.getunicode('tiley', default=None)
# 
#         if self.level:
#             self.level = self.level.split(",")
#             try:
#                 self.level = ",".join([str(int(x)) for x in self.level if x])
#             except:
#                 self.level = "1,2,3"
#         if self.bbox:
#             try:
#                 self.bbox = list(map(lambda x: float(x), self.bbox.split(',')))
#             except:
#                 self.bbox = None
#         if self.users:
#             self.users = self.users.split(",")
#         if self.limit is not None and self.limit > max_limit:
#             self.limit = max_limit
#         if self.country and not re.match(r"^([a-z_]+)(\*|)$", self.country):
#             self.country = ''
#         if self.useDevItem == "true":
#             self.useDevItem = True
#         elif self.useDevItem == "all":
#             pass
#         else:
#             self.useDevItem = False
#         if self.start_date:
#             self.start_date = utils.str_to_datetime(self.start_date)
#         if self.end_date:
#             self.end_date = utils.str_to_datetime(self.end_date)
#         if self.tags:
#             self.tags = self.tags.split(",")
# 
#         if self.osm_type and self.osm_type not in ['node', 'way', 'relation']:
#             self.osm_type = None
#         if self.osm_id and not self.osm_type:
#             self.osm_id = None


from bottle import default_app, route, response, HTTPError, HTTPResponse
from modules.params import Params
from modules import query, tiles
from shapely.geometry import Point, Polygon
import math
import mapbox_vector_tile
    db.execute(sql, {"lon1":lon1, "lat1":lat1, "lon2":lon2, "lat2":lat2, "count":COUNT})

    features = []
    for row in db.fetchall():
        count, x, y, color = row
        count = max(
          int(math.log(count) / math.log(limit / ((z-4+1+math.sqrt(COUNT))**2)) * 255),
          1 if count > 0 else 0
        )
        if count > 0:
          count = 255 if count > 255 else count
          features.append({
            "geometry": Polygon([(x, y), (x - 1, y), (x - 1, y - 1), (x, y - 1)]),
            "properties": {
                "color": int(color[1:], 16),
                "count": count}
          })

    response.content_type = 'application/vnd.mapbox-vector-tile'
    return mapbox_vector_tile.encode([{
        "name": "issues",
        "features": features
    }], extents=COUNT)


@route('/issues/<z:int>/<x:int>/<y:int>.<format:ext>')
def issues_mvt(db, z, x, y, format):
    lon1,lat2 = tiles.tile2lonlat(x,y,z)
    lon2,lat1 = tiles.tile2lonlat(x+1,y+1,z)


# Path: modules/params.py
# class Params:
#     def __init__(self, default_limit = 100, max_limit = 500):
#         self.bbox       = request.query.getunicode('bbox', default=None)
#         self.item       = request.query.getunicode('item')
#         self.source     = request.query.getunicode('source', default='')
#         self.classs     = request.query.getunicode('class', default='')
#         self.users      = request.query.getunicode('username', default='')
#         self.level      = request.query.getunicode('level', default='1,2,3')
#         self.full       = request.query.getunicode('full', default=False)
#         self.zoom       = request.query.get('zoom', type=int, default=10)
#         self.limit      = request.query.get('limit', type=int, default=default_limit)
#         self.country    = request.query.getunicode('country', default=None)
#         self.useDevItem = request.query.getunicode('useDevItem', default=False)
#         self.status     = request.query.getunicode('status', default="open")
#         self.start_date = request.query.getunicode('start_date', default=None)
#         self.end_date   = request.query.getunicode('end_date', default=None)
#         self.tags       = request.query.getunicode('tags', default=None)
#         self.fixable    = request.query.getunicode('fixable', default=None)
#         self.osm_type   = request.query.getunicode('osm_type', default=None)
#         self.osm_id     = request.query.get('osm_id', type=int, default=None)
#         self.tilex      = request.query.getunicode('tilex', default=None)
#         self.tiley      = request.query.getunicode('tiley', default=None)
# 
#         if self.level:
#             self.level = self.level.split(",")
#             try:
#                 self.level = ",".join([str(int(x)) for x in self.level if x])
#             except:
#                 self.level = "1,2,3"
#         if self.bbox:
#             try:
#                 self.bbox = list(map(lambda x: float(x), self.bbox.split(',')))
#             except:
#                 self.bbox = None
#         if self.users:
#             self.users = self.users.split(",")
#         if self.limit is not None and self.limit > max_limit:
#             self.limit = max_limit
#         if self.country and not re.match(r"^([a-z_]+)(\*|)$", self.country):
#             self.country = ''
#         if self.useDevItem == "true":
#             self.useDevItem = True
#         elif self.useDevItem == "all":
#             pass
#         else:
#             self.useDevItem = False
#         if self.start_date:
#             self.start_date = utils.str_to_datetime(self.start_date)
#         if self.end_date:
#             self.end_date = utils.str_to_datetime(self.end_date)
#         if self.tags:
#             self.tags = self.tags.split(",")
# 
#         if self.osm_type and self.osm_type not in ['node', 'way', 'relation']:
#             self.osm_type = None
#         if self.osm_id and not self.osm_type:
#             self.osm_id = None


# Path: api/issues_tiles.py
from bottle import default_app, route, response, HTTPError, HTTPResponse
from modules.params import Params
from modules import query, tiles
from shapely.geometry import Point, Polygon
import math
import mapbox_vector_tile

    db.execute(sql, {"lon1":lon1, "lat1":lat1, "lon2":lon2, "lat2":lat2, "count":COUNT})

    features = []
    for row in db.fetchall():
        count, x, y, color = row
        count = max(
          int(math.log(count) / math.log(limit / ((z-4+1+math.sqrt(COUNT))**2)) * 255),
          1 if count > 0 else 0
        )
        if count > 0:
          count = 255 if count > 255 else count
          features.append({
            "geometry": Polygon([(x, y), (x - 1, y), (x - 1, y - 1), (x, y - 1)]),
            "properties": {
                "color": int(color[1:], 16),
                "count": count}
          })

    response.content_type = 'application/vnd.mapbox-vector-tile'
    return mapbox_vector_tile.encode([{
        "name": "issues",
        "features": features
    }], extents=COUNT)


@route('/issues/<z:int>/<x:int>/<y:int>.<format:ext>')
def issues_mvt(db, z, x, y, format):
    lon1,lat2 = tiles.tile2lonlat(x,y,z)
    lon2,lat1 = tiles.tile2lonlat(x+1,y+1,z)


    params = Params(max_limit=50 if z > 18 else 10000)