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



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

def cached_map(cached_map_type = 'CachedMapItem::WebLevel1')
  m = cached_maps.where(cached_map_type:).first
  m ||= create_cached_map
  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



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

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)



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

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



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

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


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
43
# File 'app/models/otu/maps.rb', line 15

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


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

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