Skip to content

Instantly share code, notes, and snippets.

@rab
Created January 25, 2009 20:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rab/52513 to your computer and use it in GitHub Desktop.
Save rab/52513 to your computer and use it in GitHub Desktop.
POI helpers for your GPSr
#!/usr/bin/env ruby -w
#
# Copyright (c) 2008 Rob Biedenharn
# Rob [at] AgileConsultingLLC.com
# Rob_Biedenharn [at] alum.mit.edu
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
=begin
Takes lines in the form of:
Full_Address (Name) Note
Full_Address (Name) Note [Longitude,Latitude]
Geocodes the address and formats a Garmin POI CSV output record as:
Longitude,Latitude,"Name","Street_Address\nCity, State Zip\nNote"
The bracketed longitude and latitude will override the output coordinates, if
present.
The Name and Note can have semicolons which will be transformed to line
breaks. The Name shows in a larger font on the Garmin GPSr units and the
second line will be preceeded by a space so that when both lines are show for
the name in list views, they won't run together.
=end
require 'rubygems'
gem 'geocoder'
require 'geocoder'
us = Geocoder::GeoCoderUs.new
def us.to_csv name, result, note
puts(%{%.6f,%.6f,"%s","%s"}%[result.longitude, result.latitude, name.gsub(/\s*;\s*/,"\n "),
["#{result.address}",
"#{result.city}, #{result.state} #{result.zip}",
note && note.gsub(/\s*;\s*/,"\n")].compact.join("\n")
])
end
ARGF.each do |line|
address, name, note, longitude, latitude =
line.match(/\A(.*)\s*\((.*)\)\s*(.*?)(?:\s*\[([-0-9.]+),([-0-9.]+)\])?\n?\z/).captures
begin
result = us.geocode address
if longitude && latitude
precision = result.precision rescue nil
warning = result.warning rescue nil
result = Geocoder::Result.new(latitude.to_f, longitude.to_f,
result.address, result.city, result.state, result.zip,
result.country, precision, warning)
end
us.to_csv name, result, note
$stderr.print '.'
rescue Geocoder::GeocodingError => e
$stderr.puts "#{e.message}: #{address}"
street, city, state, zip = address.match(/\A(.*), (.*), (.*) ([0-9]{5}(?:-?[0-9]{4})?\z/).captures
us.to_csv name, Geocoder::Result.new((latitude || 0).to_f, (longitude || 0).to_f,
street, city,state,zip,'',nil,nil), note
end
end
$stderr.puts
__END__
#!/usr/bin/env ruby -w
#
# Copyright (c) 2008 Rob Biedenharn
# Rob [at] AgileConsultingLLC.com
# Rob_Biedenharn [at] alum.mit.edu
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
=begin
Given a CSV file that may have duplicate Garmin POI entries, output all likely
unique entries first followed by groups of likely duplicate sets (pairs,
triplets, etc.)
Longitude,Latitude,"Name","Note"
Duplication is "likely" if the Lat/Long are each within 0.001 degree (100m or so)
If there is a duplicate pair and both the Latitude and Longitude are within
0.0001 degree, the one with the longer name will be output and the other
discarded as a duplicate (under the assumption that a longer name has more
detail and is thus a better representation). Pairs with one or both the
coordinates separated by more than 0.0001 degree and all sets of more than two
will be output preceeded by a line starting with "----".
=end
require 'rubygems'
gem 'fastercsv'
require 'fastercsv'
def as_csv ary
ary.map{|e| e.kind_of?(Numeric) ? e : [e].to_csv(:force_quotes => true, :row_sep => '')}.join(',')
end
filename = ARGV.shift
# $stderr.puts "Reading: #{filename}"
pois = FasterCSV.read(filename, :converters => :numeric) # [ [long,lat,name,note], [long,lat,name,note], ... ]
poi_sets = Hash.new {|h,k| h[k] = []}
pois.each do |long,lat,name,note|
pin = "%07d,%06d"%[(long * 1000.0).round, (lat * 1000.0).round]
poi_sets[pin] << [long,lat,name,note]
end
dups = {}
poi_sets.each do |pin,ary|
case ary.size
when 1
puts as_csv(ary[0])
when 2
# check for lat/long both within 1e-4 and puts the one with longer name
if (ary[0][0] - ary[1][0]).abs < 0.0001 && (ary[0][1] - ary[1][1]).abs < 0.0001
if ary[0][2].size > ary[1][2].size
puts as_csv(ary[0])
else
puts as_csv(ary[1])
end
else
dups[pin] = ary
end
else
dups[pin] = ary
end
end
dups.each do |pin,ary|
puts "---- #{pin}"
ary.sort_by {|long,lat,name,note| [-name.length,name,lat,long,note]}.each do |point|
puts as_csv(point)
end
end
__END__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment