Module: Queries::Concerns::Geo

Extended by:
ActiveSupport::Concern
Included in:
AssertedDistribution::Filter, Queries::CollectingEvent::Filter, Otu::Filter
Defined in:
lib/queries/concerns/geo.rb

Overview

Helpers for geo-related queries

!! You must call set_geo_params in initialize()

Concern specs are in

spec/lib/queries/source/filter_spec.rb

Instance Method Summary collapse

Instance Method Details

#gazetteer_id_facetObject



105
106
107
108
109
110
111
112
# File 'lib/queries/concerns/geo.rb', line 105

def gazetteer_id_facet
  return nil if gazetteer_id.empty?

  i = ::GeographicItem.joins(:gazetteers).where(gazetteers: { id: gazetteer_id })
  wkt_shape = ::Queries::GeographicItem.st_union(i).to_a.first['st_union'].to_s

  from_wkt(wkt_shape)
end

#param_shapes_by_typeObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/queries/concerns/geo.rb', line 45

def param_shapes_by_type
  geographic_area_ids = []
  gazetteer_ids = []
  i = 0
  geo_shape_id.each do |id|
    if geo_shape_type[i] == 'GeographicArea'
      geographic_area_ids << id
    else
      gazetteer_ids << id
    end
    i += 1
  end

  [geographic_area_ids, gazetteer_ids]
end

#set_geo_params(params) ⇒ Object



39
40
41
42
43
# File 'lib/queries/concerns/geo.rb', line 39

def set_geo_params(params)
  @geo_shape_type = params[:geo_shape_type]
  @geo_shape_id = integer_param(params, :geo_shape_id)
  @geo_mode = boolean_param(params, :geo_mode)
end

#shapes_for_geo_modeArray

Returns Lists of the shapes corresponding to the existing geo_shape_id, geo_shape_type, and geo_mode parameters, separated by shape type (GeographicArea and Gazetteer). For geo_mode ==

nil ("Exact"), returns shapes of each geo_shape_id/type
true ("Spatial") same as nil
false ("Descendants") returns shapes of each geo_shape_id/type and each
  of its descendants - each Gazetteer is considered a descendant of
  itself.

Returns:

  • (Array)

    Lists of the shapes corresponding to the existing geo_shape_id, geo_shape_type, and geo_mode parameters, separated by shape type (GeographicArea and Gazetteer). For geo_mode ==

    nil ("Exact"), returns shapes of each geo_shape_id/type
    true ("Spatial") same as nil
    false ("Descendants") returns shapes of each geo_shape_id/type and each
      of its descendants - each Gazetteer is considered a descendant of
      itself
    


70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/queries/concerns/geo.rb', line 70

def shapes_for_geo_mode
  geographic_area_ids, gazetteer_ids = param_shapes_by_type

  geographic_area_shapes = shapes_for_geo_mode_by_type(
      'GeographicArea', geographic_area_ids
    )
  gazetteer_shapes = shapes_for_geo_mode_by_type(
      'Gazetteer', gazetteer_ids
    )

  [geographic_area_shapes, gazetteer_shapes]
end

#shapes_for_geo_mode_by_type(shape_string, ids) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/queries/concerns/geo.rb', line 83

def shapes_for_geo_mode_by_type(shape_string, ids)
  shape = shape_string.constantize
  return shape.none if ids.empty?

  a = nil

  case geo_mode
  # exact and spatial start the same
  when nil, true
    a = shape.where(id: ids)
  when false # descendants
    if shape_string == 'Gazetteer'
      # For Gazetteers, descendants is the same as exact
      a = shape.where(id: ids)
    else
      a = shape.descendants_of_any(ids)
    end
  end

  a
end