Provide extra context for namefinder results where the nearest place
authorTom Hughes <tom@compton.nu>
Wed, 9 Apr 2008 23:43:38 +0000 (23:43 +0000)
committerTom Hughes <tom@compton.nu>
Wed, 9 Apr 2008 23:43:38 +0000 (23:43 +0000)
is small (a hamlet, village or suburb). Closes #794.

app/controllers/geocoder_controller.rb

index a4c264a93791885454454684b7c71b0a7f70a83e..66962c7f6bf58d035e4125ec8e7053050e38e66c 100644 (file)
@@ -129,20 +129,51 @@ private
       type = named.attributes["info"].to_s.capitalize
       name = named.attributes["name"].to_s
       description = named.elements["description"].to_s
       type = named.attributes["info"].to_s.capitalize
       name = named.attributes["name"].to_s
       description = named.elements["description"].to_s
+
       if name.empty?
         prefix = ""
         name = type
       else
         prefix = "#{type} "
       end
       if name.empty?
         prefix = ""
         name = type
       else
         prefix = "#{type} "
       end
+
       if place
         distance = format_distance(place.attributes["approxdistance"].to_i)
         direction = format_direction(place.attributes["direction"].to_i)
       if place
         distance = format_distance(place.attributes["approxdistance"].to_i)
         direction = format_direction(place.attributes["direction"].to_i)
-        placename = place.attributes["name"].to_s
+        placename = format_name(place.attributes["name"].to_s)
         suffix = ", #{distance} #{direction} of #{placename}"
         suffix = ", #{distance} #{direction} of #{placename}"
+
+        if place.attributes["rank"].to_i <= 30
+          parent = nil
+          parentrank = 0
+          parentdistance = 0
+
+          place.elements.each("nearestplaces/named") do |nearest|
+            nearestrank = nearest.attributes["rank"].to_i
+            nearestdistance = nearest.attributes["distance"].to_f
+
+            if nearestrank > parentrank or
+               ( nearestrank == parentrank and nearestdistance < parentdistance )
+              parent = nearest
+              parentrank = nearestrank
+              parentdistance = nearestdistance
+            end
+          end
+
+          if parent
+            parentname = format_name(parent.attributes["name"].to_s)
+
+            if  place.attributes["info"].to_s == "suburb"
+              suffix = "#{suffix}, #{parentname}"
+            else
+              suffix = "#{suffix} (near #{parentname})"
+            end
+          end
+        end
       else
         suffix = ""
       end
       else
         suffix = ""
       end
+
       results.push({:lat => lat, :lon => lon, :zoom => zoom,
                     :prefix => prefix, :name => name, :suffix => suffix,
                     :description => description})
       results.push({:lat => lat, :lon => lon, :zoom => zoom,
                     :prefix => prefix, :name => name, :suffix => suffix,
                     :description => description})
@@ -245,6 +276,10 @@ private
     return "west"
   end
 
     return "west"
   end
 
+  def format_name(name)
+    return name.gsub(/( *\[[^\]]*\])*$/, "")
+  end
+
   def count_results(results)
     count = 0
 
   def count_results(results)
     count = 0