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_shape_type,
  :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_shape_type: [],
  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, #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, #sound_query, #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, #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.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/queries/asserted_distribution/filter.rb', line 90

def initialize(query_params)
  super
  @asserted_distribution_id =
    integer_param(params, :asserted_distribution_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)


42
43
44
# File 'lib/queries/asserted_distribution/filter.rb', line 42

def asserted_distribution_id
  @asserted_distribution_id
end

#asserted_distribution_shape_typeArray

Returns:

  • (Array)


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

def asserted_distribution_shape_type
  @asserted_distribution_shape_type
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



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

def descendants
  @descendants
end

#geo_jsonObject

Returns the value of attribute geo_json.



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

def geo_json
  @geo_json
end

#geographic_area_idArray

Parameters:

  • geographic_area_id (Array, Integer, String)

Returns:

  • (Array)


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

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)
    


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

def geographic_area_mode
  @geographic_area_mode
end

#geographic_item_idArray

Parameters:

  • geographic_item_id (Array, Integer, String)

Returns:

  • (Array)


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

def geographic_item_id
  @geographic_item_id
end

#otu_idArray

Parameters:

  • otu_id (Array, Integer, String)

Returns:

  • (Array)


46
47
48
# File 'lib/queries/asserted_distribution/filter.rb', line 46

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



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

def presence
  @presence
end

#radiusObject

Integer in Meters

!! defaults to 100m


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

def radius
  @radius
end

#taxon_name_idArray

Parameters:

  • all (taxon name ids, nil)

    Otus matching these taxon names

Returns:

  • (Array)


76
77
78
# File 'lib/queries/asserted_distribution/filter.rb', line 76

def taxon_name_id
  @taxon_name_id
end

#wktObject

Returns the value of attribute wkt.



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

def wkt
  @wkt
end

Instance Method Details

#and_clausesObject



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

def and_clauses
  [
    otu_id_facet,
    presence_facet,
  ]
end

#asserted_distribution_geo_facetObject



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/queries/asserted_distribution/filter.rb', line 262

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

  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 # spatial
    i = ::Queries.union(::GeographicItem, [a,b])
    wkt_shape =
      ::Queries::GeographicItem.st_union(i)
        .to_a.first['st_union'].to_s

    return from_wkt(wkt_shape)
  end

  referenced_klass_union([a,b])
end

#asserted_distribution_geo_facet_by_type(shape_string, shape_ids) ⇒ Object



289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/queries/asserted_distribution/filter.rb', line 289

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_shape_type_facetObject



255
256
257
258
259
260
# File 'lib/queries/asserted_distribution/filter.rb', line 255

def asserted_distribution_shape_type_facet
  return nil if asserted_distribution_shape_type.empty?

  ::AssertedDistribution
    .where(asserted_distribution_shape_type:)
end

#biological_association_query_facetObject



350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/queries/asserted_distribution/filter.rb', line 350

def biological_association_query_facet
  return nil if biological_association_query.nil?
  s = 'WITH query_ad_ba AS (' + biological_association_query.all.to_sql + ') '

  a = ::AssertedDistribution
    .joins("JOIN query_ad_ba as query_ad_ba1 on asserted_distributions.otu_id = query_ad_ba1.biological_association_subject_id AND query_ad_ba1.biological_association_subject_type = 'Otu'")

  b = ::AssertedDistribution
    .joins("JOIN query_ad_ba as query_ad_ba2 on asserted_distributions.otu_id = query_ad_ba2.biological_association_object_id AND query_ad_ba2.biological_association_object_type = 'Otu'")

  s << referenced_klass_union([a,b]).to_sql

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

#dwc_occurrence_query_facetObject



365
366
367
368
369
370
371
372
373
374
375
# File 'lib/queries/asserted_distribution/filter.rb', line 365

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



153
154
155
156
157
158
# File 'lib/queries/asserted_distribution/filter.rb', line 153

def from_wkt(wkt_shape)
  a = from_wkt_geographic_area(wkt_shape)
  b = from_wkt_gazetteer(wkt_shape)

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

#from_wkt_gazetteer(wkt_shape) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/queries/asserted_distribution/filter.rb', line 178

def from_wkt_gazetteer(wkt_shape)
  i = ::GeographicItem.joins(:gazetteers).where(::GeographicItem.covered_by_wkt_sql(wkt_shape))

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

  s = 'WITH query_wkt_ad AS (' + j.to_sql + ') ' +
    ::AssertedDistribution
    .joins('JOIN query_wkt_ad as query_wkt_ad1 on query_wkt_ad1.id = asserted_distributions.asserted_distribution_shape_id')
    .where(asserted_distribution_shape_type: 'Gazetteer')
    .to_sql

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

#from_wkt_geographic_area(wkt_shape) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/queries/asserted_distribution/filter.rb', line 160

def from_wkt_geographic_area(wkt_shape)

  i = ::GeographicItem.joins(:geographic_areas).where(::GeographicItem.covered_by_wkt_sql(wkt_shape))

  j = ::GeographicArea.joins(:geographic_items).where(geographic_items: i)
  k = ::GeographicArea.descendants_of(j) # Add children that might not be caught because they don't have shapes

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

  s = 'WITH query_wkt_ad AS (' + l.all.to_sql + ') ' +
    ::AssertedDistribution
    .joins('JOIN query_wkt_ad as query_wkt_ad1 on query_wkt_ad1.id = asserted_distributions.asserted_distribution_shape_id')
    .where(asserted_distribution_shape_type: 'GeographicArea')
    .to_sql

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

#geo_json_facetObject

Shape is a Hash in GeoJSON format



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/queries/asserted_distribution/filter.rb', line 193

def geo_json_facet
  return nil if geo_json.nil?

  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



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

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



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

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,
    wkt_facet,
    asserted_distribution_shape_type_facet
  ]
end

#otu_id_facetObject



304
305
306
307
# File 'lib/queries/asserted_distribution/filter.rb', line 304

def otu_id_facet
  return nil if otu_id.empty?
  table[:otu_id].in(otu_id)
end

#otu_query_facetObject



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

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.otu_id')
    .to_sql
  ::AssertedDistribution.from('(' + s + ') as asserted_distributions')
end

#presence_facetObject



139
140
141
142
143
144
145
146
# File 'lib/queries/asserted_distribution/filter.rb', line 139

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:



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/queries/asserted_distribution/filter.rb', line 222

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



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

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.joins(:otu).joins(j.join_sources).where(z)
  else
    ::AssertedDistribution.joins(:otu).where(otus: {taxon_name_id:})
  end
end

#taxon_name_query_facetObject



340
341
342
343
344
345
346
347
348
# File 'lib/queries/asserted_distribution/filter.rb', line 340

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
    .joins(:otu)
    .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')
end

#wkt_facetObject



148
149
150
151
# File 'lib/queries/asserted_distribution/filter.rb', line 148

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