Created
October 4, 2010 22:26
-
-
Save bamnet/610570 to your computer and use it in GitHub Desktop.
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
icons = Hash.new | |
@vehicles.each do |v| | |
if !v.latest_position.nil? | |
icon = v.icon_id || 0 | |
heading = v.latest_position.heading | |
if icons.has_key?(icon) | |
if icons[icon].has_key?(heading) | |
icons[icon][heading].push(v) | |
else | |
icons[icon][heading] = [v] | |
end | |
else | |
icons[icon] = Hash.new | |
icons[icon][heading] = [v] | |
end | |
end | |
end | |
gmax = 2 | |
key_count = [gmax, icons.keys.length].max | |
while icons.values.collect{|v| v.length}.inject(0){|sum,item| sum + item} > key_count: | |
# Identify who gets combined based on which two nodes are the closest in value (lowest diff) | |
diff = 360 # Hold the difference for an in-place identification | |
icon = nil # The "icon" we're going to reduce | |
place = [] # The two heading values that will be combined | |
icons.each do |key, value| | |
headings = value.keys | |
headings.sort! | |
for i in (1...(headings.length)) do | |
t_diff = headings[i] - headings[i-1] | |
if t_diff < diff | |
diff = t_diff | |
icon = key | |
place = [headings[i-1], headings[i]] | |
end | |
end | |
end | |
# Now do the reduce step. | |
new_heading = ((place[0] + place[1])/2).to_i | |
combined = icons[icon][place[0]] + icons[icon][place[1]] | |
icons[icon].delete(place[0]) | |
icons[icon].delete(place[1]) | |
icons[icon][new_heading] = combined | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The problem is that Google Static Maps only lets you have GMAX custom icons on the map at a time. We have N shuttles to graph, and each has an icon assigned by it's heading. Each shuttle can also be assigned a different colored icon. For simplicity purposes, we assume there are never more than GMAX colors in use (because I don't know how to reduce colors).
The icon data-structure is a two level hash (color > heading) holding an array, that looks like this:
The idea is to reduce this dataset to GMAX color-heading pairs, so from red-90, red-100, blue-10 to something like red-95, blue-10.