]> gitweb.michael.orlitzky.com - dead/census-tools.git/commitdiff
Added the Census module.
authorMichael Orlitzky <michael@orlitzky.com>
Sun, 27 Sep 2009 20:56:04 +0000 (16:56 -0400)
committerMichael Orlitzky <michael@orlitzky.com>
Sun, 27 Sep 2009 20:56:04 +0000 (16:56 -0400)
Factored out the find_average_population_density database code into the Census.Database class.

bin/find_avg_population_density
src/Census.py [new file with mode: 0644]

index 05e2733928765e16b253cbe7b5eeb478f9393cdb..712db20f96c3c53465d1b8131826cb37547fb88d 100755 (executable)
@@ -7,18 +7,17 @@ Find the average population density of a set of GPS coordinates.
 import sys
 import os
 import site
-import pgdb
 from optparse import OptionParser
 
 # 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 Census
 import Configuration.Defaults
 import ExitCodes
 import GPS
 import StringUtils
-import SummaryFile1
 
 
 """
@@ -60,6 +59,12 @@ parser.add_option('-U',
                   help='The username who has access to the database.',
                   default=Configuration.Defaults.DATABASE_USERNAME)
 
+parser.add_option('-s',
+                  '--srid',
+                  type="int",
+                  help="SRID of the input geometry. Defaults to %s." % Configuration.Defaults.SRID,
+                  default=Configuration.Defaults.SRID)
+
 (options, args) = parser.parse_args()
 
 if len(args) < 2:
@@ -73,38 +78,17 @@ coords = GPS.Coordinates()
 coords.longitude = float(args[0])
 coords.latitude = float(args[1])
 
-conn = pgdb.connect(host=options.host,
-                    database=options.database,
-                    user=options.username)
-
-cursor = conn.cursor()
-
-###########
-# WARNING #
-###########
-#
-# Most GIS software, including PostGIS and the associated libraries,
-# store and manipulate GPS coordinates in (longitude, latitude) format
-# rather than (latitude, longitude) format.
-#
-
-query = """
-SELECT population_density
-FROM (sf1_blocks INNER JOIN tiger
-      ON sf1_blocks.tiger_blkidfp00=tiger.blkidfp00)
-WHERE ST_Contains(tiger.the_geom,
-                  ST_SetSRID(ST_Point(%.6f, %.6f), 4269));
-""" % (coords.longitude, coords.latitude)
-
-cursor.execute(query)
-rows = cursor.fetchall()
-
-if len(rows) > 0:
-    avg_density = rows[0][0]
+cdb = Census.Database(options.host,
+                      options.database,
+                      options.username,
+                      options.srid)
+
+avg_density = cdb.find_average_population_density(coords)
+
+if (avg_density != None):
     print str(avg_density)
 else:
     print 'Error: No rows returned.'
     print 'Did you pass (longitude, latitude) in the correct order?'
     raise SystemExit(ExitCodes.NO_RESULTS)
-    
-conn.close()
+
diff --git a/src/Census.py b/src/Census.py
new file mode 100644 (file)
index 0000000..eff25d4
--- /dev/null
@@ -0,0 +1,46 @@
+import pgdb
+
+import GPS
+
+
+class Database:
+    """
+    This class wraps all of the operations that we'd like to perform
+    on the census database. Most of the utility scripts will just call
+    one or two methods from within this class.
+    """
+
+    def __init__(self, _host, _database, _username, _srid):
+        self.connection = pgdb.connect(host=_host,
+                                       database=_database,
+                                       user=_username)
+        self.srid = _srid
+
+
+    def __del__(self):
+        self.connection.close()
+
+        
+    def find_average_population_density(self, coords):
+        """
+        Find the average population density at a set of GPS coordinates.
+        """
+        cursor = self.connection.cursor()
+                
+        query = """
+        SELECT population_density
+        FROM (sf1_blocks INNER JOIN tiger
+              ON sf1_blocks.tiger_blkidfp00=tiger.blkidfp00)
+        WHERE ST_Contains(tiger.the_geom,
+                          ST_SetSRID(ST_Point(%.6f, %.6f), %d));
+        """
+        
+        sql_params = (coords.longitude, coords.latitude, self.srid)        
+        cursor.execute(query, sql_params)        
+        rows = cursor.fetchall()
+        
+        if len(rows) > 0:
+            return rows[0][0]
+        else:
+            return None
+