Skip to content

Instantly share code, notes, and snippets.

@venj
Created March 31, 2014 07:14
Show Gist options
  • Save venj/9886937 to your computer and use it in GitHub Desktop.
Save venj/9886937 to your computer and use it in GitHub Desktop.
Files for process csv files to make kml.
#!/usr/bin/env ruby
require "FileUtils"
require "csv"
include FileUtils
Dir["*.csv"].each do |dat|
basename = File.basename(dat, ".csv")
outfile_name = "#{basename}.kml"
open(outfile_name, "w+") do |out|
out.puts %[<?xml version="1.0" encoding="UTF-8"?>]
out.puts %[<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">]
out.puts %[<Document>]
out.puts "<name>#{outfile_name}</name>"
CSV.open(dat).each_with_index do |line, index|
lat = line[1]
lng = line[0]
out.puts %Q(<Placemark>
<name>#{basename}_#{index}</name>
<styleUrl>#m_ylw-pushpin</styleUrl>
<Point>
<gx:drawOrder>#{index+1}</gx:drawOrder>
<coordinates>#{lng.to_f},#{lat.to_f},0</coordinates>
</Point>
</Placemark>)
end
out.puts "</Document>"
out.puts "</kml>"
end
end
#!/usr/bin/env ruby
# encoding = UTF-8
require "csv"
require "FileUtils"
include FileUtils
Dir["*.csv"].each do |dat|
basename = File.basename(dat, ".csv")
outfile_name = "#{basename}.kml"
open(outfile_name, "w+") do |out|
out.puts %[<?xml version="1.0" encoding="UTF-8"?>]
out.puts %[<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">]
out.puts %[<Document>]
out.puts "<name>#{outfile_name}</name>"
out.puts %[<Style id="red-line">]
out.puts %[<LineStyle>]
out.puts %[<color>ff0000ff</color>]
out.puts %[</LineStyle>]
out.puts %[</Style>]
out.puts %[<Placemark>]
out.puts %Q[<name>#{outfile_name}</name>]
out.puts %[<styleUrl>#red-line</styleUrl>]
out.puts %[<LineString>]
out.puts %[<coordinates>]
# open(dat).readlines.each_with_index do |line, index|
# parts = line.split(",")
# lat = parts[0]
# lng = parts[1]
# height = parts[2]
# out.puts "#{lng},#{lat},#{height} "
# end
csv = CSV.open(dat)
csv.each do |row|
out.puts "#{row[0].to_f},#{row[1].to_f},#{row[2].to_f} "
end
out.puts "</coordinates>"
out.puts "</LineString>"
out.puts "</Placemark>"
out.puts "</Document>"
out.puts "</kml>"
end
end
#!/usr/bin/env ruby
require "FileUtils"
include FileUtils
Dir["*.txt"].each do |dat|
basename = File.basename(dat, ".txt")
outfile_name = "#{basename}.kml"
open(outfile_name, "w+") do |out|
out.puts %[<?xml version="1.0" encoding="UTF-8"?>]
out.puts %[<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">]
out.puts %[<Document>]
out.puts "<name>#{outfile_name}</name>"
out.puts %[<Style id="red-line">]
out.puts %[<LineStyle>]
out.puts %[<color>ff0000ff</color>]
out.puts %[</LineStyle>]
out.puts %[</Style>]
out.puts %[<Placemark>]
out.puts %Q[<name>#{outfile_name}</name>]
out.puts %[<styleUrl>#red-line</styleUrl>]
out.puts %[<LineString>]
out.puts %[<coordinates>]
open(dat).readlines.each_with_index do |line, index|
parts = line.split(",")
lat = parts[0]
lng = parts[1]
height = parts[2]
out.puts "#{lng},#{lat},#{height} "
end
out.puts "</coordinates>"
out.puts "</LineString>"
out.puts "</Placemark>"
out.puts "</Document>"
out.puts "</kml>"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment