]>
gitweb.michael.orlitzky.com - dead/census-tools.git/blob - drag_wkt_along_kml_path
072d72a768fec0747fa6e13bb8b88efcd5a084c3
4 We take a Well-Known Text string and a KML file as parameters. The WKT
5 string is converted into a polygon (hopefully), and the KML file is
6 parsed for a linestring, which defines a path. We then drag the
7 polygon along the linestring, and return the resulting polygon as KML.
14 from optparse
import OptionParser
16 # Basically, add '../src' and '../lib/Shapely' to our path.
17 # Needed for the imports that follow.
18 site
.addsitedir(os
.path
.dirname(os
.path
.abspath(sys
.argv
[0])) + '/../src')
19 site
.addsitedir(os
.path
.dirname(os
.path
.abspath(sys
.argv
[0])) + '/../lib/Shapely')
21 import Configuration
.Defaults
26 usage
= '%prog [options] <well-known text> <kml filename>'
28 # -h (help) Conflicts with -h HOSTNAME
29 parser
= OptionParser(usage
=usage
, add_help_option
= False)
31 # Use this module's docstring as the description.
32 parser
.description
= __doc__
.strip()
34 parser
.add_option('-h',
36 help='The hostname/address where the database is located.',
37 default
=Configuration
.Defaults
.DATABASE_HOST
)
39 parser
.add_option('-d',
41 help='The database in which the population data are stored.',
42 default
=Configuration
.Defaults
.DATABASE_NAME
)
44 parser
.add_option('-U',
46 help='The username who has access to the database.',
47 default
=Configuration
.Defaults
.DATABASE_USERNAME
)
49 parser
.add_option('-o',
51 help='Optional output file path. Defaults to stdout.')
53 parser
.add_option('-n',
55 help='Name to give the geometry object in the KML document.',
58 parser
.add_option('-s',
61 help="SRID of the input geometry. Defaults to %s." % Configuration
.Defaults
.SRID
,
62 default
=Configuration
.Defaults
.SRID
)
64 (options
, args
) = parser
.parse_args()
68 ERROR: You must supply both a Well-Known Text string, and a KML file
69 containing a linestring.
72 print '' # Print a newline
73 raise SystemExit(ExitCodes
.NOT_ENOUGH_ARGS
)
77 kml_filename
= args
[1]
79 f
= open(kml_filename
, 'r')
83 coords
= KML
.LineString
.tuples_from_kml(kml
)
84 p
= Geometry
.Polygon
.from_wkt(wkt_string
)
88 for i
in range(len(coords
) - 1):
89 # For each coordinate (except the last), we want to:
90 # a) Translate our polygon to the coordinate.
91 # b) Drag the polygon to the next coordinate.
92 this_coord
= Geometry
.TwoVector(coords
[i
][0], coords
[i
][1])
93 next_coord
= Geometry
.TwoVector(coords
[i
+1][0], coords
[i
+1][1])
94 drag_vector
= (next_coord
- this_coord
)
95 tp
= p
.translate(this_coord
)
97 if (death_tube
== None):
98 death_tube
= tp
.drag(drag_vector
)
100 death_tube
= death_tube
.union(tp
.drag(drag_vector
))
103 conn
= pgdb
.connect(host
=options
.host
,
104 database
=options
.database
,
105 user
=options
.username
)
108 # We'll use this cursor for all of our queries.
109 cursor
= conn
.cursor()
112 # We use one query that basically just imports the WKT string and
113 # immediately exports it as KML. The geometry must have an SRID when
114 # ST_AsKml is called, so we provide one to ST_GeomFromText.
115 kml_query
= "SELECT ST_AsKml(ST_GeomFromText('%s', %s))" % (death_tube
.wkt(), options
.srid
)
117 cursor
.execute(kml_query
)
118 rows
= cursor
.fetchall()
119 kml_representation
= rows
[0][0]
125 # Create a semi-transparent blue polygon style, and add it to the
127 hex_value
= "b0ff0000"
128 s
= KML
.Style(initial_id
=('default'))
129 poly_style
= KML
.PolyStyle()
130 color
= KML
.Color(hex_value
)
131 poly_style
.children
.append(color
)
132 s
.children
.append(poly_style
)
135 # We're only going to have one placemark -- the object defined by our
137 placemark
= KML
.Placemark()
138 name
= KML
.Name(options
.name
)
139 placemark
.children
.append(name
)
141 # This applies the red polygon style defined earlier to our placemark.
142 styleurl
= KML
.StyleUrl('default')
143 placemark
.children
.append(styleurl
)
145 # The database query is going to give us raw KML. For example, if our
146 # input WKT represents a polygon, the output of ST_AsKml will contain
147 # <Polygon>...</Polygon> and everything therein.
148 rawkml
= KML
.RawText(kml_representation
)
149 placemark
.children
.append(rawkml
)
150 doc
.children
.append(placemark
)
152 # Default the output file to sys.stdout. If we were passed an outfile
153 # as an argument, use that instead.
154 output_file
= sys
.stdout
155 if (options
.outfile
!= None):
156 output_file
= open(options
.outfile
, 'w')
158 # Write the KML and get out of here.
159 output_file
.write(doc
.to_kml())