From 3e2892296075147dda830dfb8ec08aa083a73b4f Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Tue, 10 Nov 2009 23:40:01 -0500 Subject: [PATCH] Added the path2pop script. --- bin/path2pop | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100755 bin/path2pop diff --git a/bin/path2pop b/bin/path2pop new file mode 100755 index 0000000..9298a92 --- /dev/null +++ b/bin/path2pop @@ -0,0 +1,87 @@ +#!/usr/bin/env python + +""" +We take a Well-Known Text string and one or more KML files as +parameters. We convert the WKT string to a polygon, and drag that +polygon along the paths defined within the KML files. For each path, +the result is another (longer) polygon, whose contained population is +output. +""" + +import os +import site +import sys + + +# Basically, add '../src' to our path. +# Needed for the imports that follow. +site.addsitedir(os.path.dirname(os.path.abspath(sys.argv[0])) + '/../src') + +import Census +import CLI +import Configuration.Defaults +import ExitCodes +import Geometry +import KML + +usage = '%prog [options] , [kml file #2,...]' + +# -h (help) Conflicts with -h HOSTNAME +parser = CLI.default_option_parser(usage) + +# Use this module's docstring as the description. +parser.description = __doc__.strip() + +parser.add_option('-s', + '--srid', + type="int", + help="SRID of the input geometry. Defaults to %s." % Configuration.Defaults.SRID, + default=Configuration.Defaults.SRID) + +(options, args) = parser.parse_args() + +if len(args) < 2: + print """ +ERROR: You must supply both a Well-Known Text string, and a KML file + containing a linestring. +""" + parser.print_help() + print '' # Print a newline + raise SystemExit(ExitCodes.NOT_ENOUGH_ARGS) + + +wkt_string = args[0] +paths = args[1:] + +for path in paths: + f = open(path, 'r') + kml = f.read() + f.close() + + coords = KML.LineString.tuples_from_kml(kml) + p = Geometry.Polygon.from_wkt(wkt_string) + + death_tube = None + + for i in range(len(coords) - 1): + # For each coordinate (except the last), we want to: + # a) Translate our polygon to the coordinate. + # b) Drag the polygon to the next coordinate. + this_coord = Geometry.TwoVector(coords[i][0], coords[i][1]) + next_coord = Geometry.TwoVector(coords[i+1][0], coords[i+1][1]) + drag_vector = (next_coord - this_coord) + tp = p.translate(this_coord) + + if (death_tube == None): + death_tube = tp.drag(drag_vector) + else: + death_tube = death_tube.union(tp.drag(drag_vector)) + + cdb = Census.Database(options.host, + options.database, + options.username, + options.srid) + + pop = cdb.find_contained_population(death_tube.wkt()) + + print "Path: %s; Population: %d" % (path, pop) -- 2.44.2