Class: GeoRb::GeoCoders::Nominatim

Inherits:
Base
  • Object
show all
Defined in:
lib/geo_rb/geo_coders/nominatim.rb

Constant Summary collapse

DEFAULT_NOMINATIM_DOMAIN =
"https://nominatim.openstreetmap.org".freeze
API_PATHS =
{
  search: "/search",
  reverse: "/reverse"
}.freeze
STRUCTURED_QUERY_PARAMS =
%w[street city county state country postalcode].freeze

Instance Method Summary collapse

Methods inherited from Base

#call

Instance Method Details

#geocode(query, detailed: false, language: nil, country_codes: [], viewbox: nil, exactly_one: true, limit: nil, bounded: false) ⇒ Object

Parameters:

  • query: (String | Hash)

    query can be a string or a hash for a structured query whose keys are one of: ‘street`, `city`, `county`, `state`, `country`, `postalcode`. For more information, see Nominatim’s documentation for ‘structured requests`: nominatim.org/release-docs/develop/api/Search

  • detailed: (Boolean) (defaults to: false)

    Include a breakdown of the address into elements

  • exactly_one: (Boolean) (defaults to: true)

    Return one result or a list of results, if available.

  • limit: (nil | Integer) (defaults to: nil)

    Maximum amount of results to return from Nominatim. Unless exactly_one is set to False, limit will always be 1.

  • country_codes: (Array) (defaults to: [])

    Limit search results to a specific country (or a list of countries). A country_code should be the ISO 3166-1alpha2 code, e.g. “gb“ for the United Kingdom, “de“ for Germany, etc.

  • viewbox: (nil | Array) (defaults to: nil)

    refer this area to find search results. Requires an array of Pointer. By default this is treated as a hint, if you want to restrict results to this area, specify “bounded=True“ as well.

  • bounded: (Boolean) (defaults to: false)

    Restrict the results to only items contained within the bounding “viewbox“.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/geo_rb/geo_coders/nominatim.rb', line 30

def geocode(query, detailed: false, language: nil, country_codes: [], viewbox: nil, exactly_one: true, limit: nil, bounded: false)
  params = case query
    when Hash
      query.slice(STRUCTURED_QUERY_PARAMS)
    else
      {'q': query}
  end

  if exactly_one
    params[:limit] = 1
  elsif limit.nil?
    raise "Limit cannot be less than 1" if limit < 1
    params[:limit] = limit
  end

  params[:bounded] = 1 if bounded
  params[:addressdetails] = 1 if detailed
  params[:countrycodes] = country_codes.join(",") unless country_codes.empty?
  params[:format] = "json"
  params[:"accept-language"] = language if language
  params[:viewbox] = format_bounding_box(viewbox[0], viewbox[1]) if viewbox

  url = build_url(:search, params)
  GeoRb.logger.debug url
  proc = ->(data) { data.first } if exactly_one
  call(url, proc)
end