Пример #1
0
def import_data_to_db(fips=[]):
    """
    Imports shape file data into the database, requires shp2pgsql to be avialable on the path (it is called via os.popen)
    :param fips: An optional list of state fips codes (integers) to import.
    :return:
    """
    for data_dir in (PLACES_DIR, ROADS_DIR):
        for f in os.listdir(data_dir):
            if f.endswith(".shp"):
                # If we don't have a fips list specified,
                # OR our file name contains one of the fips codes we care about...
                if fips == [] or True in ["_" + str(x) + "_" in f for x in fips]:
                    command = "shp2pgsql -s 4269 -a -W latin1 {0} gis.{1}"\
                        .format(data_dir + os.path.sep + f,
                                data_dir.split(os.path.sep)[-1])
                    DEFAULT_LOGGER.info("Running " + command)
                    import_lines = os.popen(command).readlines()
                    DEFAULT_LOGGER.info("Importing {0} values into database".format(str(len(import_lines))))
                    execute_import_statements(import_lines)

    vacuum_full()