Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. # OSM Points and Streets
  2. # Build from nodes and ways
  3.  
  4. # For each node, add it to node-ids with lat and lng
  5. # For each way, add them to Streets
  6. ## If Street has a node with a previous street, build a connectsto relation between the two
  7.  
  8. # 0) Opening the file
  9. from py2neo import Graph, Node, Relationship
  10. # from lxml import etree
  11.  
  12. g = Graph(user="neo4j", password="Swift")
  13.  
  14.  
  15. osmfile = open('bangkok_thailand.osm', 'r')
  16.  
  17. inway = False
  18. wayname = ""
  19. waynodes = []
  20. addedways = []
  21. wayids = {}
  22. knownnodes = {}
  23. isHighway = False
  24. osmwayid = ""
  25.  
  26. for line in osmfile:
  27.  
  28. # 2) Add Streets
  29. if(line.find('<way') > -1):
  30. inway = True
  31. osmwayid = line[ line.find('id=') + 4 : len(line) ]
  32. osmwayid = osmwayid[ 0 : osmwayid.find('"') ]
  33. #print(osmwayid)
  34.  
  35. elif(inway == True):
  36. if(line.find('<nd ref') > -1):
  37. # add this node id
  38. id = line[ line.find('ref="') + 5 : len(line) ]
  39. id = id[ 0 : id.find('"') ]
  40. waynodes.append( id )
  41.  
  42. elif(line.find('k="highway"') > -1):
  43. isHighway = True
  44.  
  45. elif(line.find('k="name"') > -1):
  46. # found the road name
  47. wayname = line[ line.find('v="') + 3 : len(line) ]
  48. wayname = wayname[ 0 : wayname.find('"') ]
  49. # use database's preferred parsing of street names
  50. wayname = wayname.lower().replace(' ','')
  51.  
  52. elif(line.find('</way>') > -1):
  53. inway = False
  54.  
  55. # only add a road (highway=*)
  56. if(wayname != "" and isHighway == True):
  57. # check if way needs to be added to the database
  58. street = g.find_one("Street", "nameslug", wayname)
  59. if(street == None):
  60. print('add ' + wayname)
  61.  
  62. # create street in database
  63. street = Node("Street", nodeids=waynodes, nameslug=wayname)
  64. tx = g.begin()
  65. tx.create(street)
  66. tx.commit()
  67.  
  68. else:
  69. # know this street, add the way id
  70. street['nodeids'] = street['nodeids'] + waynodes
  71. street.push()
  72.  
  73. # now add relationships to nodes in the way
  74. for node in waynodes:
  75. if(node in knownnodes):
  76. streetnames = g.run("MATCH (n:Street) WHERE {nodeid} IN n.nodeids RETURN n.nameslug LIMIT 25", nodeid=node)
  77. for streetrecord in streetnames:
  78. streetname = streetrecord['n.nameslug']
  79. if streetname == wayname:
  80. continue
  81. print('matching ' + streetname + ' and ' + wayname)
  82. street2 = g.find_one("Street", "nameslug", streetname)
  83. if street2 is None:
  84. continue
  85. intersect = Relationship(street, "MEETS", street2)
  86. intersect2 = Relationship(street2, "MEETS", street)
  87. tx = g.begin()
  88. tx.create(intersect)
  89. tx.create(intersect2)
  90. tx.commit()
  91.  
  92. print("connection made")
  93.  
  94. knownnodes[node] = wayname
  95.  
  96. # reset way defaults
  97. wayname = ""
  98. waynodes = []
  99. isHighway = False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement