# 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):


import os
import sys
import transaction
import json
import pandas as pd
from fiona import collection
from shapely.geometry import shape, MultiPolygon
from sqlalchemy import engine_from_config
from pyramid.paster import (
    get_appsettings,
    setup_logging,
    )
from pyramid.scripts.common import parse_vars
from ..models import (
    DBSession,
    AdminZone,
    AdminZoneFinance,
    Base,
    )




def usage(argv):
    cmd = os.path.basename(argv[0])
    print('usage: %s <config_uri> <filepath>\n'
          '(example: "%s development.ini")' % (cmd, cmd))
    sys.exit(1)

def extract_adminzone_data(city):
    properties = city['properties']
    g = shape(city['geometry'])
    if g.type == 'Polygon':
        g = MultiPolygon([g])
    return {'name': properties['NOM_COMM'],
            'code_department': properties['CODE_DEPT'],
            'code_city': properties['CODE_COMM'],
            'admin_level': 5,
            'geometry': "SRID=4326;" + g.wkt}

def main(argv=sys.argv):
    if len(argv) < 3:
        usage(argv)
    config_uri = argv[1]
    filename = argv[2]
    setup_logging(config_uri)
    settings = get_appsettings(config_uri)
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)

# 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/scripts/filladminzonefinance.py
import os
import sys
import transaction
import json
import pandas as pd
from fiona import collection
from shapely.geometry import shape, MultiPolygon
from sqlalchemy import engine_from_config
from pyramid.paster import (
    get_appsettings,
    setup_logging,
    )
from pyramid.scripts.common import parse_vars
from ..models import (
    DBSession,
    AdminZone,
    AdminZoneFinance,
    Base,
    )





def usage(argv):
    cmd = os.path.basename(argv[0])
    print('usage: %s <config_uri> <filepath>\n'
          '(example: "%s development.ini")' % (cmd, cmd))
    sys.exit(1)

def extract_adminzone_data(city):
    properties = city['properties']
    g = shape(city['geometry'])
    if g.type == 'Polygon':
        g = MultiPolygon([g])
    return {'name': properties['NOM_COMM'],
            'code_department': properties['CODE_DEPT'],
            'code_city': properties['CODE_COMM'],
            'admin_level': 5,
            'geometry': "SRID=4326;" + g.wkt}

def main(argv=sys.argv):
    if len(argv) < 3:
        usage(argv)
    config_uri = argv[1]
    filename = argv[2]
    setup_logging(config_uri)
    settings = get_appsettings(config_uri)
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)

    code_az_id = dict(DBSession.query(AdminZone.code_insee, AdminZone.id).all())