]> gitweb.michael.orlitzky.com - dead/census-tools.git/commitdiff
Added the path2pop script.
authorMichael Orlitzky <michael@orlitzky.com>
Wed, 11 Nov 2009 04:40:01 +0000 (23:40 -0500)
committerMichael Orlitzky <michael@orlitzky.com>
Wed, 11 Nov 2009 04:40:01 +0000 (23:40 -0500)
bin/path2pop [new file with mode: 0755]

diff --git a/bin/path2pop b/bin/path2pop
new file mode 100755 (executable)
index 0000000..9298a92
--- /dev/null
@@ -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] <well-known text> <kml file #1>, [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)