# 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
    return DBSession.query(
        func.min(func.ST_XMin(AdminZone.geometry)),
        func.min(func.ST_YMin(AdminZone.geometry)),
        func.max(func.ST_XMax(AdminZone.geometry)),
        func.max(func.ST_YMax(AdminZone.geometry)),
    ).first()

def france_layer(layer_id, query):
    engine = DBSession.get_bind()
    datasource = {
        'type': 'postgis',
        'table': "(%s) as map_table" % query,
        'user': engine.url.username,
        'host': engine.url.host,
        'dbname': engine.url.database,
        'password': engine.url.password,
        'srid': SRID,
    }

    layer = {
        'name':layer_id, #useful ? keep name or id ?
        'id': layer_id,
        'geometry': 'polygon',
        '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)

# 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

    return DBSession.query(
        func.min(func.ST_XMin(AdminZone.geometry)),
        func.min(func.ST_YMin(AdminZone.geometry)),
        func.max(func.ST_XMax(AdminZone.geometry)),
        func.max(func.ST_YMax(AdminZone.geometry)),
    ).first()

def france_layer(layer_id, query):
    engine = DBSession.get_bind()
    datasource = {
        'type': 'postgis',
        'table': "(%s) as map_table" % query,
        'user': engine.url.username,
        'host': engine.url.host,
        'dbname': engine.url.database,
        'password': engine.url.password,
        'srid': SRID,
    }

    layer = {
        'name':layer_id, #useful ? keep name or id ?
        'id': layer_id,
        'geometry': 'polygon',
        '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))