X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;ds=inline;f=bin%2Fsf1blocks2sql;fp=bin%2Fsf1blocks2sql;h=1918a2956640e0f0daebc5d9f01f50e6cb811513;hb=b6cea827835222aa38004dd599e24c3ecc5c596b;hp=0000000000000000000000000000000000000000;hpb=af1c16f00734808c24e61e776db5bedd52494d13;p=dead%2Fcensus-tools.git diff --git a/bin/sf1blocks2sql b/bin/sf1blocks2sql new file mode 100755 index 0000000..1918a29 --- /dev/null +++ b/bin/sf1blocks2sql @@ -0,0 +1,65 @@ +#!/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.NotEnoughArgs) + +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, + tiger_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.tiger_blkidfp00(), + b.pop100, + b.population_density())