Skip to content

Instantly share code, notes, and snippets.

@ajturner
Created May 29, 2009 13:15
Show Gist options
  • Save ajturner/119944 to your computer and use it in GitHub Desktop.
Save ajturner/119944 to your computer and use it in GitHub Desktop.
# description: given a KML file or URL this script caches all the remote files to a local directory and creates a KMZ (zipped KML)
# usage: cache_kml.rb "http://maps.nanaimo.ca/data/property/109332.kml"
%w{ rubygems open-uri uri }.each {|gem| require gem}
kml = ARGV[0]
filename = URI.parse(kml).path.split("/").last
dir = filename.gsub(/\.\w+/,'')
file = open(kml).read
urls = URI.extract(file)
system("mkdir #{dir}")
urls.each do |url|
uri = URI.parse(url)
unless uri.path.nil?
puts uri.to_s
ufile = uri.path.split("/").last
system("curl -s --create-dirs -o #{dir}#{uri.path} #{uri.scheme}://#{uri.host}#{uri.path}")
file.gsub!("#{uri.scheme}://#{uri.host}#{uri.path}","#{dir}#{uri.path}")
end
end
File.open("#{dir}/doc.kml", "w") {|f| f << file}
system("zip -r #{dir} #{dir}.kmz")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment