class LayersController < ApplicationController layout "application" before_filter :login_required, :except => [:show, :index, :wms, :export] before_filter :check_if_layer_is_editable, :only => [:edit, :update, :destroy] helper :sort include SortHelper def index sort_init('updated_at', {:default_order => "desc"}) sort_update @html_title = "Listing all Layers " paginate_params = { :page => params[:page], :per_page => 10, :order => sort_clause, :include =>[:maps] } @layers = Layer.paginate(paginate_params) end def show @layer = Layer.find_by_id params[:id] @html_title = "Layer #{@layer.id} on " if logged_in? && (@layer.user = current_user or current_user.has_role?("editor")) @maps = @layer.maps else @maps = @layer.maps.public end respond_to do |format| format.html # show.html.erb # choice of kml show_reflect for nicer wms reflection and show_region for multiscales format.kml { render :action => 'show_reflect', :layout => false } end end def new #assume that the user if logged in @html_title = "Make new layer -" @layer = Layer.new @maps = current_user.maps.warped end def create @layer = Layer.new params[:layer] #@maps = current_user.maps.warped @layer.user = current_user #@layer.maps = Map.find(params[:map_ids]) if params[:map_ids] if params[:map_ids] selected_maps = Map.find(params[:map_ids]) selected_maps.each {|map| @layer.maps << map} end if @layer.save flash[:notice] = "Layer was successfully created." redirect_to layer_url(@layer) else redirect_to new_layer_url end end def edit @layer = Layer.find(params[:id]) @html_title = "Editing Layer #{@layer.id} on" if (!current_user.own_this_layer?(params[:id]) and current_user.has_role?("editor")) @maps = @layer.user.maps.warped else @maps = current_user.maps.warped #current_user.maps.warped end end def update @layer = Layer.find(params[:id]) @maps = current_user.maps.warped @layer.maps = Map.find(params[:map_ids]) if params[:map_ids] if @layer.update_attributes(params[:layer]) @layer.update_layer flash[:notice] = "Layer was successfully updated." redirect_to layer_url(@layer) else redirect_to edit_layer_url end end def delete @layer = Layer.find(params[:id]) end def destroy if logged_in? and (current_user.own_this_layer?(params[:id]) or current_user.has_role?("editor")) @layer = Layer.find(params[:id]) else flash[:notice] = "Sorry, you cannot delete other people's layers!" redirect_to layers_path end if @layer.destroy flash[:notice] = "Layer deleted!" else flash[:notice] = "Layer wasnt deleted" end respond_to do |format| format.html { redirect_to(layers_url) } format.xml { head :ok } end end def export @layer = Layer.find(params[:id]) end def wms #insert mapscript here :) mapscript_wms end require 'mapscript' include Mapscript def mapscript_wms() @layer = Layer.find(params[:id]) ows = Mapscript::OWSRequest.new ok_params = Hash.new # params.each {|k,v| k.upcase! } frozen string error params.each {|k,v| ok_params[k.upcase] = v } [:request, :version, :transparency, :service, :srs, :width, :height, :bbox, :format, :srs].each do |key| ows.setParameter(key.to_s, ok_params[key.to_s.upcase]) unless ok_params[key.to_s.upcase].nil? end ows.setParameter("STYLES", "") ows.setParameter("LAYERS", "image") #ows.setParameter("COVERAGE", "image") map = Mapscript::MapObj.new(File.join(RAILS_ROOT, '/db/maptemplates/wms.map')) projfile = File.join(RAILS_ROOT, '/lib/proj') map.setConfigOption("PROJ_LIB", projfile) #map.setProjection("init=epsg:900913") map.applyConfigOptions # logger.info map.getProjection map.setMetaData("wms_onlineresource", "http://" + request.host_with_port + "/layers/wms/#{@layer.id}") raster = Mapscript::LayerObj.new(map) raster.name = "image" raster.type = Mapscript::MS_LAYER_RASTER raster.tileindex = @layer.tileindex_path raster.tileitem = "Location" raster.status = Mapscript::MS_ON #raster.setProjection( "+init=" + str(epsg).lower() ) raster.dump = Mapscript::MS_TRUE #raster.setProjection('init=epsg:4326') raster.metadata.set('wcs_formats', 'GEOTIFF') raster.metadata.set('wms_title', @layer.name) raster.metadata.set('wms_srs', 'EPSG:4326 EPSG:4269 EPSG:900913') raster.debug= Mapscript::MS_TRUE Mapscript::msIO_installStdoutToBuffer result = map.OWSDispatch(ows) content_type = Mapscript::msIO_stripStdoutBufferContentType || "text/plain" result_data = Mapscript::msIO_getStdoutBufferBytes send_data result_data, :type => content_type, :disposition => "inline" Mapscript::msIO_resetHandlers end private def check_if_layer_is_editable if logged_in? and (current_user.own_this_layer?(params[:id]) or current_user.has_role?("editor")) @layer = Layer.find(params[:id]) elsif Layer.find(params[:id]).user.nil? @layer = Layer.find(params[:id]) else flash[:notice] = "Sorry, you cannot edit other people's layers" redirect_to layer_path end end def mapserver_wms #use Map.map_file_path so we don't have to do a db call status = params["STATUS"].to_s.downcase || "unwarped" styles = "&styles=" # required to stop mapserver being pedantic on older versions mapserver_url = MAPSERVER_URL + '?map=' + Layer.mapfile_path(params[:id]) + styles + "&layers=" + params[:id].to_s mapserver_url += "&"+request.query_string redirect_to(mapserver_url) end end