Class: Queries::AssertedDistribution::Filter

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

Constant Summary collapse

PARAMS =
[
  :asserted_distribution_id,
  :descendants,
  :geo_json,
  :geographic_area_id,
  :geographic_item_id,
  :geographic_area_mode,
  :otu_id,
  :presence,
  :radius,
  :taxon_name_id,
  :wkt,
  asserted_distribution_id: [],
  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, #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.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/queries/asserted_distribution/filter.rb', line 78

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)
  @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
  @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_notes_params(params)
  set_tags_params(params)
end

Instance Attribute Details

#asserted_distribution_idArray

Parameters:

  • asserted_distribution_id (Array, Integer, String)

Returns:

  • (Array)


33
34
35
# File 'lib/queries/asserted_distribution/filter.rb', line 33

def asserted_distribution_id
  @asserted_distribution_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



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

def descendants
  @descendants
end

#geo_jsonObject

Returns the value of attribute geo_json.



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

def geo_json
  @geo_json
end

#geographic_area_idArray

Parameters:

  • geographic_area_id (Array, Integer, String)

Returns:

  • (Array)


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

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)
    


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

def geographic_area_mode
  @geographic_area_mode
end

#geographic_item_idArray

Parameters:

  • geographic_item_id (Array, Integer, String)

Returns:

  • (Array)


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

def geographic_item_id
  @geographic_item_id
end

#otu_idArray

Parameters:

  • otu_id (Array, Integer, String)

Returns:

  • (Array)


37
38
39
# File 'lib/queries/asserted_distribution/filter.rb', line 37

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



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

def presence
  @presence
end

#radiusObject

Integer in Meters

!! defaults to 100m


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

def radius
  @radius
end

#taxon_name_idArray

Parameters:

  • all (taxon name ids, nil)

    Otus matching these taxon names

Returns:

  • (Array)


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

def taxon_name_id
  @taxon_name_id
end

#wktObject

Returns the value of attribute wkt.



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

def wkt
  @wkt
end

Instance Method Details

#and_clausesObject



285
286
287
288
289
290
# File 'lib/queries/asserted_distribution/filter.rb', line 285

def and_clauses
  [
    otu_id_facet,
    presence_facet,
  ]
end

#biological_association_query_facetObject



258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/queries/asserted_distribution/filter.rb', line 258

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



273
274
275
276
277
278
279
280
281
282
283
# File 'lib/queries/asserted_distribution/filter.rb', line 273

def dwc_occurrence_query_facet
  return nil if dwc_occurrence_query.nil?

   s = ::AssertedDistribution
    .with(query_dwc_ad: dwc_occurrence_query.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



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/queries/asserted_distribution/filter.rb', line 134

def from_wkt(wkt_shape)
  i = ::GeographicItem.joins(:geographic_areas).where(::GeographicItem.contained_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 a 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.geographic_area_id')
    .to_sql

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

#geo_json_facetObject

Shape is a Hash in GeoJSON format



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/queries/asserted_distribution/filter.rb', line 151

def geo_json_facet
  return nil if geo_json.nil?
  if i = spatial_query

    # All spatial records
    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)

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

    return ::AssertedDistribution.where( geographic_area: l )
  else
    return nil
  end
end

#geographic_area_id_facetObject



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/queries/asserted_distribution/filter.rb', line 186

def geographic_area_id_facet
  return nil if geographic_area_id.empty?

  a = nil

  case geographic_area_mode
  when nil, true # exact and spatial start the same
    a = ::GeographicArea.where(id: geographic_area_id)
  when false # descendants
    a = ::GeographicArea.descendants_of_any(geographic_area_id)
  end

  b = nil # from AssertedDistributions

  case geographic_area_mode
  when nil, false # exact, descendants
    b = ::AssertedDistribution.where(geographic_area: a)
  when true # spatial
    i = ::GeographicItem.joins(:geographic_areas).where(geographic_areas: a) # .unscope
    wkt_shape = ::GeographicItem.st_union(i).to_a.first['collection'].to_s # todo, check
    return from_wkt(wkt_shape)
  end

  b
end

#geographic_item_id_facetObject



217
218
219
220
221
222
# File 'lib/queries/asserted_distribution/filter.rb', line 217

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



292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/queries/asserted_distribution/filter.rb', line 292

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

    geographic_area_id_facet,
    geographic_item_id_facet,
    taxon_name_id_facet,
    wkt_facet,
  ]
end

#otu_id_facetObject



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

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

#otu_query_facetObject



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

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



120
121
122
123
124
125
126
127
# File 'lib/queries/asserted_distribution/filter.rb', line 120

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:



171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/queries/asserted_distribution/filter.rb', line 171

def spatial_query
  if geometry = RGeo::GeoJSON.decode(geo_json)
    case geometry.geometry_type.to_s
    when 'Point'
      ::GeographicItem.joins(:geographic_areas).where( ::GeographicItem.within_radius_of_wkt_sql(geometry.to_s, radius ) )
    when 'Polygon', 'MultiPolygon'
      ::GeographicItem.joins(:geographic_areas).where(::GeographicItem.contained_by_wkt_sql(geometry.to_s))
    else
      nil
    end
  else
    nil
  end
end

#taxon_name_id_facetObject



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

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



248
249
250
251
252
253
254
255
256
# File 'lib/queries/asserted_distribution/filter.rb', line 248

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



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

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