From: Michael Orlitzky Date: Mon, 19 Oct 2009 14:56:26 +0000 (-0400) Subject: Added the lines2kml script. X-Git-Url: http://gitweb.michael.orlitzky.com/?p=dead%2Fcensus-tools.git;a=commitdiff_plain;h=717a5d47751909c5b2e0d9e984c340cb2e50be82 Added the lines2kml script. --- diff --git a/bin/lines2kml b/bin/lines2kml new file mode 100755 index 0000000..9a4b6d3 --- /dev/null +++ b/bin/lines2kml @@ -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()