Skip to content

Instantly share code, notes, and snippets.

@lwu
Created February 4, 2009 01:04
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/57862 to your computer and use it in GitHub Desktop.
Save lwu/57862 to your computer and use it in GitHub Desktop.
Get the shapefile for a query string using Yahoo/Flickr webservices
#!/usr/bin/env ruby
%w[open-uri hpricot].each {|lib| require lib }
YAHOO_APP_ID = "[yourappidhere]"
FLICKR_KEY = "[yourapikeyhere]"
raise "alarms" if YAHOO_APP_ID =~ /here/
raise "no query arg specified" if ARGV.empty?
# command line query => Y! where query => Flickr getInfo query => wget shapefile => ogr2ogr to KML
query = ARGV[0].gsub(' ', '%20')
yurl = "http://where.yahooapis.com/v1/places.q(#{query})?appid=#{YAHOO_APP_ID}"
yahoo_doc = Hpricot(open(yurl))
woe_id = (yahoo_doc/"woeid").inner_html
puts "Calling #{yurl}"
meth = "flickr.places.getInfo"
url = "http://api.flickr.com/services/rest/?method=#{meth}&api_key=#{FLICKR_KEY}&woe_id=#{woe_id}"
flickr_doc = Hpricot(open(url))
shapefile_url = (flickr_doc/"shapefile").inner_html
desc = (flickr_doc/"place")[0]['name']
puts "\n ** Found '#{desc}' with woeid=#{woe_id} for query '#{query}'! **\n"
raise "I didn't find a shapefile here! #{flickr_doc/'place'}" if shapefile_url.empty?
`wget #{shapefile_url}`
shapefile_tgz = shapefile_url.split('/')[-1]
`tar xvfz #{shapefile_tgz}` && `mkdir -p tarballs` && `mv #{shapefile_tgz} tarballs`
dir = shapefile_name = shapefile_tgz.split('_')[0]
`touch #{dir}/#{query}.nfo` # create a dummy file with query as the name
# shapefile => KML
`cd #{dir}; ogr2ogr -f KML #{query.gsub(' ', '_')}.kml #{shapefile_name}.shp`
#puts "cd #{dir}; ogr2ogr -f KML #{query.gsub(' ', '_')}.kml #{shapefile_name}"
puts "Output files=" + `ls #{dir}`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment