]> gitweb.michael.orlitzky.com - dead/census-tools.git/blob - bin/wkt2kml
Set a default usage=None for the CLI.default_option_parser() function.
[dead/census-tools.git] / bin / wkt2kml
1 #!/usr/bin/env python
2
3 """
4 Convert an OGC Well-Known Text string to a Keyhole Markup Language
5 (KML) file.
6 """
7
8 """
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.
13
14 There is prior art[2] (written in Perl) should we ever desire to do the
15 conversions correctly, sans-database.
16
17 [1] http://en.wikipedia.org/wiki/Well-known_text
18 [2] http://search.cpan.org/dist/Geo-Converter-WKT2KML/
19 """
20
21 import os
22 import pgdb
23 import site
24 import sys
25
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')
29
30 import CLI
31 import Configuration.Defaults
32 import ExitCodes
33 import GPS
34 import SummaryFile1
35 import KML
36
37
38 """
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'.
42
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.
46 """
47
48 parser = CLI.default_option_parser()
49
50 # Use this module's docstring as the description.
51 parser.description = __doc__.strip()
52
53 parser.add_option('-o',
54 '--outfile',
55 help='Optional output file path. Defaults to stdout.')
56
57 parser.add_option('-s',
58 '--srid',
59 help="SRID of the input geometry. Defaults to %s." % Configuration.Defaults.SRID,
60 default=Configuration.Defaults.SRID)
61
62 parser.add_option('-n',
63 '--name',
64 help='Name to give the geometry object in the KML document.',
65 default='WKT Object')
66
67 (options, args) = parser.parse_args()
68
69
70 if len(args) < 1:
71 print 'ERROR: You must provide a geometry object in Well-Known Text (WKT) format.'
72 parser.print_help()
73 print '' # Print a newline. Durrrr.
74 raise SystemExit(ExitCodes.NOT_ENOUGH_ARGS)
75
76
77 conn = pgdb.connect(host=options.host,
78 database=options.database,
79 user=options.username)
80
81 # We'll use this cursor for all of our queries.
82 cursor = conn.cursor()
83
84
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)
89
90 cursor.execute(kml_query)
91 rows = cursor.fetchall()
92 kml_representation = rows[0][0]
93 conn.close()
94
95
96 doc = KML.Document()
97
98 # Create a semi-transparent red polygon style, and add it to the
99 # document.
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)
106 doc.styles.append(s)
107
108 # We're only going to have one placemark -- the object defined by our
109 # input WKT.
110
111 placemark = KML.Placemark()
112 name = KML.Name(options.name)
113 placemark.children.append(name)
114
115 # This applies the red polygon style defined earlier to our placemark.
116 styleurl = KML.StyleUrl('default')
117 placemark.children.append(styleurl)
118
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)
125
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')
131
132 # Write the KML and get out of here.
133 output_file.write(doc.to_kml())
134 output_file.close()