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,
  :otu_scope,
  :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, #depiction_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)


132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/queries/image/filter.rb', line 132

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



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

def biocuration_class_id
  @biocuration_class_id
end

#collection_object_idArray

Returns images depicting collecting_object.

Returns:

  • (Array)

    images depicting collecting_object



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

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
    


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

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)


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

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



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

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



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

def freeform_svg
  @freeform_svg
end

#image_idArray

Returns:

  • (Array)


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

def image_id
  @image_id
end

#otu_idArray

Returns images depicting otus.

Returns:

  • (Array)

    images depicting otus



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

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)


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

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



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

def sled_image
  @sled_image
end

#sled_image_idArray

Returns:

  • (Array)


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

def sled_image_id
  @sled_image_id
end

#sqed_depiction_idArray

Returns:

  • (Array)


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

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



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

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



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

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



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

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



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

def type_material_depictions
  @type_material_depictions
end

Instance Method Details

#biocuration_facetObject



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

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



426
427
428
429
# File 'lib/queries/image/filter.rb', line 426

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



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

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



358
359
360
# File 'lib/queries/image/filter.rb', line 358

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

#collection_object_facet_observationsObject



362
363
364
# File 'lib/queries/image/filter.rb', line 362

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



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/queries/image/filter.rb', line 333

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)


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

def collection_object_table
  ::CollectionObject.arel_table
end

#depiction_object_type_facetObject



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

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)


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

def depiction_table
  ::Depiction.arel_table
end

#depictions_facetObject



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

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

#freeform_svg_facetObject



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

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



502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
# File 'lib/queries/image/filter.rb', line 502

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



409
410
411
412
413
# File 'lib/queries/image/filter.rb', line 409

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



404
405
406
407
# File 'lib/queries/image/filter.rb', line 404

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



415
416
417
418
# File 'lib/queries/image/filter.rb', line 415

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



400
401
402
# File 'lib/queries/image/filter.rb', line 400

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



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/queries/image/filter.rb', line 381

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



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

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



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

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



298
299
300
301
302
303
304
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
# File 'lib/queries/image/filter.rb', line 298

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

  otu_ids += ::Otu.coordinate_otu_ids(otu_id) 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)


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

def otu_table
  ::Otu.arel_table
end

#query_facets_facet(name = nil) ⇒ Object



484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
# File 'lib/queries/image/filter.rb', line 484

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



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

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



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

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



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

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



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

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)


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

def taxon_determination_table
  ::TaxonDetermination.arel_table
end

#taxon_name_id_facetObject



431
432
433
434
435
436
437
438
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
# File 'lib/queries/image/filter.rb', line 431

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)


421
422
423
424
# File 'lib/queries/image/filter.rb', line 421

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)


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

def type_materials_table
  ::TypeMaterial.arel_table
end