Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: KML Library?

Tags:

ruby

kml

I am searching for a ruby library to export location data into KML files. The data to be exported contains mainly simple points with latitude and longitude, but I'd also like to be able to export more complex polygons.

I tried kamelopard, but didn't find it satisfactory as it is missing a tutorial. It wasn't obvious to me where to start using the library. There is another candidate which is called kamel. Unfortunately I couldn't install that one, due to a missing dependency which I couldn't resolve.

So, which library are you using to create a KML file programmatically in Ruby? Or do you use the builder gem and create the XML yourself?

like image 932
evnu Avatar asked Nov 17 '11 14:11

evnu


1 Answers

I solved my problem by using schleyfox-ruby_kml, which is a fork of kmlr. With this, it is easy to generate a kml for a set of placemarks. See the following example from the README:

require 'kml'

kml = KMLFile.new
folder = KML::Folder.new(:name => 'Melbourne Stations')
[
  ["Flinders St",    -37.818078, 144.966811],
  ["Southern Cross", -37.818358, 144.952417],
].each do |name, lat, lng|
  folder.features << KML::Placemark.new(
    :name => name, 
    :geometry => KML::Point.new(:coordinates => {:lat => lat, :lng => lng})
  )
end
kml.objects << folder
puts kml.render

The gem also provides classes to generate polygons and so forth.

like image 120
evnu Avatar answered Sep 22 '22 07:09

evnu