#!/usr/bin/python import re # Reads from a file formatted as miles.dat and builds a graph from this data def buildGraph(fileref): cities = [] g = {} for line in fileref.readlines(): if re.match('\*', line): # Comments, just ignore them pass # ignore elif re.match('\d', line): # Adjacency data, update graph dists = line.split() # Scan all the distances in this line and insert this data into g for dist in dists: # Update the graph with adjacencies # Missing Code else: # this line provides city data data = re.split('[\[\,\]]+', line) # Append to the list of cities # Insert the city data into g # Missing Code return g # Takes a graph whose vertices have associated longitude, latitude info. and # write this into a given kml file def writeKML(g, filename): outfile = open(filename, "wt") outfile.write('\n') outfile.write('\n') # Write info. corresponding to each vertex in the graph for v in g.keys(): outfile.write('\n') outfile.write('' + v + ', ' + g[v][0] + '\n') outfile.write('' + v + ', ' + g[v][0] + '\n') outfile.write('\n') outfile.write('' + str(-1*g[v][2]) + ', ' + str(g[v][1]) + ', ' + '0\n') outfile.write('\n') outfile.write('\n') outfile.write('') outfile.close() def main(): fileref = open(pickAFile(), 'r') g = buildGraph(fileref) writeKML(g, "/mnt/nfs/netapp1/fs3/sriram/python/graph.kml") for v in g.keys(): printNow(v) printNow(g[v])