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