geonames_local 3.3.0 → 3.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fdef4304a7290c1a586d7e9b437153021bbc6b37
4
- data.tar.gz: 41eb2353c1e671d5e6986922221aed29563eb5d5
3
+ metadata.gz: c788cde1271fbcde5596ca18134547b68ebe2488
4
+ data.tar.gz: 983752fea10114e374c21e75e23b5e21808c31fe
5
5
  SHA512:
6
- metadata.gz: 01b20afbb5e96032c2882dde15dbc7025e77bd39a50884b6f54f4bf8acc4981273e715eeb472f39cf1db1f4d0c437f8dad4c07640d4506f10042d9024c643ed7
7
- data.tar.gz: 5f194b2ddc720aa9fdaeb6ef3fd155c52933dba1bd3b9abda4a3c48fca37af590e398144a0e515f13c69112656468f60855b397f64f87bc663fd3cb34e51d98f
6
+ metadata.gz: a7a08a957601e2663126bd6fe7c5021b46b6527e0e15a1549e8b6971b5ad688443f82c73aad92ba44e93f4e48567a6034a108c742c24d92bb472af2fb92ee369
7
+ data.tar.gz: eb7b597058aa178b5e8f28f1d565a9b586dd7099d3b6675b3d32bc99ca45c1fc13d2b7f3020b532d5e12a7ae0af69f71af223e95cbb10b1ba13413a161f31204
data/README.md CHANGED
@@ -11,7 +11,7 @@ Use
11
11
 
12
12
  Install where you want to populate the DB:
13
13
 
14
- gem install geonames_local
14
+ gem install geonames_local
15
15
 
16
16
 
17
17
  You will also need in your system:
@@ -25,24 +25,30 @@ Config
25
25
 
26
26
  Create a config yml file:
27
27
 
28
-
29
- geonames init
30
-
28
+ geonames init
31
29
 
32
30
  Will generate a "geonames.yml" file on your folder.
33
31
  The file is self explanatory.
34
32
 
35
- geonames -c geonames.yml
33
+ Geonames splits the nations/countries database from the rest, so:
34
+
35
+
36
+ To populate the nations database for the first time use:
37
+
38
+ geonames -c geoconfig.yml nations
39
+
40
+
41
+ Then you can run geonames to populate regions/cities...
42
+
43
+ geonames -c geonames.yml
36
44
 
37
45
  To run it. Use -v for verbose.
38
46
 
39
- If you are not sure your country/nation code, use:
40
47
 
41
- geonames list <search>
48
+ If you are not sure your country/nation code, use:
42
49
 
43
- To populate the nations database for the first time use:
50
+ geonames list <search>
44
51
 
45
- geonames -c geoconfig.yml nations
46
52
 
47
53
 
48
54
  Adapters
data/bin/geonames CHANGED
@@ -3,8 +3,8 @@
3
3
  # $KCODE = "u" # -Ku
4
4
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
5
 
6
- require "geonames_local"
7
- require "geonames_local/cli"
6
+ require 'geonames_local'
7
+ require 'geonames_local/cli'
8
8
  include Geonames
9
9
 
10
10
  Geonames::CLI.work(ARGV)
@@ -25,8 +25,8 @@ Gem::Specification.new do |gem|
25
25
  "README.md"
26
26
  ]
27
27
 
28
- gem.add_dependency('mongoid', ['>= 3.0.0'])
29
- gem.add_dependency('geopolitical', ['>= 0.8.0'])
28
+ gem.add_dependency('mongoid', ['~> 3.1.0'])
29
+ gem.add_dependency('geopolitical', ['> 0.8.1'])
30
30
 
31
31
  gem.post_install_message = %q{
32
32
  Geonames Local
@@ -1,8 +1,6 @@
1
1
  #
2
2
  # Geonames Extension
3
3
  #
4
- $:.unshift(File.dirname(__FILE__)) unless
5
- $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
6
4
 
7
5
  # Require Libs
8
6
  require 'geonames_local/features/spot'
@@ -11,32 +11,35 @@ require 'geonames_local/cli'
11
11
 
12
12
 
13
13
  module Geonames
14
+ #
15
+ # Command Line Interface for Geonames Local
16
+ #
17
+ #
14
18
  class CLI
15
19
  def self.parse_options(argv)
16
20
  options = {}
17
21
 
18
22
  argv.options do |opts|
19
- opts.banner = "Geonames Command Line Usage\n\n geonames <nation code(s)> <opts>\n\n\n"
20
-
21
-
22
- opts.on("-l", "--level LEVEL", String, "The level of logging to report" ) { |level| options[:level] = level }
23
- opts.on("-d", "--dump", "Dump DB before all" ) { options[:dump] = true }
24
- opts.separator ""
25
- opts.separator "Config file:"
26
- opts.on("-c", "--config CONFIG", String, "Geonames Config file path" ) { |file| options[:config] = file }
27
- opts.on("-i", "--import CONFIG", String, "Geonames Import SHP/DBF/GPX" ) { |file| options[:shp] = file }
28
- opts.separator ""
29
- opts.separator "SHP Options:"
30
- opts.on("--map TYPE", Array, "Use zone/road to import" ) { |s| options[:map] = s.map(&:to_sym) }
31
- opts.on("--type TYPE", String, "Use zone/road to import" ) { |s| options[:type] = s }
32
- opts.on("--city CITY", String, "Use city gid to import" ) { |s| options[:city] = s }
33
- opts.on("--nation NATION", String, "Use nation gid to import" ) { |s| options[:nation] = s }
34
- opts.separator ""
35
- opts.separator "Common Options:"
36
- opts.on("-h", "--help", "Show this message" ) { puts opts; exit }
37
- opts.on("-v", "--verbose", "Turn on logging to STDOUT" ) { |bool| options[:verbose] = bool }
38
- opts.on("-V", "--version", "Show version") { puts Geonames::VERSION; exit }
39
- opts.separator ""
23
+ opts.banner = 'Geonames Command Line Usage\n\n geonames <nation code(s)> <opts>\n\n\n'
24
+
25
+ opts.on('-l', '--level LEVEL', String, 'The level of logging to report') { |level| options[:level] = level }
26
+ opts.on('-d', '--dump', 'Dump DB before all') { options[:dump] = true }
27
+ opts.separator ''
28
+ opts.separator 'Config file:'
29
+ opts.on('-c', '--config CONFIG', String, 'Geonames Config file path') { |file| options[:config] = file }
30
+ opts.on('-i', '--import CONFIG', String, 'Geonames Import SHP/DBF/GPX') { |file| options[:shp] = file }
31
+ opts.separator ''
32
+ opts.separator 'SHP Options:'
33
+ opts.on('--map TYPE', Array, 'Use zone/road to import') { |s| options[:map] = s.map(&:to_sym) }
34
+ opts.on('--type TYPE', String, 'Use zone/road to import') { |s| options[:type] = s }
35
+ opts.on('--city CITY', String, 'Use city gid to import') { |s| options[:city] = s }
36
+ opts.on('--nation NATION', String, 'Use nation gid to import') { |s| options[:nation] = s }
37
+ opts.separator ''
38
+ opts.separator 'Common Options:'
39
+ opts.on('-h', '--help', 'Show this message') { puts opts; exit }
40
+ opts.on('-v', '--verbose', 'Turn on logging to STDOUT') { |bool| options[:verbose] = bool }
41
+ opts.on('-V', '--version', 'Show version') { puts Geonames::VERSION; exit }
42
+ opts.separator ''
40
43
  begin
41
44
  opts.parse!
42
45
  if argv.empty? && !options[:config]
@@ -56,23 +59,26 @@ module Geonames
56
59
  class << self
57
60
 
58
61
  def load_config
62
+ info "Loading config file..."
59
63
  if Opt[:config]
60
64
  Opt.merge! YAML.load(File.read(Opt[:config]))
61
65
  else
62
66
  # Load config/geonames.yml if there's one
63
- if File.exists?(cfg = File.join("config", "geonames.yml"))
67
+ if File.exists?(cfg = File.join('config', 'geonames.yml'))
64
68
  Opt.merge! YAML.load(File.read(cfg))
65
69
  else
66
- raise
70
+ fail
67
71
  end
68
72
  end
69
73
  rescue
70
- info "Cant't find config file"
74
+ info "Can't find config file"
71
75
  exit
72
76
  end
73
77
 
74
78
  # Ugly but works?
75
79
  def work(argv)
80
+ info "Geopolitical Local Start!"
81
+
76
82
  trap(:INT) { stop! }
77
83
  trap(:TERM) { stop! }
78
84
  Opt.merge! parse_options(argv)
@@ -90,7 +96,7 @@ module Geonames
90
96
  #
91
97
  if argv[0] =~ /list|codes/
92
98
  Codes.each do |key,val|
93
- str = [val.values, key.to_s].join(" ").downcase
99
+ str = [val.values, key.to_s].join(' ').downcase
94
100
  if s = argv[1]
95
101
  next unless str =~ /#{s.downcase}/
96
102
  end
@@ -103,9 +109,9 @@ module Geonames
103
109
  # If arguments scaffold, config, write down yml.
104
110
  #
105
111
  if argv[0] =~ /scaff|conf|init/
106
- fname = (argv[1] || "geonames") + ".yml"
112
+ fname = (argv[1] || 'geonames') + '.yml'
107
113
  if File.exist?(fname)
108
- puts "File exists."
114
+ puts "File exists: #{fname}"
109
115
  else
110
116
  puts "Writing to #{fname}"
111
117
  `cp #{File.join(File.dirname(__FILE__), 'config', 'geonames.yml')} #{fname}`
@@ -116,7 +122,7 @@ module Geonames
116
122
  #
117
123
  # Require georuby optionally
118
124
  #
119
- require "geo_ruby" if Opt[:mapping] && Opt[:mapping][:geom]
125
+ require 'geo_ruby' if Opt[:mapping] && Opt[:mapping][:geom]
120
126
 
121
127
  #
122
128
  # Load config if we got til here
@@ -142,7 +148,7 @@ module Geonames
142
148
  dump = Geonames::Dump.new(:all, :dump)
143
149
  info "\n---\nTotal #{dump.data.length} parsed."
144
150
 
145
- info "Writing to nations DB"
151
+ info 'Writing to nations DB'
146
152
  Geonames::Models::MongoWrapper.nations dump.data, Opt[:clean]
147
153
 
148
154
  # Regions, Cities....
@@ -151,10 +157,10 @@ module Geonames
151
157
  dump = Geonames::Dump.new(Opt[:nations], :dump).data
152
158
  info "\n---\nTotal #{dump.size} parsed. #{zip.size} zips."
153
159
 
154
- info "Join dump << zip"
160
+ info 'Join dump << zip'
155
161
  dump = unify!(dump, zip).group_by(&:kind)
156
162
 
157
- info "Writing to DB..."
163
+ info 'Writing to DB...'
158
164
  Geonames::Models::MongoWrapper.batch dump, Opt[:clean]
159
165
  # info "Writing cities..."
160
166
  # Geonames::Models::City.from_batch dump[:city]
@@ -165,13 +171,13 @@ module Geonames
165
171
  def load_adapter(name)
166
172
  begin
167
173
  require "geonames_local/models/#{name}"
168
- # rescue LoadError
169
- # puts "Can't find adapter for #{name}"
170
- # stop!
174
+ rescue LoadError
175
+ info "Can't find adapter for #{name}"
176
+ stop!
171
177
  end
172
178
  end
173
179
 
174
- def unify! dump, zip
180
+ def unify!(dump, zip)
175
181
  start = Time.now
176
182
  dump.map! do |spot|
177
183
  if other = zip.find { |d| d.code == spot.code }
@@ -181,16 +187,15 @@ module Geonames
181
187
  spot
182
188
  end
183
189
  end
184
- info "Done. #{(Time.now-start).to_i}s"
190
+ info "Done. #{(Time.now - start).to_i}s"
185
191
  dump
186
192
  end
187
193
 
188
194
  def stop!
189
- puts "Closing Geonames..."
195
+ puts 'Closing Geonames...'
190
196
  exit
191
197
  end
192
198
 
193
-
194
199
  end # class < self
195
200
 
196
201
  end # CLI
@@ -76,13 +76,12 @@ module Geonames
76
76
  end
77
77
  end
78
78
  total = Time.now - start
79
- info "#{red} #{@kind} entries parsed in #{total} sec (#{(red/total).to_i}/s)"
79
+ info "#{red} #{@kind} spots parsed #{total}s (#{(red / total).to_i}/s)"
80
80
  end
81
81
  rescue Errno::ENOENT => e
82
- info "Failed to download #{file}, skipping."
82
+ info "Failed to download #{file}, skipping. #{e}"
83
83
  end
84
84
 
85
-
86
85
  end
87
86
 
88
87
  end
@@ -1,4 +1,7 @@
1
1
  module Geonames
2
+ #
3
+ # Main Ruby Model for Geonames Spot Concept
4
+ #
2
5
  class Spot
3
6
  attr_accessor :gid, :name, :ascii, :lat, :lon, :nation, :kind,
4
7
  :code, :pop, :tz, :geom, :region, :zip, :abbr, :id
@@ -16,7 +19,7 @@ module Geonames
16
19
  return unless params.instance_of? String
17
20
  kind == :zip ? parse_zip(params) : parse(params)
18
21
  if @kind == :region
19
- @name.gsub!(/Estado d\w\s/, "")
22
+ @name.gsub!(/Estado d\w\s/, '')
20
23
  @abbr = get_abbr
21
24
  end
22
25
  end
@@ -26,9 +29,9 @@ module Geonames
26
29
  # This works 75% of the time in brazil heh
27
30
  #
28
31
  def get_abbr
29
- s = @name.split(" ")
32
+ s = @name.split(' ')
30
33
  if s.length > 1
31
- [s[0][0].chr,s[-1][0].chr].map(&:upcase).join
34
+ [s[0][0].chr, s[-1][0].chr].map(&:upcase).join
32
35
  else
33
36
  s[0][0..1].upcase
34
37
  end
@@ -37,9 +40,9 @@ module Geonames
37
40
  #
38
41
  # Parse Geonames Dump Export
39
42
  #
40
- def parse row
43
+ def parse(row)
41
44
  gid, @name, @ascii, @alternates, lat, lon, feat, kind,
42
- @nation, cc2, @region, @code, adm3, adm4, @pop, @ele,
45
+ @nation, _cc2, @region, @code, _adm3, _adm4, @pop, @ele,
43
46
  @gtop, @tz, @up = row.split(/\t/)
44
47
 
45
48
  @gid = @geoname_id = gid.to_i
@@ -53,8 +56,8 @@ module Geonames
53
56
  # Parse Geonames Zip Export
54
57
  #
55
58
  def parse_zip(row)
56
- nation, @zip, @name, a1, a1c, a2, @code, a3, a3c,
57
- lat, lon, acc = row.split(/\t/)
59
+ _nation, @zip, @name, _a1, _a1c, _a2, @code, _a3, _a3c,
60
+ lat, lon, _acc = row.split(/\t/)
58
61
 
59
62
  @kind = :city
60
63
  parse_geom(lat, lon)
@@ -67,10 +70,10 @@ module Geonames
67
70
  def parse_geom(lat, lon)
68
71
  @lat, @lon = lat.to_f, lon.to_f
69
72
 
70
- if defined?("GeoRuby")
73
+ if defined? GeoRuby
71
74
  @geom = GeoRuby::SimpleFeatures::Point.from_x_y(@lon, @lat)
72
75
  else
73
- { :lat => @lat, :lon => @lon }
76
+ { lat: @lat, lon: @lon }
74
77
  end
75
78
  end
76
79
 
@@ -81,38 +84,35 @@ module Geonames
81
84
  #
82
85
  # Parse Time
83
86
  def updated_at
84
- Time.utc(*@up.split("-"))
87
+ Time.utc(*@up.split('-'))
85
88
  end
86
89
 
87
90
  # Translate geonames ADMx to models
88
91
  def human_code(code)
89
92
  case code
90
- when 'ADM1' then :region
91
- when 'ADM2', 'ADM3', 'ADM4' then :city
93
+ when 'ADM1' then :region
94
+ when 'ADM2', 'ADM3', 'ADM4' then :city
92
95
  else :other
93
96
  end
94
97
  end
95
98
 
96
99
  class << self
97
100
 
98
- def nearest(x,y)
99
- from_hash(Adapter.find_near(@coll, x, y, 1)[0])
101
+ attr_accessor :collection
102
+
103
+ def nearest(x, y)
104
+ from_hash(Adapter.find_near(collection, x, y, 1)[0])
100
105
  end
101
106
 
102
107
  def from_hash(hsh)
103
- spot = self.new
104
- hsh.each { |key, val| spot.instance_variable_set("@"+key, val) }
108
+ spot = new
109
+ hsh.each { |key, val| spot.instance_variable_set("@#{key}", val) }
105
110
  spot.geom = GeoRuby::SimpleFeatures::Point.from_x_y(*spot.geom)
106
111
  spot
107
112
  end
108
113
 
109
- def set_coll(name)
110
- @coll = name
111
- end
112
-
113
- def collection
114
- @coll
115
- end
116
114
  end
115
+
117
116
  end
117
+
118
118
  end
@@ -4,6 +4,7 @@ require 'geopolitical/helpers'
4
4
 
5
5
  Mongoid.configure do |config|
6
6
  #config.master = Mongo::Connection.new.db("symbolize_test")
7
+ info "Using Mongoid v#{Mongoid::VERSION}"
7
8
  info "Mongoid connecting to #{Opt[:db]}"
8
9
  config.connect_to(Opt[:db][:name])
9
10
  end
@@ -18,14 +19,6 @@ module Geonames
18
19
 
19
20
  class << self
20
21
 
21
- def nations data, clean
22
- Nation.delete_all if clean
23
- data.each do |row|
24
- create Nation, parse_nation(row) rescue nil
25
- end
26
- end
27
-
28
-
29
22
  def batch data, clean = false
30
23
  [Region, City].each(&:delete_all) if clean
31
24
 
@@ -35,7 +28,7 @@ module Geonames
35
28
  end
36
29
 
37
30
  def create klass, data
38
- info "#{klass}.new #{data}"
31
+ # info "#{klass}.new #{data}"
39
32
  klass.create! data
40
33
  rescue => e
41
34
  warn "Prob com spot #{e} #{e.backtrace.join("\n")}"
@@ -47,6 +40,15 @@ module Geonames
47
40
  end
48
41
  end
49
42
 
43
+ #
44
+ # Parse Nations
45
+ #
46
+ def nations data, clean
47
+ Nation.delete_all if clean
48
+ data.each do |row|
49
+ create Nation, parse_nation(row) rescue nil
50
+ end
51
+ end
50
52
 
51
53
  def parse_nation row
52
54
  abbr, iso3, ison, fips, name, capital, area, pop, continent,
@@ -62,7 +64,9 @@ module Geonames
62
64
  }
63
65
  end
64
66
 
65
-
67
+ #
68
+ # Parse Regions
69
+ #
66
70
  def parse_region s
67
71
  nation = Nation.find_by(abbr: /#{s.nation}/i)
68
72
  info "Region: #{s.name} / #{s.abbr}"
@@ -73,7 +77,10 @@ module Geonames
73
77
  }
74
78
  end
75
79
 
76
- def parse_city s
80
+ #
81
+ # Parse Cities
82
+ #
83
+ def parse_city(s)
77
84
  region = Region.find_by(code: s.region)
78
85
  slug = City.new(slug: s.ascii).slug
79
86
  attempt = slug.dup
@@ -95,8 +102,8 @@ module Geonames
95
102
  end
96
103
 
97
104
  end
98
- end
99
105
 
106
+ end
100
107
 
101
108
  # class Nation < Geonames::Spot
102
109
 
@@ -105,7 +112,8 @@ module Geonames
105
112
  # end
106
113
 
107
114
  # def to_hash
108
- # { "gid" => @gid.to_s, "name" => @name, "kind" => "nation", "code" => @code}
115
+ # { "gid" => @gid.to_s, "name" => @name,
116
+ # "kind" => "nation", "code" => @code}
109
117
  # end
110
118
 
111
119
  # def export
@@ -117,8 +125,6 @@ module Geonames
117
125
  # end
118
126
  # end
119
127
 
120
-
121
-
122
128
  # class Zip
123
129
  # include Mongoid::Document
124
130
 
@@ -1,3 +1,3 @@
1
1
  module Geonames
2
- VERSION = '3.3.0'
2
+ VERSION = '3.3.1'
3
3
  end
metadata CHANGED
@@ -1,43 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geonames_local
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.3.0
4
+ version: 3.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcos Piccinini
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-08 00:00:00.000000000 Z
11
+ date: 2013-10-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongoid
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 3.0.0
19
+ version: 3.1.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 3.0.0
26
+ version: 3.1.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: geopolitical
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - ">"
32
32
  - !ruby/object:Gem::Version
33
- version: 0.8.0
33
+ version: 0.8.1
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - ">"
39
39
  - !ruby/object:Gem::Version
40
- version: 0.8.0
40
+ version: 0.8.1
41
41
  description: Dumps geonames data to feed a local db
42
42
  email: x@nofxx.com
43
43
  executables: