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

Source Code for Module mosp_tools.node_finder

  1  #!/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3   
  4  """ Tool for finding node ids by clicking on a GUI map 
  5      - load a map into gui (libchamplain based) 
  6      - on click, next available way node is shown and information written to stderr 
  7      - map is not centered/zoomed, user has to move to map 
  8  """ 
  9   
 10  import champlain 
 11  import clutter 
 12  import math 
 13  import gobject 
 14   
 15  import select 
 16  import sys 
 17  sys.path.append("..") 
 18   
 19  from mosp.geo.utm import utm_to_latlong, latlong_to_utm 
 20  from mosp.geo import osm, utils 
 21   
 22  __author__ = "F. Ludwig" 
 23  __copyright__ = "(c) 2010-2011, DCSec, Leibniz Universitaet Hannover, Germany" 
 24  __license__ = "GPLv3" 
 25  __status__ = "unmaintained" 
 26   
 27   
 28  MARKER_SIZE = 10                                    #: size of the node marker 
 29  DEFAULT_MARKER_COLOR = [0.1,0.1,0.9,1.0]            #: color of the node marker 
 30  POSITION = [52.381790828929, 9.719464266585755]     #: position the map is centered on 
 31  SCREEN_SIZE = [800, 600]                            #: size of the window 
 32   
 33   
34 -class AnimatedMarker(champlain.Marker) :
35 """The AnimatedMarker will extend the champlain.Marker class"""
36 - def __init__(self,color=None) :
37 champlain.Marker.__init__(self) 38 39 if not color : 40 color = DEFAULT_MARKER_COLOR 41 42 # Cairo definition of the marker 43 bg = clutter.CairoTexture(MARKER_SIZE, MARKER_SIZE) 44 cr = bg.cairo_create() 45 #cr.set_source_rgb(0, 0, 0) 46 cr.arc(MARKER_SIZE / 2.0, MARKER_SIZE / 2.0, MARKER_SIZE / 2.0, 47 0, 2 * math.pi) 48 #cr.close_path() 49 cr.set_source_rgba(*color) 50 cr.fill() 51 self.add(bg)
52 #bg.set_anchor_point_from_gravity(clutter.GRAVITY_CENTER) 53 #bg.set_position(0, 0) 54 55
56 -class Finder(object):
57 """Map GUI visualization. 58 @author: F. Ludwig""" 59
60 - def update(self):
61 """Update GUI and stderr output.""" 62 s = select.select([sys.stdin], [], [], 0.0) 63 while s[0]: 64 t, id, x, y = [int(i) for i in sys.stdin.readline().split(' ')] 65 coords = utm_to_latlong(x, y, self.zone) 66 self.markers[id].set_position(coords[1], coords[0]) 67 print id, t, x, y 68 print coords 69 s = select.select([sys.stdin], [], [], 0.0) 70 71 gobject.timeout_add(200, self.update)
72 73
74 - def resize_actor(self, stage, box, flags):
75 """Resize actor.""" 76 self.actor.set_size(int(stage.get_width()), int(stage.get_height())) 77 print "\n\n\n\n\n\n", stage.get_allocation_box(), "\n\n\n\n\n\n\n\n\n"
78 79
80 - def main(self):
81 """Node finder main loads map and inits the GUI.""" 82 self.data = osm.OSMModel('../data/hannover2.osm') 83 self.data.initialize(self.data) 84 global markers 85 gobject.threads_init() 86 clutter.init() 87 stage = clutter.Stage(default=True) 88 self.actor = champlain.View() 89 layer = champlain.Layer() 90 self.marker = AnimatedMarker() 91 layer.add(self.marker) 92 93 stage.set_user_resizable(True) 94 stage.connect("allocation-changed", self.resize_actor) 95 stage.connect("button-press-event", self.button_pressed) 96 97 stage.set_size(*SCREEN_SIZE) 98 self.actor.set_size(*SCREEN_SIZE) 99 stage.add(self.actor) 100 101 layer.show() 102 self.actor.add_layer(layer) 103 104 # Finish initialising the map view 105 self.actor.set_property("zoom-level", 16) 106 self.actor.set_property("scroll-mode", champlain.SCROLL_MODE_KINETIC) 107 self.actor.center_on(*POSITION) 108 109 stage.show() 110 111 clutter.main()
112
113 - def button_pressed(self, stage, event):
114 """When mouse-button is pressed, find node that is closest to the 115 mouse-pointer.""" 116 coords = self.actor.get_coords_from_event(event) 117 coords_utm = latlong_to_utm(coords[1], coords[0]) 118 class Node: 119 x = coords_utm[0] 120 y = coords_utm[1]
121 nearest = self.data.way_nodes[0] 122 nearest_dist = utils.distance(Node, nearest) 123 for node in self.data.way_nodes: 124 if utils.distance(Node, node) < nearest_dist: 125 nearest = node 126 nearest_dist = utils.distance(Node, node) 127 128 print 129 print "==========KLICK==========" 130 print nearest, nearest_dist 131 print coords 132 self.marker.set_position(nearest.lat, nearest.lon)
133 134
135 -def main():
136 """Starts a libchamplain-based Finder (map GUI) and its main methods.""" 137 f = Finder() 138 f.main()
139 140 141 if __name__ == '__main__': 142 main() 143