import math from django.db import models from django.contrib.gis.db.models.fields import PointField from django.contrib.gis.db.models.functions import Distance from django.contrib.gis.measure import D class Marker(models.Model): timestamp = models.DateTimeField(unique=True) location = PointField(dim=2) alt = models.FloatField("Altitude") hdop = models.IntegerField(null=True, blank=True) speed = models.FloatField("Speed in km/h", null=True, blank=True) class Trip(models.Model): startTime = models.DateTimeField() endTime = models.DateTimeField() name = models.CharField(max_length=255) description = models.TextField(null=True, blank=True) class CensoredLocation(models.Model): location = PointField(dim=2) radius = models.IntegerField(default=800) name = models.CharField(max_length=255) def delete_markers(self) -> int: matches = Marker.objects.filter(location__distance_lt=( self.location, distance_to_decimal_degrees(D(m=self.radius), self.location.y) )) count = len(matches) matches.delete() return count def distance_to_decimal_degrees(distance, latitude): """ Source of formulae information: 1. https://en.wikipedia.org/wiki/Decimal_degrees 2. http://www.movable-type.co.uk/scripts/latlong.html :param distance: an instance of `from django.contrib.gis.measure.Distance` :param latitude: y - coordinate of a point/location """ lat_radians = latitude * (math.pi / 180) # 1 longitudinal degree at the equator equal 111,319.5m equiv to 111.32km return distance.m / (111_319.5 * math.cos(lat_radians))