Beispiel #1
0
def heat(db, z, x, y):
    COUNT = 32

    lon1, lat2 = tiles.tile2lonlat(x, y, z)
    lon2, lat1 = tiles.tile2lonlat(x + 1, y + 1, z)

    params = query._params()
    items = query._build_where_item(params.item, "dynpoi_item")
    params.tilex = x
    params.tiley = y
    params.zoom = z
    params.lat = None
    params.lon = None

    if params.zoom > 18:
        return

    db.execute("""
SELECT
    SUM((SELECT SUM(t) FROM UNNEST(number) t))
FROM
    dynpoi_item
WHERE
""" + items)
    limit = db.fetchone()
    if limit and limit[0]:
        limit = float(limit[0])
    else:
        return HTTPError(404)

    join, where = query._build_param(None,
                                     params.source,
                                     params.item,
                                     params.level,
                                     params.users,
                                     params.classs,
                                     params.country,
                                     params.useDevItem,
                                     params.status,
                                     params.tags,
                                     params.fixable,
                                     tilex=params.tilex,
                                     tiley=params.tiley,
                                     zoom=params.zoom)
    join = join.replace("%", "%%")
    where = where.replace("%", "%%")

    sql = """
SELECT
    COUNT(*),
    ((lon-%(lon1)s) * %(count)s / (%(lon2)s-%(lon1)s) + 0.5)::int AS latn,
    ((lat-%(lat1)s) * %(count)s / (%(lat2)s-%(lat1)s) + 0.5)::int AS lonn,
    mode() WITHIN GROUP (ORDER BY dynpoi_item.marker_color) AS color
FROM
""" + join + """
WHERE
""" + where + """
GROUP BY
    latn,
    lonn
"""
    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)
Beispiel #2
0
def heat(db, z, x, y):
    x2,y1 = num2deg(x,y,z)
    x1,y2 = num2deg(x+1,y+1,z)

    params = query._params()
    params.bbox = [y1, x1, y2, x2]
    items = query._build_where_item(params.item, "dynpoi_item")

    db.execute("""
SELECT
    SUM((SELECT SUM(t) FROM UNNEST(number) t))
FROM
    dynpoi_item
WHERE
""" + items)
    max = db.fetchone()
    if max and max[0]:
        max = float(max[0])
    else:
        response.content_type = 'image/png'
        return static_file("images/tile-empty.png", root='static')

    join, where = query._build_param(None, None, params.bbox, params.source, params.item, params.level, params.users, params.classs, params.country, params.useDevItem, params.status, params.tags, params.fixable)
    join = join.replace("%", "%%")
    where = where.replace("%", "%%")

    COUNT=32

    sql = """
SELECT
    COUNT(*),
    (((lon-%(y1)s))*%(count)s/(%(y2)s-%(y1)s)-0.5)::int AS latn,
    (((lat-%(x1)s))*%(count)s/(%(x2)s-%(x1)s)-0.5)::int AS lonn
FROM
""" + join + """
WHERE
""" + where + """
GROUP BY
    latn,
    lonn
"""
    db.execute(sql, {"x1":x1, "y1":y1, "x2":x2, "y2":y2, "count":COUNT})
    im = Image.new("RGB", (256,256), "#ff0000")
    draw = ImageDraw.Draw(im)

    transparent_area = (0,0,256,256)
    mask = Image.new('L', im.size, color=255)
    mask_draw = ImageDraw.Draw(mask)
    mask_draw.rectangle(transparent_area, fill=0)

    for row in db.fetchall():
        count, x, y = row
        count = int(math.log(count) / math.log(max / ((z-4+1+math.sqrt(COUNT))**2)) * 255)
        count = 255 if count > 255 else count
        r = [(x*256/COUNT,(COUNT-1-y)*256/COUNT), ((x+1)*256/COUNT-1,((COUNT-1-y+1)*256/COUNT-1))]
        mask_draw.rectangle(r, fill=count)

    im.putalpha(mask)
    del draw

    buf = StringIO.StringIO()
    im.save(buf, 'PNG')
    response.content_type = 'image/png'
    return buf.getvalue()
Beispiel #3
0
def heat(db, z, x, y):
    COUNT=32

    x2,y1 = num2deg(x,y,z)
    x1,y2 = num2deg(x+1,y+1,z)

    params = query._params()
    params.bbox = [y1, x1, y2, x2]
    items = query._build_where_item(params.item, "dynpoi_item")

    db.execute("""
SELECT
    SUM((SELECT SUM(t) FROM UNNEST(number) t))
FROM
    dynpoi_item
WHERE
""" + items)
    max = db.fetchone()
    if max and max[0]:
        max = float(max[0])
    else:
        response.content_type = 'image/png'
        return static_file("images/tile-empty.png", root='static')

    join, where = query._build_param(None, None, params.bbox, params.source, params.item, params.level, params.users, params.classs, params.country, params.useDevItem, params.status, params.tags, params.fixable)
    join = join.replace("%", "%%")
    where = where.replace("%", "%%")

    sql = """
SELECT
    COUNT(*),
    (((lon-%(y1)s))*%(count)s/(%(y2)s-%(y1)s)-0.5)::int AS latn,
    (((lat-%(x1)s))*%(count)s/(%(x2)s-%(x1)s)-0.5)::int AS lonn,
    mode() WITHIN GROUP (ORDER BY dynpoi_item.marker_color) AS color
FROM
""" + join + """
WHERE
""" + where + """
GROUP BY
    latn,
    lonn
"""
    db.execute(sql, {"x1":x1, "y1":y1, "x2":x2, "y2":y2, "count":COUNT})
    im = Image.new("RGB", (256,256), "#ff0000")
    draw = ImageDraw.Draw(im)

    transparent_area = (0,0,256,256)
    mask = Image.new('L', im.size, color=255)
    mask_draw = ImageDraw.Draw(mask)
    mask_draw.rectangle(transparent_area, fill=0)

    for row in db.fetchall():
        count, x, y, color = row
        count = int(math.log(count) / math.log(max / ((z-4+1+math.sqrt(COUNT))**2)) * 255)
        count = 255 if count > 255 else count
        count = count
        r = [(x*256/COUNT,(COUNT-1-y)*256/COUNT), ((x+1)*256/COUNT-1,((COUNT-1-y+1)*256/COUNT-1))]
        mask_draw.rectangle(r, fill=count)
        draw.rectangle(r, fill=color)

    im.putalpha(mask)
    del draw

    buf = StringIO.StringIO()
    im.save(buf, 'PNG')
    response.content_type = 'image/png'
    return buf.getvalue()
Beispiel #4
0
def heat(db, z, x, y):
    COUNT=32

    x2,y1 = num2deg(x,y,z)
    x1,y2 = num2deg(x+1,y+1,z)

    params = query._params()
    params.bbox = [y1, x1, y2, x2]
    items = query._build_where_item(params.item, "dynpoi_item")

    db.execute("""
SELECT
    SUM((SELECT SUM(t) FROM UNNEST(number) t))
FROM
    dynpoi_item
WHERE
""" + items)
    limit = db.fetchone()
    if limit and limit[0]:
        limit = float(limit[0])
    else:
        global MVT_EMPTY
        if not MVT_EMPTY:
            MVT_EMPTY = mapbox_vector_tile.encode([])
        response.content_type = 'application/vnd.mapbox-vector-tile'
        return MVT_EMPTY

    join, where = query._build_param(params.bbox, params.source, params.item, params.level, params.users, params.classs, params.country, params.useDevItem, params.status, params.tags, params.fixable)
    join = join.replace("%", "%%")
    where = where.replace("%", "%%")

    sql = """
SELECT
    COUNT(*),
    ((lon-%(y1)s) * %(count)s / (%(y2)s-%(y1)s) + 0.5)::int AS latn,
    ((lat-%(x1)s) * %(count)s / (%(x2)s-%(x1)s) + 0.5)::int AS lonn,
    mode() WITHIN GROUP (ORDER BY dynpoi_item.marker_color) AS color
FROM
""" + join + """
WHERE
""" + where + """
GROUP BY
    latn,
    lonn
"""
    db.execute(sql, {"x1":x1, "y1":y1, "x2":x2, "y2":y2, "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)
Beispiel #5
0
def heat(db, z, x, y):
    COUNT=32

    lon1,lat2 = tiles.tile2lonlat(x,y,z)
    lon2,lat1 = tiles.tile2lonlat(x+1,y+1,z)

    params = query._params()
    items = query._build_where_item(params.item, "dynpoi_item")
    params.tilex = x
    params.tiley = y
    params.zoom = z
    params.lat = None
    params.lon = None

    if params.zoom > 18:
        return

    db.execute("""
SELECT
    SUM((SELECT SUM(t) FROM UNNEST(number) t))
FROM
    dynpoi_item
WHERE
""" + items)
    limit = db.fetchone()
    if limit and limit[0]:
        limit = float(limit[0])
    else:
        return HTTPError(404)

    join, where = query._build_param(None, params.source, params.item, params.level, params.users, params.classs, params.country, params.useDevItem, params.status, params.tags, params.fixable, tilex=params.tilex, tiley=params.tiley, zoom=params.zoom)
    join = join.replace("%", "%%")
    where = where.replace("%", "%%")

    sql = """
SELECT
    COUNT(*),
    ((lon-%(lon1)s) * %(count)s / (%(lon2)s-%(lon1)s) + 0.5)::int AS latn,
    ((lat-%(lat1)s) * %(count)s / (%(lat2)s-%(lat1)s) + 0.5)::int AS lonn,
    mode() WITHIN GROUP (ORDER BY dynpoi_item.marker_color) AS color
FROM
""" + join + """
WHERE
""" + where + """
GROUP BY
    latn,
    lonn
"""
    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)