fix: 🐛 fixed issue in both back and frontend that would prevent geojson data from beeing loaded

This commit is contained in:
2025-01-09 01:15:20 +01:00
parent 77cf71e288
commit c97499196c
12 changed files with 6308 additions and 187 deletions

View File

@ -6,7 +6,7 @@ from geojson import Feature, LineString
from geopy.distance import geodesic
from errors.NotFoundException import NotFoundError
from modules.geoObjects import Track, Waypoint
from modules.geoObjects import Driver, Track, Vehicle, Waypoint
class GPXHandler:
__dbSession: Session
@ -16,8 +16,9 @@ class GPXHandler:
pass
# handles converting a gpx file into usable data
def parse(self, file, driver, vehicle):
self.__gpx = gpxpy.parse(file)
def parse(self, file, driver:Driver, vehicle:Vehicle):
print(f"filename: {file}")
self.__gpx = gpxpy.parse(open(f"./uploads/{file}"))
if not driver:
raise ValueError("no driver found")
@ -34,47 +35,45 @@ class GPXHandler:
trackName = track.name or f"Track-{datetime.now().isoformat()}" # todo using time.now might end up being misleading and to be reworked
# initializes track values
self.__startTime,
self.__endTime,
self.__trackDistance,
self.__waypoints = None, None, 0, []
self.startTime = None
self.endTime = None
self.trackDistance = 0
self.waypoints = []
# grab all waypoints from a track
for segment in track.segments:
for point in segment.points:
self.__waypoints.append(point)
if start_time is None or point.time < start_time:
start_time = point.time
if end_time is None or point.time > end_time:
end_time = point.time
self.waypoints.append(point)
if self.startTime is None or point.time < self.startTime:
self.startTime = point.time
if self.endTime is None or point.time > self.endTime:
self.endTime = point.time
# calculate distance between 2 waypoints
for i in range(1, len(self.__waypoints)):
total_distance += self.__waypoints[i - 1].distance_3d(self.__waypoints[i])
for i in range(1, len(self.waypoints)):
self.trackDistance += self.waypoints[i - 1].distance_3d(self.waypoints[i])
# push values to the database
track = Track(
trackName=trackName,
vehicle=vehicle.id,
driver=driver.id,
date=start_time.date() if start_time else None,
distance=total_distance,
vehicle=vehicle,
driver=driver,
date=self.startTime.date() if self.startTime else None,
distance=self.trackDistance,
speed=0,
start=start_time,
end=end_time
)
self.__dbSession.add(track)
self.__dbSession.commit()
for point in self.__waypoints:
for point in self.waypoints:
waypoint = Waypoint(
lat=point.latitude,
lon=point.longitude,
ele=point.elevation,
speed=None,
time=point.time,
track=track.id
track=track
)
self.__dbSession.add(waypoint)
@ -84,10 +83,11 @@ class GPXHandler:
def getTrack(self, trackID):
track = self.__dbSession.query(Track).filter_by(id=trackID).first()
if not track:
raise NotFoundError(f"track with id {trackID} not found")
raise NotFoundError(f"track with id {trackID} not found", errors=[])
# fetches waypoints for a given track and converts them into geoJSON
waypoints = self.__dbSession.query(Waypoint).filter_by(track=track.id).all()
waypoints = track.waypoints
coordinates = [(wp.lon, wp.lat) for wp in waypoints]
feature = Feature(geometry=LineString(coordinates))
return feature
@ -109,8 +109,8 @@ class GPXHandler:
# "name": track.vehicle.name
# } if track.vehicle else None,
# "distance": track.distance,
# "start_time": track.start.isoformat() if track.start else None,
# "end_time": track.end.isoformat() if track.end else None,
# "startTime": track.start.isoformat() if track.start else None,
# "endTime": track.end.isoformat() if track.end else None,
}
for track in tracks # iterates all tracks and appends them to the list
]
@ -134,7 +134,7 @@ class GPXHandler:
"name": track.vehicle.name
} if track.vehicle else None,
"distance": track.distance,
"start_time": track.start.isoformat() if track.start else None,
"startTime": track.start.isoformat() if track.start else None,
"end_time": track.end.isoformat() if track.end else None,
}
for track in tracks # iterates all tracks and appends them to the list