From a5198f3a14deabad8f272211cfd3bfd8f326b14e Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Fri, 8 Nov 2024 13:45:51 +0100 Subject: [PATCH] Update gpx serializer to make it a valid gpx file --- app/serializers/points/gpx_serializer.rb | 21 ++++++++++++++------- app/services/exports/create.rb | 5 +++-- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/app/serializers/points/gpx_serializer.rb b/app/serializers/points/gpx_serializer.rb index 35b5863d..c5c25cca 100644 --- a/app/serializers/points/gpx_serializer.rb +++ b/app/serializers/points/gpx_serializer.rb @@ -1,26 +1,33 @@ # frozen_string_literal: true class Points::GpxSerializer - def initialize(points) + def initialize(points, name) @points = points + @name = name end def call - gpx = GPX::GPXFile.new + gpx_file = GPX::GPXFile.new(name: "dawarich_#{name}") + track = GPX::Track.new(name: "dawarich_#{name}") + + gpx_file.tracks << track + + track_segment = GPX::Segment.new + track.segments << track_segment points.each do |point| - gpx.waypoints << GPX::Waypoint.new( + track_segment.points << GPX::TrackPoint.new( lat: point.latitude.to_f, lon: point.longitude.to_f, - time: point.recorded_at, - ele: point.altitude.to_f + elevation: point.altitude.to_f, + time: point.recorded_at ) end - gpx + gpx_file.to_s.sub('= ? AND timestamp <= ?', start_at.to_i, end_at.to_i) + .where(timestamp: start_at.to_i..end_at.to_i) + .order(timestamp: :asc) end def create_export_finished_notification @@ -68,7 +69,7 @@ class Exports::Create end def process_gpx_export(points) - Points::GpxSerializer.new(points).call + Points::GpxSerializer.new(points, export.name).call end def create_export_file(data)