Advertisement
Guest User

Untitled

a guest
Apr 4th, 2013
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import xml.dom.minidom as m
  2. from pprint import pprint
  3. import re
  4.  
  5. kml = "d:\state_pts_MI.kml"
  6. dom = m.parse(kml)
  7.  
  8. placemarks = dom.getElementsByTagName("Placemark")
  9.  
  10. r = {}
  11.  
  12. # this is how we split the key/value pairs inside the description nodes
  13. splitre = re.compile("([\w\s]+):(.*)")
  14.  
  15. unnamed_counter = 0
  16.  
  17. # one placemark is one jail
  18. for p in placemarks:
  19.  
  20.     # get name of jail from <name> node, or generate an UUID
  21.     if p.getElementsByTagName("name").length > 0:
  22.         jail_name = p.getElementsByTagName("name")[0].firstChild.data
  23.     else:
  24.         unnamed_counter += 1
  25.         jail_name = "unnamed"+str(unnamed_counter)
  26.  
  27.     # get coordinates as list (split coordinates from KML at comma)
  28.     coords = p.getElementsByTagName("coordinates")[0].firstChild.data.split(",")
  29.     #pprint(coords)
  30.     # convert to float so we can use the precision printing formats to define number of decimals, e.g. by using %.3f or %.6f
  31.     lon = float(coords[0])
  32.     lat = float(coords[1])
  33.  
  34.     # prepare results dict for this jail
  35.     r[jail_name] = {}
  36.     r[jail_name]["lon"] = lon
  37.     r[jail_name]["lat"] = lat
  38.  
  39.     # exactly one description per jail
  40.     d = p.getElementsByTagName("description")[0]
  41.  
  42.     # grab one line per key-value-pair
  43.     descTextLines = d.toxml().replace("<description>","").replace("</description>","").replace("<div>","").split("</div>")
  44.    
  45.     for line in descTextLines:
  46.         if line.strip() != "":
  47.             # only if we have a nonempty line
  48.             # split it by using the regexp we defined
  49.             rem = splitre.match(line)
  50.             kv = rem.groups()
  51.             # now we have the braced capture groups in kv
  52.  
  53.             r[jail_name][kv[0]] = kv[1].strip()
  54.  
  55. # display the results
  56.  
  57. total_inmates = 0
  58. for jailname in r.keys():
  59.     print("%s is @ %.3f,%.3f and has %d inmates." % (jailname, r[jailname]['lon'], r[jailname]['lat'], int(r[jailname]['2010 Correctional Population'])));
  60.     total_inmates += int(r[jailname]['2010 Correctional Population'])
  61.  
  62. print("Total inmates: %d" % total_inmates)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement