]>
gitweb.michael.orlitzky.com - dead/census-tools.git/blob - wkt2pop
af3559577faa9919aed275beaa9fbb305f3e14fe
4 Find the total population contained within a geometric object.
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
13 [1] http://en.wikipedia.org/wiki/Well-known_text
20 from optparse
import OptionParser
22 # Basically, add '../src' to our path.
23 # Needed for the imports that follow.
24 site
.addsitedir(os
.path
.dirname(os
.path
.abspath(sys
.argv
[0])) + '/../src')
26 import Configuration
.Defaults
29 usage
= '%prog [options] <well-known text representation>'
31 # -h (help) Conflicts with -h HOSTNAME
32 parser
= OptionParser(usage
=usage
, add_help_option
= False)
34 # Use this module's docstring as the description.
35 parser
.description
= __doc__
.strip()
37 parser
.add_option('-h',
39 help='The hostname/address where the database is located.',
40 default
=Configuration
.Defaults
.DATABASE_HOST
)
42 parser
.add_option('-d',
44 help='The database in which the population data are stored.',
45 default
=Configuration
.Defaults
.DATABASE_NAME
)
47 parser
.add_option('-U',
49 help='The username who has access to the database.',
50 default
=Configuration
.Defaults
.DATABASE_USERNAME
)
52 parser
.add_option('-s',
54 help="SRID of the input geometry. Defaults to %s." % Configuration
.Defaults
.SRID
,
55 default
=Configuration
.Defaults
.SRID
)
58 (options
, args
) = parser
.parse_args()
61 print "\nERROR: You must supply a geometric object in Well-Known Text format.\n"
63 print '' # Print a newline.
64 raise SystemExit(ExitCodes
.NOT_ENOUGH_ARGS
)
67 conn
= pgdb
.connect(host
=options
.host
,
68 database
=options
.database
,
69 user
=options
.username
)
71 cursor
= conn
.cursor()
73 # We're ready to build our query, one step at a time. Firsy, we store
74 # the Text->Geom conversion in a variable; this just makes the query a
75 # little easier to read.
76 geometric_object
= "ST_GeomFromText('%s', %s)" % (args
[0], options
.srid
)
78 # We want to compute the population "under" the geometric object. We
79 # can compute the percentage of a block that is covered by taking the
80 # area of (the intersection of the object and the block) divided by
81 # the total area of the block.
83 # Once we know the percentage covered, we just multiply that value by
84 # the total population in the block to find the population that is
85 # covered. The sum of these values over all blocks is our final
89 SELECT SUM(sf1_blocks.pop100 *
90 ( ST_Area(ST_Intersection(%s, tiger.the_geom))
91 / ST_Area(tiger.the_geom) )
92 ) AS covered_population
93 """ % geometric_object
96 # Join our two block tables, so that we have both the demographic
100 FROM (sf1_blocks INNER JOIN tiger
101 ON sf1_blocks.tiger_blkidfp00 = tiger.blkidfp00)
105 # We only need to calculate the covered population for the blocks
106 # that actually intersect our object.
109 WHERE (ST_Intersects(%s, tiger.the_geom))
110 """ % geometric_object
113 # And we only take the first result, since they're all going to be the
114 # same (our query returns the sum once for each block).
120 cursor
.execute(query
)
121 rows
= cursor
.fetchall()
124 population
= rows
[0][0]
127 print 'Error: No rows returned.'
128 raise SystemExit(ExitCodes
.NO_RESULTS
)