# Path: server/localfinance/models.py
# SRID = 4326
# ADMIN_LEVEL_CITY = 5
# ADMIN_LEVEL_CITY_ARR = 6
# class AdminZone(Base):
# class AdminZoneFinance(Base):
# class Stats(Base):
#     def code_insee(self):
# 
# Path: server/localfinance/cache.py



import re
import json
import os
import numpy as np
import brewer2mpl
from sqlalchemy import func, cast, Float, select, alias, and_
from sqlalchemy.sql import compiler
from psycopg2.extensions import adapt as sqlescape
from .models import DBSession, AdminZone, AdminZoneFinance, Stats, SRID, ADMIN_LEVEL_CITY
from .cache import region
        'srs': '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs',
        'Datasource': datasource,
    }
    return layer

def map_query(year, variable, variable_filter):
    q = DBSession.query(AdminZone.id, AdminZone.geometry, AdminZone.name, AdminZone.code_insee.label('code_insee'), AdminZoneFinance.year, variable)
    return compile_query(q.filter(AdminZoneFinance.year==year).filter(AdminZone.admin_level==ADMIN_LEVEL_CITY).filter(variable_filter).join(AdminZoneFinance, AdminZone.id==AdminZoneFinance.adminzone_id))

def scale_mss(layer, var_name, x, colors):
    styles = []
    def price_style(x_min, x_max, color):
        template = "[%(var_name)s>%(min)s][%(var_name)s<=%(max)s] { polygon-fill: %(color)s; line-color: %(color)s; }"
        return template % { 'var_name': var_name,
                            'min': x_min,
                            'max': x_max,
                            'color': color }
    for x_min, x_max, color in zip(x[:-1], x[1:], colors):
        styles.append(price_style(x_min, x_max, color) )
    return "#%s::variable{\n%s\n}"%(layer, '\n'.join(styles))

class Map(object):
    def __init__(self, year, name):
        variables = MAPS_CONFIG.get(name)
        query = map_query(year, variables['sql_variable'].label(name), variables['sql_filter'])
        layer = "layer_%s"%name

        # style
        # build scale based on quantiles
        size = 9

# Path: server/localfinance/models.py
# SRID = 4326
# ADMIN_LEVEL_CITY = 5
# ADMIN_LEVEL_CITY_ARR = 6
# class AdminZone(Base):
# class AdminZoneFinance(Base):
# class Stats(Base):
#     def code_insee(self):
# 
# Path: server/localfinance/cache.py



# Path: server/localfinance/maps.py
import re
import json
import os
import numpy as np
import brewer2mpl
from sqlalchemy import func, cast, Float, select, alias, and_
from sqlalchemy.sql import compiler
from psycopg2.extensions import adapt as sqlescape
from .models import DBSession, AdminZone, AdminZoneFinance, Stats, SRID, ADMIN_LEVEL_CITY
from .cache import region

        'srs': '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs',
        'Datasource': datasource,
    }
    return layer

def map_query(year, variable, variable_filter):
    q = DBSession.query(AdminZone.id, AdminZone.geometry, AdminZone.name, AdminZone.code_insee.label('code_insee'), AdminZoneFinance.year, variable)
    return compile_query(q.filter(AdminZoneFinance.year==year).filter(AdminZone.admin_level==ADMIN_LEVEL_CITY).filter(variable_filter).join(AdminZoneFinance, AdminZone.id==AdminZoneFinance.adminzone_id))

def scale_mss(layer, var_name, x, colors):
    styles = []
    def price_style(x_min, x_max, color):
        template = "[%(var_name)s>%(min)s][%(var_name)s<=%(max)s] { polygon-fill: %(color)s; line-color: %(color)s; }"
        return template % { 'var_name': var_name,
                            'min': x_min,
                            'max': x_max,
                            'color': color }
    for x_min, x_max, color in zip(x[:-1], x[1:], colors):
        styles.append(price_style(x_min, x_max, color) )
    return "#%s::variable{\n%s\n}"%(layer, '\n'.join(styles))

class Map(object):
    def __init__(self, year, name):
        variables = MAPS_CONFIG.get(name)
        query = map_query(year, variables['sql_variable'].label(name), variables['sql_filter'])
        layer = "layer_%s"%name

        # style
        # build scale based on quantiles
        size = 9

        stats = DBSession.query(Stats).filter(Stats.name==name).first()