Created
August 23, 2012 00:28
-
-
Save mapmeld/3430822 to your computer and use it in GitHub Desktop.
OSM-Network-Build.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# OSM Points and Streets | |
# Build from nodes and ways | |
# For each way, add them to Streets | |
## Flag each node with the Street's ID | |
## If Street shares a node with a previous Street, build a connectsto relation between the two | |
# Optional firstToAdd parameter lets you pick up networking where you left off | |
firstToAdd = None | |
# firstToAdd = "featherdr" | |
# Opening the file | |
import urllib, urllib2 | |
osmfile = open('macon.osm', 'r') | |
nodes = { } | |
inway = False | |
wayname = "" | |
waynodes = [] | |
addedways = [] | |
wayids = {} | |
isHighway = False | |
for line in osmfile: | |
# Add Streets | |
if(line.find('<way') > -1): | |
inway = True | |
elif(inway == True): | |
if(line.find('<nd ref') > -1): | |
# add this node id | |
id = line[ line.find('ref="') + 5 : len(line) ] | |
id = id[ 0 : id.find('"') ] | |
waynodes.append( id ) | |
elif(line.find('k="highway"') > -1): | |
isHighway = True | |
elif(line.find('k="name"') > -1): | |
# found the road name | |
wayname = line[ line.find('v="') + 3 : len(line) ] | |
wayname = wayname[ 0 : wayname.find('"') ] | |
# use database's preferred parsing of street names | |
wayname = wayname.lower().replace(' ','') | |
elif(line.find('</way>') > -1): | |
# only care about roads with names | |
if(wayname != "" and isHighway == True): | |
# check if way needs to be added to the database | |
if(not (wayname in addedways)): | |
print wayname | |
addedways.append( wayname ) | |
values = { | |
"name": wayname | |
} | |
data = urllib.urlencode(values) | |
# store final url: /streets/ID | |
if((firstToAdd is None) or (firstToAdd == wayname)): | |
wayids[ wayname ] = urllib2.urlopen(urllib2.Request('http://localhost:3000/streets', data)).geturl().split('streets/')[1] | |
firstToAdd = None | |
else: | |
# retrieve this way ID by name | |
wayids[ wayname ] = urllib2.urlopen('http://localhost:3000/streetname/' + wayname).read() | |
# now add relationships to nodes in the way | |
for node in waynodes: | |
if(nodes.has_key(node)): | |
for streetid in nodes[node]: | |
# bidirectional | |
if(streetid == wayids[wayname]): | |
continue | |
if(firstToAdd is None): | |
values = { | |
"streetid": wayids[wayname] | |
} | |
data = urllib.urlencode(values) | |
urllib2.urlopen(urllib2.Request('http://localhost:3000/streets/' + streetid + '/follow', data)).read() | |
values = { | |
"streetid": streetid | |
} | |
data = urllib.urlencode(values) | |
urllib2.urlopen(urllib2.Request('http://localhost:3000/streets/' + wayids[wayname] + '/follow', data)).read() | |
print "connection made" | |
nodes[node].append( wayids[ wayname ] ) | |
else: | |
nodes[node] = [ wayids[wayname] ] | |
# reset way defaults | |
wayname = "" | |
waynodes = [] | |
isHighway = False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment