Class: Queries::Observation::Filter

Inherits:
Query::Filter show all
Includes:
Concerns::Citations, Concerns::Confidences, Concerns::DataAttributes, Concerns::Depictions, Concerns::Geo, Concerns::Notes, Concerns::Protocols, Concerns::Tags
Defined in:
lib/queries/observation/filter.rb

Overview

!! TODO: needs tests

Constant Summary collapse

PARAMS =
[
  :character_state_id,
  :collection_object_id,
  :descendants,
  :descriptor_id,
  :descriptor_id,
  :geo_json,
  :geo_mode,
  :geo_shape_id,
  :geo_shape_type,
  :observation_id,
  :observation_matrix_id,
  :observation_object_global_id,
  :observation_object_id,
  :observation_object_type,
  :observation_type,
  :otu_id,
  :radius,
  :sound_id,
  :taxon_name_id,
  :wkt,

  charater_state_id: [],
  collection_object_id: [],
  descriptor_id: [],
  geo_shape_id: [],
  geo_shape_type: [],
  observation_id: [],
  observation_matrix_id: [],
  observation_object_type: [],
  observation_object_id: [],
  observation_type: [],
  otu_id: [],
  sound_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, #conveyance_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, #roll_call, #sound_query, #taxon_name_query, #taxon_name_relationship_query, #venn, #venn_ignore_pagination, #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, #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, #disable_paging, included_annotator_facets, instantiated_base_filter, inverted_subqueries, #model_id_facet, #object_global_id_facet, #only_project?, #paging_state, params, #permitted_params, #process_url_into_params, #project_id_facet, query_name, #set_nested_queries, #set_paging, set_paging, #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.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/queries/observation/filter.rb', line 105

def initialize(query_params)
  super

  @observation_id = params[:observation_id]
  @otu_id = params[:otu_id]
  @sound_id = params[:sound_id]
  @collection_object_id = params[:collection_object_id]
  @observation_object_global_id = params[:observation_object_global_id]
  @descriptor_id = params[:descriptor_id]
  @observation_type = params[:observation_type]
  @character_state_id = params[:character_state_id]
  @observation_matrix_id = params[:observation_matrix_id]
  @observation_object_id = params[:observation_object_id]
  @observation_object_type = params[:observation_object_type]

  @taxon_name_id = params[:taxon_name_id]
  @descendants = boolean_param(params, :descendants)

  set_confidences_params(params)
  set_data_attributes_params(params)
  set_protocols_params(params)
  set_citations_params(params)
  set_depiction_params(params)
  set_tags_params(params)
  set_notes_params(params)
  set_geo_params(params)
end

Instance Attribute Details

#character_state_idObject

Returns Array.

Returns:

  • Array



57
58
59
# File 'lib/queries/observation/filter.rb', line 57

def character_state_id
  @character_state_id
end

#collection_object_idObject

Returns Array.

Returns:

  • Array



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

def collection_object_id
  @collection_object_id
end

#descendantsObject

Returns Boolean.

Returns:

  • Boolean



63
64
65
# File 'lib/queries/observation/filter.rb', line 63

def descendants
  @descendants
end

#descriptor_idObject

Returns Array.

Returns:

  • Array



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

def descriptor_id
  @descriptor_id
end

#geo_jsonObject

Returns the value of attribute geo_json.



68
69
70
# File 'lib/queries/observation/filter.rb', line 68

def geo_json
  @geo_json
end

#observation_idObject

Returns Array.

Returns:

  • Array



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

def observation_id
  @observation_id
end

#observation_matrix_idArray

Returns:

  • (Array)


71
72
73
# File 'lib/queries/observation/filter.rb', line 71

def observation_matrix_id
  @observation_matrix_id
end

#observation_object_global_idObject

Returns String, nil.

Returns:

  • String, nil



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

def observation_object_global_id
  @observation_object_global_id
end

#observation_object_idObject

Returns Array.

Returns:

  • Array



80
81
82
# File 'lib/queries/observation/filter.rb', line 80

def observation_object_id
  @observation_object_id
end

#observation_object_typeObject

Returns Array.

Returns:

  • Array



83
84
85
# File 'lib/queries/observation/filter.rb', line 83

def observation_object_type
  @observation_object_type
end

#observation_typeObject

Returns Array.

Returns:

  • Array



77
78
79
# File 'lib/queries/observation/filter.rb', line 77

def observation_type
  @observation_type
end

#otu_idObject

Returns Array.

Returns:

  • Array



86
87
88
# File 'lib/queries/observation/filter.rb', line 86

def otu_id
  @otu_id
end

#radiusObject

Integer in Meters

!! defaults to 100m


90
91
92
# File 'lib/queries/observation/filter.rb', line 90

def radius
  @radius
end

#sound_idObject

Returns Array.

Returns:

  • Array



93
94
95
# File 'lib/queries/observation/filter.rb', line 93

def sound_id
  @sound_id
end

#taxon_name_idObject

Returns Array.

Returns:

  • Array



96
97
98
# File 'lib/queries/observation/filter.rb', line 96

def taxon_name_id
  @taxon_name_id
end

#wktObject

Returns String.

Returns:

  • String



99
100
101
# File 'lib/queries/observation/filter.rb', line 99

def wkt
  @wkt
end

Instance Method Details

#and_clausesObject



393
394
395
396
397
398
399
400
401
402
403
404
405
# File 'lib/queries/observation/filter.rb', line 393

def and_clauses
  [
    character_state_id_facet,
    collection_object_id_facet,
    descriptor_id_facet,
    observation_object_global_id_facet,
    observation_object_id_facet,
    observation_object_type_facet,
    observation_type_facet,
    otu_id_facet,
    sound_id_facet
  ]
end

#asserted_distribution_query_facetObject



370
371
372
373
374
375
376
# File 'lib/queries/observation/filter.rb', line 370

def asserted_distribution_query_facet
  return nil if asserted_distribution_query.nil?
  ::Observation
    .with(ad: asserted_distribution_query.all)
    .joins("JOIN ad ON observations.id = ad.asserted_distribution_object_id AND ad.asserted_distribution_object_type = 'Observation' AND observations.observation_object_type = 'Otu'")
    .distinct
end

#biological_association_geo_facet_by_type(shape_string, shape_ids) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/queries/observation/filter.rb', line 233

def biological_association_geo_facet_by_type(shape_string, shape_ids)
  case geo_mode
  when nil, false # exact, descendants
    ::Observation
      .joins("JOIN asserted_distributions ON asserted_distributions.asserted_distribution_object_id = observations.id AND asserted_distributions.asserted_distribution_object_type = 'Observation' AND observations.observation_object_type = 'Otu'")
      .where(asserted_distributions: {
        asserted_distribution_shape: shape_ids
     })
  when true # spatial
    m = shape_string.tableize
    b = ::GeographicItem.joins(m.to_sym).where(m => shape_ids)
  end
end

#character_state_id_facetArel::Node?

Returns:

  • (Arel::Node, nil)


308
309
310
311
# File 'lib/queries/observation/filter.rb', line 308

def character_state_id_facet
  return nil if character_state_id.empty?
  table[:character_state_id].in(character_state_id)
end

#collection_object_id_facetObject



296
297
298
299
# File 'lib/queries/observation/filter.rb', line 296

def collection_object_id_facet
  return nil if collection_object_id.empty?
  table[:observation_object_id].eq(collection_object_id).and(table[:observation_object_type].eq('CollectionObject'))
end

#descriptor_id_facetObject



333
334
335
336
# File 'lib/queries/observation/filter.rb', line 333

def descriptor_id_facet
  return nil if descriptor_id.empty?
  table[:descriptor_id].in(descriptor_id)
end

#descriptor_query_facetObject



338
339
340
341
342
343
344
345
346
347
# File 'lib/queries/observation/filter.rb', line 338

def descriptor_query_facet
  return nil if descriptor_query.nil?

  s = 'WITH query_desc_obs AS (' + descriptor_query.all.to_sql + ') ' +
    ::Observation
    .joins('JOIN query_desc_obs as query_desc_obs1 on observations.descriptor_id = query_desc_obs1.id')
    .to_sql

  ::Observation.from('(' + s + ') as observations')
end

#from_wkt(wkt_shape) ⇒ Object



182
183
184
185
186
187
188
189
190
191
# File 'lib/queries/observation/filter.rb', line 182

def from_wkt(wkt_shape)
  a = ::Queries::AssertedDistribution::Filter.new(
    wkt: wkt_shape, project_id:,
    asserted_distribution_object_type: 'Observation'
  )

  ::Observation
    .with(ad: a.all)
    .joins("JOIN ad ON ad.asserted_distribution_object_id = observations.id AND observations.observation_object_type = 'Otu'")
end

#geo_json_facetObject



193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/queries/observation/filter.rb', line 193

def geo_json_facet
  return nil if geo_json.blank?
  return ::Observation.none if roll_call

  a = ::Queries::AssertedDistribution::Filter.new(
    geo_json:, project_id:, radius:,
    asserted_distribution_object_type: 'Observation'
  )

  ::Observation
    .with(ad: a.all)
    .joins("JOIN ad ON ad.asserted_distribution_object_id = observations.id AND observations.observation_object_type = 'Otu'")
end

#matching_character_state_idArel::Node?

Returns:

  • (Arel::Node, nil)


292
293
294
# File 'lib/queries/observation/filter.rb', line 292

def matching_character_state_id
  character_state_id.blank? ? nil : table[:character_state_id].eq(character_state_id)
end

#merge_clausesObject



407
408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'lib/queries/observation/filter.rb', line 407

def merge_clauses
  [
    *OBSERVABLE_TYPES.collect{ |t| observable_facet(t.underscore) },
    descriptor_query_facet,
    observation_matrix_id_facet,

    taxon_name_query_facet,
    asserted_distribution_query_facet,
    taxon_name_id_facet,
    wkt_facet,
    geo_json_facet,
    observation_geo_facet,
  ]
end

#observable_facet(name) ⇒ Object



378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/queries/observation/filter.rb', line 378

def observable_facet(name)
  return nil if name.nil?

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

  return nil if q.nil?

  s = ::Observation
    .with(query_obs: q.all)
    .joins("JOIN query_obs ON observations.observation_object_id = query_obs.id AND observations.observation_object_type = '#{name.camelize}'")
    .to_sql

  ::Observation.from('(' + s + ') as observations')
end

#observation_geo_facetObject



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/queries/observation/filter.rb', line 207

def observation_geo_facet
  return nil if geo_shape_id.empty? || geo_shape_type.empty? ||
    # TODO: this should raise an error(?)
    geo_shape_id.length != geo_shape_type.length
  return ::Observation.none if roll_call

  geographic_area_shapes, gazetteer_shapes = shapes_for_geo_mode

  a = biological_association_geo_facet_by_type(
    'GeographicArea', geographic_area_shapes
  )

  b = biological_association_geo_facet_by_type(
    'Gazetteer', gazetteer_shapes
  )

  if geo_mode == true # spatial
    i = ::Queries.union(::GeographicItem, [a,b])
    u = ::Queries::GeographicItem.st_union_text(i).to_a.first

    return from_wkt(u['st_astext'])
  end

  referenced_klass_union([a,b])
end

#observation_matrix_id_facetObject



273
274
275
276
# File 'lib/queries/observation/filter.rb', line 273

def observation_matrix_id_facet
  return nil if observation_matrix_id.empty?
  ::Observation.in_observation_matrix(observation_matrix_id)
end

#observation_object_global_id_facetArel::Node?

Returns:

  • (Arel::Node, nil)


279
280
281
282
283
284
285
286
287
288
289
# File 'lib/queries/observation/filter.rb', line 279

def observation_object_global_id_facet
  return nil if observation_object_global_id.blank?

  # TODO - make a hash method to parameterize these values
  o = GlobalID::Locator.locate(observation_object_global_id)

  a = o.id
  b = o.class.base_class.name

  table[:observation_object_id].eq(a).and(table[:observation_object_type].eq(b))
end

#observation_object_id_facetObject



323
324
325
326
# File 'lib/queries/observation/filter.rb', line 323

def observation_object_id_facet
  return nil if observation_object_id.empty?
  table[:observation_object_id].in(observation_object_id)
end

#observation_object_type_facetObject



328
329
330
331
# File 'lib/queries/observation/filter.rb', line 328

def observation_object_type_facet
  return nil if observation_object_type.empty?
  table[:observation_object_type].in(observation_object_type)
end

#observation_type_facetArel::Node?

Returns:

  • (Arel::Node, nil)


302
303
304
305
# File 'lib/queries/observation/filter.rb', line 302

def observation_type_facet
  return nil if observation_type.empty?
  table[:type].in(observation_type)
end

#otu_id_facetObject



313
314
315
316
# File 'lib/queries/observation/filter.rb', line 313

def otu_id_facet
  return nil if otu_id.empty?
  table[:observation_object_id].in(otu_id).and(table[:observation_object_type].eq('Otu'))
end

#sound_id_facetObject



318
319
320
321
# File 'lib/queries/observation/filter.rb', line 318

def sound_id_facet
  return nil if sound_id.empty?
  table[:observation_object_id].in(sound_id).and(table[:observation_object_type].eq('Sound'))
end

#taxon_name_id_facetObject



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/queries/observation/filter.rb', line 247

def taxon_name_id_facet
  return nil if taxon_name_id.empty?

  if descendants
    t = ::TaxonName.descendants_of(taxon_name_id)
  else
    t = ::TaxonName.where(id: taxon_name_id)
  end

  a = ::Observation.joins("JOIN otus ON observations.observation_object_id = otus.id AND observations.observation_object_type = 'Otu'")
    .where(otus: {taxon_name_id: t})

  b = ::Observation.joins("JOIN collection_objects ON observations.observation_object_id = collection_objects.id AND observations.observation_object_type = 'CollectionObject'")
    .joins("JOIN taxon_determinations ON taxon_determinations.taxon_determination_object_id = collection_objects.id AND taxon_determinations.taxon_determination_object_type = 'CollectionObject'")
    .joins('JOIN otus ON taxon_determinations.otu_id = otus.id')
    .joins('JOIN taxon_names ON taxon_names.id = otus.id')
    .where(taxon_names: {id:  t})

  e = ::Queries::Extract::Filter.new(taxon_name_id:, descendants:, project_id:).all

  c = ::Observation.joins("JOIN extracts ON observations.observation_object_id = extracts.id AND observations.observation_object_type = 'Extract'")
    .where(extracts: {id: e})

  ::Observation.from( '(' + [a,b,c].collect{|q| "(#{q.to_sql})" }.join(' UNION ') + ') as observations' )
end

#taxon_name_query_facetObject



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/queries/observation/filter.rb', line 349

def taxon_name_query_facet
  return nil if taxon_name_query.nil?
  s = 'WITH query_tn_obs AS (' + taxon_name_query.all.to_sql + ') '

  a = ::Observation
    .joins("JOIN collection_objects on observations.observation_object_id = collection_objects.id and observations.observation_object_type = 'CollectionObject'")
    .joins("JOIN taxon_determinations on collection_objects.id = taxon_determinations.taxon_determination_object_id AND taxon_determinations.taxon_determination_objec_type = 'CollectionObject'")
    .joins('JOIN otus on taxon_determinations.otu_id = otus.id')
    .joins('JOIN query_tn_obs as query_tn_obs1 on otus.taxon_name_id = query_tn_obs1.id')
    .to_sql

  b = ::Observation
    .joins("JOIN otus on observations.observation_object_id = otus.id and observations.observation_object_type = 'Otu'")
    .joins('JOIN query_tn_obs as query_tn_obs2 on otus.taxon_name_id = query_tn_obs2.id')
    .to_sql

  s << ::Observation.from("((#{a}) UNION (#{b})) as observations").to_sql

  ::Observation.from('(' + s + ') as observations')
end

#wkt_facetObject



177
178
179
180
# File 'lib/queries/observation/filter.rb', line 177

def wkt_facet
  return nil if wkt.nil?
  from_wkt(wkt)
end