Module: OvertureMaps::DivisionSearch

Defined in:
lib/overture_maps/division_search.rb

Overview

Resolves location names to division areas. Prefers the locally imported overture_divisions table (fast, offline); falls back to querying the divisions theme on Overture's bucket. Both paths return the same shape: { id:, name:, subtype:, country:, region:, bbox: BoundingBox, area_km2: } ordered largest-area first.

Class Method Summary collapse

Class Method Details

.local(query:, limit: 20) ⇒ Object

Returns nil (fall back to remote) when the table is missing, empty, unreachable, or has no match for the query.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/overture_maps/division_search.rb', line 18

def local(query:, limit: 20)
  model = Models::Division
  return nil unless model.table_exists?

  rows = model.search_by_name(query).largest_first.limit(limit)
  return nil if rows.empty?

  rows.filter_map do |division|
    bbox = division.to_bounding_box
    next unless bbox

    {
      id: division.id,
      name: division.name,
      subtype: division.subtype,
      country: division.country,
      region: division.region,
      bbox: bbox,
      area_km2: Util.bbox_area_km2(bbox)
    }
  end.then { |results| results.empty? ? nil : results }
rescue StandardError
  nil
end

.search(query:, release: nil, limit: 20) ⇒ Object



11
12
13
14
# File 'lib/overture_maps/division_search.rb', line 11

def search(query:, release: nil, limit: 20)
  local(query: query, limit: limit) ||
    Import::Downloader.search_divisions(query: query, release: release, limit: limit)
end