def loader(request): """Recreate sample schema from shapefiles and tear down when done.""" # Configure TableLoader to use directory containing sample shapefiles. root_path = os.path.dirname(__file__) data_path = os.path.join(root_path, '../../test_data') loader = TableLoader(directory=data_path) # Recreate PostgreSQL sample schema. with loader.database.cursor() as cur: cur.execute(""" CREATE EXTENSION IF NOT EXISTS postgis; DROP SCHEMA IF EXISTS sample CASCADE; CREATE SCHEMA sample; """) loader.database.refresh() # Load all shapefiles in test data directory. for filename in os.listdir(data_path): file_root, file_ext = os.path.splitext(filename) if file_ext.lower() == '.shp': shp_path = os.path.join(data_path, filename) table_name = 'sample.' + file_root loader.load_shp(shp_path, table_name) # Reproject all non-conforming SRIDs into project SRID. conform_srids(loader.srid, schema=loader.tables.sample) # Tear down sample schema when done. def teardown(): with loader.database.cursor() as cur: cur.execute("DROP SCHEMA sample CASCADE;") request.addfinalizer(teardown) return loader
import os import string import subprocess import numpy as np import pandas as pd from spandex import TableLoader, TableFrame from spandex.io import df_to_db import urbansim.sim.simulation as sim import utils loader = TableLoader() staging = loader.tables.staging ## Assumptions. # Use codes were classified manually because the assessor classifications # are meant for property tax purposes. These classifications should be # reviewed and revised. res_codes = { 'single': ['01', '51', '52', '53'], 'multi': [string.zfill(i, 2) for i in range(2, 6) + range(7, 10) + range(89, 99)], 'mixed': [] } exempt_codes = [] ## Register input tables. tf = TableFrame(staging.parcels_smt, index_col='apn')