Module: Kladr_sqlite

Defined in:
lib/kladr_converter.rb

Defined Under Namespace

Classes: City, Kladr, Kladr_street, Street

Class Method Summary collapse

Class Method Details

.create_linked_tablesObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/kladr_converter.rb', line 33

def Kladr_sqlite.create_linked_tables
  ActiveRecord::Schema.define do
   create_table "kladrs" do |t|
      t.column "name", :string, :limit => 40
      t.column "socr", :string, :limit => 10
      t.column "code", :string, :limit => 13
    end
  end
  
  ActiveRecord::Schema.define do
    create_table "cities" do |t|
      t.column "name", :string, :limit => 40
      t.column "socr", :string, :limit => 10
    end
  end
  
  ActiveRecord::Schema.define do
    create_table "streets" do |t|
      t.column "name", :string, :limit => 40
      t.column "socr", :string, :limit => 10
      t.column "city_id", :integer
    end
  end
end

.create_raw_tablesObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/kladr_converter.rb', line 58

def Kladr_sqlite.create_raw_tables
  ActiveRecord::Schema.define do
    create_table "kladrs" do |t|
      t.column "name", :string, :limit => 40
      t.column "socr", :string, :limit => 10
      t.column "code", :string, :limit => 13
      t.column "index", :string, :limit => 6
      t.column "gninmb", :string, :limit => 4
      t.column "uno", :string, :limit => 4
      t.column "ocatd", :string, :limit => 11
      t.column "status", :string, :limit => 1
    end
  end

  ActiveRecord::Schema.define do
    create_table "streets" do |t|
      t.column "name", :string, :limit => 40
      t.column "socr", :string, :limit => 10
      t.column "code", :string, :limit => 17
      t.column "index", :string, :limit => 6
      t.column "gninmb", :string, :limit => 4
      t.column "uno", :string, :limit => 4
      t.column "ocatd", :string, :limit => 11
    end
  end
end

.get_city_code(code) ⇒ Object



29
30
31
# File 'lib/kladr_converter.rb', line 29

def Kladr_sqlite.get_city_code( code )
  return code[ 0..10 ] + "00"
end

.is_city(code) ⇒ Object



25
26
27
# File 'lib/kladr_converter.rb', line 25

def Kladr_sqlite.is_city( code )
  return ( ( code[ 5 ] != "0" ) || ( code[ 6 ] != "0" ) || ( code[ 7 ] != "0" ) )
end

.save_linked(kladr_name, streets_name, db_name, progress) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/kladr_converter.rb', line 86

def Kladr_sqlite.save_linked( kladr_name, streets_name, db_name, progress )
  kladr = DBF::Table.new( kladr_name )
  streets = DBF::Table.new( streets_name )

  ActiveRecord::Base.establish_connection(
    :adapter => "sqlite3",
    :dbfile  => db_name,
    :database => db_name
  )

  create_linked_tables
  @converter = Iconv.new( 'UTF-8//IGNORE', 'CP866' )

  if progress then
    kladr_bar = ProgressBar.new( 'KLADR loading', kladr.count )
    street_bar = ProgressBar.new( 'Streets loading', streets.count )
  end

  streets.each do |record|
    street = Kladr.create( :name => @converter.iconv( record.name ),
                            :socr => @converter.iconv( record.socr ),
                            :code => get_city_code( @converter.iconv( record.code ) ) )
    if progress then
      kladr_bar.inc
    end
  end

  kladr.each do |record|
    code = @converter.iconv( record.code )
    if ( is_city( code ) ) then
      name = @converter.iconv( record.name )
      socr = @converter.iconv( record.socr )
      city = City.create( :name => name,
                          :socr => socr )

      streets = Kladr.all( :conditions => "code = '#{code}'" )

      if streets.count != 0 then
        streets.each do |street|
          city.streets.create( :name => street.name,
                                  :socr => street.socr)
        end
      end
      Kladr.delete_all( "code = '#{code}'" )
    end
  end

end

.save_raw(kladr_name, streets_name, db_name, progress) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/kladr_converter.rb', line 135

def Kladr_sqlite.save_raw( kladr_name, streets_name, db_name, progress )
  kladr = DBF::Table.new( kladr_name )
  streets = DBF::Table.new( streets_name )

  ActiveRecord::Base.establish_connection(
    :adapter => "sqlite3",
    :dbfile  => db_name,
    :database => db_name
  )

  create_raw_tables
  @converter = Iconv.new( 'UTF-8//IGNORE', 'CP866' )

  if progress then
    kladr_bar = ProgressBar.new( 'KLADR loading', kladr.count )
    street_bar = ProgressBar.new( 'Streets loading', streets.count )
  end

  kladr.each do |record|
    kladr = Kladr.create( :name => @converter.iconv( record.name ),
                          :socr => @converter.iconv( record.socr ),
                          :code => get_city_code( @converter.iconv( record.code ) ),
                          :index => @converter.iconv( record.index ),
                          :gninmb => @converter.iconv( record.gninmb ),
                          :uno => @converter.iconv( record.uno ),
                          :ocatd => @converter.iconv( record.ocatd ),
                          :status => @converter.iconv( record.status ) )
    if progress then
      kladr_bar.inc
    end
  end

  streets.each do |record|
    street = Kladr_street.create( :name => @converter.iconv( record.name ),
                            :socr => @converter.iconv( record.socr ),
                            :code => @converter.iconv( record.code ),
                            :index => @converter.iconv( record.index ),
                            :gninmb => @converter.iconv( record.gninmb ),
                            :uno => @converter.iconv( record.uno ),
                            :ocatd => @converter.iconv( record.ocatd ) )
    street_bat.inc
  end

end