]> gitweb.michael.orlitzky.com - dead/census-tools.git/blob - bin/wkt2pop
Set a default usage=None for the CLI.default_option_parser() function.
[dead/census-tools.git] / bin / wkt2pop
1 #!/usr/bin/python
2
3 """
4 Find the total population contained within a geometric object.
5 """
6
7 """
8 Our input is an OGC Well-Known Text[1] string. This string is used as
9 part of a database query that finds the population contained within
10 (i.e. 'underneath') the geometric object corresponding to the WKT
11 string.
12
13 [1] http://en.wikipedia.org/wiki/Well-known_text
14 """
15
16 import sys
17 import os
18 import site
19
20 # Basically, add '../src' to our path.
21 # Needed for the imports that follow.
22 site.addsitedir(os.path.dirname(os.path.abspath(sys.argv[0])) + '/../src')
23
24 import Census
25 import CLI
26 import Configuration.Defaults
27 import ExitCodes
28
29 usage = '%prog [options] <well-known text representation>'
30 parser = CLI.default_option_parser(usage)
31
32 # Use this module's docstring as the description.
33 parser.description = __doc__.strip()
34
35
36 parser.add_option('-s',
37 '--srid',
38 type="int",
39 help="SRID of the input geometry. Defaults to %s." % Configuration.Defaults.SRID,
40 default=Configuration.Defaults.SRID)
41
42
43 (options, args) = parser.parse_args()
44
45 if len(args) < 1:
46 print "\nERROR: You must supply a geometric object in Well-Known Text format.\n"
47 parser.print_help()
48 print '' # Print a newline.
49 raise SystemExit(ExitCodes.NOT_ENOUGH_ARGS)
50
51
52 cdb = Census.Database(options.host,
53 options.database,
54 options.username,
55 options.srid)
56
57 population = cdb.find_contained_population(args[0])
58
59 if (population != None):
60 print population
61 else:
62 print 'Error: No rows returned.'
63 raise SystemExit(ExitCodes.NO_RESULTS)