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,
  :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: [],
  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

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



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/queries/asserted_distribution/filter.rb', line 108

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]
  @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)


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

def asserted_distribution_id
  @asserted_distribution_id
end

#asserted_distribution_object_idArray

Parameters:

  • asserted_distribution_object_id (Array, Integer, String)

Returns:

  • (Array)


52
53
54
# File 'lib/queries/asserted_distribution/filter.rb', line 52

def asserted_distribution_object_id
  @asserted_distribution_object_id
end

#asserted_distribution_object_typeArray

Parameters:

  • asserted_distribution_object_type (Array, Integer, String)

Returns:

  • (Array)


56
57
58
# File 'lib/queries/asserted_distribution/filter.rb', line 56

def asserted_distribution_object_type
  @asserted_distribution_object_type
end

#asserted_distribution_shape_typeArray

Returns:

  • (Array)


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

def asserted_distribution_shape_type
  @asserted_distribution_shape_type
end

#biological_association_idArray

Parameters:

  • biological_association_id (Array, Integer, String)

Returns:

  • (Array)


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

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



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

def descendants
  @descendants
end

#geo_jsonObject

Returns the value of attribute geo_json.



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

def geo_json
  @geo_json
end

#geographic_area_idArray

Parameters:

  • geographic_area_id (Array, Integer, String)

Returns:

  • (Array)


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

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)
    


79
80
81
# File 'lib/queries/asserted_distribution/filter.rb', line 79

def geographic_area_mode
  @geographic_area_mode
end

#geographic_item_idArray

Parameters:

  • geographic_item_id (Array, Integer, String)

Returns:

  • (Array)


72
73
74
# File 'lib/queries/asserted_distribution/filter.rb', line 72

def geographic_item_id
  @geographic_item_id
end

#otu_idArray

Parameters:

  • otu_id (Array, Integer, String)

Returns:

  • (Array)


64
65
66
# File 'lib/queries/asserted_distribution/filter.rb', line 64

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



89
90
91
# File 'lib/queries/asserted_distribution/filter.rb', line 89

def presence
  @presence
end

#radiusObject

Integer in Meters

!! defaults to 100m


103
104
105
# File 'lib/queries/asserted_distribution/filter.rb', line 103

def radius
  @radius
end

#taxon_name_idArray

Parameters:

  • all (taxon name ids, nil)

    Otus matching these taxon names

Returns:

  • (Array)


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

def taxon_name_id
  @taxon_name_id
end

#wktObject

Returns the value of attribute wkt.



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

def wkt
  @wkt
end

Class Method Details

.from_geographic_items(geographic_items_sql) ⇒ Object



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

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



230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/queries/asserted_distribution/filter.rb', line 230

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



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

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



476
477
478
479
480
481
482
483
484
485
# File 'lib/queries/asserted_distribution/filter.rb', line 476

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



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

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



360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/queries/asserted_distribution/filter.rb', line 360

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



308
309
310
311
312
313
314
315
316
317
# File 'lib/queries/asserted_distribution/filter.rb', line 308

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



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

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



326
327
328
329
330
331
# File 'lib/queries/asserted_distribution/filter.rb', line 326

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



382
383
384
385
386
387
388
# File 'lib/queries/asserted_distribution/filter.rb', line 382

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



439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# File 'lib/queries/asserted_distribution/filter.rb', line 439

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



464
465
466
467
468
469
470
471
472
473
474
# File 'lib/queries/asserted_distribution/filter.rb', line 464

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



189
190
191
192
193
194
195
196
197
198
# File 'lib/queries/asserted_distribution/filter.rb', line 189

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



246
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
272
# File 'lib/queries/asserted_distribution/filter.rb', line 246

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



390
391
392
393
394
395
# File 'lib/queries/asserted_distribution/filter.rb', line 390

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



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

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

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

#observation_query_facetObject



421
422
423
424
425
426
427
# File 'lib/queries/asserted_distribution/filter.rb', line 421

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



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

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



412
413
414
415
416
417
418
419
# File 'lib/queries/asserted_distribution/filter.rb', line 412

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



175
176
177
178
179
180
181
182
# File 'lib/queries/asserted_distribution/filter.rb', line 175

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

#spatial_queryGeographicItem scope

Returns:



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/queries/asserted_distribution/filter.rb', line 275

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



397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/queries/asserted_distribution/filter.rb', line 397

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



429
430
431
432
433
434
435
436
437
# File 'lib/queries/asserted_distribution/filter.rb', line 429

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



184
185
186
187
# File 'lib/queries/asserted_distribution/filter.rb', line 184

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