From e8ada9b71b916e7f891f2b3b0d82d51fe61ad1f3 Mon Sep 17 00:00:00 2001 From: Schuyler Erle Date: Thu, 16 Jun 2011 22:17:06 +0000 Subject: [PATCH] Fix a bug in edge interpolation caused by zero-length segments. --- demos/cli.rb | 2 +- lib/geocoder/us/database.rb | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/demos/cli.rb b/demos/cli.rb index ada93cd..daf8182 100644 --- a/demos/cli.rb +++ b/demos/cli.rb @@ -1,7 +1,7 @@ require 'geocoder/us/database' require 'pp' -db = Geocoder::US::Database.new("/mnt/geocoder-us/geocoder.db", :debug=>true) +db = Geocoder::US::Database.new("/mnt/tiger2010/geocoder.db", :debug=>true) result = db.geocode(ARGV[0]) pp(result) print "#{result[0][:lat]} N, #{-result[0][:lon]} W\n" diff --git a/lib/geocoder/us/database.rb b/lib/geocoder/us/database.rb index be2b1fa..cb2c026 100644 --- a/lib/geocoder/us/database.rb +++ b/lib/geocoder/us/database.rb @@ -571,6 +571,7 @@ def interpolate (points, fraction, side, offset=0.000075) (1...points.length).each {|n| total += distance(points[n-1], points[n])} target = total * fraction for n in 1...points.length + next if points[n-1] == points[n] # because otherwise step==0 and dx/dy==NaN step = distance(points[n-1], points[n]) if step < target target -= step @@ -582,7 +583,9 @@ def interpolate (points, fraction, side, offset=0.000075) return street_side_offset(offset*side, points[n-1], found) end end - # raise "Can't happen!" + # in a pathological case, points[n-1] == points[n] for n==-1 + # so *sigh* just forget interpolating and return points[-1] + return points[-1] end # Find and replace the city, state, and county information