Advertisement
Guest User

GIS SE paste for 56621

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