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