]> gitweb.michael.orlitzky.com - dead/census-tools.git/blobdiff - bin/find_avg_population_density
Modified the default_option_parser function to return a parser using a VerbatimHelpFo...
[dead/census-tools.git] / bin / find_avg_population_density
index e6d8ac75906c7bf722e0dd310c309d2a26d820f5..42ed3e5138c69a3319b083134b8fa041e9cbc5c3 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 CLI
 import Configuration.Defaults
 import ExitCodes
 import GPS
 import StringUtils
-import SummaryFile1
 
 
 """
@@ -39,26 +38,16 @@ for idx, value in enumerate(sys.argv):
     
 usage = '%prog [options] <longitude> <latitude>'
 
-# -h (help) Conflicts with -h HOSTNAME
-parser = OptionParser(usage=usage, add_help_option = False)
+parser = CLI.default_option_parser(usage)
 
 # Use this module's docstring as the description.
 parser.description = __doc__
 
-parser.add_option('-h',
-                  '--host',
-                  help='The hostname/address where the database is located.',
-                  default=Configuration.Defaults.DATABASE_HOST)
-
-parser.add_option('-d',
-                  '--database',
-                  help='The database in which the population data are stored.',
-                  default=Configuration.Defaults.DATABASE_NAME)
-
-parser.add_option('-U',
-                  '--username',
-                  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()
 
@@ -73,38 +62,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()
+