geonames_rails 0.1.6 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/generators/geonames_rails/migration_templates/geonames_tables.rb +18 -1
- data/lib/generators/geonames_rails/models_templates/models/country.rb +1 -0
- data/lib/generators/geonames_rails/models_templates/models/division.rb +3 -0
- data/lib/geonames_rails/loader.rb +19 -6
- data/lib/geonames_rails/mappings/division.rb +15 -0
- data/lib/geonames_rails/puller.rb +1 -1
- data/lib/geonames_rails/writers/active_record.rb +22 -1
- data/lib/geonames_rails/writers/dry_run.rb +6 -0
- data/lib/tasks/geonames_rails.rake +3 -3
- metadata +6 -4
@@ -59,9 +59,13 @@ class CreateGeonamesTables < ActiveRecord::Migration
|
|
59
59
|
t.string :country_iso_code_two_letters
|
60
60
|
# [9] cc2 : alternate country codes, comma separated, ISO-3166 2-letter country code, 60 characters
|
61
61
|
# [10] admin1 code : fipscode (subject to change to iso code), isocode for the us and ch, see file admin1Codes.txt for display names of this code; varchar(20)
|
62
|
+
t.string :admin_1_code
|
62
63
|
# [11] admin2 code : code for the second administrative division, a county in the US, see file admin2Codes.txt; varchar(80)
|
64
|
+
t.string :admin_2_code
|
63
65
|
# [12] admin3 code : code for third level administrative division, varchar(20)
|
66
|
+
t.string :admin_3_code
|
64
67
|
# [13] admin4 code : code for fourth level administrative division, varchar(20)
|
68
|
+
t.string :admin_4_code
|
65
69
|
# [14] population : integer
|
66
70
|
t.integer :population
|
67
71
|
# [15] elevation : in meters, integer
|
@@ -72,10 +76,23 @@ class CreateGeonamesTables < ActiveRecord::Migration
|
|
72
76
|
end
|
73
77
|
|
74
78
|
add_index :cities, :geonames_id, :unique => true
|
79
|
+
|
80
|
+
create_table :divisions do |t|
|
81
|
+
t.integer :country_id
|
82
|
+
t.string :code
|
83
|
+
t.string :full_code
|
84
|
+
t.string :name
|
85
|
+
t.string :ascii_name
|
86
|
+
t.integer :geonames_id
|
87
|
+
end
|
88
|
+
|
89
|
+
add_index :divisions, :full_code, :unique => true
|
90
|
+
add_index :divisions, :code
|
91
|
+
add_index :divisions, :geonames_id, :unique => true
|
75
92
|
end
|
76
93
|
|
77
94
|
def self.down
|
78
95
|
# drop all the tables
|
79
|
-
%w(countries cities).each { |t| drop_table t }
|
96
|
+
%w(countries cities divisions).each { |t| drop_table t }
|
80
97
|
end
|
81
98
|
end
|
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'geonames_rails/mappings/base'
|
2
2
|
require 'geonames_rails/mappings/city'
|
3
3
|
require 'geonames_rails/mappings/country'
|
4
|
+
require 'geonames_rails/mappings/division'
|
4
5
|
require 'zip/zipfilesystem'
|
5
6
|
|
6
7
|
module GeonamesRails
|
@@ -17,8 +18,8 @@ module GeonamesRails
|
|
17
18
|
@puller.pull if @puller # pull geonames files down
|
18
19
|
|
19
20
|
load_countries
|
20
|
-
|
21
21
|
load_cities
|
22
|
+
load_divisions
|
22
23
|
|
23
24
|
@puller.cleanup if @puller # cleanup the geonames files
|
24
25
|
end
|
@@ -38,14 +39,26 @@ module GeonamesRails
|
|
38
39
|
end
|
39
40
|
end
|
40
41
|
end
|
41
|
-
|
42
|
-
def
|
43
|
-
|
44
|
-
|
42
|
+
|
43
|
+
def load_divisions
|
44
|
+
log_message "opening divisions file"
|
45
|
+
File.open(File.join(Rails.root, 'tmp', 'admin1CodesASCII.txt'), 'r') do |f|
|
46
|
+
f.each_line do |line|
|
47
|
+
# skip comments
|
48
|
+
next if line.match(/^#/) || line.match(/^iso/i)
|
49
|
+
|
50
|
+
division_mapping = Mappings::Division.new(line)
|
51
|
+
result = @writer.write_division(division_mapping)
|
52
|
+
|
53
|
+
#log_message result
|
54
|
+
end
|
45
55
|
end
|
56
|
+
|
46
57
|
end
|
47
58
|
|
48
|
-
def
|
59
|
+
def load_cities
|
60
|
+
city_file = "cities1000"
|
61
|
+
|
49
62
|
log_message "Loading city file #{city_file}"
|
50
63
|
cities = []
|
51
64
|
|
@@ -4,7 +4,7 @@ module GeonamesRails
|
|
4
4
|
@temp_geonames_files = []
|
5
5
|
target_dir = File.join(Rails.root, 'tmp')
|
6
6
|
|
7
|
-
file_names = %w(cities1000.zip
|
7
|
+
file_names = %w(cities1000.zip admin1CodesASCII.txt countryInfo.txt)
|
8
8
|
file_names.each do |file_name|
|
9
9
|
url = "http://download.geonames.org/export/dump/#{file_name}"
|
10
10
|
|
@@ -24,6 +24,23 @@ module GeonamesRails
|
|
24
24
|
"#{created_or_updating} db record for #{iso_code}"
|
25
25
|
end
|
26
26
|
|
27
|
+
def write_division(division_mapping)
|
28
|
+
iso_code = division_mapping[:full_code][0..1]
|
29
|
+
country = Country.find_by_iso_code_two_letter(iso_code)
|
30
|
+
|
31
|
+
division = Division.find_or_initialize_by_geonames_id(division_mapping[:geonames_id])
|
32
|
+
created_or_updating = division.new_record? ? 'Creating' : 'Updating'
|
33
|
+
division.country_id = country.id
|
34
|
+
division.code = division_mapping[:full_code][3..-1]
|
35
|
+
|
36
|
+
division.attributes = division_mapping.slice(:full_code,
|
37
|
+
:name,
|
38
|
+
:ascii_name,
|
39
|
+
:geonames_id)
|
40
|
+
division.save!
|
41
|
+
|
42
|
+
"#{created_or_updating} db record for #{division_mapping[:full_code]}"
|
43
|
+
end
|
27
44
|
|
28
45
|
|
29
46
|
def write_cities(country_code, city_mappings)
|
@@ -42,7 +59,11 @@ module GeonamesRails
|
|
42
59
|
:country_iso_code_two_letters,
|
43
60
|
:population,
|
44
61
|
:geonames_timezone_id,
|
45
|
-
:geonames_id
|
62
|
+
:geonames_id,
|
63
|
+
:admin_1_code,
|
64
|
+
:admin_2_code,
|
65
|
+
:admin_3_code,
|
66
|
+
:admin_4_code)
|
46
67
|
|
47
68
|
city.save!
|
48
69
|
end
|
@@ -7,6 +7,12 @@ module GeonamesRails
|
|
7
7
|
"Dry run of country #{country_mapping[:name]} should have been OK"
|
8
8
|
end
|
9
9
|
|
10
|
+
def write_division(division_mapping)
|
11
|
+
raise "must have a of division mapping" unless division_mapping
|
12
|
+
|
13
|
+
"Dry run of country #{division_mapping[:division_id]} should have been OK"
|
14
|
+
end
|
15
|
+
|
10
16
|
def write_cities(country_code, city_mappings)
|
11
17
|
raise "can't create cities without a country" unless country_code
|
12
18
|
raise "must have a set of city mappings" unless city_mappings
|
@@ -2,19 +2,19 @@ require 'geonames_rails'
|
|
2
2
|
|
3
3
|
namespace :geonames_rails do
|
4
4
|
desc 'pull down the geonames data from the server'
|
5
|
-
task :
|
5
|
+
task :pull => :environment do
|
6
6
|
GeonamesRails::Puller.new.pull
|
7
7
|
end
|
8
8
|
|
9
9
|
desc 'pull geonames data, load into db, then clean up after itself'
|
10
|
-
task :
|
10
|
+
task :run => :environment do
|
11
11
|
puller = GeonamesRails::Puller.new
|
12
12
|
writer = ENV['DRY_RUN'] ? GeonamesRails::Writers::DryRun.new : GeonamesRails::Writers::ActiveRecord.new
|
13
13
|
GeonamesRails::Loader.new(puller, writer).load_data
|
14
14
|
end
|
15
15
|
|
16
16
|
desc 'load the data from files you already have laying about'
|
17
|
-
task :
|
17
|
+
task :load => :environment do
|
18
18
|
writer = ENV['DRY_RUN'] ? GeonamesRails::Writers::DryRun.new : GeonamesRails::Writers::ActiveRecord.new
|
19
19
|
GeonamesRails::Loader.new(nil, writer).load_data
|
20
20
|
end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 0.
|
7
|
+
- 2
|
8
|
+
- 0
|
9
|
+
version: 0.2.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Marius Andra
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2012-
|
19
|
+
date: 2012-04-16 00:00:00 +02:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|
@@ -58,6 +58,7 @@ files:
|
|
58
58
|
- lib/geonames_rails/writers/dry_run.rb
|
59
59
|
- lib/geonames_rails/mappings/city.rb
|
60
60
|
- lib/geonames_rails/mappings/base.rb
|
61
|
+
- lib/geonames_rails/mappings/division.rb
|
61
62
|
- lib/geonames_rails/mappings/country.rb
|
62
63
|
- lib/geonames_rails/puller.rb
|
63
64
|
- lib/geonames_rails/loader.rb
|
@@ -65,6 +66,7 @@ files:
|
|
65
66
|
- lib/generators/geonames_rails/migration_generator.rb
|
66
67
|
- lib/generators/geonames_rails/migration_templates/geonames_tables.rb
|
67
68
|
- lib/generators/geonames_rails/models_templates/models/city.rb
|
69
|
+
- lib/generators/geonames_rails/models_templates/models/division.rb
|
68
70
|
- lib/generators/geonames_rails/models_templates/models/country.rb
|
69
71
|
- lib/generators/geonames_rails/models_generator.rb
|
70
72
|
- lib/tasks/geonames_rails.rake
|