Module: Queries::Concerns::Geo

Overview

Helpers for geo-related queries

!! You must call set_geo_params in initialize()

Instance Method Summary collapse

Instance Method Details

#collecting_events_for_geographic_item_condition(geographic_item_condition_sql) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/queries/concerns/geo.rb', line 117

def collecting_events_for_geographic_item_condition(geographic_item_condition_sql)
  # Through georeferences.
  a = ::CollectingEvent
    .joins(:geographic_items)
    .where(geographic_item_condition_sql)

  if !geo_collecting_event_geographic_area
    a
  else
    # Through geographic area.
    # (Note a union b is the same with/out the georef join here since CE
    # GAs contain all of their georefs - but speed doesn't seem to change
    # much either way.)
    b = ::CollectingEvent
      .joins(geographic_area: [:geographic_items])
      .left_joins(:georeferences)
      .where(georeferences: { id: nil })
      .where(geographic_item_condition_sql)

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

#param_shapes_by_typeObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/queries/concerns/geo.rb', line 51

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



44
45
46
47
48
49
# File 'lib/queries/concerns/geo.rb', line 44

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)
  @geo_collecting_event_geographic_area = boolean_param(params, :geo_collecting_event_geographic_area)
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
    


76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/queries/concerns/geo.rb', line 76

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



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

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
  when nil # exact
    a = shape.where(id: ids)
  when true #spatial
    if shape_string == 'GeographicArea'
      # In spatial mode GAs must have shape.
      a = shape.joins(:geographic_items).where(id: ids)
    else
      a = shape.where(id: ids)
    end
  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