models.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import math
  2. from django.db import models
  3. from django.contrib.gis.db.models.fields import PointField
  4. from django.contrib.gis.db.models.functions import Distance
  5. from django.contrib.gis.measure import D
  6. class Marker(models.Model):
  7. timestamp = models.DateTimeField(unique=True)
  8. location = PointField(dim=2)
  9. alt = models.FloatField("Altitude")
  10. hdop = models.IntegerField(null=True, blank=True)
  11. speed = models.FloatField("Speed in km/h", null=True, blank=True)
  12. class Trip(models.Model):
  13. startTime = models.DateTimeField()
  14. endTime = models.DateTimeField()
  15. name = models.CharField(max_length=255)
  16. description = models.TextField(null=True, blank=True)
  17. class CensoredLocation(models.Model):
  18. location = PointField(dim=2)
  19. radius = models.IntegerField(default=800)
  20. name = models.CharField(max_length=255)
  21. def delete_markers(self) -> int:
  22. matches = Marker.objects.filter(location__distance_lt=(
  23. self.location,
  24. distance_to_decimal_degrees(D(m=self.radius), self.location.y)
  25. ))
  26. count = len(matches)
  27. matches.delete()
  28. return count
  29. def distance_to_decimal_degrees(distance, latitude):
  30. """
  31. Source of formulae information:
  32. 1. https://en.wikipedia.org/wiki/Decimal_degrees
  33. 2. http://www.movable-type.co.uk/scripts/latlong.html
  34. :param distance: an instance of `from django.contrib.gis.measure.Distance`
  35. :param latitude: y - coordinate of a point/location
  36. """
  37. lat_radians = latitude * (math.pi / 180)
  38. # 1 longitudinal degree at the equator equal 111,319.5m equiv to 111.32km
  39. return distance.m / (111_319.5 * math.cos(lat_radians))