Class: Distribution

Inherits:
Object
  • Object
show all
Defined in:
lib/distribution.rb

Overview

An array of OTU distributions

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Distribution) initialize(source_object_types: [ :asserted_distribution, :collecting_event_geographic_area, :collecting_event_georeference], otus: [], preferred_georeference_only: false)

Returns a new instance of Distribution



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/distribution.rb', line 27

def initialize(
  source_object_types: [
    :asserted_distribution,
    :collecting_event_geographic_area,
    :collecting_event_georeference],
  otus: [],
  preferred_georeference_only: false # does nothing at present
)
  @preferred_georeference_only = preferred_georeference_only
  @source_object_types         = source_object_types
  @otus                        = otus
end

Instance Attribute Details

- (Object) map_source_objects

A cache of the aggregate data to be parsed into json per OTU

otu_id => [
  [ asserted_distribution, geographic_area, type ], 
  [ collecting_event, georeference, type ], 
  [ collecting_event, geographic_area, type ]
]



16
17
18
# File 'lib/distribution.rb', line 16

def map_source_objects
  @map_source_objects
end

- (Object) otus

the list of OTUs to generate distributions for



23
24
25
# File 'lib/distribution.rb', line 23

def otus
  @otus
end

- (Object) preferred_georeference_only

Returns the value of attribute preferred_georeference_only



25
26
27
# File 'lib/distribution.rb', line 25

def preferred_georeference_only
  @preferred_georeference_only
end

- (Object) source_object_types

A parameter that indicates which types of data should be extracted for the list of OTUs provided

[ asserted_distribution, :collecting_event_geographic_area, :collecting_event_georeference ]


20
21
22
# File 'lib/distribution.rb', line 20

def source_object_types
  @source_object_types
end

Instance Method Details

- (Object) asserted_distribution_properties(json, asserted_distribution, data)



155
156
157
158
159
# File 'lib/distribution.rb', line 155

def asserted_distribution_properties(json, asserted_distribution, data)
  json['properties'].merge!('label' => asserted_distribution.geographic_area.name)
  json['properties']['metadata'].merge!('GeographicArea' => asserted_distribution.geographic_area.attributes)
  json['properties']['metadata'].merge!('Source' => asserted_distribution.source.attributes)
end

- (Object) build_map_source_objects



44
45
46
47
48
49
50
51
52
53
# File 'lib/distribution.rb', line 44

def build_map_source_objects
  @map_source_objects = {} if @map_source_objects.nil?
  otus.each do |o|
    @map_source_objects[o.id] = []
    source_object_types.each do |t|
      insert_source_objects(o, t)
    end
  end
  @map_source_objects
end

- (Object) collecting_event_geographic_area_properties(json, collecting_event, data)



161
162
163
164
# File 'lib/distribution.rb', line 161

def collecting_event_geographic_area_properties(json, collecting_event, data)
  json['properties'].merge!('label' => collecting_event.geographic_area.name)
  json['properties']['metadata'].merge!('GeographicArea' => collecting_event.geographic_area.attributes)
end

- (Object) collecting_event_georeference_properties(json, georeference, data)



166
167
168
# File 'lib/distribution.rb', line 166

def collecting_event_georeference_properties(json, georeference, data)
  json['properties'].merge!('label' => "Collecting event #{data.id}.")
end

- (Object) geographic_item_for_collecting_event_geographic_area(collecting_event)



82
83
84
# File 'lib/distribution.rb', line 82

def geographic_item_for_collecting_event_geographic_area(collecting_event)
  collecting_event.geographic_area.geographic_items.first
end

- (Object) geographic_item_for_collecting_event_georeference(collecting_event)



86
87
88
# File 'lib/distribution.rb', line 86

def geographic_item_for_collecting_event_georeference(collecting_event)
  asserted_distribution.geographic_area.geographic_items.first
end

- (Object) get_data_for_asserted_distributions(otu)



90
91
92
# File 'lib/distribution.rb', line 90

def get_data_for_asserted_distributions(otu)
  otu.asserted_distributions # .where(geographic_area has a shape clause)
end

- (Object) get_data_for_collecting_event_geographic_areas(otu)

have a better has_many method for this I think



95
96
97
# File 'lib/distribution.rb', line 95

def get_data_for_collecting_event_geographic_areas(otu)
  otu.collecting_events.joins(geographic_area: [:geographic_items])
end

- (Object) get_data_for_collecting_event_georeferences(otu)



99
100
101
# File 'lib/distribution.rb', line 99

def get_data_for_collecting_event_georeferences(otu)
  [otu]
end

- (Object) insert_for_asserted_distribution(otu, source, type)

json insert necessary



63
64
65
# File 'lib/distribution.rb', line 63

def insert_for_asserted_distribution(otu, source, type)
  insert_source_object(otu, source, source.geographic_area, type)
end

- (Object) insert_for_collecting_event_geographic_area(otu, source, type)



67
68
69
# File 'lib/distribution.rb', line 67

def insert_for_collecting_event_geographic_area(otu, source, type)
  insert_source_object(otu, source, source.geographic_area, type)
end

- (Object) insert_for_collecting_event_georeference(otu, source, type)



71
72
73
74
75
76
# File 'lib/distribution.rb', line 71

def insert_for_collecting_event_georeference(otu, source, type)
  georeferences = (preferred_georeference_only ? [source.georeferences.first] : source.georeferences)
  georeferences.each do |g|
    insert_source_object(otu, source, g, type)
  end
end

- (Object) insert_source_object(otu, source, data, type)



78
79
80
# File 'lib/distribution.rb', line 78

def insert_source_object(otu, source, data, type)
  @map_source_objects[otu.id].push([source, data, type])
end

- (Object) insert_source_objects(otu, type)



55
56
57
58
59
60
# File 'lib/distribution.rb', line 55

def insert_source_objects(otu, type)
  records_with_data = self.send("get_data_for_#{type}s", otu) # all returned data must have geographic_item at this point
  records_with_data.each do |r|
    self.send("insert_for_#{type}", otu, r, type)
  end
end

- (Object) to_json



104
105
106
107
108
109
110
111
112
113
114
115
116
117
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
# File 'lib/distribution.rb', line 104

def to_json
  result = {
    'otu_ids' => map_source_objects.keys
  }

  i = 1
  j = 0
  map_source_objects.keys.each do |otu_id|
    feature_collection = {
      'type'     => 'FeatureCollection',
      'features' => []
    }
    colors             = [
      ["black", 0x000000], ["blue", 0x000088], ["orange", 0xDD6600], ["green", 0x008800], ["red", 0x880000],
      ["purple", 0x880088], ["yellow", 0xAAAA55], ["brown", 0x664400], ["gray", 0x666666],
      ["white", 0xFFFFFF], ["shadow", 0x888888]]
    opacities          = {'asserted_distribution' => 0.66, 'collecting_event_georeference' => 0.44, 'collecting_event_geographic_area' => 0.22}
    map_source_objects[otu_id].each do |source, data, type|
      source_class = source.class.name
      route_base   = source.class.table_name

      color_index = (j + 1) % 8 # only 8 colors, excluding black and white (mousover color)
      color_name  = colors[color_index][0]
      color       = sprintf('#%06X', colors[color_index][1])

      json = data.to_simple_json_feature
      json['properties'].merge!(
        'id'          => i,
        'source'      => source_class,
        'source_type' => type.to_s,
        'metadata'    => {source_class => source.attributes},
        'api'         => {
          'concise_details'  => "/#{route_base}/#{data.id}/concise_details",
          'expanded_details' => "/#{route_base}/#{data.id}/expanded_details"
        },
        'colorName'   => color_name,
        'fillColor'   => color,
        'fillOpacity' => opacities[type.to_s]
      )

      send("#{type}_properties", json, source, data)

      feature_collection['features'].push(json)
      i = i + 1
    end
    j = j + 1
    result.merge!(otu_id => feature_collection)
  end
  result
end