Skip to content

Instantly share code, notes, and snippets.

@lwu
Created February 4, 2009 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lwu/58280 to your computer and use it in GitHub Desktop.
Save lwu/58280 to your computer and use it in GitHub Desktop.
Download the OSM XML that contains a given KML (bounding box)
#!/usr/bin/env ruby
%w[open-uri hpricot].each {|lib| require lib }
# get KML bbox and grab OSM XML
raise "no query arg specified" if ARGV.empty?
kml_filename = ARGV[0]
doc = Hpricot(open(kml_filename))
coords = (doc/"coordinates")[0].inner_html.split.map { |x| x.split(',').map {|s| s.to_f } }
longs = coords.transpose[0]
lats = coords.transpose[1]
puts "$('minlat').value = #{lats.min}; $('maxlat').value=#{lats.max};"
puts "$('minlon').value = #{longs.min}; $('maxlon').value=#{longs.max};"
bbox = [longs.min, lats.min, longs.max, lats.max].join(',')
murl = "http://api.openstreetmap.org/api/0.5/map?bbox=#{bbox}"
puts "Downloading #{murl}"
`curl "#{murl}" -o #{kml_filename.split('.')[0]}.osm`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment