geonames_local 2.0.1 → 3.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2e73d4bb7dd75933118a18ad64f13563e47b64e5
4
+ data.tar.gz: fc75d8a753756a87433b16c9da287cb9bd9bc090
5
+ SHA512:
6
+ metadata.gz: 82c9f917f022ba89fe63b7cb817e04f4652ba37eaf651760cd5a1c4409a2d090cbf495b23e0117c9f7eedeb860da3f6164aff83f22ebd58be3d72d81ae5a2f2c
7
+ data.tar.gz: 50d6134373286472f0980f4e68046c355eba088c89311a2b3f0148ff4463284d0c0456d147f19dca033586e4cc1159900cca452e217680a378b12e1eb4e71bc7
data/Gemfile CHANGED
@@ -2,7 +2,7 @@ source 'http://rubygems.org'
2
2
  gemspec # Specify gem's dependencies in mongoid_geospatial.gemspec
3
3
 
4
4
  group :development do
5
- gem 'mongoid', '~> 3.0'
5
+ gem 'mongoid'
6
6
  gem 'mongoid_geospatial'
7
7
  # gem 'pg'
8
8
  gem 'rgeo'
File without changes
data/README.rdoc CHANGED
@@ -9,8 +9,8 @@ No hit limit, fast as possible.
9
9
 
10
10
  To use one adapter, install the corresponding gem:
11
11
 
12
+ MongoDB => mongoid, geopolitical (optional: mongo_ext)
12
13
  PostgreSQL => pg
13
- MongoDB => mongoid (optional: mongo_ext)
14
14
  Tokyo => tokyocabinet
15
15
 
16
16
  You will also need in your system:
data/geonames.yml CHANGED
@@ -1,16 +1,17 @@
1
1
  #
2
2
  # Geonames Local Config Example
3
3
  #
4
- :store: mongodb # postgis / tokyo
5
- :codes: [br] # to store all countries informations in the countries table, just write [country]
4
+ :store: mongodb
6
5
  :level: city
6
+ :nations: [br]
7
+ :locales: [pt, en]
7
8
  :min_pop: 100000
8
9
  :mapping:
9
10
  :name: name
10
11
  :geom: true
11
12
  :db:
12
13
  :host: localhost
13
- :name: geonames_test
14
+ :name: baccafe_development
14
15
  :user:
15
16
  :pass:
16
17
  :purge: false
@@ -8,23 +8,22 @@ Gem::Specification.new do |gem|
8
8
 
9
9
  gem.authors = ["Marcos Piccinini"]
10
10
  gem.default_executable = %q{geonames}
11
- gem.description = %q{Dump and feed a tokyo cabinet for local geonames search}
11
+ gem.description = %q{Dumps geonames data to feed a local db}
12
12
  gem.email = %q{x@nofxx.com}
13
+ gem.license = "MIT"
13
14
 
14
15
  gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
15
16
  gem.files = `git ls-files`.split("\n")
16
17
  gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
18
  gem.name = "geonames_local"
18
19
  gem.require_paths = ["lib"]
19
- gem.summary = %q{Dump and feed a tokyo local geonames db}
20
-
20
+ gem.summary = %q{Dumps geonames data for local usage}
21
21
 
22
22
  gem.extra_rdoc_files = [
23
- "LICENSE",
23
+ "MIT-LICENSE",
24
24
  "README.rdoc"
25
25
  ]
26
26
 
27
-
28
27
  gem.post_install_message = %q{
29
28
  Geonames Local
30
29
  --------------
@@ -16,13 +16,9 @@ module Geonames
16
16
  options = {}
17
17
 
18
18
  argv.options do |opts|
19
- opts.banner = <<BANNER
20
- Geonames Command Line Usage:
19
+ opts.banner = "Geonames Command Line Usage\n\n geonames <nation code(s)> <opts>\n\n\n"
21
20
 
22
- geonames <country code(s)> <opts>
23
21
 
24
- geonames
25
- BANNER
26
22
  opts.on("-l", "--level LEVEL", String, "The level of logging to report" ) { |level| options[:level] = level }
27
23
  opts.on("-d", "--dump", "Dump DB before all" ) { options[:dump] = true }
28
24
  opts.separator ""
@@ -34,7 +30,7 @@ BANNER
34
30
  opts.on("--map TYPE", Array, "Use zone/road to import" ) { |s| options[:map] = s.map(&:to_sym) }
35
31
  opts.on("--type TYPE", String, "Use zone/road to import" ) { |s| options[:type] = s }
36
32
  opts.on("--city CITY", String, "Use city gid to import" ) { |s| options[:city] = s }
37
- opts.on("--country COUNTRY", String, "Use country gid to import" ) { |s| options[:country] = s }
33
+ opts.on("--nation NATION", String, "Use nation gid to import" ) { |s| options[:nation] = s }
38
34
  opts.separator ""
39
35
  opts.separator "Common Options:"
40
36
  opts.on("-h", "--help", "Show this message" ) { puts opts; exit }
@@ -54,24 +50,37 @@ BANNER
54
50
  end
55
51
  options
56
52
  end
53
+
57
54
  private_class_method :parse_options
58
55
 
59
56
  class << self
60
57
 
58
+ def load_config
59
+ if Opt[:config]
60
+ Opt.merge! YAML.load(File.read(Opt[:config]))
61
+ else
62
+ # Load config/geonames.yml if there's one
63
+ if File.exists?(cfg = File.join("config", "geonames.yml"))
64
+ Opt.merge! YAML.load(File.read(cfg))
65
+ else
66
+ raise
67
+ end
68
+ end
69
+ rescue
70
+ info "Cant't find config file"
71
+ exit
72
+ end
73
+
61
74
  # Ugly but works?
62
75
  def work(argv)
63
76
  trap(:INT) { stop! }
64
77
  trap(:TERM) { stop! }
65
78
  Opt.merge! parse_options(argv)
66
-
67
- if Opt[:config]
68
- Opt.merge! YAML.load(File.read(Opt[:config]))
79
+ if Opt[:locales].nil? || Opt[:locales].empty?
80
+ Opt[:locales] = ['en']
69
81
  end
70
82
 
71
- # Load config/geonames.yml if there's one
72
- if File.exists?(cfg = File.join("config", "geonames.yml"))
73
- Opt.merge! YAML.load(File.read(cfg))
74
- end
83
+ load_config
75
84
 
76
85
  if shp = Opt[:shp]
77
86
  SHP.import(shp)
@@ -115,7 +124,7 @@ BANNER
115
124
  # Export Data as CSV or JSON
116
125
  #
117
126
  if argv[0] =~ /csv|json/
118
- Geonames::Export.new(Country.all).to_csv
127
+ Geonames::Export.new(Nation.all).to_csv
119
128
 
120
129
  #
121
130
  # Do the magic! Import Geonames Data
@@ -124,24 +133,28 @@ BANNER
124
133
  load_adapter(Opt[:store])
125
134
  info "Using adapter #{Opt[:store]}.."
126
135
 
127
- if argv[0] =~ /coun|nati/
128
- dump = Geonames::Dump.new(:country, :dump)
136
+ # Nations
137
+ if Opt[:nations].empty? || argv[0] =~ /coun|nati/
138
+ info "\nPopulating 'nations' database..."
139
+ dump = Geonames::Dump.new(:all, :dump)
129
140
  info "\n---\nTotal #{dump.data.length} parsed."
130
141
 
131
- info "Writing to DB"
132
- Geonames::Models::Country.from_batch(dump.data)
142
+ info "Writing to nations DB"
143
+ Geonames::Models::MongoWrapper.nations dump.data, Opt[:clean]
144
+
145
+ # Regions, Cities....
133
146
  else
134
- zip = Geonames::Dump.new(Opt[:codes], :zip).data
135
- dump = Geonames::Dump.new(Opt[:codes], :dump).data
147
+ zip = Geonames::Dump.new(Opt[:nations], :zip).data
148
+ dump = Geonames::Dump.new(Opt[:nations], :dump).data
136
149
  info "\n---\nTotal #{dump.size} parsed. #{zip.size} zips."
137
150
 
138
151
  info "Join dump << zip"
139
152
  dump = unify!(dump, zip).group_by(&:kind)
140
153
 
141
- info "Writing provinces..."
142
- Geonames::Models::Province.from_batch dump[:province]
143
- info "Writing cities..."
144
- Geonames::Models::City.from_batch dump[:city]
154
+ info "Writing to DB..."
155
+ Geonames::Models::MongoWrapper.batch dump, Opt[:clean]
156
+ # info "Writing cities..."
157
+ # Geonames::Models::City.from_batch dump[:city]
145
158
  end
146
159
  end
147
160
  end
@@ -149,9 +162,9 @@ BANNER
149
162
  def load_adapter(name)
150
163
  begin
151
164
  require "geonames_local/models/#{name}"
152
- rescue LoadError
153
- puts "Can't find adapter for #{name}"
154
- stop!
165
+ # rescue LoadError
166
+ # puts "Can't find adapter for #{name}"
167
+ # stop!
155
168
  end
156
169
  end
157
170
 
@@ -20,13 +20,13 @@ SET default_with_oids = false;
20
20
  --
21
21
  -- TOC entry 2245 (class 1259 OID 21665)
22
22
  -- Dependencies: 2544 2545 2546 3 992
23
- -- Name: cities; Type: TABLE; Schema: public; Owner: -; Tablespace:
23
+ -- Name: cities; Type: TABLE; Schema: public; Owner: -; Tablespace:
24
24
  --
25
25
 
26
26
  CREATE TABLE cities (
27
27
  id integer NOT NULL,
28
- country_id integer NOT NULL,
29
- province_id integer,
28
+ nation_id integer NOT NULL,
29
+ region_id integer,
30
30
  name character varying(255) NOT NULL,
31
31
  gid integer,
32
32
  zip integer,
@@ -72,7 +72,7 @@ ALTER TABLE cities ALTER COLUMN id SET DEFAULT nextval('cities_id_seq'::regclass
72
72
  --
73
73
  -- TOC entry 2548 (class 2606 OID 21670)
74
74
  -- Dependencies: 2245 2245
75
- -- Name: cities_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace:
75
+ -- Name: cities_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace:
76
76
  --
77
77
 
78
78
  ALTER TABLE ONLY cities
@@ -82,16 +82,16 @@ ALTER TABLE ONLY cities
82
82
  --
83
83
  -- TOC entry 2549 (class 1259 OID 21697)
84
84
  -- Dependencies: 2245
85
- -- Name: index_cities_on_country_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
85
+ -- Name: index_cities_on_nation_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
86
86
  --
87
87
 
88
- CREATE INDEX index_cities_on_country_id ON cities USING btree (country_id);
88
+ CREATE INDEX index_cities_on_nation_id ON cities USING btree (nation_id);
89
89
 
90
90
 
91
91
  --
92
92
  -- TOC entry 2550 (class 1259 OID 21699)
93
93
  -- Dependencies: 2245 1869
94
- -- Name: index_cities_on_geom; Type: INDEX; Schema: public; Owner: -; Tablespace:
94
+ -- Name: index_cities_on_geom; Type: INDEX; Schema: public; Owner: -; Tablespace:
95
95
  --
96
96
 
97
97
  CREATE INDEX index_cities_on_geom ON cities USING gist (geom);
@@ -100,7 +100,7 @@ CREATE INDEX index_cities_on_geom ON cities USING gist (geom);
100
100
  --
101
101
  -- TOC entry 2551 (class 1259 OID 21695)
102
102
  -- Dependencies: 2245
103
- -- Name: index_cities_on_gid; Type: INDEX; Schema: public; Owner: -; Tablespace:
103
+ -- Name: index_cities_on_gid; Type: INDEX; Schema: public; Owner: -; Tablespace:
104
104
  --
105
105
 
106
106
  CREATE INDEX index_cities_on_gid ON cities USING btree (gid);
@@ -109,7 +109,7 @@ CREATE INDEX index_cities_on_gid ON cities USING btree (gid);
109
109
  --
110
110
  -- TOC entry 2552 (class 1259 OID 21694)
111
111
  -- Dependencies: 2245
112
- -- Name: index_cities_on_name; Type: INDEX; Schema: public; Owner: -; Tablespace:
112
+ -- Name: index_cities_on_name; Type: INDEX; Schema: public; Owner: -; Tablespace:
113
113
  --
114
114
 
115
115
  CREATE INDEX index_cities_on_name ON cities USING btree (name);
@@ -118,16 +118,16 @@ CREATE INDEX index_cities_on_name ON cities USING btree (name);
118
118
  --
119
119
  -- TOC entry 2553 (class 1259 OID 21698)
120
120
  -- Dependencies: 2245
121
- -- Name: index_cities_on_province_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
121
+ -- Name: index_cities_on_region_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
122
122
  --
123
123
 
124
- CREATE INDEX index_cities_on_province_id ON cities USING btree (province_id);
124
+ CREATE INDEX index_cities_on_region_id ON cities USING btree (region_id);
125
125
 
126
126
 
127
127
  --
128
128
  -- TOC entry 2554 (class 1259 OID 21696)
129
129
  -- Dependencies: 2245
130
- -- Name: index_cities_on_zip; Type: INDEX; Schema: public; Owner: -; Tablespace:
130
+ -- Name: index_cities_on_zip; Type: INDEX; Schema: public; Owner: -; Tablespace:
131
131
  --
132
132
 
133
133
  CREATE INDEX index_cities_on_zip ON cities USING btree (zip);
@@ -155,10 +155,10 @@ SET default_with_oids = false;
155
155
  --
156
156
  -- TOC entry 2249 (class 1259 OID 21688)
157
157
  -- Dependencies: 3
158
- -- Name: countries; Type: TABLE; Schema: public; Owner: -; Tablespace:
158
+ -- Name: nations; Type: TABLE; Schema: public; Owner: -; Tablespace:
159
159
  --
160
160
 
161
- CREATE TABLE countries (
161
+ CREATE TABLE nations (
162
162
  id integer NOT NULL,
163
163
  name character varying(30) NOT NULL,
164
164
  abbr character varying(2) NOT NULL
@@ -168,10 +168,10 @@ CREATE TABLE countries (
168
168
  --
169
169
  -- TOC entry 2248 (class 1259 OID 21686)
170
170
  -- Dependencies: 3 2249
171
- -- Name: countries_id_seq; Type: SEQUENCE; Schema: public; Owner: -
171
+ -- Name: nations_id_seq; Type: SEQUENCE; Schema: public; Owner: -
172
172
  --
173
173
 
174
- CREATE SEQUENCE countries_id_seq
174
+ CREATE SEQUENCE nations_id_seq
175
175
  START WITH 1
176
176
  INCREMENT BY 1
177
177
  NO MAXVALUE
@@ -182,10 +182,10 @@ CREATE SEQUENCE countries_id_seq
182
182
  --
183
183
  -- TOC entry 2548 (class 0 OID 0)
184
184
  -- Dependencies: 2248
185
- -- Name: countries_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
185
+ -- Name: nations_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
186
186
  --
187
187
 
188
- ALTER SEQUENCE countries_id_seq OWNED BY countries.id;
188
+ ALTER SEQUENCE nations_id_seq OWNED BY nations.id;
189
189
 
190
190
 
191
191
  --
@@ -194,17 +194,17 @@ ALTER SEQUENCE countries_id_seq OWNED BY countries.id;
194
194
  -- Name: id; Type: DEFAULT; Schema: public; Owner: -
195
195
  --
196
196
 
197
- ALTER TABLE countries ALTER COLUMN id SET DEFAULT nextval('countries_id_seq'::regclass);
197
+ ALTER TABLE nations ALTER COLUMN id SET DEFAULT nextval('nations_id_seq'::regclass);
198
198
 
199
199
 
200
200
  --
201
201
  -- TOC entry 2545 (class 2606 OID 21693)
202
202
  -- Dependencies: 2249 2249
203
- -- Name: countries_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace:
203
+ -- Name: nations_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace:
204
204
  --
205
205
 
206
- ALTER TABLE ONLY countries
207
- ADD CONSTRAINT countries_pkey PRIMARY KEY (id);
206
+ ALTER TABLE ONLY nations
207
+ ADD CONSTRAINT nations_pkey PRIMARY KEY (id);
208
208
 
209
209
 
210
210
  --
@@ -229,12 +229,12 @@ SET default_with_oids = false;
229
229
  --
230
230
  -- TOC entry 2247 (class 1259 OID 21680)
231
231
  -- Dependencies: 3
232
- -- Name: provinces; Type: TABLE; Schema: public; Owner: -; Tablespace:
232
+ -- Name: regions; Type: TABLE; Schema: public; Owner: -; Tablespace:
233
233
  --
234
234
 
235
- CREATE TABLE provinces (
235
+ CREATE TABLE regions (
236
236
  id integer NOT NULL,
237
- country_id integer NOT NULL,
237
+ nation_id integer NOT NULL,
238
238
  name character varying(255) NOT NULL,
239
239
  abbr character varying(2) NOT NULL,
240
240
  gid integer
@@ -244,10 +244,10 @@ CREATE TABLE provinces (
244
244
  --
245
245
  -- TOC entry 2246 (class 1259 OID 21678)
246
246
  -- Dependencies: 3 2247
247
- -- Name: provinces_id_seq; Type: SEQUENCE; Schema: public; Owner: -
247
+ -- Name: regions_id_seq; Type: SEQUENCE; Schema: public; Owner: -
248
248
  --
249
249
 
250
- CREATE SEQUENCE provinces_id_seq
250
+ CREATE SEQUENCE regions_id_seq
251
251
  START WITH 1
252
252
  INCREMENT BY 1
253
253
  NO MAXVALUE
@@ -258,10 +258,10 @@ CREATE SEQUENCE provinces_id_seq
258
258
  --
259
259
  -- TOC entry 2552 (class 0 OID 0)
260
260
  -- Dependencies: 2246
261
- -- Name: provinces_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
261
+ -- Name: regions_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
262
262
  --
263
263
 
264
- ALTER SEQUENCE provinces_id_seq OWNED BY provinces.id;
264
+ ALTER SEQUENCE regions_id_seq OWNED BY regions.id;
265
265
 
266
266
 
267
267
  --
@@ -270,53 +270,53 @@ ALTER SEQUENCE provinces_id_seq OWNED BY provinces.id;
270
270
  -- Name: id; Type: DEFAULT; Schema: public; Owner: -
271
271
  --
272
272
 
273
- ALTER TABLE provinces ALTER COLUMN id SET DEFAULT nextval('provinces_id_seq'::regclass);
273
+ ALTER TABLE regions ALTER COLUMN id SET DEFAULT nextval('regions_id_seq'::regclass);
274
274
 
275
275
 
276
276
  --
277
277
  -- TOC entry 2549 (class 2606 OID 21685)
278
278
  -- Dependencies: 2247 2247
279
- -- Name: provinces_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace:
279
+ -- Name: regions_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace:
280
280
  --
281
281
 
282
- ALTER TABLE ONLY provinces
283
- ADD CONSTRAINT provinces_pkey PRIMARY KEY (id);
282
+ ALTER TABLE ONLY regions
283
+ ADD CONSTRAINT regions_pkey PRIMARY KEY (id);
284
284
 
285
285
 
286
286
  --
287
287
  -- TOC entry 2544 (class 1259 OID 21701)
288
288
  -- Dependencies: 2247
289
- -- Name: index_provinces_on_abbr; Type: INDEX; Schema: public; Owner: -; Tablespace:
289
+ -- Name: index_regions_on_abbr; Type: INDEX; Schema: public; Owner: -; Tablespace:
290
290
  --
291
291
 
292
- CREATE INDEX index_provinces_on_abbr ON provinces USING btree (abbr);
292
+ CREATE INDEX index_regions_on_abbr ON regions USING btree (abbr);
293
293
 
294
294
 
295
295
  --
296
296
  -- TOC entry 2545 (class 1259 OID 21703)
297
297
  -- Dependencies: 2247
298
- -- Name: index_provinces_on_country_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
298
+ -- Name: index_regions_on_nation_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
299
299
  --
300
300
 
301
- CREATE INDEX index_provinces_on_country_id ON provinces USING btree (country_id);
301
+ CREATE INDEX index_regions_on_nation_id ON regions USING btree (nation_id);
302
302
 
303
303
 
304
304
  --
305
305
  -- TOC entry 2546 (class 1259 OID 21702)
306
306
  -- Dependencies: 2247
307
- -- Name: index_provinces_on_gid; Type: INDEX; Schema: public; Owner: -; Tablespace:
307
+ -- Name: index_regions_on_gid; Type: INDEX; Schema: public; Owner: -; Tablespace:
308
308
  --
309
309
 
310
- CREATE INDEX index_provinces_on_gid ON provinces USING btree (gid);
310
+ CREATE INDEX index_regions_on_gid ON regions USING btree (gid);
311
311
 
312
312
 
313
313
  --
314
314
  -- TOC entry 2547 (class 1259 OID 21700)
315
315
  -- Dependencies: 2247
316
- -- Name: index_provinces_on_name; Type: INDEX; Schema: public; Owner: -; Tablespace:
316
+ -- Name: index_regions_on_name; Type: INDEX; Schema: public; Owner: -; Tablespace:
317
317
  --
318
318
 
319
- CREATE INDEX index_provinces_on_name ON provinces USING btree (name);
319
+ CREATE INDEX index_regions_on_name ON regions USING btree (name);
320
320
 
321
321
 
322
322
  -- Completed on 2010-03-19 05:45:19 BRT
@@ -324,8 +324,3 @@ CREATE INDEX index_provinces_on_name ON provinces USING btree (name);
324
324
  --
325
325
  -- PostgreSQL database dump complete
326
326
  --
327
-
328
-
329
-
330
-
331
-
@@ -2,14 +2,15 @@
2
2
  # Geonames Local Config Example
3
3
  #
4
4
  :store: mongodb
5
- :codes: [br] # to store all countries informations in the countries table, just write [country]
6
5
  :level: city
6
+ :nations: [br, us] # [] to populate nations database
7
+ :locales: [pt, en]
7
8
  :mapping:
8
9
  :name: name
9
10
  :geom: true
10
11
  :db:
11
12
  :host: localhost
12
- :dbname: rtrac_development
13
- :user: postgres
14
- :password:
15
- :purge: false
13
+ :dbname: geonames_local
14
+ # :user:
15
+ # :password:
16
+ # :purge: false
@@ -1,31 +1,30 @@
1
1
  module Geonames
2
2
  class Dump
3
+ # Geonames base URL
3
4
  URL = "http://download.geonames.org/export/"
5
+ # Work temporary files
4
6
  TMP = "/tmp/geonames/"
5
7
 
6
- def initialize(codes, kind)
7
- @codes = codes
8
+ def initialize(target, kind)
8
9
  @kind = kind
9
10
  @data = []
10
- if codes.respond_to? :each
11
- for code in codes
12
- work code
13
- end
14
- elsif codes == :country
15
- countries
11
+ if target.respond_to? :each
12
+ target.each { |n| work(n) }
13
+ elsif target == :all
14
+ nations
16
15
  end
17
16
  end
18
17
 
19
- def countries
20
- info "\nDumping country database"
21
- file = get_file('country')
18
+ def nations
19
+ info "\nDumping nation database"
20
+ file = get_file('nation')
22
21
  download file
23
22
  parse file
24
23
  end
25
24
 
26
- def work code
27
- info "\nWorking on #{@kind} for #{code}"
28
- file = get_file(code)
25
+ def work nation
26
+ info "\nWorking on #{@kind} for #{nation}"
27
+ file = get_file(nation)
29
28
  download file
30
29
  uncompress file
31
30
  parse file
@@ -35,8 +34,8 @@ module Geonames
35
34
  @data
36
35
  end
37
36
 
38
- def get_file(code)
39
- code == "country" ? "countryInfo.txt" : "#{code.upcase}.zip"
37
+ def get_file(nation)
38
+ nation == "nation" ? "countryInfo.txt" : "#{nation.upcase}.zip"
40
39
  end
41
40
 
42
41
  def download(file)
@@ -28,7 +28,7 @@ module Geonames
28
28
  info "Free sample\n" + utf.inspect
29
29
  @sample = true
30
30
  end
31
- @type.new(Opt[:map], utf, Opt[:country].upcase, Opt[:city])
31
+ @type.new(Opt[:map], utf, Opt[:nation].upcase, Opt[:city])
32
32
  end
33
33
 
34
34
  def parse
@@ -2,9 +2,9 @@ require "rubygems"
2
2
  require "geo_ruby"
3
3
 
4
4
  class Road
5
- attr_reader :city, :province, :country, :zone, :name, :geom, :kind, :table
5
+ attr_reader :city, :region, :nation, :zone, :name, :geom, :kind, :table
6
6
 
7
- def initialize(keys, vals, country=nil, city=nil)
7
+ def initialize(keys, vals, nation=nil, city=nil)
8
8
  s = vals.split("\t")
9
9
  r = {}
10
10
  keys.each_with_index do |k, i|
@@ -16,7 +16,7 @@ class Road
16
16
  @geom = parse_geom(r[:geom])
17
17
  @kind = parse_kind(kind)
18
18
  @city = city
19
- @country = country
19
+ @nation = nation
20
20
  @table = :roads
21
21
  end
22
22
 
@@ -1,7 +1,7 @@
1
1
  module Geonames
2
2
  class Spot
3
- attr_accessor :gid, :name, :ascii, :lat, :lon, :country, :kind,
4
- :code, :pop, :tz, :geom, :province, :zip, :abbr, :id
3
+ attr_accessor :gid, :name, :ascii, :lat, :lon, :nation, :kind,
4
+ :code, :pop, :tz, :geom, :region, :zip, :abbr, :id
5
5
  alias :x :lon
6
6
  alias :y :lat
7
7
  alias :geoname_id :gid
@@ -11,17 +11,20 @@ module Geonames
11
11
  # = Geonames Spot
12
12
  #
13
13
  # Every geoname type will be parsed as a spot
14
+ #
14
15
  def initialize(params = nil, kind = nil)
15
16
  return unless params.instance_of? String
16
17
  kind == :zip ? parse_zip(params) : parse(params)
17
- if @kind == :province
18
+ if @kind == :region
18
19
  @name.gsub!(/Estado d\w\s/, "")
19
20
  @abbr = get_abbr
20
21
  end
21
22
  end
22
23
 
23
- # Geonames does not have province/state abbr..#fail!
24
+ #
25
+ # Geonames does not have region/state abbr..#fail!
24
26
  # This works 75% of the time in brazil heh
27
+ #
25
28
  def get_abbr
26
29
  s = @name.split(" ")
27
30
  if s.length > 1
@@ -33,32 +36,34 @@ module Geonames
33
36
 
34
37
  #
35
38
  # Parse Geonames Dump Export
36
- def parse(row)
39
+ #
40
+ def parse row
37
41
  gid, @name, @ascii, @alternates, lat, lon, feat, kind,
38
- @country, cc2, adm1, adm2, adm3, adm4, @pop, @ele,
39
- @gtop, @tz, @up = row.split(/\t/)
40
- parse_geom(lat, lon)
42
+ @nation, cc2, @region, @code, adm3, adm4, @pop, @ele,
43
+ @gtop, @tz, @up = row.split(/\t/)
44
+
41
45
  @gid = @geoname_id = gid.to_i
42
46
  @kind = human_code(kind)
43
- @province = adm1
44
- @code = adm2
45
47
 
46
- # puts "#{@kind} - #{@code} - #{@province}"
48
+ parse_geom(lat, lon)
49
+ # puts "#{@kind} - #{@code} - #{@region}"
47
50
  end
48
51
 
49
52
  #
50
53
  # Parse Geonames Zip Export
54
+ #
51
55
  def parse_zip(row)
52
- # country, zip, @name, state, state_code, procince, province_code, community, community_code, lat, lon, acc = row.split(/\t/)
53
- country, zip, @name, a1, @code, a2, a2c, a3, a3c, lat, lon, acc = row.split(/\t/)
54
- parse_geom(lat, lon)
55
- # @code = adm1
56
+ nation, @zip, @name, a1, a1c, a2, @code, a3, a3c,
57
+ lat, lon, acc = row.split(/\t/)
58
+
56
59
  @kind = :city
57
- @zip = zip.split("-")[0]
60
+ parse_geom(lat, lon)
61
+ # puts "#{row}\n---\n#{@kind} - #{@code} - #{@zip} #{lat}x#{lon}"
58
62
  end
59
63
 
60
64
  #
61
65
  # Parse Geom to float or GeoRuby Point
66
+ #
62
67
  def parse_geom(lat, lon)
63
68
  @lat, @lon = lat.to_f, lon.to_f
64
69
 
@@ -82,32 +87,32 @@ module Geonames
82
87
  # Translate geonames ADMx to models
83
88
  def human_code(code)
84
89
  case code
85
- when 'ADM1' then :province
90
+ when 'ADM1' then :region
86
91
  when 'ADM2', 'ADM3', 'ADM4' then :city
87
- else :other
92
+ else :other
88
93
  end
89
94
  end
90
95
 
91
96
  class << self
92
97
 
93
- def nearest(x,y)
94
- from_hash(Adapter.find_near(@coll, x, y, 1)[0])
95
- end
98
+ def nearest(x,y)
99
+ from_hash(Adapter.find_near(@coll, x, y, 1)[0])
100
+ end
96
101
 
97
- def from_hash(hsh)
98
- spot = self.new
99
- hsh.each { |key, val| spot.instance_variable_set("@"+key, val) }
100
- spot.geom = GeoRuby::SimpleFeatures::Point.from_x_y(*spot.geom)
101
- spot
102
- end
102
+ def from_hash(hsh)
103
+ spot = self.new
104
+ hsh.each { |key, val| spot.instance_variable_set("@"+key, val) }
105
+ spot.geom = GeoRuby::SimpleFeatures::Point.from_x_y(*spot.geom)
106
+ spot
107
+ end
103
108
 
104
- def set_coll(name)
105
- @coll = name
106
- end
109
+ def set_coll(name)
110
+ @coll = name
111
+ end
107
112
 
108
- def collection
109
- @coll
110
- end
113
+ def collection
114
+ @coll
115
+ end
111
116
  end
112
117
  end
113
118
  end
@@ -1,5 +1,6 @@
1
1
  require 'mongoid'
2
2
  require 'mongoid_geospatial'
3
+ require 'geopolitical/helpers'
3
4
 
4
5
  Mongoid.configure do |config|
5
6
  #config.master = Mongo::Connection.new.db("symbolize_test")
@@ -8,173 +9,121 @@ end
8
9
 
9
10
  module Geonames
10
11
  module Models
11
-
12
-
13
- class City < Geonames::Spot
14
- include Mongoid::Document
15
- include Mongoid::Geospatial
16
- store_in :collection => "cities"
17
-
18
- field :ascii, type: String
19
- field :slug, type: String
20
- field :name, type: String
21
- field :abbr, type: String
22
- field :area
23
- field :gid, type: Integer
24
- field :zip, type: Integer
25
- field :geom, type: Point, spatial: true
26
-
27
- belongs_to :province
28
- belongs_to :country, index: true
29
- # has_many :hoods
30
-
31
- before_validation :set_defaults
32
-
33
- validates :name, :country, presence: true
34
- validates :name, :uniqueness => { :scope => :province_id }
35
-
36
- index name: 1
37
- index slug: 1
38
- index geom: '2d'
39
-
40
- #spatial_index :geom
41
-
42
- scope :ordered, order_by(name: 1)
43
-
44
- def set_defaults
45
- self.country ||= province.try(:country)
46
- self.slug ||= name.try(:downcase) # don't use slugize
47
- end
48
-
49
- def self.search(search, page)
50
- cities = search ? where(:field => /#{search}/i) : all
51
- cities.page(page)
52
- end
53
-
54
- def <=> other
55
- self.name <=> other.name
56
- end
57
-
58
- def to_s
59
- "#{name}/#{abbr}"
60
- end
61
-
62
- def self.from_batch data
63
- data.each do |city|
64
- info "Writing city #{city.name}"
65
- next unless city.country
66
- city = new.parse(city)
67
- city.country = city.province.country
68
- city.save
12
+ require 'geopolitical/../../app/models/hood'
13
+ require 'geopolitical/../../app/models/city'
14
+ require 'geopolitical/../../app/models/region'
15
+ require 'geopolitical/../../app/models/nation'
16
+ module MongoWrapper
17
+
18
+ class << self
19
+
20
+ def nations data, clean
21
+ Nation.delete_all # if clean
22
+ data.each do |row|
23
+ create Nation, parse_nation(row) rescue nil
24
+ end
69
25
  end
70
- end
71
26
 
72
- def parse(spot)
73
- self.name, self.ascii = spot.name, spot.ascii
74
- self.code, self.gid = spot.code, spot.gid
75
- self.province = Province.find_by(code: spot.province)
76
- self.geom = [spot.lon, spot.lat]
77
- self.abbr = province.abbr
78
- self
79
- end
80
27
 
81
- end
28
+ def batch data, clean = false
29
+ [Region, City].each(&:delete_all) #if clean
82
30
 
31
+ @regions, @cities = data[:region], data[:city]
32
+ @regions.each { |r| create Region, parse_region(r) }
33
+ @cities.each { |c| create City, parse_city(c) }
34
+ end
83
35
 
84
- class Province < Geonames::Spot
85
- include Mongoid::Document
86
- store_in :collection => "provinces"
36
+ def create klass, data
37
+ klass.create! data
38
+ rescue => e
39
+ info "Prob com spot #{klass}, #{data} #{e}"
40
+ end
87
41
 
88
- field :gid, type: Integer # geonames id
89
- field :code, type: String
90
- field :name, type: String
91
- field :abbr, type: String
92
- field :codes, type: Array # phone code
42
+ def translate txt
43
+ name_i18n = Opt[:locales].reduce({}) do |h, l|
44
+ h.merge({ l => txt })
45
+ end
46
+ end
93
47
 
94
- belongs_to :country
95
- has_many :cities
96
48
 
97
- validates :name, presence: true
98
- validates :country, presence: true
49
+ def parse_nation row
50
+ abbr, iso3, ison, fips, name, capital, area, pop, continent,
51
+ tld, cur_code, cur_name, phone, pos_code, pos_regex,
52
+ langs, gid, neighbours = row.split(/\t/)
53
+ info "Nation: #{name}/#{abbr}"
54
+ # info "#{row}"
55
+ # info "------------------------"
56
+ {
57
+ name_translations: translate(name),
58
+ zip: pos_code, cash: cur_code, gid: gid,
59
+ abbr: abbr, slug: name.downcase, code: iso3, lang: langs
60
+ }
61
+ end
99
62
 
100
- index name: 1
101
- index codes: 1
102
63
 
103
- scope :ordered, order_by(name: 1)
64
+ def parse_region s
65
+ nation = Nation.find_by(abbr: /#{s.nation}/i)
66
+ info "Region: #{s.name} / #{s.abbr}"
67
+ {
68
+ name_translations: translate(s.name),
69
+ gid: s.gid, abbr: s.abbr,
70
+ nation: nation, code: s.region
71
+ }
72
+ end
104
73
 
105
- def self.from_batch data
106
- data.each do |province|
107
- info "Writing province #{province.name}"
108
- next unless province.country
109
- province = new.parse(province)
110
- province.country = Country.find_by(abbr: /#{province.country}/i)
111
- province.save!
74
+ def parse_city s
75
+ region = Region.find_by(code: s.region)
76
+ slug = City.new(slug: s.ascii).slug
77
+ attempt = slug.dup
78
+ try = 1
79
+ until City.where(slug: attempt.downcase).first.nil?
80
+ attempt = "#{slug}-#{region.abbr}-#{try}"
81
+ try += 1
82
+ break if try > 7
83
+ end
84
+
85
+ info s.inspect
86
+ info "City: #{s.zip} | #{slug} - #{s.name} / #{region.try(:abbr)}"
87
+ {
88
+ name_translations: translate(s.name),
89
+ slug: attempt, gid: s.gid, code: s.code,
90
+ souls: s.pop, geom: [s.lon, s.lat],
91
+ region: region, abbr: region.abbr, zip: s.zip # tz
92
+ }
112
93
  end
113
- end
114
94
 
115
- def parse(spot)
116
- self.code, self.name = spot.province, spot.name
117
- self.gid = spot.gid
118
- self
119
95
  end
120
-
121
96
  end
122
97
 
123
98
 
124
- class Country < Geonames::Spot
125
- include Mongoid::Document
126
- store_in :collection => "countries"
99
+ # class Nation < Geonames::Spot
127
100
 
128
- field :gid, type: Integer # geonames id
129
- field :name, type: String
130
- field :abbr, type: String
131
- field :code # optional phone/whatever code
132
101
 
133
- has_many :cities
134
- has_many :provinces
102
+ # def parse row
103
+ # end
135
104
 
136
- validates :abbr, :name, presence: true, uniqueness: true
105
+ # def to_hash
106
+ # { "gid" => @gid.to_s, "name" => @name, "kind" => "nation", "code" => @code}
107
+ # end
137
108
 
138
- index name: 1
139
- index code: 1
109
+ # def export
110
+ # [@gid, @code, @name]
111
+ # end
140
112
 
141
- scope :ordered, order_by(name: 1)
113
+ # def export_header
114
+ # ["gid", "code", "name"]
115
+ # end
116
+ # end
142
117
 
143
118
 
144
- def parse row
145
- self.abbr, @iso3, @ison, @fips, self.name, @capital, @area, @pop, continent, tld,
146
- currency, phone, postal, langs, gid, neighbours = row.split(/\t/)
147
- self
148
- end
149
119
 
150
- def self.from_batch data
151
- data.each do |spot|
152
- new.parse(spot).save
153
- end
154
- end
120
+ # class Zip
121
+ # include Mongoid::Document
155
122
 
156
- def to_hash
157
- { "gid" => @gid.to_s, "name" => @name, "kind" => "country", "code" => @code}
158
- end
123
+ # field :code
124
+ # belongs_to :city
159
125
 
160
- def export
161
- [@gid, @code, @name]
162
- end
163
-
164
- def export_header
165
- ["gid", "code", "name"]
166
- end
167
- end
168
-
169
-
170
-
171
- class Zip
172
- include Mongoid::Document
173
-
174
- field :code
175
- belongs_to :city
176
-
177
- end
126
+ # end
178
127
 
179
128
  end
180
129
  end
@@ -1,19 +1,19 @@
1
1
  module Geonames
2
2
  module Models
3
- module AR
3
+ #module Postgis
4
4
 
5
5
  class City < ActiveRecord::Base
6
6
  attr_accessor :x, :y, :z
7
7
 
8
- belongs_to :province
9
- belongs_to :country
8
+ belongs_to :region
9
+ belongs_to :nation
10
10
 
11
- validates_presence_of :country
11
+ validates_presence_of :nation
12
12
  validates_presence_of :name
13
- # validates_uniqueness_of :name, :scope => :province_id
13
+ # validates_uniqueness_of :name, :scope => :region_id
14
14
 
15
15
  def abbr
16
- province.try(:abbr) || country.abbr
16
+ region.try(:abbr) || nation.abbr
17
17
  end
18
18
 
19
19
  def geom=(val)
@@ -25,22 +25,22 @@ module Geonames
25
25
 
26
26
  # Instantiate self.geom as a Point
27
27
  def validation
28
- self.country ||= province.country
28
+ self.nation ||= region.nation
29
29
  unless !@x || !@y || @x == "" || @y == ""
30
30
  self.geom = Point.from_x_y(@x.to_f, @y.to_f)
31
31
  end
32
32
  end
33
33
  end
34
34
 
35
- class Province < ActiveRecord::Base
35
+ class Region < ActiveRecord::Base
36
36
  has_many :cities
37
- belongs_to :country
37
+ belongs_to :nation
38
38
 
39
- validates_uniqueness_of :name, :abbr, :scope => :country_id
39
+ validates_uniqueness_of :name, :abbr, :scope => :nation_id
40
40
  end
41
41
 
42
- class Country < ActiveRecord::Base
43
- has_many :provinces
42
+ class Nation < ActiveRecord::Base
43
+ has_many :regions
44
44
  has_many :cities
45
45
  validates_presence_of :name, :abbr
46
46
  validates_uniqueness_of :name, :abbr
@@ -51,8 +51,6 @@ module Geonames
51
51
  end
52
52
 
53
53
 
54
- end
54
+ #end
55
55
  end
56
56
  end
57
-
58
-
@@ -2,11 +2,11 @@ module Geonames
2
2
  module Models
3
3
  module Tokyo
4
4
 
5
- class Country
5
+ class Nation
6
6
  attr_accessor :code, :name, :gid, :iso, :capital, :pop, :currency
7
7
 
8
8
  def self.all
9
- Tokyo.new.all({ :kind => "country" }).map do |c|
9
+ Tokyo.new.all({ :kind => "nation" }).map do |c|
10
10
  new(c)
11
11
  end
12
12
  end
@@ -46,11 +46,11 @@ module Geonames
46
46
  end
47
47
 
48
48
  def cities
49
- # qry.addcond("country", TBDQRY::QSTREQ, @code)
49
+ # qry.addcond("nation", TBDQRY::QSTREQ, @code)
50
50
  end
51
51
 
52
52
  def to_hash
53
- # { "gid" => @gid.to_s, "name" => @name, "kind" => "country", "code" => @code, "currency" => @currency}
53
+ # { "gid" => @gid.to_s, "name" => @name, "kind" => "nation", "code" => @code, "currency" => @currency}
54
54
  { "gid" => @gid.to_s, "iso" => @iso, "iso3" => @iso3, "iso_num" => @ison, "fips" => @fips,
55
55
  "name" => @name, "capital" => @capital, "area" => @area, "population" => @pop,
56
56
  "continent" => @continent, "tld" => @tld, "currency_code" => @currency, "currency_name" => @currency_name,
@@ -68,11 +68,11 @@ module Geonames
68
68
 
69
69
  end
70
70
  end
71
- class Province
71
+ class Region
72
72
  attr_accessor :code, :name, :gid
73
73
 
74
74
  def self.all
75
- Tokyo.new.all({ :kind => "province" }).map do |c|
75
+ Tokyo.new.all({ :kind => "region" }).map do |c|
76
76
  new(c)
77
77
  end
78
78
  end
@@ -1,3 +1,3 @@
1
1
  module Geonames
2
- VERSION = '2.0.1'
2
+ VERSION = '3.1.1'
3
3
  end
metadata CHANGED
@@ -1,29 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geonames_local
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
5
- prerelease:
4
+ version: 3.1.1
6
5
  platform: ruby
7
6
  authors:
8
7
  - Marcos Piccinini
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-10-31 00:00:00.000000000 Z
11
+ date: 2013-09-05 00:00:00.000000000 Z
13
12
  dependencies: []
14
- description: Dump and feed a tokyo cabinet for local geonames search
13
+ description: Dumps geonames data to feed a local db
15
14
  email: x@nofxx.com
16
15
  executables:
17
16
  - geonames
18
17
  extensions: []
19
18
  extra_rdoc_files:
20
- - LICENSE
19
+ - MIT-LICENSE
21
20
  - README.rdoc
22
21
  files:
23
- - .document
24
22
  - .gitignore
25
23
  - Gemfile
26
- - LICENSE
24
+ - MIT-LICENSE
27
25
  - README.rdoc
28
26
  - Rakefile
29
27
  - bin/geonames
@@ -63,31 +61,40 @@ files:
63
61
  - task/benchmark.rb
64
62
  - task/benchmark_cabinet.rb
65
63
  homepage: http://github.com/nofxx/geonames_local
66
- licenses: []
67
- post_install_message: ! "\nGeonames Local\n--------------\n\nTo use the adapter, install
68
- the corresponding gem:\n\n PostgreSQL => pg\n MongoDB => mongoid (optional:
69
- mongo_ext)\n Tokyo => tokyocabinet\n\nHave fun because:\n"
64
+ licenses:
65
+ - MIT
66
+ metadata: {}
67
+ post_install_message: |2
68
+
69
+ Geonames Local
70
+ --------------
71
+
72
+ To use the adapter, install the corresponding gem:
73
+
74
+ PostgreSQL => pg
75
+ MongoDB => mongoid (optional: mongo_ext)
76
+ Tokyo => tokyocabinet
77
+
78
+ Have fun because:
70
79
  rdoc_options: []
71
80
  require_paths:
72
81
  - lib
73
82
  required_ruby_version: !ruby/object:Gem::Requirement
74
- none: false
75
83
  requirements:
76
- - - ! '>='
84
+ - - '>='
77
85
  - !ruby/object:Gem::Version
78
86
  version: '0'
79
87
  required_rubygems_version: !ruby/object:Gem::Requirement
80
- none: false
81
88
  requirements:
82
- - - ! '>='
89
+ - - '>='
83
90
  - !ruby/object:Gem::Version
84
91
  version: '0'
85
92
  requirements: []
86
93
  rubyforge_project:
87
- rubygems_version: 1.8.23
94
+ rubygems_version: 2.0.7
88
95
  signing_key:
89
- specification_version: 3
90
- summary: Dump and feed a tokyo local geonames db
96
+ specification_version: 4
97
+ summary: Dumps geonames data for local usage
91
98
  test_files:
92
99
  - spec/geonames_local/adapters/mongodb_spec.rb
93
100
  - spec/geonames_local/adapters/postgis_spec.rb
data/.document DELETED
@@ -1,5 +0,0 @@
1
- README.rdoc
2
- lib/**/*.rb
3
- bin/*
4
- features/**/*.feature
5
- LICENSE