|
@@ -5,7 +5,8 @@ from datetime import datetime, timedelta
|
|
|
import colorsys
|
|
|
from math import radians, cos, sin, asin, sqrt
|
|
|
|
|
|
-from django.contrib.gis.geos.point import Point
|
|
|
+from django.contrib.gis.geos import Point, LineString
|
|
|
+
|
|
|
|
|
|
from ..models import Marker, Trip
|
|
|
|
|
@@ -42,22 +43,28 @@ def create_trip(markers: list[Marker]) -> Trip:
|
|
|
startTime = markers[0].timestamp,
|
|
|
endTime = markers[-1].timestamp,
|
|
|
name = f"Trip {markers[0].timestamp}",
|
|
|
- color = get_path_color(markers[0].timestamp),
|
|
|
+ color = get_path_color(markers[0].timestamp)
|
|
|
)
|
|
|
- # elif trip.startTime == markers[0].timestamp and trip.endTime == markers[-1].timestamp:
|
|
|
- # data = json.loads(gzip.decompress(trip.path))
|
|
|
- # if len(data) == len(markers):
|
|
|
- # print("Trip already exists")
|
|
|
- # return trip
|
|
|
+ elif trip.startTime == markers[0].timestamp and trip.endTime == markers[-1].timestamp:
|
|
|
+ data = json.loads(gzip.decompress(trip.path))
|
|
|
+ if len(data) > len(markers) * 0.9 and len(data) <= len(markers):
|
|
|
+ print("Trip already exists")
|
|
|
+ return trip
|
|
|
|
|
|
trip.startTime = markers[0].timestamp
|
|
|
trip.endTime = markers[-1].timestamp
|
|
|
+ trip.totalTime = trip.endTime - trip.startTime
|
|
|
+ center = markers[len(markers)//2].location
|
|
|
+ trip.center = Point(center.x, center.y)
|
|
|
+
|
|
|
total_distance = 0 # m
|
|
|
topSpeed = 0 # km/h
|
|
|
ascendHeight = 0 # m
|
|
|
descendHeight = 0 # m
|
|
|
movementTime = timedelta(0)
|
|
|
|
|
|
+ minLat = minLon = maxLat = maxLon = None
|
|
|
+
|
|
|
lastSpeed = 0
|
|
|
i = 1
|
|
|
|
|
@@ -80,6 +87,11 @@ def create_trip(markers: list[Marker]) -> Trip:
|
|
|
|
|
|
total_distance += dist
|
|
|
topSpeed = max(topSpeed, speed)
|
|
|
+
|
|
|
+ minLat = min(minLat, point.location.y) if minLat is not None else point.location.y
|
|
|
+ minLon = min(minLon, point.location.x) if minLon is not None else point.location.x
|
|
|
+ maxLat = max(maxLat, point.location.y) if maxLat is not None else point.location.y
|
|
|
+ maxLon = max(maxLon, point.location.x) if maxLon is not None else point.location.x
|
|
|
|
|
|
if speed > 2.0: # km/h
|
|
|
movementTime += abs(point.timestamp - prev_point.timestamp)
|
|
@@ -97,7 +109,9 @@ def create_trip(markers: list[Marker]) -> Trip:
|
|
|
trip.ascendHeight = round(ascendHeight, 1) # m
|
|
|
trip.descendHeight = round(descendHeight, 1) # m
|
|
|
trip.movementTime = movementTime
|
|
|
-
|
|
|
+
|
|
|
+ trip.line = LineString((markers[0].location.x, markers[0].location.y), (markers[-1].location.x, markers[-1].location.y))
|
|
|
+
|
|
|
trip.path = points_to_blob(markers)
|
|
|
|
|
|
return trip
|