]>
gitweb.michael.orlitzky.com - dead/census-tools.git/blob - bin/find_avg_population_density
4 Find the average population density of a set of GPS coordinates.
10 from optparse
import OptionParser
12 # Basically, add '../src' to our path.
13 # Needed for the imports that follow.
14 site
.addsitedir(os
.path
.dirname(os
.path
.abspath(sys
.argv
[0])) + '/../src')
17 import Configuration
.Defaults
24 Parse the command line options. There's some magic involved here;
25 otherwise, optparse woule like nothing more than to interpret GPS
26 coordinates such as '-78.000000' as options.
28 for idx
, value
in enumerate(sys
.argv
):
29 # Please oh please don't rename this script to something which
30 # validates as a negative float.
31 if (StringUtils
.is_negative_float(value
)):
32 sys
.argv
.insert(idx
, '--')
33 # We have to break here, otherwise the negative argument
34 # will get shifted down the list, and we'll hit it again
35 # in the next iteration of enumerate(). And then it'll
36 # get shifted down the list...
39 usage
= '%prog [options] <longitude> <latitude>'
41 # -h (help) Conflicts with -h HOSTNAME
42 parser
= OptionParser(usage
=usage
, add_help_option
= False)
44 # Use this module's docstring as the description.
45 parser
.description
= __doc__
.strip()
47 parser
.add_option('-h',
49 help='The hostname/address where the database is located.',
50 default
=Configuration
.Defaults
.DATABASE_HOST
)
52 parser
.add_option('-d',
54 help='The database in which the population data are stored.',
55 default
=Configuration
.Defaults
.DATABASE_NAME
)
57 parser
.add_option('-U',
59 help='The username who has access to the database.',
60 default
=Configuration
.Defaults
.DATABASE_USERNAME
)
62 parser
.add_option('-s',
65 help="SRID of the input geometry. Defaults to %s." % Configuration
.Defaults
.SRID
,
66 default
=Configuration
.Defaults
.SRID
)
68 (options
, args
) = parser
.parse_args()
71 print "\nERROR: You must supply a longitude and latitude.\n"
73 print '' # Print a newline
74 raise SystemExit(ExitCodes
.NOT_ENOUGH_ARGS
)
77 coords
= GPS
.Coordinates()
78 coords
.longitude
= float(args
[0])
79 coords
.latitude
= float(args
[1])
81 cdb
= Census
.Database(options
.host
,
86 avg_density
= cdb
.find_average_population_density(coords
)
88 if (avg_density
!= None):
89 print str(avg_density
)
91 print 'Error: No rows returned.'
92 print 'Did you pass (longitude, latitude) in the correct order?'
93 raise SystemExit(ExitCodes
.NO_RESULTS
)