Skip to content

Instantly share code, notes, and snippets.

@dbrady
Created June 28, 2012 21:43
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 dbrady/3014120 to your computer and use it in GitHub Desktop.
Save dbrady/3014120 to your computer and use it in GitHub Desktop.
Ruby script to generate an animated map of Utah that is very, VERY on fire
#!/usr/bin/env ruby
#
# Generate an astonishingly accurate, animated map of Utah with
# thousands of wildfire pins scattered around it by throwing dozens of
# random lat/lng pairs at the google static maps API.
# NOTE: This script requires ImageMagick. Good luck with that.
# returns a latitude and longitude that lies within the state of Utah
#
# Fudge/Kludge Note: technically the 4.75 should be a 5.0 but the fire
# pins are centered at their bottoms, and they end up looking like
# they're in Idaho.
#
# Utah occupies a 5-degree square from 37N to 42N and from 109W to
# 114W, with a 2N x 1W notch taken out of its top right so Wyoming
# could be a rectangle. This grabs a random lat/lng inside the square
# that is not inside Wyoming.
def lat_lng_in_utah
x=y=1000.0
until x<-111.0 || y < 41.0
x = -114.0 + (rand * 5.0)
y = 37.0 + (rand * 4.75)
end
[y,x]
end
# Formats a lat/lng pair as a float with only .1 decimal places. This
# is so we can cram as many lat/lng pairs into the URL as possible
# (Google will choke if you try to pass in a url longer than 2048
# bytes).
def google_map_coord(lat, lng)
"%3.1f,%3.1f" % [lat, lng]
end
# With the formatting above, 133 max number of pins we can stick on
# the map and still fit under Googles 2k limit.
def map_link(num_fires)
"http://maps.googleapis.com/maps/api/staticmap?sensor=false&center=39.5,-111.8&size=600x800&zoom=7&markers=icon:http://www.utahfireinfo.gov/markers/fMarkers/flame.png%7C" + (0..num_fires).map { google_map_coord(*lat_lng_in_utah) } * "%7C"
end
# Print command to the console, then execute it
def execute(cmd)
puts cmd
system cmd
end
# ======================================================================
# MAIN SCRIPT
# ----------------------------------------------------------------------
# Generate 20 frames of PNG images (thank you, Google!)
20.times do |i|
execute "wget '#{map_link(133)}' -O omg_fire_map_#{'%02d' % i}.png"
end
# Stitch them together into a gif (this is the part that requires
# ImageMagick)
#
# -delay 10 is the number of ms to delay between frames
# -loop 0 means loop forever
execute "convert -delay 10 -loop 0 omg_fire_map_*.png omg_fire.gif"
@dbrady
Copy link
Author

dbrady commented Jun 28, 2012

Example output: http://bit.ly/LTUXtl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment