X-Git-Url: https://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FCensus.py;fp=src%2FCensus.py;h=eff25d4fbacd45e3053015462921aad55d46c68a;hb=591f5177a7f58f6d5acc3314574eaf9f19dad963;hp=0000000000000000000000000000000000000000;hpb=5bb33fa7a3c9af7acee24d7e51cccbb668c93ab6;p=dead%2Fcensus-tools.git diff --git a/src/Census.py b/src/Census.py new file mode 100644 index 0000000..eff25d4 --- /dev/null +++ b/src/Census.py @@ -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 +