models.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import math
  2. from datetime import datetime, timedelta
  3. from django.db import models
  4. from django.contrib.gis.db.models.fields import PointField
  5. from django.contrib.gis.measure import D
  6. from colorfield.fields import ColorField
  7. class Marker(models.Model):
  8. timestamp = models.DateTimeField(unique=True)
  9. location = PointField(dim=2)
  10. alt = models.FloatField("Altitude")
  11. hdop = models.IntegerField(null=True, blank=True)
  12. speed = models.FloatField("Speed in km/h", null=True, blank=True)
  13. class Meta:
  14. ordering = ('timestamp',)
  15. class Trip(models.Model):
  16. startTime = models.DateTimeField()
  17. endTime = models.DateTimeField()
  18. name = models.CharField(max_length=255)
  19. description = models.TextField(null=True, blank=True)
  20. color = ColorField()
  21. distance = models.FloatField("Distance in m", default=0)
  22. topSpeed = models.FloatField("Top speed in km/h", default=0)
  23. avgSpeed = models.FloatField("Average speed in km/h", default=0)
  24. ascendHeight = models.FloatField("Ascend height in m", default=0)
  25. descendHeight = models.FloatField("Descend height in m", default=0)
  26. movementTime = models.DurationField("Movement time", default=timedelta(0))
  27. path = models.BinaryField("Path", default=bytes())
  28. class Meta:
  29. ordering = ('startTime',)
  30. class CensoredLocation(models.Model):
  31. location = PointField(dim=2)
  32. radius = models.IntegerField(default=800)
  33. name = models.CharField(max_length=255)
  34. def delete_markers(self) -> int:
  35. matches = Marker.objects.filter(location__distance_lt=(
  36. self.location,
  37. distance_to_decimal_degrees(D(m=self.radius), self.location.y)
  38. ))
  39. count = len(matches)
  40. matches.delete()
  41. return count
  42. def distance_to_decimal_degrees(distance, latitude):
  43. """
  44. Source of formulae information:
  45. 1. https://en.wikipedia.org/wiki/Decimal_degrees
  46. 2. http://www.movable-type.co.uk/scripts/latlong.html
  47. :param distance: an instance of `from django.contrib.gis.measure.Distance`
  48. :param latitude: y - coordinate of a point/location
  49. """
  50. lat_radians = latitude * (math.pi / 180)
  51. # 1 longitudinal degree at the equator equal 111,319.5m equiv to 111.32km
  52. return distance.m / (111_319.5 * math.cos(lat_radians))