Class: Queries::AssertedDistribution::Filter

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

Constant Summary collapse

PARAMS =
[
  :asserted_distribution_id,
  :asserted_distribution_object_id,
  :asserted_distribution_object_type,
  :asserted_distribution_shape_type,
  :biological_association_id,
  :geo_shape_id,
  :geo_mode,
  :geo_shape_type,
  :descendants,
  :geo_json,
  :geographic_area_id,
  :geographic_item_id,
  :geographic_area_mode,
  :otu_id,
  :presence,
  :radius,
  :source_id,
  :shape_type,
  :taxon_name_id,
  :wkt,
  asserted_distribution_id: [],
  asserted_distribution_object_id: [],
  asserted_distribution_object_type: [],
  asserted_distribution_shape_type: [],
  biological_association_id: [],
  geo_shape_id: [],
  geo_shape_type: [],
  geographic_area_id: [],
  geographic_item_id: [],
  otu_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

#anatomical_part_query, #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

Class Method Summary collapse

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?, #only_project_or_less?, #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.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/queries/asserted_distribution/filter.rb', line 114

def initialize(query_params)
  super
  @asserted_distribution_id =
    integer_param(params, :asserted_distribution_id)
  @asserted_distribution_object_id =
    integer_param(params, :asserted_distribution_object_id)
  @asserted_distribution_object_type =
    params[:asserted_distribution_object_type]
  @biological_association_id =
    integer_param(params, :biological_association_id)
  @descendants = boolean_param(params, :descendants)
  @geo_json = params[:geo_json]
  @geographic_area_id = integer_param(params,:geographic_area_id)
  @geographic_item_id = integer_param(params, :geographic_item_id)
  @geographic_area_mode = boolean_param(params, :geographic_area_mode)
  @otu_id = integer_param(params, :otu_id)
  @presence = boolean_param(params, :presence)
  @radius = params[:radius].presence || 100.0
  @asserted_distribution_shape_type =
    params[:asserted_distribution_shape_type]
  @source_id = integer_param(params, :source_id)
  @taxon_name_id = integer_param(params, :taxon_name_id)
  @wkt = params[:wkt]

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

Instance Attribute Details

#asserted_distribution_idArray

Parameters:

  • asserted_distribution_id (Array, Integer, String)

Returns:

  • (Array)


50
51
52
# File 'lib/queries/asserted_distribution/filter.rb', line 50

def asserted_distribution_id
  @asserted_distribution_id
end

#asserted_distribution_object_idArray

Parameters:

  • asserted_distribution_object_id (Array, Integer, String)

Returns:

  • (Array)


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

def asserted_distribution_object_id
  @asserted_distribution_object_id
end

#asserted_distribution_object_typeArray

Parameters:

  • asserted_distribution_object_type (Array, Integer, String)

Returns:

  • (Array)


58
59
60
# File 'lib/queries/asserted_distribution/filter.rb', line 58

def asserted_distribution_object_type
  @asserted_distribution_object_type
end

#asserted_distribution_shape_typeArray

Returns:

  • (Array)


112
113
114
# File 'lib/queries/asserted_distribution/filter.rb', line 112

def asserted_distribution_shape_type
  @asserted_distribution_shape_type
end

#biological_association_idArray

Parameters:

  • biological_association_id (Array, Integer, String)

Returns:

  • (Array)


62
63
64
# File 'lib/queries/asserted_distribution/filter.rb', line 62

def biological_association_id
  @biological_association_id
end

#descendantsBoolean?

Returns true - include descendants of taxon_name_id in scope false, nil - only exact matches.

Returns:

  • (Boolean, nil)

    true - include descendants of taxon_name_id in scope false, nil - only exact matches



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

def descendants
  @descendants
end

#geo_jsonObject

Returns the value of attribute geo_json.



85
86
87
# File 'lib/queries/asserted_distribution/filter.rb', line 85

def geo_json
  @geo_json
end

#geographic_area_idArray

Parameters:

  • geographic_area_id (Array, Integer, String)

Returns:

  • (Array)


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

def geographic_area_id
  @geographic_area_id
end

#geographic_area_modeBoolean?

Returns How to treat GeographicAreas

nil - non-spatial match by only those records matching the geographic_area_id exactly
true - spatial match
false - non-spatial match (descendants).

Returns:

  • (Boolean, nil)

    How to treat GeographicAreas

    nil - non-spatial match by only those records matching the geographic_area_id exactly
    true - spatial match
    false - non-spatial match (descendants)
    


81
82
83
# File 'lib/queries/asserted_distribution/filter.rb', line 81

def geographic_area_mode
  @geographic_area_mode
end

#geographic_item_idArray

Parameters:

  • geographic_item_id (Array, Integer, String)

Returns:

  • (Array)


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

def geographic_item_id
  @geographic_item_id
end

#otu_idArray

Parameters:

  • otu_id (Array, Integer, String)

Returns:

  • (Array)


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

def otu_id
  @otu_id
end

#presenceBoolean?

Returns true - Return AssertedDistributions where the OTU is asserted as present according to the Source false - Return AssertedDistributions where the OTU is asserted as absent according to the Source nil - both.

Returns:

  • (Boolean, nil)

    true - Return AssertedDistributions where the OTU is asserted as present according to the Source false - Return AssertedDistributions where the OTU is asserted as absent according to the Source nil - both



91
92
93
# File 'lib/queries/asserted_distribution/filter.rb', line 91

def presence
  @presence
end

#radiusObject

Integer in Meters

!! defaults to 100m


109
110
111
# File 'lib/queries/asserted_distribution/filter.rb', line 109

def radius
  @radius
end

#source_idArray

Parameters:

  • source_id (Array, Integer, String)

Returns:

  • (Array)


100
101
102
# File 'lib/queries/asserted_distribution/filter.rb', line 100

def source_id
  @source_id
end

#taxon_name_idArray

Parameters:

  • all (taxon name ids, nil)

    Otus matching these taxon names

Returns:

  • (Array)


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

def taxon_name_id
  @taxon_name_id
end

#wktObject

Returns the value of attribute wkt.



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

def wkt
  @wkt
end

Class Method Details

.from_geographic_items(geographic_items_sql) ⇒ Object



211
212
213
214
215
216
217
218
# File 'lib/queries/asserted_distribution/filter.rb', line 211

def self.from_geographic_items(geographic_items_sql)
  # TODO: can we combine GAs and GAZs *before* joining with
  # geographic_items_sql? That would save a spatial contains query.
  a = self.geographic_areas_for_geographic_items(geographic_items_sql)
  b = self.gazetteers_for_geographic_items(geographic_items_sql)

  ::Queries.union(::AssertedDistribution, [a,b])
end

.gazetteers_for_geographic_items(geographic_items_sql) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/queries/asserted_distribution/filter.rb', line 241

def self.gazetteers_for_geographic_items(geographic_items_sql)
  i = ::GeographicItem.joins(:gazetteers).where(geographic_items_sql)

  gazetteer_ids = ::Gazetteer
    .joins(:geographic_item).merge(i).pluck(:id)

  if gazetteer_ids.empty?
    return ::AssertedDistribution.none
  end

  ::AssertedDistribution
    .where(asserted_distribution_shape_id: gazetteer_ids,
      asserted_distribution_shape_type: 'Gazetteer')
end

.geographic_areas_for_geographic_items(geographic_items_sql) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/queries/asserted_distribution/filter.rb', line 220

def self.geographic_areas_for_geographic_items(geographic_items_sql)
  i = ::GeographicItem
    .joins(:geographic_areas)
    .where(geographic_items_sql)

  j = ::GeographicArea.joins(:geographic_items).merge(i).pluck(:id)

  # TODO: There are only 58 GAs with shape that have a descendant without
  # shape: could this be a lookup table instead?
  k = ::GeographicArea.descendants_of(j).pluck(:id) # Add children that might not be caught because they don't have shapes

  geographic_area_ids = (j + k).uniq
  if geographic_area_ids.empty?
    return ::AssertedDistribution.none
  end

  ::AssertedDistribution
    .where(asserted_distribution_shape_id: geographic_area_ids,
      asserted_distribution_shape_type: 'GeographicArea')
end

Instance Method Details

#and_clausesObject



496
497
498
499
500
501
502
503
504
505
# File 'lib/queries/asserted_distribution/filter.rb', line 496

def and_clauses
  [
    biological_association_id_facet,
    otu_id_facet,
    presence_facet,
    asserted_distribution_shape_type_facet,
    asserted_distribution_object_type_facet,
    asserted_distribution_object_facet,
  ]
end

#asserted_distribution_geo_facetObject



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/queries/asserted_distribution/filter.rb', line 344

def asserted_distribution_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 ::AssertedDistribution.none if roll_call

  geographic_area_shapes, gazetteer_shapes = shapes_for_geo_mode

  a = asserted_distribution_geo_facet_by_type(
    'GeographicArea', geographic_area_shapes
  )

  b = asserted_distribution_geo_facet_by_type(
    'Gazetteer', gazetteer_shapes
  )

  if geo_mode != true # exact or descendants
    return referenced_klass_union([a,b])
  end

  # Spatial.
  i = ::Queries.union(::GeographicItem, [a,b])
  self.class.from_geographic_items(
    ::GeographicItem.covered_by_geographic_items_sql(i)
  )
end

#asserted_distribution_geo_facet_by_type(shape_string, shape_ids) ⇒ Object



371
372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/queries/asserted_distribution/filter.rb', line 371

def asserted_distribution_geo_facet_by_type(shape_string, shape_ids)
  b = nil

  case geo_mode
  when nil, false # exact, descendants
    b = ::AssertedDistribution
      .where(asserted_distribution_shape: shape_ids)
  when true # spatial
    m = shape_string.tableize
    b = ::GeographicItem.joins(m.to_sym).where(m => shape_ids)
  end

  b
end

#asserted_distribution_object_facetObject



319
320
321
322
323
324
325
326
327
328
# File 'lib/queries/asserted_distribution/filter.rb', line 319

def asserted_distribution_object_facet
  return nil if asserted_distribution_object_type.length != 1 ||
    asserted_distribution_object_id.empty?

  table[:asserted_distribution_object_id]
    .in(asserted_distribution_object_id).and(
      table[:asserted_distribution_object_type]
        .eq(asserted_distribution_object_type.first)
    )
end

#asserted_distribution_object_type_facetObject



330
331
332
333
334
335
# File 'lib/queries/asserted_distribution/filter.rb', line 330

def asserted_distribution_object_type_facet
  return nil if asserted_distribution_object_type.empty?

  table[:asserted_distribution_object_type]
    .in(asserted_distribution_object_type)
end

#asserted_distribution_shape_type_facetObject



337
338
339
340
341
342
# File 'lib/queries/asserted_distribution/filter.rb', line 337

def asserted_distribution_shape_type_facet
  return nil if asserted_distribution_shape_type.empty?

  table[:asserted_distribution_shape_type]
    .in(asserted_distribution_shape_type)
end

#biological_association_id_facetObject



393
394
395
396
397
398
399
# File 'lib/queries/asserted_distribution/filter.rb', line 393

def biological_association_id_facet
  return nil if biological_association_id.empty?
  table[:asserted_distribution_object_type].eq('BiologicalAssociation')
    .and(table[:asserted_distribution_object_id]
      .in(biological_association_id)
    )
end

#biological_association_query_facetObject



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/asserted_distribution/filter.rb', line 459

def biological_association_query_facet
  return nil if biological_association_query.nil?
  # On the BA itself.
  a = ::AssertedDistribution
    .with(ba: biological_association_query.all)
    .joins("JOIN ba ON ba.id = asserted_distributions.asserted_distribution_object_id AND asserted_distributions.asserted_distribution_object_type = 'BiologicalAssociation'")

  # On subject/object of the BA.
  b = ::AssertedDistribution
    .with(ba: biological_association_query.all)
    .joins("JOIN ba ON asserted_distributions.asserted_distribution_object_id = ba.biological_association_subject_id AND ba.biological_association_subject_type = 'Otu'")

  c = ::AssertedDistribution
    .with(ba: biological_association_query.all)
    .joins("JOIN ba ON asserted_distributions.asserted_distribution_object_id = ba.biological_association_object_id AND ba.biological_association_object_type = 'Otu'")

  # On a graph containing the BA.
  d = ::AssertedDistribution
    .with(ba: biological_association_query.all)
    .joins("JOIN biological_associations_graphs ON biological_associations_graphs.id = asserted_distributions.asserted_distribution_object_id AND asserted_distributions.asserted_distribution_object_type = 'BiologicalAssociationsGraph' " \
    "JOIN biological_associations_biological_associations_graphs ON biological_associations_biological_associations_graphs.biological_associations_graph_id = biological_associations_graphs.id JOIN ba ON ba.id = biological_associations_biological_associations_graphs.biological_association_id")

    referenced_klass_union([a,b,c,d])
end

#dwc_occurrence_query_facetObject



484
485
486
487
488
489
490
491
492
493
494
# File 'lib/queries/asserted_distribution/filter.rb', line 484

def dwc_occurrence_query_facet
  return nil if dwc_occurrence_query.nil?

   s = ::AssertedDistribution
    .with(query_dwc_ad: dwc_occurrence_query.all.select(:dwc_occurrence_object_id, :dwc_occurrence_object_type, :id))
    .joins(:dwc_occurrence)
    .joins('JOIN query_dwc_ad as query_dwc_ad1 on query_dwc_ad1.id = dwc_occurrences.id')
    .to_sql

  ::AssertedDistribution.from('(' + s + ') as asserted_distributions').distinct
end

#from_wkt(wkt_shape) ⇒ Object



200
201
202
203
204
205
206
207
208
209
# File 'lib/queries/asserted_distribution/filter.rb', line 200

def from_wkt(wkt_shape)
  a = self.class.geographic_areas_for_geographic_items(
    ::GeographicItem.covered_by_wkt_sql(wkt_shape)
  )
  b = self.class.gazetteers_for_geographic_items(
    ::GeographicItem.covered_by_wkt_sql(wkt_shape)
  )

  ::Queries.union(::AssertedDistribution, [a, b])
end

#geo_json_facetObject

Shape is a Hash in GeoJSON format



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/queries/asserted_distribution/filter.rb', line 257

def geo_json_facet
  return nil if geo_json.nil?
  return ::AssertedDistribution.none if roll_call

  i = spatial_query
  return nil if i.nil?

  j = ::GeographicArea.joins(:geographic_items).where(geographic_items: i)

  # Expand to include all descendants of any spatial match!
  # We only care about areas actually used here.
  k = ::GeographicArea.joins(:asserted_distributions).descendants_of(j)

  ga_ids = ::GeographicArea.from("((#{j.to_sql}) UNION (#{k.to_sql})) as geographic_areas").distinct

  gz_ids = ::Gazetteer.joins(:geographic_item).where(geographic_item: i)

  return ::AssertedDistribution
    .where( asserted_distribution_shape: ga_ids )
    .where( asserted_distribution_shape_type: 'GeographicArea')
    .or(
      ::AssertedDistribution
        .where( asserted_distribution_shape: gz_ids )
        .where( asserted_distribution_shape_type: 'Gazetteer')
    )

end

#geographic_item_id_facetObject



401
402
403
404
405
406
# File 'lib/queries/asserted_distribution/filter.rb', line 401

def geographic_item_id_facet
  return nil if geographic_item_id.empty?
  ::GeographicArea.joins(:geographic_areas_geographic_items).where(
    geographic_areas_geographic_items: {geographic_item_id:}
  )
end

#merge_clausesObject



507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
# File 'lib/queries/asserted_distribution/filter.rb', line 507

def merge_clauses
  [
    dwc_occurrence_query_facet,
    biological_association_query_facet,
    geo_json_facet,
    otu_query_facet,
    taxon_name_query_facet,
    source_id_facet,

    asserted_distribution_geo_facet,
    geographic_item_id_facet,
    taxon_name_id_facet,
    observation_query_facet,
    wkt_facet,
  ]
end

#observation_query_facetObject



441
442
443
444
445
446
447
# File 'lib/queries/asserted_distribution/filter.rb', line 441

def observation_query_facet
  return nil if observation_query.nil?
  ::AssertedDistribution
    .with(obs: observation_query.all)
    .joins("JOIN obs ON obs.id = asserted_distributions.asserted_distribution_object_id AND asserted_distributions.asserted_distribution_object_type = 'Observation'")
    .distinct
end

#otu_id_facetObject



386
387
388
389
390
391
# File 'lib/queries/asserted_distribution/filter.rb', line 386

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

#otu_query_facetObject



432
433
434
435
436
437
438
439
# File 'lib/queries/asserted_distribution/filter.rb', line 432

def otu_query_facet
  return nil if otu_query.nil?
  s = 'WITH query_otu_ad AS (' + otu_query.all.to_sql + ') ' +
    ::AssertedDistribution
    .joins("JOIN query_otu_ad AS query_otu_ad1 ON query_otu_ad1.id = asserted_distributions.asserted_distribution_object_id AND asserted_distributions.asserted_distribution_object_type = 'Otu'")
    .to_sql
  ::AssertedDistribution.from('(' + s + ') as asserted_distributions')
end

#presence_facetObject



186
187
188
189
190
191
192
193
# File 'lib/queries/asserted_distribution/filter.rb', line 186

def presence_facet
  return nil if presence.nil?
  if presence
    table[:is_absent].eq_any(['f', nil]) # !! not eq_any()
  else
    table[:is_absent].eq('t')
  end
end

#source_id_facetObject



423
424
425
426
427
428
429
430
# File 'lib/queries/asserted_distribution/filter.rb', line 423

def source_id_facet
  return nil if source_id.empty?

  ::AssertedDistribution
    .joins(:citations)
    .where(citations: { source_id: })
    .distinct
end

#spatial_queryGeographicItem scope

Returns:



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/queries/asserted_distribution/filter.rb', line 286

def spatial_query
  if geometry = RGeo::GeoJSON.decode(geo_json)
    case geometry.geometry_type.to_s
    when 'Point'
      a = ::GeographicItem
        .joins(:geographic_areas)
        .where(
          ::GeographicItem.within_radius_of_wkt_sql(geometry.to_s, radius)
        )
      b = ::GeographicItem
        .joins(:gazetteers)
        .where(
          ::GeographicItem.within_radius_of_wkt_sql(geometry.to_s, radius)
        )

      ::Queries.union(::GeographicItem, [a, b])
    when 'Polygon', 'MultiPolygon'
      a = ::GeographicItem
        .joins(:geographic_areas)
        .where(::GeographicItem.covered_by_wkt_sql(geometry.to_s))
      b = ::GeographicItem
        .joins(:gazetteers)
        .where(::GeographicItem.covered_by_wkt_sql(geometry.to_s))

      ::Queries.union(::GeographicItem, [a, b])
    else
      nil
    end
  else
    nil
  end
end

#taxon_name_id_facetObject



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

def taxon_name_id_facet
  return nil if taxon_name_id.empty?
  if descendants
    h = Arel::Table.new(:taxon_name_hierarchies)
    o = Arel::Table.new(:otus)

    j = o.join(h, Arel::Nodes::InnerJoin).on(o[:taxon_name_id].eq(h[:descendant_id]))
    z = h[:ancestor_id].in(taxon_name_id)

    ::AssertedDistribution.with_otus.joins(j.join_sources).where(z)
  else
    ::AssertedDistribution.with_otus.where(otus: {taxon_name_id:})
  end
end

#taxon_name_query_facetObject



449
450
451
452
453
454
455
456
457
# File 'lib/queries/asserted_distribution/filter.rb', line 449

def taxon_name_query_facet
  return nil if taxon_name_query.nil?
  s = 'WITH query_tn_ad AS (' + taxon_name_query.all.to_sql + ') ' +
    ::AssertedDistribution
    .with_otus
    .joins('JOIN query_tn_ad as query_tn_ad1 on query_tn_ad1.id = otus.taxon_name_id')
    .to_sql
  ::AssertedDistribution.from('(' + s + ') as asserted_distributions').distinct
end

#wkt_facetObject



195
196
197
198
# File 'lib/queries/asserted_distribution/filter.rb', line 195

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