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