Package mosp_tools :: Module analyze_map
[hide private]
[frames] | no frames]

Source Code for Module mosp_tools.analyze_map

  1  #!/bin/env python 
  2  """Tool for analyzing a osm map 
  3      - load a map and calculates statistics 
  4          - counts and differentiates partitions 
  5          - counts border nodes 
  6      - outputs OSM data "as is in simulator" to stderr 
  7        can be stored in file to analyse in JOSM 
  8      - outputs other stats to stdout 
  9  """ 
 10   
 11  import sys 
 12  sys.path.append("..")  
 13   
 14  from mosp.core import Simulation 
 15  from mosp.geo import osm 
 16  from mosp.geo import utm 
 17   
 18  __author__ = "B. Henne" 
 19  __contact__ = "henne@dcsec.uni-hannover.de" 
 20  __copyright__ = "(c) 2011, DCSec, Leibniz Universitaet Hannover, Germany" 
 21  __license__ = "GPLv3" 
 22   
 23           
24 -def count_neighbors(nodes):
25 """Feed in mosp.core.Simulation.nodes aka self.sim.nodes aka s.sim.nodes.""" 26 neighbor_number_stats = [ 0 for i in xrange(254) ] 27 for n in nodes: 28 neighbor_number_stats[len(n.neighbors)] += 1 29 for i in xrange(254): 30 if neighbor_number_stats[i] > 0: 31 print ' #neighbors=%2d count=%5d' % (i, neighbor_number_stats[i])
32 33
34 -def count_partitions(nodes):
35 """Feed in mosp.core.Simulation.nodes aka self.sim.nodes aka s.sim.nodes. 36 Returns (1) array containing index=nodes[i].id, value=partition number 37 and (2) number of partitions.""" 38 part = 0 39 remaining = len(nodes) 40 neighbors = [ 0 for i in xrange(remaining)] 41 while remaining > 0: 42 i = 0 43 while neighbors[i] != 0: 44 i += 1 45 start = nodes[i] 46 part += 1 47 todo = [ start ] 48 neighbors[start.id] = part 49 while len(todo) > 0: 50 node = todo.pop() 51 for n in node.neighbors: 52 if neighbors[n.id] == 0: 53 todo.append(n) 54 neighbors[n.id] = part 55 size = len([n for n in neighbors if n == part]) 56 print ' node %5d is part of partition %2d with %5d nodes' % (start.id, part, size) 57 remaining = len([n for n in neighbors if n == 0]) 58 return neighbors, part
59 60 67 68 76 77 write('<osm version=\'0.6\' generator=\'mosp:analyse_map.py\'>') 78 write(' <bounds minlat=\'%s\' minlon=\'%s\' maxlat=\'%s\' maxlon=\'%s\' origin=\'mosp:analyse_map.py\' />' % (geo.bounds['minlat'], geo.bounds['minlon'], geo.bounds['maxlat'], geo.bounds['maxlon'])) 79 for n in geo.way_nodes: 80 id = n.id+1 81 assert id != 0 82 write(' <node id=\'%s\' user=\'mosp\' uid=\'1337\' visible=\'true\' version=\'1\' lat=\'%s\' lon=\'%s\'>' % (id, n.lat, n.lon)) 83 write(' <tag k=\'mosp_node_id\' v=\'%s\' />' % n.id) 84 write(' <tag k=\'osm_node_id\' v=\'%s\' />' % n.osm_id) 85 write(' <tag k=\'from_list\' v=\'geo.way_nodes\' />') 86 if geo.out_of_bb(n): 87 write(' <tag k=\'out_of_bb\' v=\'yes\' />') 88 for k, v in n.tags.iteritems(): 89 write(' <tag k=\'%s\' v=%s />' % (k, quoteattr(escape(v)))) 90 if 'border' in n.tags: 91 write(' <tag k=\'exit\' v=\'yes\' />') 92 if partitions is not None: 93 write(' <tag k=\'partition\' v=\'%s\' />' % partitions[n.id]) 94 write(' </node>') 95 for w in geo.obj: 96 id = w.id+1 97 assert id != 0 98 write(' <way id=\'%s\' uid=\'1337\' user=\'mosp\' visible=\'true\' version=\'1\'>' % id) 99 for k, v in w.tags.iteritems(): 100 write(' <tag k=\'%s\' v=%s />' % (k, quoteattr(escape(v)))) 101 for n in w.nodes: 102 id = n.id+1 103 assert id != 0 104 write(' <nd ref=\'%s\' />' % id) 105 write(' </way>') 106 for n in geo.non_way_nodes: 107 id = n.id+900001 108 assert id != 0 109 write(' <node id=\'%s\' user=\'mosp\' uid=\'1337\' visible=\'true\' version=\'1\' lat=\'%s\' lon=\'%s\'>' % (id, n.lat, n.lon)) 110 write(' <tag k=\'mosp_node_id\' v=\'%s\' />' % n.id) 111 write(' <tag k=\'osm_node_id\' v=\'%s\' />' % n.osm_id) 112 write(' <tag k=\'from_list\' v=\'geo.non_way_nodes\' />') 113 if geo.out_of_bb(n): 114 write(' <tag k=\'out_of_bb\' v=\'yes\' />') 115 for k, v in n.tags.iteritems(): 116 write(' <tag k=\'%s\' v=%s />' % (k, quoteattr(escape(v)))) 117 write(' </node>') 118 write('</osm>') 119 120 121
122 -def main():
123 """This tool helps to analyze the map data after having been loaded into mosp. 124 This can help to find mistakes in maps and map usage in the simulator. 125 This tool can be used to show and remove partitioned graphs. To do this, 126 count the partitions, then output map as osm data, open it in JOSM, search 127 for partition:1, partition:2, ... open the original map and fix it.""" 128 129 map = '../data/kl0.osm' 130 s = Simulation(geo=osm.OSMModel(map), rel_speed=30, seed=200) 131 print '\n Analysing map %s' % map 132 print '\n number of nodes: %5d (all)' % len(s.geo.way_nodes) 133 print ' number of nodes: %5d (no border)' % len([n for n in s.geo.way_nodes if "border" not in n.tags]) 134 print ' number of nodes: %5d (border)' % len([n for n in s.geo.way_nodes if "border" in n.tags]) 135 print ' number of nodes: %5d (no out_of_bb)' % len([n for n in s.geo.way_nodes if not s.geo.out_of_bb(n)]) 136 print ' number of nodes: %5d (out_of_bb)' % len([n for n in s.geo.way_nodes if s.geo.out_of_bb(n)]) 137 print ' number of nodes: %5d (no border/out_of_bb)' % len([m for m in [n for n in s.geo.way_nodes if not s.geo.out_of_bb(n)] if "border" not in m.tags]) 138 print ' number of nodes: %5d (border/out_of_bb)' % len([m for m in [n for n in s.geo.way_nodes if s.geo.out_of_bb(n)] if "border" in m.tags]) 139 print '\n number of neighbors:' 140 count_neighbors(s.geo.way_nodes) 141 print '\n graph partitions:' 142 p, n = count_partitions(s.geo.way_nodes) 143 print '' 144 for i in xrange(1,n+1): 145 print_partition(s.geo.way_nodes, p, i) 146 print_osm(s.geo, p) 147 for node in s.geo.way_nodes: 148 print node.id, node.lon, node.lat, utm.utm_to_latlong(node.x, node.y, node.z)
149 150 151 if __name__ == '__main__': 152 main() 153