]>
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.
11 # Basically, add '../src' to our path.
12 # Needed for the imports that follow.
13 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 parser
= CLI
.default_option_parser(usage
)
43 # Use this module's docstring as the description.
44 parser
.description
= __doc__
46 parser
.add_option('-s',
49 help="SRID of the input geometry. Defaults to %s." % Configuration
.Defaults
.SRID
,
50 default
=Configuration
.Defaults
.SRID
)
52 (options
, args
) = parser
.parse_args()
55 print "\nERROR: You must supply a longitude and latitude.\n"
57 print '' # Print a newline
58 raise SystemExit(ExitCodes
.NOT_ENOUGH_ARGS
)
61 coords
= GPS
.Coordinates()
62 coords
.longitude
= float(args
[0])
63 coords
.latitude
= float(args
[1])
65 cdb
= Census
.Database(options
.host
,
70 avg_density
= cdb
.find_average_population_density(coords
)
72 if (avg_density
!= None):
73 print str(avg_density
)
75 print 'Error: No rows returned.'
76 print 'Did you pass (longitude, latitude) in the correct order?'
77 raise SystemExit(ExitCodes
.NO_RESULTS
)