def self.kml_to_wkt(kml)
doc = REXML::Document.new(kml)
wkt = ""
if ["Point", "LineString", "Polygon" ].include?(doc.root.name)
case doc.root.name
when "Point" then
coords = doc.elements["/Point/coordinates"].text.gsub(/\n/," ")
wkt = doc.root.name.upcase + "(" + split_coords(coords).join(' ') + ")"
when "LineString" then
coords = doc.elements["/LineString/coordinates"].text.gsub(/\n/," ")
coords = split_coords(coords)
wkt = doc.root.name.upcase + "(" + coords.join(",") + ")"
when "Polygon" then
bounds = []
bounds << doc.elements["/Polygon/outerBoundaryIs/LinearRing/coordinates"].text
inner_coords_elements = doc.elements.each("/Polygon/innerBoundaryIs/LinearRing/coordinates") do |inner_coords|
inner_coords = inner_coords.text
bounds << inner_coords
end
wkt = doc.root.name.upcase + "(" + bounds.map do |bound|
bound.gsub!(/\n/, " ")
bound = split_coords(bound)
if bound.first != bound.last
bound.push bound.first
end
"(" + bound.join(",") + ")"
end.join(",") + ")"
end
end
return wkt
end