Merge pull request #373 from Freika/fix/gpx-valid-export

Update gpx serializer to make it a valid gpx file
This commit is contained in:
Evgenii Burmakin 2024-11-08 16:52:42 +01:00 committed by GitHub
commit ed0b28f553
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 34 additions and 18 deletions

View file

@ -1 +1 @@
0.16.1
0.16.2

View file

@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
# 0.16.2 - 2024-11-08
### Fixed
- Exported GPX file now being correctly recognized as valid by Garmin Connect, Adobe Lightroom and (probably) other services. Previously, the exported GPX file was not being recognized as valid by these services.
# 0.16.1 - 2024-11-08
### Fixed

View file

@ -1,26 +1,36 @@
# 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::GPXFile.new(
name: "dawarich_#{name}",
gpx_data: 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)

View file

@ -4,26 +4,25 @@ require 'rails_helper'
RSpec.describe Points::GpxSerializer do
describe '#call' do
subject(:serializer) { described_class.new(points).call }
subject(:serializer) { described_class.new(points, 'some_name').call }
let(:points) { create_list(:point, 3) }
let(:geojson_data) { Points::GeojsonSerializer.new(points).call }
let(:gpx) { GPX::GeoJSON.convert_to_gpx(geojson_data:) }
it 'returns GPX file' do
expect(serializer).to be_a(GPX::GPXFile)
end
it 'includes waypoints' do
expect(serializer.waypoints.size).to eq(3)
expect(serializer.tracks[0].points.size).to eq(3)
end
it 'includes waypoints with correct attributes' do
serializer.waypoints.each_with_index do |waypoint, index|
serializer.tracks[0].points.each_with_index do |track_point, index|
point = points[index]
expect(waypoint.lat).to eq(point.latitude)
expect(waypoint.lon).to eq(point.longitude)
expect(waypoint.time).to eq(point.recorded_at)
expect(track_point.lat).to eq(point.latitude)
expect(track_point.lon).to eq(point.longitude)
expect(track_point.time).to eq(point.recorded_at)
end
end
end