Skip to content

Instantly share code, notes, and snippets.

@jasonbeverage
Last active January 3, 2016 22:19
Show Gist options
  • Save jasonbeverage/8528038 to your computer and use it in GitHub Desktop.
Save jasonbeverage/8528038 to your computer and use it in GitHub Desktop.
"""
Post processor for OSM tables imported by Imposm to rename suffixes and prefixes to shorter versions
Use like:
python gen_osm_replacment > gen_osm_replacment.sql
psql -d osm < gen_osm_replacmenet.sql
"""
replacements = [
('Avenue', 'Ave'),
('Boulevard', 'Blvd'),
('Causeway', 'Cswy'),
('Center', 'Ctr'),
('Circle', 'Cir'),
('Drive', 'Dr'),
('Expressway', 'Expy'),
('Extension', 'Ext'),
('Freeway', 'Fwy'),
('Highway', 'Hwy'),
('Junction', 'Jct'),
('Lane', 'Ln'),
('Parkway', 'Pkwy'),
('Road', 'Rd'),
('Street', 'St')
]
directions = [
('North', 'N'),
('Northwest', 'NW'),
('Northeast', 'NE'),
('South', 'S'),
('Southwest', 'SW'),
('Southeast', 'SE'),
('East', 'E'),
('West', 'W')
]
tables = [
'osm_mainroads',
'osm_motorways',
'osm_mainroads_gen0',
'osm_mainroads_gen1',
'osm_motorways_gen0',
'osm_motorways_gen1',
'osm_minorroads'
]
for i in directions:
orig = i[0]
replace = i[1]
# Replace street names that end with a direction with the abbreviation. "Main Street North" will become "Main Street N"
for t in tables:
print "update %s set name = regexp_replace(name, ' %s$', ' %s');" % ( t, orig, replace)
print ""
# Replace street names that begin with a direction with the abbreviation. "North Main Street" will become "N Main Street"
for t in tables:
print "update %s set name = regexp_replace(name, '^%s ', '%s ');" % ( t, orig, replace)
print ""
for i in replacements:
orig = i[0]
replace = i[1]
# Replace street names that end with one of the words in the replacement list with their abbreviations. "First Street" will become "First St".
for t in tables:
print "update %s set name = regexp_replace(name, '%s$', '%s');" % ( t, orig, replace)
print ""
# Now replace any instance of the world that appear with spaces around them. So part of the name but not the first word in the name
# So "First Avenue N" would become "First Ave N"
for t in tables:
print "update %s set name = replace(name, ' %s ', ' %s ');" % ( t, orig, replace)
print ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment