X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FCensus.py;h=b72da0cbd7133e66f9fb9f5c63b7c641fc58180a;hb=HEAD;hp=63f69aaccaadf355b7d1b82414841e1d2839111b;hpb=0139658183fcfe4d53b7f6c2051955c8da5d05d1;p=dead%2Fcensus-tools.git diff --git a/src/Census.py b/src/Census.py index 63f69aa..b72da0c 100644 --- a/src/Census.py +++ b/src/Census.py @@ -25,7 +25,10 @@ class Database: def __del__(self): - self.connection.close() + try: + self.connection.close() + except: + pass def find_average_population_density(self, coords): @@ -36,9 +39,8 @@ class Database: query = """ SELECT population_density - FROM (sf1_blocks INNER JOIN tiger_blocks - ON sf1_blocks.blkidfp00=tiger_blocks.blkidfp00) - WHERE ST_Contains(tiger_blocks.the_geom, + FROM blocks + WHERE ST_Contains(blocks.the_geom, ST_SetSRID(ST_Point(%.6f, %.6f), %d)); """ @@ -46,11 +48,12 @@ class Database: cursor.execute(query, sql_params) rows = cursor.fetchall() cursor.close() - - if len(rows) > 0: + + # SQL queries that return no results get returned as [[None]]. + if rows[0][0] != None: return rows[0][0] else: - return None + return 0 def find_contained_population(self, well_known_text): @@ -60,7 +63,7 @@ class Database: """ cursor = self.connection.cursor() - # We're ready to build our query, one step at a time. Firsy, we store + # We're ready to build our query, one step at a time. First, we store # the Text->Geom conversion in a variable; this just makes the query a # little easier to read. geometric_object = "ST_GeomFromText(%s, %d)" @@ -76,9 +79,9 @@ class Database: # result. # query = """ - SELECT SUM(sf1_blocks.pop100 * - ( ST_Area(ST_Intersection(%s, tiger_blocks.the_geom)) - / ST_Area(tiger_blocks.the_geom) ) + SELECT SUM(pop100 * + ( ST_Area(ST_Intersection(%s, blocks.the_geom)) + / ST_Area(blocks.the_geom) ) ) AS covered_population """ % geometric_object sql_params = (well_known_text, self.srid) @@ -87,15 +90,14 @@ class Database: # Join our two block tables, so that we have both the demographic # and geometric data. query += """ - FROM (sf1_blocks INNER JOIN tiger_blocks - ON sf1_blocks.blkidfp00 = tiger_blocks.blkidfp00) + FROM blocks """ # We only need to calculate the covered population for the blocks # that actually intersect our object. query += """ - WHERE (ST_Intersects(%s, tiger_blocks.the_geom)) + WHERE (ST_Intersects(%s, blocks.the_geom)) """ % geometric_object # geometric_object hasn't been substituted yet, so we need # to add the sql_params twice. @@ -112,10 +114,11 @@ class Database: rows = cursor.fetchall() cursor.close() - if (len(rows) > 0): + # SQL queries that return no results get returned as [[None]]. + if rows[0][0] != None: return rows[0][0] else: - return None + return 0 @@ -127,9 +130,9 @@ class Database: cursor = self.connection.cursor() query = """ - SELECT ST_AsText(tiger_blocks.the_geom) - FROM tiger_blocks - WHERE tiger_blocks.blkidfp00 = %s; + SELECT ST_AsText(the_geom) + FROM blocks + WHERE blkidfp00 = %s; """ sql_params = (blkidfp00,) @@ -137,8 +140,7 @@ class Database: rows = cursor.fetchall() cursor.close() - if len(rows) > 0: - return rows[0][0] - else: - return None + # Just pass on the None if that's what we got from the + # database. + return rows[0][0]