Skip to content

Instantly share code, notes, and snippets.

@Talleyran
Created December 13, 2010 17:30
Show Gist options
  • Save Talleyran/739273 to your computer and use it in GitHub Desktop.
Save Talleyran/739273 to your computer and use it in GitHub Desktop.
class PointShapesController < ApplicationController
caches_page :branches
def branches
respond_to{ |format|
valid_tile = Tile.valid_tile(params[:id])
raise HTTPStatus::NotFound if valid_tile.nil?
begin
ico_list = Magick::ImageList.new "public/images/icons/#{params[:style]}.png"
rescue
raise HTTPStatus::NotFound
end
@ico = ico_list.first
coord_offset_vector = Tile.pixel_vector_to_point valid_tile, @ico.columns, @ico.rows
full_query_polygon = Tile.tile_poly_with_offset(valid_tile, coord_offset_vector)
format.js{
@shapes = PointShape.
all( :conditions => ["geom && ? AND styles.name = ?", full_query_polygon, params[:style]], :include => :style ).
sort_by{|el|-el.geom.y}
raise HTTPStatus::NotFound if @shapes.empty?
}
format.png{
a = 256
il = Magick::ImageList.new
il.new_image(a, a) { self.background_color = 'transparent' }
offset = Magick::Rectangle.new(0,0,0,0)
shapes = PointShape.all( :conditions =>
["geom && ? AND styles.name = ?", full_query_polygon, params[:style]], :include => :style)
raise HTTPStatus::NotFound if shapes.empty?
shapes.sort_by{|el|-el.geom.y}.each{|el|
pixel_offset_vector = Tile.point_to_pixel_vector el, valid_tile
dx = @ico.columns/2
dy = @ico.rows
offset.x = pixel_offset_vector.x - dx
offset.y = pixel_offset_vector.y - dy
new_ico = @ico.dup
new_ico.page = offset
il << new_ico
}
im = il.flatten_images
im.format = 'png'
send_data im.to_blob, :type => 'image/png', :disposition => 'inline'
}
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment