Class: RGeoServer::Layer

Inherits:
ResourceInfo show all
Defined in:
lib/rgeoserver/layer.rb

Overview

A layer is a published resource (feature type or coverage).

Constant Summary collapse

OBJ_ATTRIBUTES =
{:enabled => 'enabled', :queryable => 'queryable', :path => 'path', :catalog => 'catalog', :name => 'name', :default_style => 'default_style', :alternate_styles => 'alternate_styles', :metadata => 'metadata', :attribution => 'attribution', :layer_type => 'type' }
OBJ_DEFAULT_ATTRIBUTES =
{
  :enabled => 'true',
  :queryable => 'true',
  :path => '/',
  :catalog => nil,
  :name => nil,
  :default_style => nil,
  :alternate_styles => [],
  :metadata => {
    'GWC.autoCacheStyles' => 'true',
    'GWC.gutter' => '0',
    'GWC.enabled' => 'true',
    'GWC.cacheFormats' => 'image/jpeg,image/png',
    'GWC.gridSets' => 'EPSG:4326,EPSG:900913'
  },
  :attribution => {
    'logo_height' => '0',
    'logo_width' => '0',
    'title' => ''
  },
  :layer_type => nil
}
@@route =
"layers"
@@resource_name =
"layer"

Instance Attribute Summary

Attributes inherited from ResourceInfo

#catalog

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ResourceInfo

#clear, #create_method, #delete, list, #new?, #profile, #profile=, #profile_xml_to_ng, #refresh, #save, #to_s, update_attribute_accessors, #update_method

Constructor Details

#initialize(catalog, options) ⇒ Layer

Returns a new instance of Layer.

Parameters:

Options Hash (options):

  • :name (String)

    required

  • :default_style (String)
  • :alternate_styles (Array<String>)


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/rgeoserver/layer.rb', line 104

def initialize catalog, options
  super(catalog)
  _run_initialize_callbacks do
    if options[:name].instance_of? Layer
      options[:name] = options[:name].name.to_s
    end
    
    raise GeoServerArgumentError, "Layer requires :name option" unless options.include? :name and options[:name].is_a? String
    @name = options[:name].to_s.strip
    ap({:init_name => @name}) if $DEBUG
    
    raise NotImplementedError, ":default_style" if options.include? :default_style
    raise NotImplementedError, ":alternate_styles" if options.include? :alternate_styles
    #@default_style = options[:default_style] || ''
    #@alternate_styles = options[:alternate_styles] || []
  end
  @route = route
end

Class Method Details

.member_xpathObject



44
45
46
# File 'lib/rgeoserver/layer.rb', line 44

def self.member_xpath
  "//#{resource_name}"
end

.resource_nameObject



36
37
38
# File 'lib/rgeoserver/layer.rb', line 36

def self.resource_name
  @@resource_name
end

.root_xpathObject



40
41
42
# File 'lib/rgeoserver/layer.rb', line 40

def self.root_xpath
  "//#{@@route}/#{@@resource_name}"
end

Instance Method Details

#create_routeObject

No direct layer creation



53
54
55
# File 'lib/rgeoserver/layer.rb', line 53

def create_route
  nil
end

#get_alternate_styles(&block) ⇒ Object



168
169
170
# File 'lib/rgeoserver/layer.rb', line 168

def get_alternate_styles &block
  self.class.list Style, @catalog, @alternate_styles, {:layer => self}, false, &block
end

#get_default_style(&block) ⇒ Object

TODO: Simplify if necessary with “/layers/<l>/styles”, as specified in the API



164
165
166
# File 'lib/rgeoserver/layer.rb', line 164

def get_default_style &block
  self.class.list Style, @catalog, @default_style, {:layer => self}, false, &block
end

#messageObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rgeoserver/layer.rb', line 61

def message
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.layer {
      xml.name @name
      xml.path path
      xml.type_ layer_type
      xml.enabled @enabled
      xml.queryable @queryable
      xml.defaultStyle {
        xml.name default_style
      }
      xml.styles {
        alternate_styles.each { |s|
          xml.style {
            xml.name s
          }
        }
      } unless alternate_styles.empty?
      xml.resource(:class => resource.class.resource_name){
        xml.name resource.name
      } unless resource.nil?
      xml. {
        .each_pair { |k,v|
          xml.entry(:key => k) {
            xml.text v
          }
        }
      }
      xml.attribution {
        xml.title attribution['title'] unless attribution['title'].empty?
        xml.logoWidth attribution['logo_width']
        xml.logoHeight attribution['logo_height']
      } if !attribution['logo_width'].nil? && !attribution['logo_height'].nil?
    }
  end
  return builder.doc.to_xml
end

#prefixed_nameObject

Return full name of resource with namespace prefix



208
209
210
211
# File 'lib/rgeoserver/layer.rb', line 208

def prefixed_name
  return "#{workspace.name}:#{name}" if self.respond_to?(:workspace)
  raise "Workspace is not defined for this resource"
end

#profile_xml_to_hash(profile_xml) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/rgeoserver/layer.rb', line 172

def profile_xml_to_hash profile_xml
  doc = profile_xml_to_ng profile_xml
  name = doc.at_xpath('//name/text()').text.strip
  link = doc.at_xpath('//resource//atom:link/@href', "xmlns:atom"=>"http://www.w3.org/2005/Atom").text.strip
  workspace, _, store = link.match(/workspaces\/(.*?)\/(.*?)\/(.*?)\/(.*?)\/#{name}.xml$/).to_a[1,3]

  h = {
    "name" => name,
    "path" => doc.at_xpath('//path/text()').to_s,
    "default_style" => doc.at_xpath('//defaultStyle/name/text()').to_s,
    "alternate_styles" => doc.xpath('//styles/style/name/text()').collect{ |s| s.to_s},
    # Types can be: VECTOR, RASTER, REMOTE, WMS
    "type" => doc.at_xpath('//type/text()').to_s,
    "enabled" => doc.at_xpath('//enabled/text()').to_s,
    "queryable" => doc.at_xpath('//queryable/text()').to_s,
    "attribution" => {
      "title" => doc.at_xpath('//attribution/title/text()').to_s,
      "logo_width" => doc.at_xpath('//attribution/logoWidth/text()').to_s,
      "logo_height" => doc.at_xpath('//attribution/logoHeight/text()').to_s
    },
    "resource" => {
      "type" => doc.at_xpath('//resource/@class').to_s,
      "name" => doc.at_xpath('//resource/name/text()').to_s,
      "store" => store,
      "workspace" => workspace
    },
    "metadata" => doc.xpath('//metadata/entry').inject({}){ |h2, e| h2.merge(e['key']=> e.text.to_s) }
  }.freeze
  h
end

#resourceObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/rgeoserver/layer.rb', line 131

def resource
  @resource ||= begin
    unless profile['resource'].empty?
      data_type = profile['resource']['type']
      workspace = profile['resource']['workspace']
      name = profile['resource']['name']
      store = profile['resource']['store']

      case data_type
      when 'coverage'
        return RGeoServer::Coverage.new @catalog, :workspace => workspace, :coverage_store => store, :name => name
      when 'featureType'
        ap({:catalog => @catalog, :workspace => workspace, :data_store => store, :name => name}) if $DEBUG
        begin
          ft = RGeoServer::FeatureType.new @catalog, :workspace => workspace, :data_store => store, :name => name
          ap({:featureType => ft, :route => ft.route}) if $DEBUG
        rescue Exception => e
          ap({:errormsg => "#{e}", :error => e, :trace => e.backtrace}) if $DEBUG
        end
        
        return ft
      else
        raise GeoServerArgumentError, 'Unknown resource type: #{data_type}'
      end
    else
      nil
    end
  rescue Exception => e
    nil
  end
end

#resource=(r) ⇒ Object



123
124
125
126
127
128
129
# File 'lib/rgeoserver/layer.rb', line 123

def resource= r
  if r.is_a?(RGeoServer::Coverage) || r.is_a?(RGeoServer::FeatureType)
    @resource = r
  else
    raise GeoServerArgumentError, 'Unknown resource type: #{r.class}'
  end
end

#routeObject



48
49
50
# File 'lib/rgeoserver/layer.rb', line 48

def route
  @@route
end

#seed(operation, options) ⇒ Object

@param operation @param options for seed message. Read the documentation

Parameters:

  • operation[Symbol] (Hash)

    a customizable set of options



235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/rgeoserver/layer.rb', line 235

def seed operation, options
  op = operation.to_sym
  sub_path = "seed/#{prefixed_name}.xml"
  case op
  when :issue
    @catalog.do_url sub_path, :post, _build_seed_request(:seed, options), {},  @catalog.gwc_client
  when :truncate
    @catalog.do_url sub_path, :post, _build_seed_request(:truncate, options), {}, @catalog.gwc_client
  when :status
    raise NotImplementedError, "#{op}"
  end
end

#update_params(name_route = @name) ⇒ Object



57
58
59
# File 'lib/rgeoserver/layer.rb', line 57

def update_params name_route = @name
  { :layer => name_route }
end

#workspaceObject



203
204
205
# File 'lib/rgeoserver/layer.rb', line 203

def workspace
  resource.workspace
end