Module: Otu::Maps

Extended by:
ActiveSupport::Concern
Included in:
Otu
Defined in:
app/models/otu/maps.rb

Instance Method Summary collapse

Instance Method Details

#cached_map(cached_map_type = 'CachedMapItem::WebLevel1') ⇒ Object

If the CachedMap is not yet built it is built here.

Returns:

  • CachedMap !! Geometry is included in these objects



40
41
42
43
44
# File 'app/models/otu/maps.rb', line 40

def cached_map(cached_map_type = 'CachedMapItem::WebLevel1')
  m = cached_maps.where(cached_map_type:).first
  m ||= create_cached_map(cached_map_type)
  m
end

#cached_map_geo_json(cached_map_type = 'CachedMapItem::WebLevel1') ⇒ Object

Returns GeoJSON A hash (JSON) formated version of the geo-json.

Returns:

  • GeoJSON A hash (JSON) formated version of the geo-json



54
55
56
57
# File 'app/models/otu/maps.rb', line 54

def cached_map_geo_json(cached_map_type = 'CachedMapItem::WebLevel1')
  s = cached_map_string(cached_map_type)
  s.present? ? JSON.parse( s ) : nil
end

#cached_map_id(cached_map_type = 'CachedMapItem::WebLevel1') ⇒ id?

Return only the id if availble (avoids loading geometry)

Returns:

  • (id, nil)

    return only the id if availble (avoids loading geometry)



48
49
50
# File 'app/models/otu/maps.rb', line 48

def cached_map_id(cached_map_type = 'CachedMapItem::WebLevel1')
  cached_maps.where(cached_map_type:).select(:id).first&.id
end

#cached_map_string(cached_map_type = 'CachedMapItem::WebLevel1') ⇒ Object

Returns String A string representation of the geo json.

Returns:

  • String A string representation of the geo json



61
62
63
64
65
66
67
68
69
# File 'app/models/otu/maps.rb', line 61

def cached_map_string(cached_map_type = 'CachedMapItem::WebLevel1')
  mid = cached_map_id # don't load the whole object

  # Takes a long time.
  mid ||= cached_map&.id # need to create the object first
  return nil if mid.nil?

  CachedMap.select('ST_AsGeoJSON(geometry) geo_json').where(id: mid).first.geo_json
end

#create_cached_map(cached_map_type = 'CachedMapItem::WebLevel1', force = false) ⇒ Object

TODO: If:

*All* children have OTUs, and all children have maps, combine those maps
    Should, in theory, speed-up higher level maps


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/models/otu/maps.rb', line 14

def create_cached_map(cached_map_type = 'CachedMapItem::WebLevel1', force = false)
  if force
    cached_map = cached_maps.where(cached_map_type:)
    cached_map&.destroy_all
  end

  # All the OTUs feeding into this map.
  otu_scope = Otu.select(:id).descendant_of_taxon_name(taxon_name_id)

  if gj = CachedMap.calculate_union(otu_scope, cached_map_type:)
    map = CachedMap.create!(
      otu: self,
      geo_json_string: gj, # Geometry conversion here
      cached_map_type:,
      reference_count: CachedMapItem.where(otu: otu_scope, type: cached_map_type).sum(:reference_count)
    )

    map
  else
    nil
  end
end

#quicker_cached_map(cached_map_type = 'CachedMapItem::WebLevel1') ⇒ Object

Prioritize an existing version



72
73
74
75
76
77
# File 'app/models/otu/maps.rb', line 72

def quicker_cached_map(cached_map_type = 'CachedMapItem::WebLevel1')
  # TODO: sort by last_updated?
  m = cached_maps.select('id, otu_id, reference_count, project_id, created_at, updated_at, cached_map_type, ST_AsGeoJSON(geometry) geo_json').where(cached_map_type:).first
  m ||= create_cached_map
  m
end