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



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

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



61
62
63
# File 'app/models/otu/maps.rb', line 61

def cached_map_geo_json(cached_map_type = 'CachedMapItem::WebLevel1')
  JSON.parse( cached_map_string(cached_map_type) )
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)



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

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



67
68
69
70
71
72
73
74
# File 'app/models/otu/maps.rb', line 67

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

  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
36
37
38
39
40
41
42
# 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
  end

  # All the OTUs feeding into this map.
  otu_scope = nil

  if self.taxon_name_id.present?
    otu_scope = Otu.select(:id).descendant_of_taxon_name(taxon_name_id)
  else
    otu_scope = Otu.select(:id).where(id:)
  end

  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

!! Always builds


78
79
80
81
82
# File 'app/models/otu/maps.rb', line 78

def quicker_cached_map(cached_map_type = 'CachedMapItem::WebLevel1')
  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