models.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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, PolygonField, LineStringField
  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, srid=4326)
  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. totalTime = models.DurationField("Total duration", default=timedelta(0))
  21. center = PointField(dim=2, srid=4326, default=None)
  22. line = LineStringField(dim=2, srid=4326, default=None, null=True)
  23. color = ColorField()
  24. distance = models.FloatField("Distance in m", default=0)
  25. topSpeed = models.FloatField("Top speed in km/h", default=0)
  26. avgSpeed = models.FloatField("Average speed in km/h", default=0)
  27. ascendHeight = models.FloatField("Ascend height in m", default=0)
  28. descendHeight = models.FloatField("Descend height in m", default=0)
  29. movementTime = models.DurationField("Movement duration", default=timedelta(0))
  30. path = models.BinaryField("Path", default=bytes())
  31. class Meta:
  32. ordering = ('startTime',)
  33. class CensoredLocation(models.Model):
  34. location = PointField(dim=2, srid=4326)
  35. radius = models.IntegerField(default=800)
  36. name = models.CharField(max_length=255)
  37. def delete_markers(self) -> int:
  38. matches = Marker.objects.filter(location__distance_lt=(
  39. self.location,
  40. distance_to_decimal_degrees(D(m=self.radius), self.location.y)
  41. ))
  42. count = len(matches)
  43. matches.delete()
  44. return count
  45. def distance_to_decimal_degrees(distance, latitude):
  46. """
  47. Source of formulae information:
  48. 1. https://en.wikipedia.org/wiki/Decimal_degrees
  49. 2. http://www.movable-type.co.uk/scripts/latlong.html
  50. :param distance: an instance of `from django.contrib.gis.measure.Distance`
  51. :param latitude: y - coordinate of a point/location
  52. """
  53. lat_radians = latitude * (math.pi / 180)
  54. # 1 longitudinal degree at the equator equal 111,319.5m equiv to 111.32km
  55. return distance.m / (111_319.5 * math.cos(lat_radians))