/
kml2ov2.rb
29 lines (21 loc) · 955 Bytes
/
kml2ov2.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/opt/local/bin/ruby
# Licensed under GPLv2
# by Bess Sadler
# inspired by a perl script written by Tor Arntsen
# 1. Read in KML file
# 2. Extract lat / long and name
# 3. Write that information to an ov2 file
require 'rubygems'
require 'nokogiri'
doc = Nokogiri::XML(open("delocator_kml.xml"))
open('output.ov2', 'w') do |f|
doc.xpath('//xmlns:Placemark', 'xmlns' => "http://earth.google.com/kml/2.0").each do |place|
coordinates = place.xpath('./xmlns:Point/xmlns:coordinates/text()', 'xmlns' => "http://earth.google.com/kml/2.0").to_s.split(',')
north = Float(coordinates[0])
east = Float(coordinates[1])
name = place.xpath('./xmlns:name/text()', 'xmlns' => "http://earth.google.com/kml/2.0").to_s
len = 1+name.length; # Include the NULL (added by 'pack')
siz = 13+len; # Size of three first fields plus the string above
f << [2, siz, (north*1E5).to_i, (east*1E5).to_i, name].pack("C I i i Z#{len}")
end
end