def distance(cls, latitude, longitude): earth_radius = 6371 pi_converter = func.pi() / 180 dlat = (latitude - cls.latitude) * pi_converter dlon = (longitude - cls.longitude) * pi_converter lat1 = (cls.latitude) * pi_converter lat2 = (latitude) * pi_converter haversine = func.sin(dlat/2) * func.sin(dlat/2) + func.sin(dlon/2) * func.sin(dlon/2) * func.cos(lat1) * func.cos(lat2) return earth_radius * 2 * func.atan2(func.sqrt(haversine), func.sqrt(1-haversine))
def haversine(cls, other): r = 6371 lat1, lon1 = cls.latitude_pos, cls.longitude_pos lat2, lon2 = other.latitude_pos, other.longitude_pos import math dlat = func.radians(lat2 - lat1) dlon = func.radians(lon2 - lon1) a = func.sin(dlat/2) * func.sin(dlat/2) + func.cos(func.radians(lat1)) \ * func.cos(math.radians(lat2)) * func.sin(dlon/2) * func.sin(dlon/2) c = 2 * func.atan2(func.sqrt(a), func.sqrt(1 - a)) d = r * c return d
def distance(pt1, pt2): lat1, lon1 = pt1 lat2, lon2 = pt2 R = 3961 dlat = func.radians(lat2 - lat1) dlon = func.radians(lon2 - lon1) a = func.power(func.sin(dlat / 2), 2) + func.cos(func.radians(lat1)) * func.cos( func.radians(lat2) ) * func.power(func.sin(dlon / 2), 2) c = 2 * func.atan2(func.sqrt(a), func.sqrt(1 - a)) d = R * c return d
def in_range(cls, latitude, longitude, radius): """Check if user in range (radius in meters).""" R = 6373.0 # approximate radius of earth in km from sqlalchemy import func lat1 = func.radians(cls.latitude) lon1 = func.radians(cls.longitude) lat2 = func.radians(latitude) lon2 = func.radians(longitude) dlon = lon2 - lon1 dlat = lat2 - lat1 a = func.pow(func.sin(dlat / 2), 2) + func.cos(lat1) * func.cos(lat2) \ * func.pow(func.sin(dlon / 2), 2) c = R * 2 * func.atan2(func.sqrt(a), func.sqrt(1 - a)) print(c) return c
def get_distance(self, user_lat, user_lng): pi180 = pi / 180 lat1 = float(user_lat) * pi180 lng1 = float(user_lng) * pi180 lat2 = Location.lat * pi180 lng2 = Location.lng * pi180 r = 6371 dlat = lat2 - lat1 dlng = lng2 - lng1 a = func.sin(dlat / 2) * func.sin(dlat / 2) + func.cos( lat1) * func.cos(lat2) * func.sin(dlng / 2) * func.sin(dlng / 2) c = 2 * func.atan2(func.sqrt(a), func.sqrt(1 - a)) km = r * c return km
def convert_vehicle(nyc3dcars_session, labeler_vehicle): """Converts the vehicle from Labeler format to NYC3DCars format.""" photo, lat, lon, alt = nyc3dcars_session.query( Photo, func.ST_Y(Photo.lla), func.ST_X(Photo.lla), func.ST_Z(Photo.lla)) \ .filter_by(id=labeler_vehicle.revision.annotation.pid) \ .options(joinedload('dataset')) \ .one() left = labeler_vehicle.x1 right = labeler_vehicle.x2 top = labeler_vehicle.y1 bottom = labeler_vehicle.y2 camera_lla = numpy.array([[lat], [lon], [alt]]) camera_enu = pygeo.LLAToENU(camera_lla.T).reshape((3, 3)) dataset_correction = numpy.array([ [photo.dataset.t1], [photo.dataset.t2], [photo.dataset.t3], ]) camera_rotation = numpy.array([ [photo.r11, photo.r12, photo.r13], [photo.r21, photo.r22, photo.r23], [photo.r31, photo.r32, photo.r33], ]) camera_up = camera_enu.T.dot( camera_rotation.T.dot(numpy.array([[0], [1], [0]]))) offset = numpy.array([[-labeler_vehicle.x], [-labeler_vehicle.z], [0]]) camera_offset = camera_up * \ labeler_vehicle.revision.cameraheight / camera_up[2] total_offset = offset - camera_offset ecef_camera = pygeo.LLAToECEF(camera_lla.T).T ecef_camera += dataset_correction ecef_total_offset = camera_enu.dot(total_offset) vehicle_ecef = ecef_camera + ecef_total_offset vehicle_type = labeler_vehicle.type model = nyc3dcars_session.query(VehicleType) \ .filter_by(label=vehicle_type) \ .one() vehicle_lla = pygeo.ECEFToLLA(vehicle_ecef.T).T theta = math.radians(-labeler_vehicle.theta) mlength = model.length mwidth = model.width car_a = -math.sin(theta) * 0.3048 * \ mlength / 2 + math.cos(theta) * 0.3048 * mwidth / 2 car_b = math.cos(theta) * 0.3048 * mlength / \ 2 + math.sin(theta) * 0.3048 * mwidth / 2 car_c = math.sin(theta) * 0.3048 * mlength / \ 2 + math.cos(theta) * 0.3048 * mwidth / 2 car_d = -math.cos(theta) * 0.3048 * \ mlength / 2 + math.sin(theta) * 0.3048 * mwidth / 2 car_corner_offset1 = camera_enu.dot(numpy.array([[car_a], [car_b], [0]])) car_corner_offset2 = camera_enu.dot(numpy.array([[car_c], [car_d], [0]])) car_corner1 = pygeo.ECEFToLLA( (vehicle_ecef + car_corner_offset1).T).T.flatten() car_corner2 = pygeo.ECEFToLLA( (vehicle_ecef - car_corner_offset1).T).T.flatten() car_corner3 = pygeo.ECEFToLLA( (vehicle_ecef + car_corner_offset2).T).T.flatten() car_corner4 = pygeo.ECEFToLLA( (vehicle_ecef - car_corner_offset2).T).T.flatten() pg_corner1 = func.ST_SetSRID( func.ST_MakePoint(car_corner1[1], car_corner1[0], car_corner1[2]), 4326) pg_corner2 = func.ST_SetSRID( func.ST_MakePoint(car_corner2[1], car_corner2[0], car_corner2[2]), 4326) pg_corner3 = func.ST_SetSRID( func.ST_MakePoint(car_corner3[1], car_corner3[0], car_corner3[2]), 4326) pg_corner4 = func.ST_SetSRID( func.ST_MakePoint(car_corner4[1], car_corner4[0], car_corner4[2]), 4326) collection = func.ST_Collect(pg_corner1, pg_corner2) collection = func.ST_Collect(collection, pg_corner3) collection = func.ST_Collect(collection, pg_corner4) car_polygon = func.ST_ConvexHull(collection) camera_ecef = pygeo.LLAToECEF(camera_lla.T).T vehicle_ecef = pygeo.LLAToECEF(vehicle_lla.T).T diff = camera_ecef - vehicle_ecef normalized = diff / numpy.linalg.norm(diff) vehicle_enu = pygeo.LLAToENU(vehicle_lla.T).reshape((3, 3)) rotated = vehicle_enu.T.dot(normalized) theta = func.acos(rotated[2][0]) view_phi = func.atan2(rotated[1][0], rotated[0][0]) vehicle_phi = math.radians(-labeler_vehicle.theta) phi = vehicle_phi - view_phi out = nyc3dcars_session.query( theta.label('theta'), phi.label('phi')) \ .one() out.phi = ((out.phi + math.pi) % (2 * math.pi)) - math.pi out.theta = ((out.theta + math.pi) % (2 * math.pi)) - math.pi view_phi = out.phi view_theta = out.theta left = labeler_vehicle.x1 right = labeler_vehicle.x2 top = labeler_vehicle.y1 bottom = labeler_vehicle.y2 for bbox_session in labeler_vehicle.bbox_sessions: if not bbox_session.user.trust: continue print(( bbox_session.user.username, labeler_vehicle.revision.annotation.pid )) left = bbox_session.x1 right = bbox_session.x2 top = bbox_session.y1 bottom = bbox_session.y2 break occlusions = [ occlusion.category for occlusion in labeler_vehicle.occlusionrankings if occlusion.occlusion_session.user.trust and occlusion.category != 5 ] if len(occlusions) == 0: return pg_lla = func.ST_SetSRID( func.ST_MakePoint(vehicle_lla[1][0], vehicle_lla[0][0], vehicle_lla[2][0]), 4326) nyc3dcars_vehicle = Vehicle( id=labeler_vehicle.id, pid=photo.id, x=labeler_vehicle.x, z=labeler_vehicle.z, theta=labeler_vehicle.theta, x1=left, x2=right, y1=top, y2=bottom, type_id=model.id, occlusion=min(occlusions), geom=car_polygon, lla=pg_lla, view_theta=view_theta, view_phi=view_phi, cropped=labeler_vehicle.cropped, ) nyc3dcars_session.add(nyc3dcars_vehicle)
def convert_vehicle(nyc3dcars_session, labeler_vehicle): """Converts the vehicle from Labeler format to NYC3DCars format.""" photo, lat, lon, alt = nyc3dcars_session.query( Photo, func.ST_Y(Photo.lla), func.ST_X(Photo.lla), func.ST_Z(Photo.lla)) \ .filter_by(id=labeler_vehicle.revision.annotation.pid) \ .options(joinedload('dataset')) \ .one() left = labeler_vehicle.x1 right = labeler_vehicle.x2 top = labeler_vehicle.y1 bottom = labeler_vehicle.y2 camera_lla = numpy.array([[lat], [lon], [alt]]) camera_enu = pygeo.LLAToENU(camera_lla.T).reshape((3, 3)) dataset_correction = numpy.array([ [photo.dataset.t1], [photo.dataset.t2], [photo.dataset.t3], ]) camera_rotation = numpy.array([ [photo.r11, photo.r12, photo.r13], [photo.r21, photo.r22, photo.r23], [photo.r31, photo.r32, photo.r33], ]) camera_up = camera_enu.T.dot( camera_rotation.T.dot(numpy.array([[0], [1], [0]]))) offset = numpy.array([[-labeler_vehicle.x], [-labeler_vehicle.z], [0]]) camera_offset = camera_up * \ labeler_vehicle.revision.cameraheight / camera_up[2] total_offset = offset - camera_offset ecef_camera = pygeo.LLAToECEF(camera_lla.T).T ecef_camera += dataset_correction ecef_total_offset = camera_enu.dot(total_offset) vehicle_ecef = ecef_camera + ecef_total_offset vehicle_type = labeler_vehicle.type model = nyc3dcars_session.query(VehicleType) \ .filter_by(label=vehicle_type) \ .one() vehicle_lla = pygeo.ECEFToLLA(vehicle_ecef.T).T theta = math.radians(-labeler_vehicle.theta) mlength = model.length mwidth = model.width car_a = -math.sin(theta) * 0.3048 * \ mlength / 2 + math.cos(theta) * 0.3048 * mwidth / 2 car_b = math.cos(theta) * 0.3048 * mlength / \ 2 + math.sin(theta) * 0.3048 * mwidth / 2 car_c = math.sin(theta) * 0.3048 * mlength / \ 2 + math.cos(theta) * 0.3048 * mwidth / 2 car_d = -math.cos(theta) * 0.3048 * \ mlength / 2 + math.sin(theta) * 0.3048 * mwidth / 2 car_corner_offset1 = camera_enu.dot(numpy.array([[car_a], [car_b], [0]])) car_corner_offset2 = camera_enu.dot(numpy.array([[car_c], [car_d], [0]])) car_corner1 = pygeo.ECEFToLLA( (vehicle_ecef + car_corner_offset1).T).T.flatten() car_corner2 = pygeo.ECEFToLLA( (vehicle_ecef - car_corner_offset1).T).T.flatten() car_corner3 = pygeo.ECEFToLLA( (vehicle_ecef + car_corner_offset2).T).T.flatten() car_corner4 = pygeo.ECEFToLLA( (vehicle_ecef - car_corner_offset2).T).T.flatten() pg_corner1 = func.ST_SetSRID( func.ST_MakePoint(car_corner1[1], car_corner1[0], car_corner1[2]), 4326) pg_corner2 = func.ST_SetSRID( func.ST_MakePoint(car_corner2[1], car_corner2[0], car_corner2[2]), 4326) pg_corner3 = func.ST_SetSRID( func.ST_MakePoint(car_corner3[1], car_corner3[0], car_corner3[2]), 4326) pg_corner4 = func.ST_SetSRID( func.ST_MakePoint(car_corner4[1], car_corner4[0], car_corner4[2]), 4326) collection = func.ST_Collect(pg_corner1, pg_corner2) collection = func.ST_Collect(collection, pg_corner3) collection = func.ST_Collect(collection, pg_corner4) car_polygon = func.ST_ConvexHull(collection) camera_ecef = pygeo.LLAToECEF(camera_lla.T).T vehicle_ecef = pygeo.LLAToECEF(vehicle_lla.T).T diff = camera_ecef - vehicle_ecef normalized = diff / numpy.linalg.norm(diff) vehicle_enu = pygeo.LLAToENU(vehicle_lla.T).reshape((3, 3)) rotated = vehicle_enu.T.dot(normalized) theta = func.acos(rotated[2][0]) view_phi = func.atan2(rotated[1][0], rotated[0][0]) vehicle_phi = math.radians(-labeler_vehicle.theta) phi = vehicle_phi - view_phi out = nyc3dcars_session.query( theta.label('theta'), phi.label('phi')) \ .one() out.phi = ((out.phi + math.pi) % (2 * math.pi)) - math.pi out.theta = ((out.theta + math.pi) % (2 * math.pi)) - math.pi view_phi = out.phi view_theta = out.theta left = labeler_vehicle.x1 right = labeler_vehicle.x2 top = labeler_vehicle.y1 bottom = labeler_vehicle.y2 for bbox_session in labeler_vehicle.bbox_sessions: if not bbox_session.user.trust: continue print((bbox_session.user.username, labeler_vehicle.revision.annotation.pid)) left = bbox_session.x1 right = bbox_session.x2 top = bbox_session.y1 bottom = bbox_session.y2 break occlusions = [ occlusion.category for occlusion in labeler_vehicle.occlusionrankings if occlusion.occlusion_session.user.trust and occlusion.category != 5 ] if len(occlusions) == 0: return pg_lla = func.ST_SetSRID( func.ST_MakePoint(vehicle_lla[1][0], vehicle_lla[0][0], vehicle_lla[2][0]), 4326) nyc3dcars_vehicle = Vehicle( id=labeler_vehicle.id, pid=photo.id, x=labeler_vehicle.x, z=labeler_vehicle.z, theta=labeler_vehicle.theta, x1=left, x2=right, y1=top, y2=bottom, type_id=model.id, occlusion=min(occlusions), geom=car_polygon, lla=pg_lla, view_theta=view_theta, view_phi=view_phi, cropped=labeler_vehicle.cropped, ) nyc3dcars_session.add(nyc3dcars_vehicle)