mirror of
https://github.com/Freika/dawarich.git
synced 2026-01-11 09:41:40 -05:00
A GPX File can have multiple tracks
This commit is contained in:
parent
646e5e729d
commit
958c792bf1
3 changed files with 3328 additions and 10 deletions
|
|
@ -10,22 +10,35 @@ class Gpx::TrackParser
|
|||
end
|
||||
|
||||
def call
|
||||
segments = json['gpx']['trk']['trkseg']
|
||||
tracks = json['gpx']['trk']
|
||||
tracks_arr = tracks.is_a?(Array) ? tracks : [tracks]
|
||||
|
||||
if segments.is_a?(Array)
|
||||
segments.each do |segment|
|
||||
segment['trkpt'].each { create_point(_1) }
|
||||
end
|
||||
else
|
||||
segments['trkpt'].each { create_point(_1) }
|
||||
end
|
||||
tracks_arr
|
||||
.map { parse_track(_1) }
|
||||
.flatten
|
||||
.reduce { |result, points| result.merge(points) { _2 + _3 } }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def parse_track(track)
|
||||
segments = track['trkseg']
|
||||
segments_arr = segments.is_a?(Array) ? segments : [segments]
|
||||
|
||||
segments_arr.map do |segment|
|
||||
trackpoints = segment['trkpt']
|
||||
|
||||
points = trackpoints.reduce(0) { _1 + create_point(_2) }
|
||||
doubles = trackpoints.size - points
|
||||
processed = points + doubles
|
||||
|
||||
{ raw_points: trackpoints.size, points:, doubles:, processed: }
|
||||
end
|
||||
end
|
||||
|
||||
def create_point(point)
|
||||
return if point['lat'].blank? || point['lon'].blank? || point['time'].blank?
|
||||
return if point_exists?(point)
|
||||
return 0 if point['lat'].blank? || point['lon'].blank? || point['time'].blank?
|
||||
return 0 if point_exists?(point)
|
||||
|
||||
Point.create(
|
||||
latitude: point['lat'].to_d,
|
||||
|
|
@ -33,8 +46,11 @@ class Gpx::TrackParser
|
|||
altitude: point['ele'].to_i,
|
||||
timestamp: Time.parse(point['time']).to_i,
|
||||
import_id: import.id,
|
||||
raw_data: point,
|
||||
user_id:
|
||||
)
|
||||
|
||||
1
|
||||
end
|
||||
|
||||
def point_exists?(point)
|
||||
|
|
|
|||
3291
spec/fixtures/files/gpx/gpx_track_multiple_tracks.gpx
vendored
Normal file
3291
spec/fixtures/files/gpx/gpx_track_multiple_tracks.gpx
vendored
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -15,6 +15,7 @@ RSpec.describe Gpx::TrackParser do
|
|||
context 'when file has a single segment' do
|
||||
it 'creates points' do
|
||||
expect { parser }.to change { Point.count }.by(301)
|
||||
expect(parser).to eq({ doubles: 4, points: 301, processed: 305, raw_points: 305 })
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -23,8 +24,18 @@ RSpec.describe Gpx::TrackParser do
|
|||
|
||||
it 'creates points' do
|
||||
expect { parser }.to change { Point.count }.by(558)
|
||||
expect(parser).to eq({ doubles: 0, points: 558, processed: 558, raw_points: 558 })
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'when file has multiple tracks' do
|
||||
let(:file_path) { Rails.root.join('spec/fixtures/files/gpx/gpx_track_multiple_tracks.gpx') }
|
||||
|
||||
it 'creates points' do
|
||||
expect { parser }.to change { Point.count }.by(407)
|
||||
expect(parser).to eq({ doubles: 0, points: 407, processed: 407, raw_points: 407 })
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue