]>
gitweb.michael.orlitzky.com - dead/census-tools.git/blob - path2pop
9298a92028254b466beeb417880a86e9040474d5
4 We take a Well-Known Text string and one or more KML files as
5 parameters. We convert the WKT string to a polygon, and drag that
6 polygon along the paths defined within the KML files. For each path,
7 the result is another (longer) polygon, whose contained population is
16 # Basically, add '../src' to our path.
17 # Needed for the imports that follow.
18 site
.addsitedir(os
.path
.dirname(os
.path
.abspath(sys
.argv
[0])) + '/../src')
22 import Configuration
.Defaults
27 usage
= '%prog [options] <well-known text> <kml file #1>, [kml file #2,...]'
29 # -h (help) Conflicts with -h HOSTNAME
30 parser
= CLI
.default_option_parser(usage
)
32 # Use this module's docstring as the description.
33 parser
.description
= __doc__
.strip()
35 parser
.add_option('-s',
38 help="SRID of the input geometry. Defaults to %s." % Configuration
.Defaults
.SRID
,
39 default
=Configuration
.Defaults
.SRID
)
41 (options
, args
) = parser
.parse_args()
45 ERROR: You must supply both a Well-Known Text string, and a KML file
46 containing a linestring.
49 print '' # Print a newline
50 raise SystemExit(ExitCodes
.NOT_ENOUGH_ARGS
)
61 coords
= KML
.LineString
.tuples_from_kml(kml
)
62 p
= Geometry
.Polygon
.from_wkt(wkt_string
)
66 for i
in range(len(coords
) - 1):
67 # For each coordinate (except the last), we want to:
68 # a) Translate our polygon to the coordinate.
69 # b) Drag the polygon to the next coordinate.
70 this_coord
= Geometry
.TwoVector(coords
[i
][0], coords
[i
][1])
71 next_coord
= Geometry
.TwoVector(coords
[i
+1][0], coords
[i
+1][1])
72 drag_vector
= (next_coord
- this_coord
)
73 tp
= p
.translate(this_coord
)
75 if (death_tube
== None):
76 death_tube
= tp
.drag(drag_vector
)
78 death_tube
= death_tube
.union(tp
.drag(drag_vector
))
80 cdb
= Census
.Database(options
.host
,
85 pop
= cdb
.find_contained_population(death_tube
.wkt())
87 print "Path: %s; Population: %d" % (path
, pop
)