Created
August 22, 2015 07:22
-
-
Save hiroaki/a93adc4f473c38c2b7bc to your computer and use it in GitHub Desktop.
convert the data format of ZweiteGPS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# NOTE: currently, convert "trk" only. this does not consider "wpt" and "rte" yet. | |
require 'json' | |
require 'rexml/document' | |
require 'time' | |
json_file = ARGV[0] | |
MEANS_MASTER = { | |
:"0" => "Walking", | |
:"1" => "Jogging", | |
:"2" => "Bicycle", | |
:"3" => "MotorCycle", | |
:"4" => "AutoMobile", | |
:"5" => "Train", | |
:"6" => "Misc", | |
} | |
additional_info = { | |
trk_name: [], | |
creator: [], | |
asset: [], | |
means: [], | |
} | |
boundsType = { | |
minlat: 90, | |
maxlat: -90, | |
minlon: 180, | |
maxlon: -180, | |
} | |
document = REXML::Document.new | |
document << REXML::XMLDecl.new('1.0', 'UTF-8') | |
gpx = REXML::Element.new 'gpx' | |
gpx.attributes['version'] = '1.1' | |
gpx.attributes['creator'] = 'ZweiteGPS' | |
trk = REXML::Element.new 'trk' | |
trkseg = REXML::Element.new 'trkseg' | |
# trkpt(s) | |
open(json_file) do |io| | |
JSON.load(io).each do |coordinate| | |
trkpt = REXML::Element.new 'trkpt' | |
trkpt.attributes['lat'] = coordinate['la'].to_s | |
trkpt.attributes['lon'] = coordinate['lo'].to_s | |
if coordinate['al'] | |
ele = REXML::Element.new 'ele' | |
ele.add_text( coordinate['al'].to_s ) | |
trkpt.add( ele ) | |
end | |
if coordinate['tm'] | |
time = REXML::Element.new 'time' | |
time.add_text( Time.at(coordinate['tm']).utc.iso8601.to_s ) | |
trkpt.add( time ) | |
end | |
# if coordinate['he'] | |
# magvar = REXML::Element.new 'magvar' | |
# magvar.add_text( coordinate['he'].to_s ) | |
# trkpt.add( magvar ) | |
# end | |
# if coordinate['dp'] | |
# desc = REXML::Element.new 'desc' | |
# desc.add_text( coordinate['dp'].to_s ) | |
# trkpt.add( desc ) | |
# end | |
# if coordinate['ln'] | |
# link = REXML::Element.new 'link' | |
# link.attributes['href'] = coordinate['ln'].to_s | |
# trkpt.add( link ) | |
# end | |
# if coordinate['ha'] | |
# hdop = REXML::Element.new 'hdop' | |
# hdop.add_text( coordinate['ha'].to_s ) | |
# trkpt.add( hdop ) | |
# end | |
# if coordinate['va'] | |
# vdop = REXML::Element.new 'vdop' | |
# vdop.add_text( coordinate['va'].to_s ) | |
# trkpt.add( vdop ) | |
# end | |
# get additional information if it exists | |
if coordinate['ow'] | |
additional_info[:creator] << coordinate['ow'].to_s | |
end | |
if coordinate['tl'] | |
additional_info[:trk_name] << coordinate['tl'].to_s | |
end | |
if coordinate['as'] | |
additional_info[:asset] << coordinate['as'].to_s | |
end | |
if coordinate['ms'] | |
additional_info[:means] << coordinate['ms'].to_s | |
end | |
trkseg << trkpt | |
# by the way, update bounds | |
if coordinate['la'] < boundsType[:minlat] | |
boundsType[:minlat] = coordinate['la'] | |
end | |
if boundsType[:maxlat] < coordinate['la'] | |
boundsType[:maxlat] = coordinate['la'] | |
end | |
if coordinate['lo'] < boundsType[:minlon] | |
boundsType[:minlon] = coordinate['lo'] | |
end | |
if boundsType[:maxlon] < coordinate['lo'] | |
boundsType[:maxlon] = coordinate['lo'] | |
end | |
end | |
end | |
# overwrite "creator" of gpx if it exists | |
unless additional_info[:creator].empty? | |
additional_info[:creator].compact! | |
gpx.attributes['creator'] = additional_info[:creator].first | |
if 1 < additional_info[:creator].size | |
warn "found multipule 'creator' data"; | |
end | |
end | |
# update "name" of trk if it exists | |
unless additional_info[:trk_name].empty? | |
additional_info[:trk_name].compact! | |
name = REXML::Element.new 'name' | |
name.add_text( additional_info[:trk_name].first ) | |
trk.add( name ) | |
if 1 < additional_info[:trk_name].size | |
warn "found multipule 'trk_name' data"; | |
end | |
end | |
# gpx metadata | |
metadata = REXML::Element.new 'metadata' | |
unless additional_info[:means].empty? | |
name = REXML::Element.new 'name' | |
name.add_text( MEANS_MASTER[additional_info[:means].first.to_s.to_sym] ) | |
metadata.add( name ) | |
if 1 < additional_info[:means].size | |
warn "found multipule 'means' data"; | |
end | |
end | |
# gpx matadata children - 'time' | |
gpx_metadata_time = REXML::Element.new 'time' | |
gpx_metadata_time.add_text(Time.now.utc.iso8601.to_s) | |
metadata.add( gpx_metadata_time ) | |
# gpx matadata children - 'bounds' | |
bounds = REXML::Element.new 'bounds' | |
bounds.attributes['minlat'] = boundsType[:minlat].to_s | |
bounds.attributes['minlon'] = boundsType[:minlon].to_s | |
bounds.attributes['maxlat'] = boundsType[:maxlat].to_s | |
bounds.attributes['maxlon'] = boundsType[:maxlon].to_s | |
metadata.add( bounds ) | |
trk << trkseg | |
gpx << metadata | |
gpx << trk | |
document << gpx | |
document.write $stdout |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment