Skip to content

Instantly share code, notes, and snippets.

@paranoiacblack
Created January 10, 2017 02:54
Show Gist options
  • Save paranoiacblack/72fc238fd2dfdd5c986c025959924232 to your computer and use it in GitHub Desktop.
Save paranoiacblack/72fc238fd2dfdd5c986c025959924232 to your computer and use it in GitHub Desktop.
Krishna errors
street_type_re = re.compile(r'\b\S+\.?$', re.IGNORECASE)
expected = ["Street", "Avenue", "Boulevard", "Drive", "Court", "Place", "Square", "Lane", "Road",
"Trail", "Parkway", "Commons"]
# UPDATE THIS VARIABLE
mapping = { "St": "Street",
"St.": "Street",
"Ave": "Avenue",
"Av": "Avenue",
"Av.": "Avenue",
"Rd.": "Road",
"Rd" : "Road",
"Blvd" : "Boulevard",
"Blvd." : "Boulevard",
"Bd." : "Boulevard",
"Dr" : "Drive",
"Dr." : "Drive",
"Ln" : "Lane",
"Ln." : "Lane",
"Wy" : "Way"
}
postalmapping = { "CA 92509" : "92509",
"90022-1601" : "90022",
"CA 90638" : "90638",
"90744-4522": "90744",
"91406-1232": "91406",
"90049-4013": "90049",
"91330-8449" : "91330"
}
#audit street type, and update name functions from quiz done earlier in the lesson
def audit_street_type(street_types, street_name):
m = street_type_re.search(street_name)
if m:
street_type = m.group()
if street_type not in expected:
street_types[street_type].add(street_name)
def audit(osmfile):
osm_file = open(osmfile, "r")
street_types = defaultdict(set)
for event, elem in ET.iterparse(osm_file, events=("start",)):
if elem.tag == "node" or elem.tag == "way":
for tag in elem.iter("tag"):
if is_street_name(tag):
audit_street_type(street_types, tag.attrib['v'])
osm_file.close()
return street_types
def update_name(name, mapping):
m = street_type_re.search(name)
if m:
if m.group() in mapping.keys():
betterstreettypes = mapping[m.group()]
bettername = street_type_re.sub(betterstreettypes,name)
return bettername
street_types = audit(SAMPLE_FILE)
for st_type, ways in street_types.iteritems():
for name in ways:
better_name = update_name(name,mapping)
print name, "=>", better_name
#audit postal code and update them according to the postal mapping
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment