Class: Layer

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/layer.rb

Constant Summary collapse

DEFAULT_SELECTION_STYLE =
{
  'POLYGON' =>
    '<PolygonSymbolizer>'+
      '<Fill>'+
        '<CssParameter name="fill">#ff0090</CssParameter>'+
        '<CssParameter name="fill-opacity">0.6</CssParameter>'+
      '</Fill>'+
      '<Stroke>'+
        '<CssParameter name="stroke">#ff0090</CssParameter>'+
        '<CssParameter name="stroke-width">2.00</CssParameter>'+
      '</Stroke>'+
    '</PolygonSymbolizer>',
  'LINESTRING' =>
    '<LineSymbolizer>'+
      '<Stroke>'+
        '<CssParameter name="stroke">#ff0090</CssParameter>'+
        '<CssParameter name="stroke-width">10.00</CssParameter>'+
      '</Stroke>'+
    '</LineSymbolizer>',
  'POINT' =>
    '<PointSymbolizer>'+
       '<Graphic>'+
         '<Mark>'+
           '<WellKnownName>circle</WellKnownName>'+
           '<Fill>'+
             '<CssParameter name="fill">#ff0090</CssParameter>'+
           '</Fill>'+
         '</Mark>'+
         '<Size>45.0</Size>'+
       '</Graphic>'+
     '</PointSymbolizer>'
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.list(ability, layer_type, topic_name) ⇒ Object

Structure for Topic selection



21
22
23
24
25
26
# File 'app/models/layer.rb', line 21

def self.list(ability, layer_type, topic_name)
  topic = Topic.accessible_by(ability).includes(:layers).where(:name => topic_name).first
  layers = topic.nil? ? [] : topic.layers.accessible_by(ability).all
  topic_layers = topic.nil? ? [] : topic.topics_layers.select {|tl| layers.include?(tl.layer) }
  wms_layer_list(ability, topic, topic_layers)
end

.wms_layer_list(ability, topic, topic_layers) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/layer.rb', line 28

def self.wms_layer_list(ability, topic, topic_layers)
  wms_layers = topic_layers.collect do |topic_layer|
    layer = topic_layer.layer
    {
      "id" => topic_layer.id,
      "layername"=> layer.name,
      "topic"=> topic.name,
      "groupname" => layer.sublayer_group.try(:name),
      "toclayertitle"=> layer.title,
      "leglayertitle"=> layer.title,
      "showscale"=> "true",
      "minscale"=> layer.minscale,
      "maxscale"=> layer.maxscale,
      "wms_sort"=> topic_layer.wms_sort, # MapServer layer order
      #"leg_sort"=> topic_layer.leg_sort, # Not used client side (Legend sort is in defined in HTML). Query result order. 
      #"query_sort"=> topic_layer.leg_sort, # deprecated
      "toc_sort"=> topic_layer.toc_sort, # Layer tree order
      "wms"=> "false",
      "visini"=> topic_layer.visini,
      "visuser"=> topic_layer.visini, #User visibility is in request_state
      "showtoc"=> "true",
      "editeable"=> ability.can?(:edit, layer)
    }
  end
  {
    "success" => true,
    "messageProperty"=> {"topic"=> topic.name, "legendtitle"=> "Legende", "legendraster"=> "true"},
    "results"=> wms_layers.size,
    "wmslayers"=> wms_layers
  }
end

Instance Method Details

#attribute(name) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/models/layer.rb', line 84

def attribute(name)
  if feature_class.nil?
    ::Attribute.new(self, name)
  else
    @attrs ||= feature_class.columns.inject({}) do |h, c|
      #logger.info "************************* feature_class column c #{c.inspect}"
      h[c.name] = ::Attribute.new(self, c.name)
      h
    end
    @attrs[name] ||= ::Attribute.new(self, name) #Add ad-hoc Attr. for calculated columns (e.g. custom SQL in query fields)
  end
  #logger.info "************************* Attribute for name '#{name}': #{@attrs[name].inspect}"
end

#feature_class ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'app/models/layer.rb', line 64

def feature_class
  fc = "Geo::#{feature_class_name}".constantize rescue nil #Geo.const_defined?(feature_class_name) seems not to work here
  fc ||= Geo.module_eval <<EOS
    class #{feature_class_name} < GeoModel
      self.table_name = '#{table}'
      self.primary_key = '#{pkey}'

      self
    end
EOS
end

#feature_class_name ⇒ Object



76
77
78
# File 'app/models/layer.rb', line 76

def feature_class_name
  table.camelize.singularize
end

#full_name ⇒ Object



60
61
62
# File 'app/models/layer.rb', line 60

def full_name
  "#{topic_name}-#{name}".downcase
end

#geometry_column ⇒ Object



80
81
82
# File 'app/models/layer.rb', line 80

def geometry_column
  feature_class.try(:geometry_column)
end

#ident_fields_for(ability) ⇒ Object



109
110
111
112
113
114
115
116
# File 'app/models/layer.rb', line 109

def ident_fields_for(ability)
  #attributes.accessible_by(ability) & fields
  #logger.info "************************* fields layer #{name}: #{ident_fields}"
  #logger.info "************************* roles: #{ability.roles.collect(&:name).join(',')}"
  allowed_fields = ident_fields.split(',').select { |f| ability.can?(:show, attribute(f))  }
  #logger.info "************************* ident_fields layer #{name}: #{allowed_fields.inspect}"
  allowed_fields
end

#info ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
# File 'app/models/layer.rb', line 176

def info
  @info ||= begin
    if File.exist?(info_file)
      "layers/custom/#{topic_name.downcase}/#{info_fname[1..-10]}"
    elsif !File.exist?(info_file_empty) && File.exist?(info_file_auto)
      "layers/custom/#{topic_name.downcase}/auto/#{info_fname[1..-10]}"
    else
      nil
    end
  end
end

#info_file ⇒ Object



163
164
165
# File 'app/models/layer.rb', line 163

def info_file
  File.join(Rails.root, 'app', 'views', 'layers', 'custom', topic_name.downcase, info_fname)
end

#info_file_auto ⇒ Object



167
168
169
# File 'app/models/layer.rb', line 167

def info_file_auto
  File.join(Rails.root, 'app', 'views', 'layers', 'custom', topic_name.downcase, 'auto', info_fname)
end

#info_file_empty ⇒ Object

ignore auto file if empty file exists



172
173
174
# File 'app/models/layer.rb', line 172

def info_file_empty
  File.join(Rails.root, 'app', 'views', 'layers', 'custom', topic_name.downcase, "_#{name}_info_leer.html.erb")
end

#info_fname ⇒ Object

Partial for identify result



159
160
161
# File 'app/models/layer.rb', line 159

def info_fname
  "_#{name}_info.html.erb"
end

#infotab ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'app/models/layer.rb', line 192

def infotab
  #  INFOLAYOUT=0^1^2^3^4^5^6^7  *** Wenn der Parameter = 1 ist, wird ein leerer String �bergeben.
  #                              *** Wenn der Parameter = 0 ist, wird der entsprechende Teil weggelassen wenn m�glich
  #
  #  LayoutString(0) = "Im Umkreis von <EM>xUmkreisx</EM> Meter(n) wurde <EM>xAnzahlx</EM> Datensatz gefunden.<br><br>"
  #  LayoutString(1) = "Im Umkreis von <EM>xUmkreisx</EM> Meter(n) wurden <EM>xAnzahlx</EM> Datens&aumltze gefunden.<br><br>"
  #  LayoutString(2) = "layer"
  #  LayoutString(3) = "infotext"
  #  LayoutString(4) = "infotab"
  #  LayoutString(5) = "tabtitle"
  #  LayoutString(6) = "tabcell"
  #  LayoutString(7) = "2"  ( If = "" Then 1 Tabellenzeile pro Record [z.B. "1" oder ""], else [z.B. "2"] 1 Zeile pro Feld)

  "infotable_horizontal"
end

#infotext(count) ⇒ Object



188
189
190
# File 'app/models/layer.rb', line 188

def infotext(count)
  count > 0 ? "resultcount_p" : "resultcount_s"
end

#legend ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
# File 'app/models/layer.rb', line 220

def legend
  @legend ||= begin
    if File.exist?(legend_file)
      "layers/custom/#{topic_name.downcase}/#{legend_fname[1..-10]}"
    elsif File.exist?(legend_file_auto)
      "layers/custom/#{topic_name.downcase}/auto/#{legend_fname[1..-10]}"
    else
      nil
    end
  end
end

#legend_file ⇒ Object



212
213
214
# File 'app/models/layer.rb', line 212

def legend_file
  File.join(Rails.root, 'app', 'views', 'layers', 'custom', topic_name.downcase, legend_fname)
end

#legend_file_auto ⇒ Object



216
217
218
# File 'app/models/layer.rb', line 216

def legend_file_auto
  File.join(Rails.root, 'app', 'views', 'layers', 'custom', topic_name.downcase, 'auto', legend_fname)
end

#legend_fname ⇒ Object



208
209
210
# File 'app/models/layer.rb', line 208

def legend_fname
  "_#{name}_legend.html.erb"
end

#query(ability, query_topic, searchbbox) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
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
# File 'app/models/layer.rb', line 118

def query(ability, query_topic, searchbbox)
  if feature_class
    begin
      #query_topic: {... customQueries: {<layername>: <query_method> }
      #e.g.
      #{"queryTopics":[{
      #   "level":"main","topic":"Lageklassen2011ZH","divCls":"legmain","layers":"seen,lageklassen-2011-flaechen,grenzen,gemeindegrenzen,bezirkslabels"
      #   customQueries: {'seen': 'tiefen_statistik'},
      #   customParams: {'tiefe': 25}
      #  }]}
      custom_query_method = query_topic['customQueries'][name] rescue nil
      logger.debug "******** #{feature_class} ***************************************************"
      features = if custom_query_method
        logger.debug "Custom query on layer #{name}: #{query_topic.inspect}"
        feature_class.send(custom_query_method, self, query_topic, searchbbox) 
      else
        logger.debug "Identify on layer #{name} with query fields #{query_fields(ability)} at #{searchbbox.inspect}"
        feature_class.identify_filter(searchbbox, searchdistance).select(query_fields(ability)).all
      end
      logger.debug "Number of features: #{features.size}"
      # calculate bbox of all features
      unless features.empty?
        envelope = GeoRuby::SimpleFeatures::Geometry.from_hex_ewkb(features.first['extent']).envelope
        features.each do |feature|
          next if feature == features.first
          envelope.extend!(GeoRuby::SimpleFeatures::Geometry.from_hex_ewkb(feature['extent']).envelope)
        end
        bbox = [envelope.lower_corner.x, envelope.lower_corner.y, envelope.upper_corner.x, envelope.upper_corner.y]
      end
    rescue Exception => e
      features = "Table: <b>#{table}</b><br/>Exception: #{e}<br/>query fields: #{query_fields(ability)}<br/>db fields: #{feature_class.column_names.join(',')}<br/>missing: <font color='red'>#{(query_fields(ability).split(',') - feature_class.column_names).join(', ')}</font><br/><br/>"
      logger.info "Identfy error on layer #{name} #{features}"
    end
    [self, features, bbox]
  else
    logger.warn "Table for layer #{name} not found. (Table name: '#{table}')"
    nil
  end
end

#query_fields(ability) ⇒ Object

def filtered(ability) feature_class.where(ability.resource_access_filter(self)) end



102
103
104
105
106
107
# File 'app/models/layer.rb', line 102

def query_fields(ability)
  return '' if feature_class.nil?
  area_field = "ST_Area(#{geometry_column.name}) AS area"
  #TODO: measure -> Measure(geometry_column, lat, lon)
  ([pkey]+ident_fields_for(ability)+[feature_class.extent_field, area_field]).join(',')
end

#quoted_wms_layers ⇒ Object



232
233
234
# File 'app/models/layer.rb', line 232

def quoted_wms_layers
  wms_layers.split(',').collect {|l| %Q<"#{l}"> }.join(',')
end

#selection_symbolizer ⇒ Object



269
270
271
272
273
274
275
276
277
# File 'app/models/layer.rb', line 269

def selection_symbolizer
  if selection_style.blank?
    gtyp = feature_class.geometry_type.sub(/^MULTI/, '').sub(/M$/, '') #MULTIPOINTM -> POINT 
    logger.error "Unsupported selection geometry type #{feature_class.geometry_type}" unless DEFAULT_SELECTION_STYLE.has_key?(gtyp)
    DEFAULT_SELECTION_STYLE[gtyp] || ''
  else
    selection_style
  end
end