Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. import xml.etree.cElementTree as ET
  2. from collections import defaultdict
  3. import re
  4. import pprint
  5.  
  6. OSMFILE = "chicago_illinois.osm"
  7.  
  8. expected = ["60183", "60513", "60126", "60007", "60660", "60440", "60022", "60642", "60185"]
  9.  
  10.  
  11. mapping = {"Wasco, IL 60183" : "60183", "IL, 60642" : "60642", "IL, 60126" : "60126",
  12. "IL 60118" : "60118", "IL 60707" : "60707", "IL 60605-1226" : "60605"}
  13.  
  14. """
  15. If the postal code contains any none digit numbers or
  16. if it is greater than length 5, it appends it to the error_codes.
  17. Else it updates the postal_codes with this_postal_code.
  18. """
  19. def audit_postal_code(error_codes, postal_codes, this_postal_code):
  20. # Append incorrect zip codes to list
  21. if this_postal_code.isdigit() == False:
  22. error_codes.append(this_postal_code)
  23. elif len(this_postal_code) != 5:
  24. error_codes.append(this_postal_code)
  25. else:
  26. postal_codes.update([this_postal_code])
  27.  
  28. def is_postal_code(elem):
  29. return (elem.attrib['k'] == "addr:postcode")
  30.  
  31. def audit_post(osmfile):
  32. # Parse osm file for incorrect postal codes
  33. osm_file = open(osmfile, "r")
  34. error_codes = []
  35. postal_codes = set([])
  36. for event, elem in ET.iterparse(osm_file, events=("start",)):
  37. if elem.tag == "node" or elem.tag == "way":
  38. for tag in elem.iter("tag"):
  39. if is_postal_code(tag):
  40. audit_postal_code(error_codes, postal_codes, tag.attrib["v"])
  41.  
  42. return error_codes, postal_codes
  43.  
  44. def update_name_postcode(this_postal_code,mapping):
  45. # split the postcode at '-' and only keep the first part
  46. if '-' in this_postal_code:
  47. this_postal_code = this_postal_code.split('-')[0]
  48. # split the postcode at '\' and only keep the first part
  49. elif '/' in this_postal_code:
  50. this_postal_code = this_postal_code.split('/')[0]
  51. if this_postal_code not in expected:
  52. if this_postal_code in mapping.keys():
  53. this_postal_code = mapping[this_postal_code]
  54. return this_postal_code
  55.  
  56.  
  57. errors, clean = audit_post(OSMFILE)
  58.  
  59. for name in errors:
  60. better_name = update_name_postcode(name, mapping)
  61. print name, "=>", better_name
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement