Skip to content

Commit

Permalink
Merge pull request #373 from Freika/fix/gpx-valid-export
Browse files Browse the repository at this point in the history
Update gpx serializer to make it a valid gpx file
  • Loading branch information
Freika authored Nov 8, 2024
2 parents 657f69d + d09a4f3 commit ed0b28f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .app_version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.16.1
0.16.2
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 17 additions & 7 deletions app/serializers/points/gpx_serializer.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 3 additions & 2 deletions app/services/exports/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def call
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
Expand Down Expand Up @@ -68,7 +69,7 @@ def process_geojson_export(points)
end

def process_gpx_export(points)
Points::GpxSerializer.new(points).call
Points::GpxSerializer.new(points, export.name).call
end

def create_export_file(data)
Expand Down
15 changes: 7 additions & 8 deletions spec/serializers/points/gpx_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,25 @@

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
Expand Down

0 comments on commit ed0b28f

Please sign in to comment.