#!/usr/bin/python """ Exports geographic header records to SQL. We take a geographic header file, and a table name as arguments, and then parse that header file to create the necessary SQL statements. The generated SQL statements refer to the table name passed as an argument. The output is written to stdout; it can either be redirected to a file, or piped directly in to the database. """ import sys import os import site # Basically, add '../src' to our path. # Needed for the imports that follow. site.addsitedir(os.path.dirname(os.path.abspath(sys.argv[0])) + '/../src') import ExitCodes import GPS import SummaryFile1 if (len(sys.argv) < 3): print "Usage: %s " % sys.argv[0] raise SystemExit(ExitCodes.NOT_ENOUGH_ARGS) geo_file_path = sys.argv[1] table_name = sys.argv[2] grp = SummaryFile1.GeoRecordParser() blocks = grp.parse_blocks(geo_file_path) sql_query = """ INSERT INTO %s (state, county, tract, block, arealand, areawatr, total_area, blkidfp00, pop100, population_density) VALUES ('%s', '%s', '%s', '%s', %.12f, %.12f, %.12f, '%s', %d, %.12f); """ for b in blocks: # Print out the INSERT statement contained in sql_query, # substituting in all of the block attributes. print sql_query % (table_name, b.state, b.county, b.tract, b.block, b.arealand, b.areawatr, b.total_area(), b.blkidfp00(), b.pop100, b.population_density())