I'm trying to set stay column field value in TestLocation model table as stay or on_move by calculating difference between lat and longs using for loop to iterate between rows of model table and if condition where to set value of stay field as stay or on_move which depends on difference as shown in views.py.
below is my model.py:
class TestLocation(models.Model):
LOCATOR_YES_NO_CHOICES = ((None, ""), (True, "Yes"), (False, "No"))
longitude = models.FloatField()
latitude = models.FloatField()
processed = models.BooleanField(
choices=LOCATOR_YES_NO_CHOICES,
max_length=3,
blank=True,
null=True,
default=None,
)
stay=models.CharField(max_length=100,null=True)
duration=models.FloatField(null=True)
and views.py funtion to set value of stay field in above model:
def mark_stay_location():
if TestLocation.objects.filter(processed=None):
k = TestLocation.objects.filter(processed=None).order_by("timestamp")
p = len(k)
# list_lat1 = []
# list_lat2 = []
# list_long1 = []
# list_long2 = []
for i in range(p - 1):
rowpairs = k[i : i + 2]
lat1 = rowpairs[0].latitude
# list_lat1.append(lat1)
lat2 = rowpairs[1].latitude
# list_lat2.append(lat2)
long1 = rowpairs[0].longitude
# list_long1.append(long1)
long2 = rowpairs[1].longitude
# list_long2.append(long2)
lat = abs(lat2 - lat1)
long = abs(long2 - long1)
a = sin(lat / 2) ** 2 + cos(lat1) * cos(lat2) * sin(long / 2) ** 2
c = 2 * atan2(sqrt(a), sqrt(1 - a))
for rowpair in rowpairs:
row_id = rowpair.id
po = TestLocation.objects.filter(id=row_id)
if lat < 0.0009 or long < 0.0009:
po.update(stay="stay")
po.update(processed=True)
else:
po.update(stay="on_move")
po.update(processed=True)
duration()
return Response(status=status.HTTP_200_OK)
and if the stay field has values in list format["stay","stay","stay","on_move","on_move","on_move"] then using timestamp(154653268 value for example ) of respective row ,I need to calculate duration between first and last stay value and again between next first on_move and last one.I took that list as example ,it maybe like this["stay","on_move","stay","on_move","stay","on_move"].