def test_decimal(self): DecimalModel.objects.create(n1=Decimal('-0.9'), n2=Decimal('0.6')) obj = DecimalModel.objects.annotate(n1_acos=ACos('n1'), n2_acos=ACos('n2')).first() self.assertIsInstance(obj.n1_acos, Decimal) self.assertIsInstance(obj.n2_acos, Decimal) self.assertAlmostEqual(obj.n1_acos, Decimal(math.acos(obj.n1))) self.assertAlmostEqual(obj.n2_acos, Decimal(math.acos(obj.n2)))
def test_float(self): FloatModel.objects.create(f1=-0.5, f2=0.33) obj = FloatModel.objects.annotate(f1_acos=ACos('f1'), f2_acos=ACos('f2')).first() self.assertIsInstance(obj.f1_acos, float) self.assertIsInstance(obj.f2_acos, float) self.assertAlmostEqual(obj.f1_acos, math.acos(obj.f1)) self.assertAlmostEqual(obj.f2_acos, math.acos(obj.f2))
def test_decimal(self): DecimalModel.objects.create(n1=Decimal("-0.9"), n2=Decimal("0.6")) obj = DecimalModel.objects.annotate(n1_acos=ACos("n1"), n2_acos=ACos("n2")).first() self.assertIsInstance(obj.n1_acos, Decimal) self.assertIsInstance(obj.n2_acos, Decimal) self.assertAlmostEqual(obj.n1_acos, Decimal(math.acos(obj.n1))) self.assertAlmostEqual(obj.n2_acos, Decimal(math.acos(obj.n2)))
def test_integer(self): IntegerModel.objects.create(small=0, normal=1, big=-1) obj = IntegerModel.objects.annotate( small_acos=ACos('small'), normal_acos=ACos('normal'), big_acos=ACos('big'), ).first() self.assertIsInstance(obj.small_acos, float) self.assertIsInstance(obj.normal_acos, float) self.assertIsInstance(obj.big_acos, float) self.assertAlmostEqual(obj.small_acos, math.acos(obj.small)) self.assertAlmostEqual(obj.normal_acos, math.acos(obj.normal)) self.assertAlmostEqual(obj.big_acos, math.acos(obj.big))
def GetDistance(point_latitude, point_longitude): # Calculate distance. See https://www.thutat.com/web/en/programming-and-tech-stuff/ # web-programming/postgres-query-with-gps-distance-calculations-without-postgis/ distance = ( ACos( Sin(Radians(F("location_lat"))) * Sin(Radians(point_latitude)) + Cos(Radians(F("location_lat"))) * Cos(Radians(point_latitude)) * Cos(Radians(F("location_lon") - point_longitude)) ) * 6371 * 1000 ) return distance
def with_frames(self): frames_asc = Frame.objects.filter(sighting=OuterRef('id')).order_by('timestamp') frames_desc = Frame.objects.filter(sighting=OuterRef('id')).order_by('-timestamp') return self.prefetch_related( Prefetch( 'frames', queryset=Frame.objects.with_flight_time(), ) ).annotate( frame_count=Count('frames'), fa=Subquery(frames_asc.values('altitude')[:1]), la=Subquery(frames_desc.values('altitude')[:1]), fz=Subquery(frames_asc.values('azimuth')[:1]), lz=Subquery(frames_desc.values('azimuth')[:1]), arc_length=Degrees( ACos( Sin(Radians(F('fa'))) * Sin(Radians(F('la'))) + Cos(Radians(F('fa'))) * Cos(Radians(F('la'))) * Cos(Radians(F('fz') - F('lz'))) ) ), )
def get_stations(self, lat, lng, filter1, filter2, filter4, filter7): queryset = Stations.objects.annotate(distance=(6371 * ACos( Cos(Radians(float(lat))) * Cos(Radians('lat')) * Cos(Radians('lng') - Radians(float(lng))) + Sin(Radians(float(lat))) * Sin(Radians('lat'))))) queryset = queryset.filter(distance__lte=10) querysetUnion = None if filter1 is not None or filter1 is True: queryset_filter_1 = queryset.filter( Q(chgerType="01") | Q(chgerType="03") | Q(chgerType="05") | Q(chgerType="06")) querysetUnion = querysetUnion.union( queryset_filter_1 ) if querysetUnion is not None else queryset_filter_1 if filter2 is not None or filter2 is True: queryset_filter_2 = queryset.filter(chgerType="02") querysetUnion = querysetUnion.union( queryset_filter_2 ) if querysetUnion is not None else queryset_filter_2 if filter4 is not None or filter4 is True: queryset_filter_4 = queryset.filter( Q(chgerType="04") | Q(chgerType="05") | Q(chgerType="06")) querysetUnion = querysetUnion.union( queryset_filter_4 ) if querysetUnion is not None else queryset_filter_4 if filter7 is not None or filter7 is True: queryset_filter_7 = queryset.filter( Q(chgerType="03") | Q(chgerType="06") | Q(chgerType="07")) querysetUnion = querysetUnion.union( queryset_filter_7 ) if querysetUnion is not None else queryset_filter_7 return queryset if querysetUnion is None else querysetUnion
def test_null(self): IntegerModel.objects.create() obj = IntegerModel.objects.annotate(null_acos=ACos('normal')).first() self.assertIsNone(obj.null_acos)