#!/usr/bin/ruby
=begin
trk2path.rb by Tomboya
>ruby trk2path.rb input.gpx
=end
require "~/myprg/ruby/mybasic"
require "rexml/document"
fname = ARGV[0]
data = REXML::Document.new(File.new(fname))
document = REXML::Document.new
kml = document.add_element("kml")
kml.add_namespace("http://earth.google.com/kml/2.0")
kml = kml.add_element("Document")
kml.add_element("name").text = fname
stylemap=kml.add_element("StyleMap")
stylemap.attributes["id"]="msn_ylw-pushpin"
pair = stylemap.add_element("Pair")
pair.add_element("key").text="normal"
pair.add_element("styleUrl").text="sn_ylw-pushpin"
pair = stylemap.add_element("Pair")
pair.add_element("key").text="highlight"
pair.add_element("styleUrl").text="sh_ylw-pushpin"
style=kml.add_element("Style")
style.attributes["id"]="sh_ylw-pushpin"
style.add_element("IconStyle").add_element("scale").text=1.2
linestyle=style.add_element("LineStyle")
linestyle.add_element("color").text = "ff1e15ff"
linestyle.add_element("width").text = 3
style=kml.add_element("Style")
style.attributes["id"]="sn_ylw-pushpin"
linestyle=style.add_element("LineStyle")
linestyle.add_element("color").text = "ff10fcff"
linestyle.add_element("width").text = 3
data.elements.each("gpx/trk"){|trk|
cord=""
time=[]
placemark=kml.add_element("Placemark")
placemark.add_element("name").text = trk.elements["name"].text
linestring=placemark.add_element("LineString")
linestring.add_element("tessellate").text = 1
coordinates=linestring.add_element("coordinates")
trk.elements.each("trkseg/trkpt"){|trkpt|
cord << trkpt.attributes["lon"] << ","
cord << trkpt.attributes["lat"] << ","
cord << trkpt.elements["ele"].text << " "
time << trkpt.elements["time"].text unless trkpt.elements["time"].nil?
}
coordinates.text=cord
placemark.add_element("description").text = "FROM #{time[0]} TO #{time[time.size-1]}" unless time==[]
placemark.add_element("styleUrl").text = "#msn_ylw-pushpin"
}
out=''
kml.write(out, 0, false, true)
mywrite("#{fname.split(".")[0]}.kml",[out])
2009/07/17
Ruby program to convert GPX to KML
GarminのeTrexでtrackを記録したGISデータ(GPX形式)をGoogleEarthで見るのに、KML形式に変換するRubyプログラムです。前はそうした変換をGPSBabelというソフトでやっていたのですが、出力したKMLファイルがあまりきれいではなかったので、Rubyで作ってみました。(mybasic.rbが必要です。)
Labels:
Garmin,
Google Earth,
Ruby
登録:
コメントの投稿 (Atom)
Pretty element of content. I simply stumbled upon your weblog
返信削除and in accession capital to assert that I get actually loved account your weblog posts.
Anyway I will be subscribing for your feeds or even I fulfillment you get admission to persistently rapidly.
Also visit my blog - Cccam Card Share