Skip to content

Commit

Permalink
Basic CSV geocodeing and address splitting Rake tasks. Plus additiona…
Browse files Browse the repository at this point in the history
…l place migration for state
  • Loading branch information
ut committed Jul 15, 2024
1 parent 1813347 commit be3de87
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 1 deletion.
2 changes: 2 additions & 0 deletions app/views/places/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@
.small-4.cell
= f.input :zip
.small-8.cell
- if @place.state
= f.input :state
= f.input :city
= f.input :country, :input_html => {:placeholder => 'Land' }
%hr
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20240715143133_add_state_to_places.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddStateToPlaces < ActiveRecord::Migration[6.1]
def change
add_column :places, :state, :string
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2024_07_12_101227) do
ActiveRecord::Schema.define(version: 2024_07_15_143133) do

create_table "active_storage_attachments", charset: "utf8mb3", collation: "utf8mb3_general_ci", force: :cascade do |t|
t.string "name", null: false
Expand Down Expand Up @@ -273,6 +273,7 @@
t.string "uid"
t.string "startdate_qualifier"
t.string "enddate_qualifier"
t.string "state"
t.index ["layer_id"], name: "index_places_on_layer_id"
end

Expand Down
71 changes: 71 additions & 0 deletions lib/tasks/geocode_addresses_in_csv.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
require 'csv'

namespace :geocode do
desc "Geocode addresses from CSV and add lat/lon columns"
task :addresses_in_csv => :environment do
input_file = Rails.root.join('tmp', 'input.csv')
output_file = Rails.root.join('tmp', 'output.csv')

CSV.open(output_file, 'w') do |csv_out|
CSV.foreach(input_file, headers: true) do |row|
if row.header_row?
csv_out << row.headers + ['lat', 'lon']
else
address = row['address']
results = Geocoder.search(address)

if results.any?
lat = results.first.latitude
lon = results.first.longitude
else
lat = ''
lon = ''
end

csv_out << row.fields + [lat, lon]
puts "Processed: #{address}"
end
end
end

puts "Geocoding complete. Output saved to #{output_file}"
end

desc "Geocode addresses and separate address components"
task :addresses_in_csv_and_separate => :environment do
input_file = Rails.root.join('tmp', 'input.csv')
output_file = Rails.root.join('tmp', 'output.csv')

CSV.open(output_file, 'w') do |csv_out|
CSV.foreach(input_file, headers: true) do |row|
if row.header_row?
csv_out << row.headers + ['address', 'city', 'state', 'zip', 'lat', 'lon']
else
full_address = row['address']
address_parts = full_address.split(',').map(&:strip)

address = address_parts[0]
city = address_parts[1]
state_zip = address_parts[2].split(' ')
state = state_zip[0]
zip = state_zip[1]

results = Geocoder.search(full_address)

if results.any?
lat = results.first.latitude
lon = results.first.longitude
else
lat = ''
lon = ''
end

csv_out << row.fields + [address, city, state, zip, lat, lon]
puts "Processed: #{full_address}"
end
end
end

puts "Geocoding complete. Output saved to #{output_file}"
end
end
1 change: 1 addition & 0 deletions spec/factories/places.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
address { 'Address' }
zip { 'Zip' }
city { 'City' }
state { 'State' }
country { 'Country' }
published { false }
imagelink { 'Some link' }
Expand Down

0 comments on commit be3de87

Please sign in to comment.