Class: Queries::Image::Filter

Inherits:
Query::Filter show all
Includes:
Concerns::Citations, Concerns::Tags
Defined in:
lib/queries/image/filter.rb

Constant Summary collapse

PARAMS =
[
  :biocuration_class_id,
  :collection_object_id,
  :collection_object_scope,
  :depiction_object_type,
  :depictions,
  :freeform_svg,
  :image_id,
  :otu_id,
  :sled_image,
  :sled_image_id,
  :source_id,
  :sqed_image,
  :taxon_name_id,
  :taxon_name_id_target,
  :type_material_depictions,

  biocuration_class_id: [],
  collection_object_id: [],
  depiction_object_type: [],
  image_id: [],
  otu_id: [],
  otu_scope: [],
  sled_image_id: [],
  source_id: [],
  taxon_name_id: [],
].freeze

Constants inherited from Query::Filter

Query::Filter::FILTER_QUERIES, Query::Filter::SUBQUERIES

Instance Attribute Summary collapse

Attributes inherited from Query::Filter

#api, #asserted_distribution_query, #biological_association_query, #biological_associations_graph_query, #collecting_event_query, #collection_object_query, #content_query, #controlled_vocabulary_term_query, #data_attribute_query, #descriptor_query, #document_query, #dwc_occurrence_query, #extract_query, #field_occurrence_query, #image_query, #loan_query, #object_global_id, #observation_query, #order_by, #otu_query, #page, #paginate, #params, #per, #person_query, #project_id, #recent, #recent_target, #taxon_name_query, #venn, #venn_mode

Attributes inherited from Query

#query_string, #terms

Instance Method Summary collapse

Methods inherited from Query::Filter

#all, #all_and_clauses, #all_merge_clauses, #and_clauses, #annotator_and_clauses, #annotator_merge_clauses, annotator_params, api_except_params, api_excluded_params, #apply_venn, #attribute_exact_facet, base_filter, base_query_name, base_query_to_h, #deep_permit, included_annotator_facets, instantiated_base_filter, inverted_subqueries, #model_id_facet, #object_global_id_facet, #only_project?, params, #permitted_params, #process_url_into_params, #project_id_facet, query_name, #query_name, #set_nested_queries, #shared_and_clauses, #subquery_vector, #target_and_clauses, #venn_query

Methods inherited from Query

#alphabetic_strings, #alphanumeric_strings, base_name, #base_name, #base_query, #build_terms, #cached_facet, #end_wildcard, #levenshtein_distance, #match_ordered_wildcard_pieces_in_cached, #no_terms?, referenced_klass, #referenced_klass, #referenced_klass_except, #referenced_klass_intersection, #referenced_klass_union, #start_and_end_wildcard, #start_wildcard, #table, #wildcard_pieces

Constructor Details

#initialize(query_params) ⇒ Filter

Returns a new instance of Filter.

Parameters:

  • params (Hash)


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/queries/image/filter.rb', line 131

def initialize(query_params)
  super

  @biocuration_class_id = params[:biocuration_class_id]
  @collection_object_id = params[:collection_object_id]
  @collection_object_scope = params[:collection_object_scope]
  @depiction_object_type = params[:depiction_object_type]
  @depictions = boolean_param(params, :depictions)
  @freeform_svg = boolean_param(params, :freeform_svg)
  @image_id = params[:image_id]
  @otu_id = params[:otu_id]
  @otu_scope = params[:otu_scope]
  @sled_image = boolean_param(params, :sled_image)
  @sled_image_id = params[:sled_image_id]
  @sqed_depiction_id = params[:sqed_depiction_id]
  @sqed_image = boolean_param(params, :sqed_image)
  @taxon_name_id = params[:taxon_name_id]
  @taxon_name_id_target = params[:taxon_name_id_target]
  @type_material_depictions = boolean_param(params, :type_material_depictions)

  set_citations_params(params)
  set_tags_params(params)
end

Instance Attribute Details

#biocuration_class_idArray

Returns of biocuration_class ids.

Returns:

  • (Array)

    of biocuration_class ids



101
102
103
# File 'lib/queries/image/filter.rb', line 101

def biocuration_class_id
  @biocuration_class_id
end

#collection_object_idArray

Returns images depicting collecting_object.

Returns:

  • (Array)

    images depicting collecting_object



37
38
39
# File 'lib/queries/image/filter.rb', line 37

def collection_object_id
  @collection_object_id
end

#collection_object_scopeObject

Parameters:

  • collection_object_scope

    One or more of:

    :all (default, includes all below)
    
    :collection_objects (those on the collection_object)
    :observations (those on just the CollectionObject observations)
     collecting_events (those on the associated collecting event)
    # maybe those on the CE
    


47
48
49
# File 'lib/queries/image/filter.rb', line 47

def collection_object_scope
  @collection_object_scope
end

#depiction_object_typeArray

Restricts Images to those that depict these classes

Parameters:

  • depiction_object_type

    one or more names of classes.

Returns:

  • (Array)


65
66
67
# File 'lib/queries/image/filter.rb', line 65

def depiction_object_type
  @depiction_object_type
end

#depictionsBoolean?

Returns true - image is used (in a depiction) false - image is not used nil - either.

Returns:

  • (Boolean, nil)

    true - image is used (in a depiction) false - image is not used nil - either



53
54
55
# File 'lib/queries/image/filter.rb', line 53

def depictions
  @depictions
end

#freeform_svgBoolean?

Returns true - image is used (in a depiction) that has svg false - is not a true image nil - ignored.

Returns:

  • (Boolean, nil)

    true - image is used (in a depiction) that has svg false - is not a true image nil - ignored



59
60
61
# File 'lib/queries/image/filter.rb', line 59

def freeform_svg
  @freeform_svg
end

#image_idArray

Returns:

  • (Array)


97
98
99
# File 'lib/queries/image/filter.rb', line 97

def image_id
  @image_id
end

#otu_idArray

Returns images depicting otus.

Returns:

  • (Array)

    images depicting otus



69
70
71
# File 'lib/queries/image/filter.rb', line 69

def otu_id
  @otu_id
end

#otu_scopeArray

A sub scope of sorts. Purpose is to gather all images possible under an OTU that are of an OTU, CollectionObject or Observation.

!! Must be used with an otu_id !!

Parameters:

  • otu_scope

    One or more of:

    :all (default, includes all below)
    
    :otu (those on the OTU)
    :otu_observations (those on just the OTU)
    :collection_object_observations (those on just those determined as the OTU)
    :collection_objects (those on just those on the collection objects)
    :type_material (those on CollectionObjects that have TaxonName used in OTU)
    :type_material_observations (those on CollectionObjects that have TaxonName used in OTU)
    
    :coordinate_otus  If present adds both.  Use downstream substraction to to diffs of with/out?
    

Returns:

  • (Array)


94
95
96
# File 'lib/queries/image/filter.rb', line 94

def otu_scope
  @otu_scope
end

#sled_imageBoolean

Returns true - yes false - is not nil - ignored.

Returns:

  • (Boolean)

    true - yes false - is not nil - ignored



110
111
112
# File 'lib/queries/image/filter.rb', line 110

def sled_image
  @sled_image
end

#sled_image_idArray

Returns:

  • (Array)


104
105
106
# File 'lib/queries/image/filter.rb', line 104

def sled_image_id
  @sled_image_id
end

#sqed_depiction_idArray

Returns:

  • (Array)


113
114
115
# File 'lib/queries/image/filter.rb', line 113

def sqed_depiction_id
  @sqed_depiction_id
end

#sqed_imageBoolean

Returns true - yes false - is not nil - ignored.

Returns:

  • (Boolean)

    true - yes false - is not nil - ignored



119
120
121
# File 'lib/queries/image/filter.rb', line 119

def sqed_image
  @sqed_image
end

#taxon_name_idProtonym.id?

Return all images depicting an Otu that is self or descendant linked to this TaxonName

Returns:

  • (Protonym.id, nil)

    return all images depicting an Otu that is self or descendant linked to this TaxonName



74
75
76
# File 'lib/queries/image/filter.rb', line 74

def taxon_name_id
  @taxon_name_id
end

#taxon_name_id_targetArray

Only used when ‘taxon_name_id` provided

Returns:

  • (Array)

    one or both of ‘Otu’, ‘CollectionObject’, defaults to both if nothing provided



124
125
126
# File 'lib/queries/image/filter.rb', line 124

def taxon_name_id_target
  @taxon_name_id_target
end

#type_material_depictionsArray

Returns depicts some that is a type specimen.

Returns:

  • (Array)

    depicts some that is a type specimen



128
129
130
# File 'lib/queries/image/filter.rb', line 128

def type_material_depictions
  @type_material_depictions
end

Instance Method Details

#biocuration_facetObject



226
227
228
229
230
231
232
# File 'lib/queries/image/filter.rb', line 226

def biocuration_facet
  return nil if biocuration_class_id.empty?
  ::Image.joins(collection_objects: [:depictions]).merge(
    ::CollectionObject::BiologicalCollectionObject.joins(:biocuration_classifications)
    .where(biocuration_classifications: {biocuration_class_id:})
  )
end

#build_depiction_facet(kind, ids) ⇒ Object



434
435
436
437
# File 'lib/queries/image/filter.rb', line 434

def build_depiction_facet(kind, ids)
  return nil if ids.empty?
  ::Image.joins(:depictions).where(depictions: {depiction_object_id: ids, depiction_object_type: kind})
end

#collection_object_facet_collecting_eventsObject



374
375
376
377
378
379
# File 'lib/queries/image/filter.rb', line 374

def collection_object_facet_collecting_events
  ::Image.joins(:observations)
    .joins("INNER JOIN collecting_events on collecting_events.id = observations.observation_object_id AND observations.observation_object_type = 'CollectingEvent'")
    .joins('INNER JOIN collection_objects on collection_objects.collecting_event_id = collecting_events.id')
    .where(collection_objects: {id: collection_object_id})
end

#collection_object_facet_collection_objectsObject



366
367
368
# File 'lib/queries/image/filter.rb', line 366

def collection_object_facet_collection_objects
  ::Image.joins(:collection_objects).where(collection_objects: {id: collection_object_id})
end

#collection_object_facet_observationsObject



370
371
372
# File 'lib/queries/image/filter.rb', line 370

def collection_object_facet_observations
  ::Image.joins(:observations).where(observations: {observation_object_type: 'CollectionObject', observation_object_id: collection_object_id })
end

#collection_object_scope_facetObject



341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/queries/image/filter.rb', line 341

def collection_object_scope_facet
  return nil if collection_object_id.empty?

  selected = []

  if collection_object_scope.include?(:all)
    selected = [
      :collection_object_facet_collection_objects,
      :collection_object_facet_observations,
      :collection_object_facet_collecting_events,
    ]
  elsif collection_object_scope.present?
    selected.push :collection_object_facet_collection_objects if collection_object_scope.include?(:collection_objects)
    selected.push :collection_object_facet_observations if collection_object_scope.include?(:observations)
    selected.push :collection_object_facet_collecting_events if collection_object_scope.include?(:collecting_events)
  else
    selected.push :collection_object_facet_collection_objects
  end

  q = selected.collect{|a| '(' + send(a).to_sql + ')'}.join(' UNION ')

  d = ::Image.from('(' + q + ')' + ' as images')
  d
end

#collection_object_tableArel::Table

Returns:

  • (Arel::Table)


212
213
214
# File 'lib/queries/image/filter.rb', line 212

def collection_object_table
  ::CollectionObject.arel_table
end

#coordinate_otu_idsObject



290
291
292
293
294
295
296
# File 'lib/queries/image/filter.rb', line 290

def coordinate_otu_ids
  ids = []
  otu_id.each do |id|
    ids += ::Otu.coordinate_otus(id).pluck(:id)
  end
  ids.uniq
end

#depiction_object_type_facetObject



234
235
236
237
# File 'lib/queries/image/filter.rb', line 234

def depiction_object_type_facet
  return nil if depiction_object_type.empty?
  ::Image.joins(:depictions).where(depictions: {depiction_object_type:})
end

#depiction_tableArel::Table

Returns:

  • (Arel::Table)


222
223
224
# File 'lib/queries/image/filter.rb', line 222

def depiction_table
  ::Depiction.arel_table
end

#depictions_facetObject



239
240
241
242
243
244
245
246
# File 'lib/queries/image/filter.rb', line 239

def depictions_facet
  return nil if depictions.nil?
  if depictions
    ::Image.joins(:depictions)
  else
    ::Image.where.missing(:depictions)
  end
end

#freeform_svg_facetObject



248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/queries/image/filter.rb', line 248

def freeform_svg_facet
  return nil if freeform_svg.nil?

  q = ::Image.left_joins(depictions: [:sled_image, :sqed_depiction])
    .where.missing(:sled_image)
    .where.not(depictions: {svg_clip: nil}).distinct

  if freeform_svg
    q
  else
    ::Queries.except(::Image.where(project_id:), q)
  end
end

#merge_clausesObject



510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
# File 'lib/queries/image/filter.rb', line 510

def merge_clauses
  s = ::Queries::Query::Filter::SUBQUERIES.select{|k,v| v.include?(:image)}.keys.map(&:to_s) - ['source']
  [
    *s.collect{|m| query_facets_facet(m)}, # Reference all the Image referencing SUBQUERIES
    biocuration_facet,
    collection_object_scope_facet,
    depiction_object_type_facet,
    depictions_facet,
    freeform_svg_facet,
    otu_id_facet,
    otu_scope_facet,
    sled_image_facet,
    sled_image_id_facet,
    sqed_image_facet,
    sqed_depiction_id_facet,
    sqed_image_facet,
    taxon_name_id_facet,
    type_material_depictions_facet,
  ]
end

#otu_facet_collection_object_observations(otu_ids) ⇒ Object



417
418
419
420
421
# File 'lib/queries/image/filter.rb', line 417

def otu_facet_collection_object_observations(otu_ids)
  ::Image.joins(:observations)
    .joins("INNER JOIN taxon_determinations on taxon_determinations.taxon_determination_object_id = observations.observation_object_id AND taxon_determinations.taxon_determination_object_type = 'CollectionObject'")
    .where(taxon_determinations: {otu_id: otu_ids}, observations: {observation_object_type: 'CollectionObject'})
end

#otu_facet_collection_objects(otu_ids) ⇒ Object



412
413
414
415
# File 'lib/queries/image/filter.rb', line 412

def otu_facet_collection_objects(otu_ids)
  ::Image.joins(collection_objects: [:taxon_determinations])
    .where(taxon_determinations: {otu_id: otu_ids})
end

#otu_facet_otu_observations(otu_ids) ⇒ Object



423
424
425
426
# File 'lib/queries/image/filter.rb', line 423

def otu_facet_otu_observations(otu_ids)
  ::Image.joins(:observations)
    .where(observations: {observation_object_id: otu_ids, observation_object_type: 'Otu'})
end

#otu_facet_otus(otu_ids) ⇒ Object



408
409
410
# File 'lib/queries/image/filter.rb', line 408

def otu_facet_otus(otu_ids)
  ::Image.joins(:depictions).where(depictions: {depiction_object_type: 'Otu', depiction_object_id: otu_ids})
end

#otu_facet_type_material(otu_ids) ⇒ Object

Find all TaxonNames, and their synonyms



389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/queries/image/filter.rb', line 389

def otu_facet_type_material(otu_ids)

  # Double check that there are otu_ids,
  #  this check exists in calling methods, but re-inforce here.
  protonyms = if otu_ids.any?
                ::Queries::TaxonName::Filter.new(
                  otu_query: { otu_id: otu_ids},
                  synonymify: true,
                  project_id:
                ).all.where(type: 'Protonym')
              else
                TaxonName.none
              end



  ::Image.joins(collection_objects: [type_materials: [:protonym]]).where(collection_objects: {type_materials: {protonym: protonyms}})
end

#otu_facet_type_material_observations(otu_ids) ⇒ Object



381
382
383
384
385
386
# File 'lib/queries/image/filter.rb', line 381

def otu_facet_type_material_observations(otu_ids)
  ::Image.joins(:observations)
    .joins("INNER JOIN type_materials on type_materials.collection_object_id = observations.observation_object_id AND observations.observation_object_type = 'CollectionObject'")
    .joins('INNER JOIN otus on otus.taxon_name_id = type_materials.protonym_id')
    .where(otus: {id: otu_ids})
end

#otu_id_facetObject



298
299
300
301
302
303
# File 'lib/queries/image/filter.rb', line 298

def otu_id_facet
  # only run this when scope not provided
  return nil if otu_id.empty? || !otu_scope.empty?

  ::Image.joins(:otus).where(otus: {id: otu_id})
end

#otu_scope_facetObject



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/queries/image/filter.rb', line 305

def otu_scope_facet
  return nil if otu_id.empty? || otu_scope.empty?

  otu_ids = otu_id
  otu_ids += coordinate_otu_ids if otu_scope.include?(:coordinate_otus)

  otu_ids.uniq!

  selected = []

  if otu_scope.include?(:all)
    selected = [
      :otu_facet_otus,
      :otu_facet_collection_objects,
      :otu_facet_otu_observations,
      :otu_facet_collection_object_observations,
      :otu_facet_type_material,
      :otu_facet_type_material_observations
    ]
  elsif otu_scope.empty?
    selected = [:otu_facet_otus]
  else
    selected.push :otu_facet_otus if otu_scope.include?(:otus)
    selected.push :otu_facet_collection_objects if otu_scope.include?(:collection_objects)
    selected.push :otu_facet_collection_object_observations if otu_scope.include?(:collection_object_observations)
    selected.push :otu_facet_otu_observations if otu_scope.include?(:otu_observations)
    selected.push :otu_facet_type_material if otu_scope.include?(:type_material)
    selected.push :otu_facet_type_material_observations if otu_scope.include?(:type_material_observations)
  end

  q = selected.collect{|a| '(' + send(a, otu_ids).to_sql + ')'}.join(' UNION ')

  d = ::Image.from('(' + q + ')' + ' as images')
  d
end

#otu_tableArel::Table

Returns:

  • (Arel::Table)


207
208
209
# File 'lib/queries/image/filter.rb', line 207

def otu_table
  ::Otu.arel_table
end

#query_facets_facet(name = nil) ⇒ Object



492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
# File 'lib/queries/image/filter.rb', line 492

def query_facets_facet(name = nil)
  return nil if name.nil?

  q = send((name + '_query').to_sym)

  return nil if q.nil?

  n = "query_#{name}_img"

  s = "WITH #{n} AS (" + q.all.to_sql + ') ' +
    ::Image
    .joins(:depictions)
    .joins("JOIN #{n} as #{n}1 on depictions.depiction_object_id = #{n}1.id AND depictions.depiction_object_type = '#{name.treetop_camelize}'")
    .to_sql

  ::Image.from('(' + s + ') as images').distinct
end

#sled_image_facetObject



271
272
273
274
275
276
277
278
# File 'lib/queries/image/filter.rb', line 271

def sled_image_facet
  return nil if sled_image.nil?
  if sled_image
    ::Image.joins(:sled_image).distinct
  else
    ::Image.where.missing(:sled_image).distinct
  end
end

#sled_image_id_facetObject



280
281
282
283
# File 'lib/queries/image/filter.rb', line 280

def sled_image_id_facet
  return nil if sled_image_id.empty?
  ::Image.joins(:sled_image).where(sled_images: {id: sled_image_id})
end

#sqed_depiction_id_facetObject



285
286
287
288
# File 'lib/queries/image/filter.rb', line 285

def sqed_depiction_id_facet
  return nil if sqed_depiction_id.empty?
  ::Image.joins(depictions: [:sqed_depiction]).where(sqed_depictions: {id: sqed_depiction_id})
end

#sqed_image_facetObject



262
263
264
265
266
267
268
269
# File 'lib/queries/image/filter.rb', line 262

def sqed_image_facet
  return nil if sqed_image.nil?
  if sqed_image
    ::Image.joins(depictions: [:sqed_depiction]).distinct
  else
    ::Image.left_joins(depictions: [:sqed_depiction]).where(sqed_depiction: {id: nil}).distinct
  end
end

#taxon_determination_tableArel::Table

Returns:

  • (Arel::Table)


202
203
204
# File 'lib/queries/image/filter.rb', line 202

def taxon_determination_table
  ::TaxonDetermination.arel_table
end

#taxon_name_id_facetObject



439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
# File 'lib/queries/image/filter.rb', line 439

def taxon_name_id_facet
  #  Image -> Depictions -> Otu -> TaxonName -> Ancestors
  return nil if taxon_name_id.empty?

  h = Arel::Table.new(:taxon_name_hierarchies)
  t = ::TaxonName.arel_table

  j1, j2, q1, q2 = nil, nil, nil, nil

  if taxon_name_id_target.include?('Otu')
    a = otu_table.alias('oj1')
    b = t.alias('tj1')
    h_alias = h.alias('th1')

    j1 = table
      .join(depiction_table, Arel::Nodes::InnerJoin).on(table[:id].eq(depiction_table[:image_id]))
      .join(a, Arel::Nodes::InnerJoin).on( depiction_table[:depiction_object_id].eq(a[:id]).and( depiction_table[:depiction_object_type].eq('Otu') ))
      .join(b, Arel::Nodes::InnerJoin).on( a[:taxon_name_id].eq(b[:id]))
      .join(h_alias, Arel::Nodes::InnerJoin).on(b[:id].eq(h_alias[:descendant_id]))

    z = h_alias[:ancestor_id].in(taxon_name_id)
    q1 = ::Image.joins(j1.join_sources).where(z)
  end

  if taxon_name_id_target.include?('CollectionObject')
    a = otu_table.alias('oj2')
    b = t.alias('tj2')
    h_alias = h.alias('th2')

    j2 = table
      .join(depiction_table, Arel::Nodes::InnerJoin).on(table[:id].eq(depiction_table[:image_id]))
      .join(collection_object_table, Arel::Nodes::InnerJoin).on( depiction_table[:depiction_object_id].eq(collection_object_table[:id]).and( depiction_table[:depiction_object_type].eq('CollectionObject') ))
      .join(taxon_determination_table, Arel::Nodes::InnerJoin).on(
        collection_object_table[:id].eq(taxon_determination_table[:taxon_determination_object_id])
      .and(taxon_determination_table[:taxon_determination_object_type].eq('CollectionObject'))
      )
        .join(a, Arel::Nodes::InnerJoin).on(  taxon_determination_table[:otu_id].eq(a[:id]) )
        .join(b, Arel::Nodes::InnerJoin).on( a[:taxon_name_id].eq(b[:id]))
        .join(h_alias, Arel::Nodes::InnerJoin).on(b[:id].eq(h_alias[:descendant_id]))

      z = h_alias[:ancestor_id].in(taxon_name_id)
      q2 = ::Image.joins(j2.join_sources).where(z)
  end

  if q1 && q2
    ::Image.from("((#{q1.to_sql}) UNION (#{q2.to_sql})) as images")
  elsif q1
    q1.distinct
  else
    q2.distinct
  end
end

#type_material_depictions_facetScope

Returns:

  • (Scope)


429
430
431
432
# File 'lib/queries/image/filter.rb', line 429

def type_material_depictions_facet
  return nil if type_material_depictions.nil?
  ::Image.joins(collection_objects: [:type_materials])
end

#type_materials_tableArel::Table

Returns:

  • (Arel::Table)


217
218
219
# File 'lib/queries/image/filter.rb', line 217

def type_materials_table
  ::TypeMaterial.arel_table
end