]> gitweb.michael.orlitzky.com - dead/census-tools.git/blobdiff - bin/sf1blocks2sql
Added the framework for the PostGIS database integration.
[dead/census-tools.git] / bin / sf1blocks2sql
diff --git a/bin/sf1blocks2sql b/bin/sf1blocks2sql
new file mode 100755 (executable)
index 0000000..1918a29
--- /dev/null
@@ -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 <geo_file> <table_name>" % 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())