# 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
from fiona import collection
from shapely.geometry import shape, MultiPolygon
from shapely.ops import cascaded_union
from shapely.wkt import loads
from sqlalchemy import engine_from_config, func
from pyramid.paster import (
    get_appsettings,
    setup_logging,
    )
from ..models import (
    DBSession,
    AdminZone,
    Base,
    ADMIN_LEVEL_CITY,
    ADMIN_LEVEL_CITY_ARR,
    SRID,
    )
    Base.metadata.create_all(engine)
    with transaction.manager:
        # script
        with collection(filepath) as cities:
            for city in cities:
                data = extract_adminzone_data(city)
                az = AdminZone(**data)
                DBSession.add(az)

    cities_with_arr = [
        {'name': 'PARIS',
         'code_city': '056',
         'code_department': '75'},
        {'name': 'MARSEILLE',
         'code_city': '055',
         'code_department': '13'},
        {'name': 'LYON',
         'code_city': '123',
         'code_department': '69'}
    ]
    with transaction.manager:
        for city in cities_with_arr:
            wkt_geoms, populations = zip(*DBSession.query(func.ST_AsText(AdminZone.geometry), AdminZone.population)\
                .filter(AdminZone.name.contains(city['name']))\
                .filter(AdminZone.admin_level==ADMIN_LEVEL_CITY_ARR)\
                .all())
            city_geom = cascaded_union([loads(wkt_geom) for wkt_geom in wkt_geoms])
            if city_geom.type == 'Polygon':
                city_geom = MultiPolygon([city_geom])
            az = AdminZone(

# 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/filladminzone.py
import os
import sys
import transaction
from fiona import collection
from shapely.geometry import shape, MultiPolygon
from shapely.ops import cascaded_union
from shapely.wkt import loads
from sqlalchemy import engine_from_config, func
from pyramid.paster import (
    get_appsettings,
    setup_logging,
    )
from ..models import (
    DBSession,
    AdminZone,
    Base,
    ADMIN_LEVEL_CITY,
    ADMIN_LEVEL_CITY_ARR,
    SRID,
    )

    Base.metadata.create_all(engine)
    with transaction.manager:
        # script
        with collection(filepath) as cities:
            for city in cities:
                data = extract_adminzone_data(city)
                az = AdminZone(**data)
                DBSession.add(az)

    cities_with_arr = [
        {'name': 'PARIS',
         'code_city': '056',
         'code_department': '75'},
        {'name': 'MARSEILLE',
         'code_city': '055',
         'code_department': '13'},
        {'name': 'LYON',
         'code_city': '123',
         'code_department': '69'}
    ]
    with transaction.manager:
        for city in cities_with_arr:
            wkt_geoms, populations = zip(*DBSession.query(func.ST_AsText(AdminZone.geometry), AdminZone.population)\
                .filter(AdminZone.name.contains(city['name']))\
                .filter(AdminZone.admin_level==ADMIN_LEVEL_CITY_ARR)\
                .all())
            city_geom = cascaded_union([loads(wkt_geom) for wkt_geom in wkt_geoms])
            if city_geom.type == 'Polygon':
                city_geom = MultiPolygon([city_geom])
            az = AdminZone(

                geometry="SRID=%s;"%SRID + city_geom.wkt, admin_level=ADMIN_LEVEL_CITY,