]> gitweb.michael.orlitzky.com - dead/census-tools.git/blob - bin/sf1blocks2sql
Moved the post_data() function in to its own static Javascript file.
[dead/census-tools.git] / bin / sf1blocks2sql
1 #!/usr/bin/python
2
3 """
4 Exports geographic header records to SQL.
5 We take a geographic header file, and a table name as arguments, and then
6 parse that header file to create the necessary SQL statements. The generated
7 SQL statements refer to the table name passed as an argument.
8
9 The output is written to stdout; it can either be redirected to a file,
10 or piped directly in to the database.
11 """
12
13 import sys
14 import os
15 import site
16
17 # Basically, add '../src' to our path.
18 # Needed for the imports that follow.
19 site.addsitedir(os.path.dirname(os.path.abspath(sys.argv[0])) + '/../src')
20
21 import ExitCodes
22 import GPS
23 import SummaryFile1
24
25
26 if (len(sys.argv) < 3):
27 print "Usage: %s <geo_file> <table_name>" % sys.argv[0]
28 raise SystemExit(ExitCodes.NOT_ENOUGH_ARGS)
29
30 geo_file_path = sys.argv[1]
31 table_name = sys.argv[2]
32
33 grp = SummaryFile1.GeoRecordParser()
34 blocks = grp.parse_blocks(geo_file_path)
35
36
37 sql_query = """
38 INSERT INTO %s (state,
39 county,
40 tract,
41 block,
42 arealand,
43 areawatr,
44 total_area,
45 blkidfp00,
46 pop100,
47 population_density)
48
49 VALUES ('%s', '%s', '%s', '%s', %.12f, %.12f, %.12f, '%s', %d, %.12f);
50 """
51
52 for b in blocks:
53 # Print out the INSERT statement contained in sql_query,
54 # substituting in all of the block attributes.
55 print sql_query % (table_name,
56 b.state,
57 b.county,
58 b.tract,
59 b.block,
60 b.arealand,
61 b.areawatr,
62 b.total_area(),
63 b.blkidfp00(),
64 b.pop100,
65 b.population_density())