Update gpx serializer to make it a valid gpx file

This commit is contained in:
Eugene Burmakin 2024-11-08 13:45:51 +01:00
parent fb799eb54a
commit a5198f3a14
2 changed files with 17 additions and 9 deletions

View file

@ -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('<gpx', '<gpx xmlns="http://www.topografix.com/GPX/1/1"')
end
private
attr_reader :points
attr_reader :points, :name
end

View file

@ -34,7 +34,8 @@ class Exports::Create
def time_framed_points
user
.tracked_points
.where('timestamp >= ? 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)