Created
February 4, 2009 19:16
-
-
Save lwu/58280 to your computer and use it in GitHub Desktop.
Download the OSM XML that contains a given KML (bounding box)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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