Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@vlandham
Created October 19, 2013 03:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vlandham/7051466 to your computer and use it in GitHub Desktop.
Save vlandham/7051466 to your computer and use it in GitHub Desktop.
from shapely.geometry import mapping, shape
from fiona import collection
import re
shapefile = "seattle.imposm-shapefiles/seattle.osm-roads.shp"
def street_direction(street):
dr = ''
if street:
street = street.lower()
if street.find('north'):
dr = 'N'
elif street.find('south'):
dr = 'S'
elif street.find('east'):
dr = 'E'
elif street.find('west'):
dr = 'W'
elif street.find('northeast'):
dr = 'NE'
return dr
def shorten_direction(direction):
dr = direction.strip()
if dr == 'NORTH':
dr = 'N'
elif dr == 'SOUTH':
dr = 'S'
elif dr == 'EAST':
dr = 'E'
elif dr == 'WEST':
dr = 'W'
elif dr == 'NORTHEAST':
dr = 'NE'
elif dr == 'NORTHWEST':
dr = 'NW'
elif dr == 'SOUTHEAST':
dr = 'SE'
elif dr == 'SOUTHWEST':
dr = 'SW'
return dr
def street_info(street):
# print street
direction = ''
direction_type = 'other'
str_type = 'name'
str_num = 0
if not street:
# print 'no name'
ret = {'direction': direction, 'dir_type':direction_type, 'str_type': str_type, 'str_num':str_num}
return ret
front_re = "^(N|NORTH|S|SOUTH|E|EAST|W|WEST|NE|NORTHEAST|NW|NORTHWEST|SE|SOUTHEAST|SW|SOUTHWEST)\s"
back_re = "\s(N|NORTH|S|SOUTH|E|EAST|W|WEST|NE|NORTHEAST|NW|NORTHWEST|SE|SOUTHEAST|SW|SOUTHWEST)$"
m = re.search(front_re, street.upper())
if m:
direction = shorten_direction(m.groups(1)[0])
direction_type = 'front'
m = re.search(back_re, street.upper())
if m:
direction = shorten_direction(m.groups(1)[0])
direction_type = 'back'
m = re.search('\D*?(\d+)\D*?$', street)
if m:
str_type = 'number'
str_num = int(m.groups(1)[0])
ret = {'direction': direction, 'dir_type':direction_type, 'str_type': str_type, 'str_num':str_num}
return ret
with collection(shapefile, "r") as input:
schema = input.schema.copy()
meta = input.meta.copy()
meta['schema']['properties']['direction'.encode("utf-8")] = 'str:6'
meta['schema']['properties']['dir_type'] = 'str:8'
meta['schema']['properties']['str_type'] = 'str:29'
meta['schema']['properties']['str_num'] = 'int:6'
print meta['schema']
with collection("roads.shp", "w", **meta) as output:
for p in input:
n = p.copy()
attrs = street_info(p['properties']['name'])
# print attrs
if n['properties']:
# print attrs
n['properties']['direction'] = attrs['direction']
n['properties']['dir_type'] = attrs['dir_type']
n['properties']['str_type'] = attrs['str_type']
n['properties']['str_num'] = attrs['str_num']
else:
# print 'no props'
print p
output.write(n)
# print p['properties']['name']
# print shape(p['geometry']).length
# print shape(p['geometry']).geom_type
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment