def show_nearby_devices(lat, lon, r, n): """ Return nearby devices. Arguments: lat - Latitude in degrees lon - Longitude in degrees r - Search radius in km n - output list length limit """ user_location = Location(lon=lon, lat=lat).to_wkt() distance = ST_Distance_Sphere(Device.location_wkb, user_location) / 1000 direction = func.degrees(func.ST_Azimuth(user_location, Device.location_wkb)) devices = session.query(Device, direction.label('direction'), distance.label('distance')).filter(distance < r).order_by(distance.asc()).limit(n).all() if not len(devices): raise NoResultFound return list(map(lambda device: {'device': device.Device.as_dict(), 'direction': round(device.direction, 2), 'distance': round(device.distance, 3)}, devices))
def trajectDirection(self): """traject direction of previous location and current location""" azimuth = func.ST_Azimuth(func.lag(self.rawlocation).over(order_by=(self.device_info_serial, self.date_time,)), self.rawlocation) return func.round(cast(func.degrees(azimuth), Numeric()), 2).label('tdirection')