def __init__(self, mapfilename, minlon, maxlon, minlat, maxlat, dotsize=30): """Initialize the heatmap generation object.""" # map(ping) configuration self.mapfile = mapfilename #: filename of underlay map from osm export self.minlon = minlon #: min longitude from osm png export self.maxlon = maxlon #: max longitude from osm png export self.minlat = minlat #: min latitude from osm png export self.maxlat = maxlat #: max latitude from osm png export # calculate values self.x_min, self.y_min = latlong_to_utm(self.minlon, self.minlat) self.x_max, self.y_max = latlong_to_utm(self.maxlon, self.maxlat) self.width = abs(self.x_max - self.x_min) self.height = abs(self.y_max - self.y_min) # setup fonts font_path = "/usr/share/fonts/" self.sans18 = ImageFont.truetype (font_path+'dejavu/DejaVuSansMono-Bold.ttf', 18) self.sans12 = ImageFont.truetype (font_path+'dejavu/DejaVuSansMono-Bold.ttf', 12) # setup images: dot self.dotsize = dotsize self.dot = self.__dotImage(self.dotsize) # setup images: backgroud map self.map = Image.open(self.mapfile) self.mapsize = self.map.size if self.map.mode != 'RGBA': self.map = self.map.convert('RGBA') draw = ImageDraw.Draw(self.map) draw.text((self.mapsize[0]-300, self.mapsize[1]-30), '(c) OpenStreetMap contributors, CC-BY-SA', font=self.sans12, fill=ImageColor.colormap['darkgrey']) draw.rectangle([(16,10),(80,30)], fill=ImageColor.colormap['lightgray']) del draw
def location(self, id='', lat='', lon='', acc='', hmac=''): """Handle incoming location from $HOSTNAME:$PORT/location?$PARAMS.""" time_received = time.time() msg_sign = self.sign.copy() msg_sign.update(id + lat + lon) msg_hash = msg_sign.hexdigest() # check HMAC if msg_hash != hmac: print 'HMAC hashes do not match!' print 'hash of message', msg_hash print 'hash received: ', hmac return '<h1>Error!</h1>' try: # extract values from received strings id_value = int(id) lat_value = float(lat) lon_value = float(lon) x, y = utm.latlong_to_utm(lon_value, lat_value) acc_value = float(acc) if acc_value > MIN_ACCURACY: print 'Received data with insufficient accuracy of {:f}. Minimal accuracy is {:d}'.format(acc_value, MIN_ACCURACY) return '<h1>Not accurate enough!</h1>' if (x - acc_value < self.min_x or x + acc_value > self.max_x or y - acc_value < self.min_y or y + acc_value > self.max_y): print 'Received data with out of bounds coordinates!' print id + ' ' +lat + ' ' +lon + ' ' + acc self.conn.send([id_value, None, None, x, y, time_received]) #self.conn.send([id_value, None, None, x, y, time_received, lat_value, lon_value]) return '<h1>Out of bounds!</h1>' except ValueError: # some value was not well formatted...ignore message print 'Received invalid data!' return '<h1>Values not well formatted!</h1>' # send data to simulation if self.free_move_only: self.conn.send([id_value, None, None, x, y, time_received]) else: match = self.fuzzy_map_match(id_value, x, y, acc_value, time_received) if match is not None: self.conn.send(match) else: self.conn.send([id_value, None, None, x, y, time_received]) #self.conn.send([id_value, None, None, x, y, time_received, lat_value, lon_value]) # save received coordinates if id not in self.received_points: self.received_points[id_value] = [self.Point(x, y, time_received)] else: self.received_points[id_value].append(self.Point(x, y, time_received)) while len(self.received_points[id_value]) > LOCATION_CACHE_SIZE: del sorted(self.received_points[id_value], key=lambda p: p.time_received)[0] print 'Received valid data: ID ' + id + ', lat ' +lat + ', lon ' +lon + ', acc ' + acc + ', at ' + str(time_received) return '<h1>Correct!</h1>'
def test_latlong_to_utm(self): """Tests latlong_to_utm() coordinates tested against: http://home.hiwaay.net/~taylorc/toolbox/geography/geoutm.html""" coords = utm.latlong_to_utm(13.73, 51.03, 33) # , zone, [0, 0]) self.assertEqual(round_utm_coord(coords[0]), 410943.61) self.assertEqual(round_utm_coord(coords[1]), 5653928.43)
def test_utm_latlong(self): """Test utm to latlong and latlong to utm by trying 100 coordinates in both functions. What about southern hemisphere?!??""" for lat in xrange(10): for lon in xrange(10): zone = utm.long_to_zone(lon) coords = utm.latlong_to_utm(lat, lon, zone) coords = utm.utm_to_latlong(coords[0], coords[1], zone, False) self.assertEqual(round_utm_coord(coords[0]), lat) self.assertEqual(round_utm_coord(coords[1]), lon)
def button_pressed(self, stage, event): """When mouse-button is pressed, find node that is closest to the mouse-pointer.""" coords = self.actor.get_coords_from_event(event) coords_utm = latlong_to_utm(coords[1], coords[0]) class Node: x = coords_utm[0] y = coords_utm[1] nearest = self.data.way_nodes[0] nearest_dist = utils.distance(Node, nearest) for node in self.data.way_nodes: if utils.distance(Node, node) < nearest_dist: nearest = node nearest_dist = utils.distance(Node, node) print print "==========KLICK==========" print nearest, nearest_dist print coords self.marker.set_position(nearest.lat, nearest.lon)