module RGeoServer # A layer group is a grouping of layers and styles that can be accessed as a single layer in a WMS GetMap request. A Layer group is often referred to as a "base map". class LayerGroup < ResourceInfo OBJ_ATTRIBUTES = {:catalog => 'catalog', :name => 'name', :layers => 'layers', :styles => 'styles', :bounds => 'bounds', :metadata => 'metadata' } OBJ_DEFAULT_ATTRIBUTES = {:catalog => nil, :name => nil, :layers => [], :styles => [], :bounds => {'minx'=>'', 'miny' =>'', 'maxx'=>'', 'maxy'=>'', 'crs' =>''}, :metadata => {} } define_attribute_methods OBJ_ATTRIBUTES.keys update_attribute_accessors OBJ_ATTRIBUTES @@route = "layergroups" @@resource_name = "layerGroup" def self.resource_name @@resource_name end def self.root_xpath "//#{@@route}/#{@@resource_name}" end def self.member_xpath "//#{resource_name}" end def route @@route end def message builder = Nokogiri::XML::Builder.new do |xml| xml.layerGroup { xml.name @name xml.layers { layers.each { |l| xml.layer { xml.name l.name } } } unless layers.nil? xml.styles { styles.each { |s| xml.style { xml.name s.name } } } unless styles.nil? xml.bounds { xml.minx bounds['minx'] xml.maxx bounds['maxx'] xml.miny bounds['miny'] xml.maxy bounds['maxy'] xml.crs bounds['crs'] } if @bounds } end return builder.doc.to_xml end # @param [RGeoServer::Catalog] catalog # @param [Hash] options # @option options [String] :name def initialize catalog, options super({}) _run_initialize_callbacks do @catalog = catalog @name = options[:name].strip end @route = route end # @param [Array] sl list of styles def styles= sl if sl.inject(true){|t,s| t && s.is_a?(RGeoServer::Style)} styles_will_change! unless sl == styles @styles = sl else raise 'Unknown list of styles' end end def styles @styles ||= begin unless profile['styles'].empty? return profile['styles'].each{ |s| RGeoServer::Style.new @catalog, :name => s.name } else nil end rescue Exception => e nil end end # @param [Array] ll list of layers def layers= ll if ll.inject(true){ |t,l| t && l.is_a?(RGeoServer::Layer) } layers_will_change! unless ll == layers @layers = ll else raise 'Unknown list of layers' end end def layers @layers ||= begin unless profile['layers'].empty? return profile['layers'].each{ |s| RGeoServer::Layer.new @catalog, :name => s.name } else nil end rescue Exception => e nil end end def profile_xml_to_hash profile_xml doc = profile_xml_to_ng profile_xml name = doc.at_xpath('//name/text()').to_s h = { "name" => name, "layers" => doc.xpath('//layers/layer/name/text()').collect{|l| l.to_s}, "styles" => doc.xpath('//styles/style/name/text()').collect{|s| s.to_s}, "bounds" => { "minx" => doc.at_xpath('//bounds/minx/text()').to_s, "maxx" => doc.at_xpath('//bounds/maxx/text()').to_s, "miny" => doc.at_xpath('//bounds/miny/text()').to_s, "maxy" => doc.at_xpath('//bounds/maxy/text()').to_s, "crs" => doc.at_xpath('//bounds/crs/text()') }, "metadata" => doc.xpath('//metadata/entry').inject({}){ |h, e| h.merge(e['key']=> e.text.to_s) } }.freeze h end end end