Skip to content

Instantly share code, notes, and snippets.

@aderyabin
Created December 29, 2012 05:57
Show Gist options
  • Save aderyabin/4404820 to your computer and use it in GitHub Desktop.
Save aderyabin/4404820 to your computer and use it in GitHub Desktop.
kladr.rake
# encoding: utf-8
# needs to be installed 7za, pgdbf
namespace :kladr do
PATH = 'tmp/kladr'
def import_options(database=nil)
host = ActiveRecord::Base.configurations[Rails.env]['host']
user = ActiveRecord::Base.configurations[Rails.env]['username']
host &&= "--host=#{host}"
user &&= "--user=#{user}"
database ||= "#{ActiveRecord::Base.configurations[Rails.env]['database']}"
"#{host} #{user} #{database}"
end
desc 'Download archive'
task download: :environment do
FileUtils.mkdir_p(File.join(Rails.root, PATH))
system "curl http://www.gnivc.ru/html/gnivcsoft/KLADR/BASE.7z > #{PATH}/BASE.7z"
end
task extract: :environment do
system "rm -rf #{PATH}/*.DBF"
system "7za e -o#{PATH} #{PATH}/BASE.7z"
end
task import: :environment do
system "pgdbf #{PATH}/KLADR.DBF | iconv -c -f cp866 -t UTF-8 | psql #{import_options}"
system "pgdbf #{PATH}/STREET.DBF | iconv -c -f cp866 -t UTF-8 | psql #{import_options}"
system "pgdbf #{PATH}/DOMA.DBF | iconv -c -f cp866 -t UTF-8 | psql #{import_options}"
UpdateKladrToNormalState.new.up
UpdateKladrStreetToNormalState.new.up
UpdateKladrDomaToNormalState.new.up
end
end
class UpdateKladrToNormalState < ActiveRecord::Migration
def up
change_table :kladr do |t|
t.integer :region_code, nil: false, default: 0
t.integer :district_code, nil: false, default: 0
t.integer :city_code, nil: false, default: 0
t.integer :town_code, nil: false, default: 0
t.integer :actuality_code, nil: false, default: 0
end
execute "ALTER TABLE kladr ADD primary key(code)"
execute "UPDATE kladr SET \
region_code = substring(code, 1, 2)::integer, \
district_code = substring(code, 3, 3)::integer, \
city_code = substring(code, 6, 3)::integer, \
town_code = substring(code, 9, 3)::integer, \
actuality_code = substring(code, 12, 2)::integer"
end
end
class UpdateKladrStreetToNormalState < ActiveRecord::Migration
def up
change_table :street do |t|
t.integer :region_code, nil: false, default: 0
t.integer :district_code, nil: false, default: 0
t.integer :city_code, nil: false, default: 0
t.integer :town_code, nil: false, default: 0
t.integer :street_code, nil: false, default: 0
t.integer :actuality_code, nil: false, default: 0
end
execute "ALTER TABLE street ADD primary key(code)"
execute "UPDATE street SET \
region_code = substring(code, 1, 2)::integer, \
district_code = substring(code, 3, 3)::integer, \
city_code = substring(code, 6, 3)::integer, \
town_code = substring(code, 9, 3)::integer, \
street_code = substring(code, 12, 4)::integer, \
actuality_code = substring(code, 16, 2)::integer"
end
end
class UpdateKladrDomaToNormalState < ActiveRecord::Migration
def up
change_table :doma do |t|
t.integer :region_code, nil: false, default: 0
t.integer :district_code, nil: false, default: 0
t.integer :city_code, nil: false, default: 0
t.integer :town_code, nil: false, default: 0
t.integer :street_code, nil: false, default: 0
t.integer :house_code, nil: false, default: 0
end
execute "ALTER TABLE doma ADD primary key(code)"
execute "UPDATE doma SET \
region_code = substring(code, 1, 2)::integer, \
district_code = substring(code, 3, 3)::integer, \
city_code = substring(code, 6, 3)::integer, \
town_code = substring(code, 9, 3)::integer, \
street_code = substring(code, 12, 4)::integer, \
house_code = substring(code, 16, 4)::integer"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment