geocoder icon indicating copy to clipboard operation
geocoder copied to clipboard

Unable to perform structured Nominatim lookup

Open janko opened this issue 5 months ago • 2 comments
trafficstars

I'm trying to perform a Nominatim lookup by passing the address via the street/city/country/state/postalcode query parameters, which is what Nominatim calls a structured lookup.

Geocoder.search(nil, params: {
  street: "...",
  city: "...",
  country: "...",
  state: "...",
  postalcode: "...",
})

Unfortunately, since Geocoder always passes the q query parameter, regardless of whether it's blank, I get an error from Nominatim:

Structured query parameters(amenity, street, city, county, state, postalcode, country) cannot be used together with 'q' parameter.

I was considering opening a PR with this change:

diff --git a/lib/geocoder/lookups/nominatim.rb b/lib/geocoder/lookups/nominatim.rb
index f7e119a..b15c089 100644
--- a/lib/geocoder/lookups/nominatim.rb
+++ b/lib/geocoder/lookups/nominatim.rb
@@ -55,7 +55,7 @@ module Geocoder::Lookup
         lat,lon = query.coordinates
         params[:lat] = lat
         params[:lon] = lon
-      else
+      elsif !query.params_given?
         params[:q] = query.sanitized_text
       end
       params
diff --git a/lib/geocoder/query.rb b/lib/geocoder/query.rb
index c509a7b..3dc15c2 100644
--- a/lib/geocoder/query.rb
+++ b/lib/geocoder/query.rb
@@ -116,8 +116,6 @@ module Geocoder
       options[:language]
     end
 
-    private # ----------------------------------------------------------------
-
     def params_given?
       !!(options[:params].is_a?(Hash) and options[:params].keys.size > 0)
     end

But I realized that maybe not all :params apply to structured lookup, some could be valid together with the q parameter.

Any idea how to solve / work around this?

janko avatar Jun 16 '25 16:06 janko

I think additionally checking that query text is blank should be sufficiently safe, so I opened a PR – https://github.com/alexreisner/geocoder/pull/1690.

janko avatar Jun 16 '25 16:06 janko

It would probably be ergonomical to make the first argument to Geocoder.search optional, but didn't want to jump the horse 😃

janko avatar Jun 16 '25 16:06 janko