]> gitweb.michael.orlitzky.com - dead/census-tools.git/commitdiff
Added the lines2kml script.
authorMichael Orlitzky <michael@orlitzky.com>
Mon, 19 Oct 2009 14:56:26 +0000 (10:56 -0400)
committerMichael Orlitzky <michael@orlitzky.com>
Mon, 19 Oct 2009 14:56:26 +0000 (10:56 -0400)
bin/lines2kml [new file with mode: 0755]

diff --git a/bin/lines2kml b/bin/lines2kml
new file mode 100755 (executable)
index 0000000..9a4b6d3
--- /dev/null
@@ -0,0 +1,103 @@
+#!/usr/bin/env python
+
+"""
+Export line (road) geometries from the database to Keyhole Markup
+Language (KML). The exported roads should have both a human-readable
+name and a globally-unique identifier (gid).
+"""
+
+from optparse import OptionParser
+import os
+import pgdb
+import site
+import sys
+
+# Basically, add '../src' to our path.
+# Needed for the imports that follow.
+site.addsitedir(os.path.dirname(os.path.abspath(sys.argv[0])) + '/../src')
+
+import Configuration.Defaults
+import ExitCodes
+import KML
+
+
+"""
+Parse the command line options. All of these are optional; defaults
+are provided for the database information and the output is written
+to stdout.
+"""
+# -h (help) Conflicts with -h HOSTNAME
+parser = OptionParser(add_help_option = False)
+
+# Use this module's docstring as the description.
+parser.description = __doc__.strip()
+
+parser.add_option('-h',
+                  '--host',
+                  help='The hostname/address where the database is located.',
+                  default=Configuration.Defaults.DATABASE_HOST)
+
+parser.add_option('-d',
+                  '--database',
+                  help='The database in which the population data are stored.',
+                  default=Configuration.Defaults.DATABASE_NAME)
+
+parser.add_option('-U',
+                  '--username',
+                  help='The username who has access to the database.',
+                  default=Configuration.Defaults.DATABASE_USERNAME)
+
+parser.add_option('-s',
+                  '--state',
+                  help='The FIPS id of the State whose roads you would like.')
+
+(options, args) = parser.parse_args()
+
+
+conn = pgdb.connect(host=options.host,
+                    database=options.database,
+                    user=options.username)
+
+
+# We'll use this cursor for all of our queries.
+cursor = conn.cursor()
+
+# If no state is passed on the command line, we want to select the
+# roads for all states.  "... WHERE statefp=statefp ..." will
+# accomplish this.
+statefp = 'statefp'
+if (options.state != None):
+    # The statefp column is a string, so we need to add single quotes
+    # if we're going to specify it.
+    statefp = "'%s'" % options.state
+
+lines_query = """
+SELECT gid, tlid, fullname, the_geom
+FROM tiger_lines
+WHERE statefp=%s
+""" % statefp
+
+cursor.execute(lines_query)
+rows = cursor.fetchall()
+doc = KML.Document()
+
+for row in rows:
+    placemark = KML.Placemark()
+
+    # Name = "Street name (tlid)"
+    fullname = row[2]
+    
+    if (fullname == None):
+        # Some streets don't have a fullname.
+        fullname = 'Unknown'
+        
+    name = KML.Name(fullname + '(' + str(row[1]) + ')')
+    
+    placemark.children.append(name)
+    geometry = KML.RawText(row[3])
+    placemark.children.append(geometry)    
+    doc.children.append(placemark)
+    
+doc.print_kml()
+
+conn.close()