]>
gitweb.michael.orlitzky.com - dead/census-tools.git/blob - bin/wkt2kml
4 Convert an OGC Well-Known Text string to a Keyhole Markup Language
7 We take a Well-Known Text[1] string as input, and optionally a
8 filename to which to write the output. While we shouldn't technically
9 *need* access to a PostGIS database to perform this conversion, it
10 makes everything a lot easier, so we require it.
12 There is prior art[2] (written in Perl) should we ever desire to do the
13 conversions correctly, sans-database.
15 [1] http://en.wikipedia.org/wiki/Well-known_text
16 [2] http://search.cpan.org/dist/Geo-Converter-WKT2KML/
24 # Basically, add '../src' to our path.
25 # Needed for the imports that follow.
26 site
.addsitedir(os
.path
.dirname(os
.path
.abspath(sys
.argv
[0])) + '/../src')
29 import Configuration
.Defaults
37 Parse the command line options. All of these are optional; defaults
38 are provided for the database information and the output is written to
39 stdout unless otherwise specified via '-o'.
41 We take an SRID too, in case there's ever a reason to override the
42 default. The name option is available in case the user would like to
43 e.g. see the object name in Google Earth.
46 parser
= CLI
.default_option_parser()
48 # Use this module's docstring as the description.
49 parser
.description
= __doc__
51 parser
.add_option('-o',
53 help='Optional output file path. Defaults to stdout.')
55 parser
.add_option('-s',
57 help="SRID of the input geometry. Defaults to %s." % Configuration
.Defaults
.SRID
,
58 default
=Configuration
.Defaults
.SRID
)
60 parser
.add_option('-n',
62 help='Name to give the geometry object in the KML document.',
65 (options
, args
) = parser
.parse_args()
69 print 'ERROR: You must provide a geometry object in Well-Known Text (WKT) format.'
71 print '' # Print a newline. Durrrr.
72 raise SystemExit(ExitCodes
.NOT_ENOUGH_ARGS
)
75 conn
= pgdb
.connect(host
=options
.host
,
76 database
=options
.database
,
77 user
=options
.username
)
79 # We'll use this cursor for all of our queries.
80 cursor
= conn
.cursor()
83 # We use one query that basically just imports the WKT string and
84 # immediately exports it as KML. The geometry must have an SRID when
85 # ST_AsKml is called, so we provide one to ST_GeomFromText.
86 kml_query
= "SELECT ST_AsKml(ST_GeomFromText('%s', %s))" % (args
[0], options
.srid
)
88 cursor
.execute(kml_query
)
89 rows
= cursor
.fetchall()
90 kml_representation
= rows
[0][0]
96 # Create a semi-transparent red polygon style, and add it to the
98 hex_value
= "900000ff"
99 s
= KML
.Style(initial_id
=('default'))
100 poly_style
= KML
.PolyStyle()
101 color
= KML
.Color(hex_value
)
102 poly_style
.children
.append(color
)
103 s
.children
.append(poly_style
)
106 # We're only going to have one placemark -- the object defined by our
109 placemark
= KML
.Placemark()
110 name
= KML
.Name(options
.name
)
111 placemark
.children
.append(name
)
113 # This applies the red polygon style defined earlier to our placemark.
114 styleurl
= KML
.StyleUrl('default')
115 placemark
.children
.append(styleurl
)
117 # The database query is going to give us raw KML. For example, if our
118 # input WKT represents a polygon, the output of ST_AsKml will contain
119 # <Polygon>...</Polygon> and everything therein.
120 rawkml
= KML
.RawText(kml_representation
)
121 placemark
.children
.append(rawkml
)
122 doc
.children
.append(placemark
)
124 # Default the output file to sys.stdout. If we were passed an outfile
125 # as an argument, use that instead.
126 output_file
= sys
.stdout
127 if (options
.outfile
!= None):
128 output_file
= open(options
.outfile
, 'w')
130 # Write the KML and get out of here.
131 output_file
.write(doc
.to_kml())