X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=bin%2Ffind_avg_population_density;h=42ed3e5138c69a3319b083134b8fa041e9cbc5c3;hb=c498de425990074f93306b066a48400739b6d38a;hp=e58d1cac06349e3462eb3c1997d38a8633d86c6f;hpb=84d512e317cac78d67bec5c64fcf761d3f5e0689;p=dead%2Fcensus-tools.git diff --git a/bin/find_avg_population_density b/bin/find_avg_population_density index e58d1ca..42ed3e5 100755 --- a/bin/find_avg_population_density +++ b/bin/find_avg_population_density @@ -1,27 +1,78 @@ #!/usr/bin/python +""" +Find the average population density of a set of GPS coordinates. +""" + +import sys +import os +import site + # Basically, add '../src' to our path. -import sys, os, site +# Needed for the imports that follow. site.addsitedir(os.path.dirname(os.path.abspath(sys.argv[0])) + '/../src') -import ExitCodes, GPS, SummaryFile1 +import Census +import CLI +import Configuration.Defaults +import ExitCodes +import GPS +import StringUtils + + +""" +Parse the command line options. There's some magic involved here; +otherwise, optparse woule like nothing more than to interpret GPS +coordinates such as '-78.000000' as options. +""" +for idx, value in enumerate(sys.argv): + # Please oh please don't rename this script to something which + # validates as a negative float. + if (StringUtils.is_negative_float(value)): + sys.argv.insert(idx, '--') + # We have to break here, otherwise the negative argument + # will get shifted down the list, and we'll hit it again + # in the next iteration of enumerate(). And then it'll + # get shifted down the list... + break + +usage = '%prog [options] ' -# Find the average population density of a set of GPS coordinates -# given a (SF1) geographic header file. +parser = CLI.default_option_parser(usage) -if (len(sys.argv) < 4): - print "Usage: %s " % sys.argv[0] - raise SystemExit(ExitCodes.NotEnoughArgs) +# Use this module's docstring as the description. +parser.description = __doc__ + +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: + print "\nERROR: You must supply a longitude and latitude.\n" + parser.print_help() + print '' # Print a newline + raise SystemExit(ExitCodes.NOT_ENOUGH_ARGS) -# If we made it here, it's sort of safe to parse the arguments -# and hope they're in order. coords = GPS.Coordinates() -coords.latitude = float(sys.argv[1]) -coords.longitude = float(sys.argv[2]) -geo_file_path = sys.argv[3] +coords.longitude = float(args[0]) +coords.latitude = float(args[1]) + +cdb = Census.Database(options.host, + options.database, + options.username, + options.srid) -avg_density = SummaryFile1.FindAveragePopulationDensity(coords, geo_file_path) +avg_density = cdb.find_average_population_density(coords) -print str(avg_density) +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)