geonames_local 3.3.1 → 3.3.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +13 -10
- data/Guardfile +2 -2
- data/Rakefile +16 -17
- data/geonames.yml +2 -2
- data/geonames_local.gemspec +17 -17
- data/lib/geonames_local/cli.rb +55 -71
- data/lib/geonames_local/data/dump.rb +18 -29
- data/lib/geonames_local/data/export.rb +4 -5
- data/lib/geonames_local/data/shp.rb +7 -8
- data/lib/geonames_local/features/road.rb +7 -10
- data/lib/geonames_local/features/spot.rb +4 -8
- data/lib/geonames_local/features/zone.rb +4 -5
- data/lib/geonames_local/geonames.rb +3 -7
- data/lib/geonames_local/models/mongodb.rb +28 -29
- data/lib/geonames_local/models/postgis.rb +37 -39
- data/lib/geonames_local/version.rb +1 -1
- data/lib/geonames_local.rb +5 -0
- data/spec/geonames_local/cli_spec.rb +2 -2
- data/spec/geonames_local/data/shp_spec.rb +8 -9
- data/spec/geonames_local/features/road_spec.rb +23 -26
- data/spec/geonames_local/features/spot_spec.rb +130 -133
- data/spec/geonames_local/features/zone_spec.rb +21 -22
- data/spec/geonames_local/models/mongodb_spec.rb +1 -1
- data/spec/geonames_local/models/tokyo_spec.rb +21 -21
- data/spec/geonames_local_spec.rb +2 -3
- data/spec/spec_ar_helper.rb +0 -3
- data/spec/spec_helper.rb +2 -4
- data/task/benchmark.rb +6 -8
- data/task/benchmark_cabinet.rb +5 -7
- metadata +9 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3d899c1e90546accbce85fa372b534d862f81d16
|
4
|
+
data.tar.gz: b0978645a9ae924f268bb8783a82e2de3564a733
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d907b4d932d092bae9b440b31226e12d8c68d8b54e5806076dde56c7b68569f5087352903d97db250d7dc603f14418499527758823a6428f20485d8cc2bee8ab
|
7
|
+
data.tar.gz: fa3189ca4d1c1ae453dcd1c7e6cd8d9fc227fd2524fbff07f87e6584e1858295a5b72ec40812b27e1353c448113af571466cc581fd82647ab34ccfccd2597489
|
data/Gemfile
CHANGED
@@ -1,15 +1,18 @@
|
|
1
1
|
source 'http://rubygems.org'
|
2
2
|
gemspec # Specify gem's dependencies in mongoid_geospatial.gemspec
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
gem 'rake'
|
5
|
+
gem 'dbf'
|
6
|
+
|
7
|
+
platforms :ruby do
|
8
8
|
gem 'rgeo'
|
9
|
-
gem 'georuby'
|
10
|
-
gem 'dbf'
|
11
|
-
gem 'rspec'
|
12
|
-
gem 'guard-rspec'
|
13
|
-
gem 'pry'
|
14
|
-
# gem 'fuubar'
|
15
9
|
end
|
10
|
+
|
11
|
+
gem 'georuby'
|
12
|
+
|
13
|
+
gem 'mongoid'
|
14
|
+
gem 'mongoid_geospatial'
|
15
|
+
|
16
|
+
gem 'pry'
|
17
|
+
gem 'rspec'
|
18
|
+
gem 'guard-rspec'
|
data/Guardfile
CHANGED
@@ -4,6 +4,6 @@ require 'guard'
|
|
4
4
|
|
5
5
|
guard 'rspec' do
|
6
6
|
watch(%r{^spec/.+_spec\.rb$})
|
7
|
-
watch(%r{^lib/(.+)\.rb$}) {
|
8
|
-
watch('spec/spec_helper.rb') {
|
7
|
+
watch(%r{^lib/(.+)\.rb$}) { 'spec' } # { |m| "spec spec/lib/#{m[1]}_spec.rb" }
|
8
|
+
watch('spec/spec_helper.rb') { 'spec' }
|
9
9
|
end
|
data/Rakefile
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
#!/usr/bin/env rake
|
2
|
-
require
|
2
|
+
require 'bundler/gem_tasks'
|
3
3
|
|
4
4
|
require 'rspec/core'
|
5
5
|
require 'rspec/core/rake_task'
|
@@ -13,49 +13,48 @@ RSpec::Core::RakeTask.new(:rcov) do |spec|
|
|
13
13
|
spec.rcov = true
|
14
14
|
end
|
15
15
|
|
16
|
-
task :
|
16
|
+
task default: :spec
|
17
17
|
|
18
18
|
require 'yard'
|
19
19
|
|
20
|
-
|
21
20
|
#
|
22
21
|
# Tokyo Tyrant rake tasks
|
23
22
|
#
|
24
23
|
namespace :tyrant do
|
25
|
-
TYRANT_DB_FILE = File.join(
|
26
|
-
TYRANT_PID_FILE = File.join(
|
27
|
-
TYRANT_LOG_FILE = File.join(
|
24
|
+
TYRANT_DB_FILE = File.join('tyrant.tct')
|
25
|
+
TYRANT_PID_FILE = File.join('tyrant.pid')
|
26
|
+
TYRANT_LOG_FILE = File.join('tyrant.log')
|
28
27
|
|
29
|
-
desc
|
28
|
+
desc 'Start Tyrant server'
|
30
29
|
task :start do
|
31
|
-
|
30
|
+
fail RuntimeError, 'Tyrant is already running.' if tyrant_running?
|
32
31
|
system "ttserver -pid #{TYRANT_PID_FILE} -log #{TYRANT_LOG_FILE} #{TYRANT_DB_FILE}&"
|
33
32
|
sleep(2)
|
34
33
|
if tyrant_running?
|
35
34
|
puts "Tyrant started successfully (pid #{tyrant_pid})."
|
36
35
|
else
|
37
|
-
puts
|
36
|
+
puts 'Failed to start tyrant push server. Check logs.'
|
38
37
|
end
|
39
38
|
end
|
40
39
|
|
41
|
-
desc
|
40
|
+
desc 'Stop Tyrant server'
|
42
41
|
task :stop do
|
43
|
-
|
42
|
+
fail RuntimeError, "Tyrant isn't running." unless tyrant_running?
|
44
43
|
system "kill #{tyrant_pid}"
|
45
44
|
sleep(2)
|
46
45
|
if tyrant_running?
|
47
46
|
puts "Tyrant didn't stopped. Check the logs."
|
48
47
|
else
|
49
|
-
puts
|
48
|
+
puts 'Tyrant stopped.'
|
50
49
|
end
|
51
50
|
end
|
52
51
|
|
53
|
-
desc
|
54
|
-
task :
|
52
|
+
desc 'Restart Tyrant server'
|
53
|
+
task restart: [:stop, :start]
|
55
54
|
|
56
|
-
desc
|
55
|
+
desc 'Get Tyrant Server Status'
|
57
56
|
task :status do
|
58
|
-
puts tyrant_running? ? "Tyrant running. (#{tyrant_pid})" :
|
57
|
+
puts tyrant_running? ? "Tyrant running. (#{tyrant_pid})" : 'Tyrant not running.'
|
59
58
|
end
|
60
59
|
end
|
61
60
|
|
@@ -67,7 +66,7 @@ def tyrant_running?
|
|
67
66
|
return false unless File.exist?(TYRANT_PID_FILE)
|
68
67
|
process_check = `ps -p #{tyrant_pid} | wc -l`
|
69
68
|
if process_check.to_i < 2
|
70
|
-
puts
|
69
|
+
puts 'Erasing pidfile...'
|
71
70
|
`rm #{TYRANT_PID_FILE}`
|
72
71
|
end
|
73
72
|
tyrant_pid
|
data/geonames.yml
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
#
|
4
4
|
:store: mongodb
|
5
5
|
:level: city
|
6
|
-
:nations: [br]
|
6
|
+
:nations: [br, it, nl]
|
7
7
|
:locales: [pt, en]
|
8
8
|
:min_pop: 100000
|
9
9
|
:mapping:
|
@@ -11,7 +11,7 @@
|
|
11
11
|
:geom: true
|
12
12
|
:db:
|
13
13
|
:host: localhost
|
14
|
-
:name:
|
14
|
+
:name: geonames_test
|
15
15
|
:user:
|
16
16
|
:pass:
|
17
17
|
:purge: false
|
data/geonames_local.gemspec
CHANGED
@@ -2,33 +2,32 @@
|
|
2
2
|
require File.expand_path('../lib/geonames_local/version', __FILE__)
|
3
3
|
|
4
4
|
Gem::Specification.new do |gem|
|
5
|
-
gem.name =
|
5
|
+
gem.name = 'geonames_local'
|
6
6
|
gem.version = Geonames::VERSION
|
7
|
-
gem.homepage =
|
7
|
+
gem.homepage = 'http://github.com/nofxx/geonames_local'
|
8
8
|
|
9
|
-
gem.authors = [
|
10
|
-
gem.default_executable =
|
11
|
-
gem.description =
|
12
|
-
gem.email =
|
13
|
-
gem.license =
|
9
|
+
gem.authors = ['Marcos Piccinini']
|
10
|
+
gem.default_executable = 'geonames'
|
11
|
+
gem.description = 'Dumps geonames data to feed a local db'
|
12
|
+
gem.email = 'x@nofxx.com'
|
13
|
+
gem.license = 'MIT'
|
14
14
|
|
15
|
-
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
15
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
|
16
16
|
gem.files = `git ls-files`.split("\n")
|
17
17
|
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
-
gem.name =
|
19
|
-
gem.require_paths = [
|
20
|
-
gem.summary =
|
21
|
-
|
18
|
+
gem.name = 'geonames_local'
|
19
|
+
gem.require_paths = ['lib']
|
20
|
+
gem.summary = 'Dumps geonames data for local usage'
|
22
21
|
|
23
22
|
gem.extra_rdoc_files = [
|
24
|
-
|
25
|
-
|
23
|
+
'MIT-LICENSE',
|
24
|
+
'README.md'
|
26
25
|
]
|
27
26
|
|
28
|
-
gem.add_dependency('mongoid', ['
|
27
|
+
gem.add_dependency('mongoid', ['> 3.0.0'])
|
29
28
|
gem.add_dependency('geopolitical', ['> 0.8.1'])
|
30
29
|
|
31
|
-
gem.post_install_message =
|
30
|
+
gem.post_install_message = '
|
32
31
|
Geonames Local
|
33
32
|
--------------
|
34
33
|
|
@@ -36,5 +35,6 @@ Use `geonames init` to create a config.yml file.
|
|
36
35
|
Or `geonames -c config.yml` to run using a config file.
|
37
36
|
|
38
37
|
Have fun!
|
39
|
-
|
38
|
+
|
39
|
+
'
|
40
40
|
end
|
data/lib/geonames_local/cli.rb
CHANGED
@@ -2,13 +2,11 @@
|
|
2
2
|
# Geonames Local
|
3
3
|
#
|
4
4
|
require 'optparse'
|
5
|
+
require 'benchmark'
|
5
6
|
# Require CLI Stuff
|
6
|
-
require 'geonames_local/geonames'
|
7
7
|
require 'geonames_local/data/shp'
|
8
8
|
require 'geonames_local/data/dump'
|
9
9
|
require 'geonames_local/data/export'
|
10
|
-
require 'geonames_local/cli'
|
11
|
-
|
12
10
|
|
13
11
|
module Geonames
|
14
12
|
#
|
@@ -23,7 +21,7 @@ module Geonames
|
|
23
21
|
opts.banner = 'Geonames Command Line Usage\n\n geonames <nation code(s)> <opts>\n\n\n'
|
24
22
|
|
25
23
|
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[:
|
24
|
+
opts.on('-d', '--dump', 'Dump DB before all') { options[:clean] = true }
|
27
25
|
opts.separator ''
|
28
26
|
opts.separator 'Config file:'
|
29
27
|
opts.on('-c', '--config CONFIG', String, 'Geonames Config file path') { |file| options[:config] = file }
|
@@ -57,14 +55,13 @@ module Geonames
|
|
57
55
|
private_class_method :parse_options
|
58
56
|
|
59
57
|
class << self
|
60
|
-
|
61
58
|
def load_config
|
62
|
-
info
|
59
|
+
info 'Loading config file...'
|
63
60
|
if Opt[:config]
|
64
61
|
Opt.merge! YAML.load(File.read(Opt[:config]))
|
65
62
|
else
|
66
63
|
# Load config/geonames.yml if there's one
|
67
|
-
if File.
|
64
|
+
if File.exist?(cfg = File.join('config', 'geonames.yml'))
|
68
65
|
Opt.merge! YAML.load(File.read(cfg))
|
69
66
|
else
|
70
67
|
fail
|
@@ -75,12 +72,42 @@ module Geonames
|
|
75
72
|
exit
|
76
73
|
end
|
77
74
|
|
78
|
-
|
79
|
-
|
80
|
-
info "Geopolitical Local Start!"
|
81
|
-
|
75
|
+
def trap_signals
|
76
|
+
puts 'Geopolitical Local Start!'
|
82
77
|
trap(:INT) { stop! }
|
83
78
|
trap(:TERM) { stop! }
|
79
|
+
end
|
80
|
+
|
81
|
+
def wrapper
|
82
|
+
Geonames::Models::MongoWrapper
|
83
|
+
end
|
84
|
+
|
85
|
+
def work_nations
|
86
|
+
info "\nPopulating 'nations' database..."
|
87
|
+
dump = Geonames::Dump.new(:all, :dump)
|
88
|
+
info "\n---\nTotal #{dump.data.length} parsed."
|
89
|
+
|
90
|
+
info 'Writing to nations DB'
|
91
|
+
wrapper.nations dump.data
|
92
|
+
end
|
93
|
+
|
94
|
+
def work_spots
|
95
|
+
info "\nPopulating 'regions' and 'cities' database..."
|
96
|
+
zip = Geonames::Dump.new(Opt[:nations], :zip).data
|
97
|
+
dump = Geonames::Dump.new(Opt[:nations], :dump).data
|
98
|
+
info "\n---\nTotal #{dump.size} parsed. #{zip.size} zip codes."
|
99
|
+
|
100
|
+
info 'Join dump << zip'
|
101
|
+
dump = unify!(dump, zip).group_by(&:kind)
|
102
|
+
|
103
|
+
info 'Writing to DB...'
|
104
|
+
wrapper.batch dump
|
105
|
+
end
|
106
|
+
|
107
|
+
# Ugly but works?
|
108
|
+
def work(argv)
|
109
|
+
start = Time.now
|
110
|
+
trap_signals
|
84
111
|
Opt.merge! parse_options(argv)
|
85
112
|
if Opt[:locales].nil? || Opt[:locales].empty?
|
86
113
|
Opt[:locales] = ['en']
|
@@ -95,7 +122,7 @@ module Geonames
|
|
95
122
|
# Return Codes and Exit
|
96
123
|
#
|
97
124
|
if argv[0] =~ /list|codes/
|
98
|
-
Codes.each do |key,val|
|
125
|
+
Codes.each do |key, val|
|
99
126
|
str = [val.values, key.to_s].join(' ').downcase
|
100
127
|
if s = argv[1]
|
101
128
|
next unless str =~ /#{s.downcase}/
|
@@ -119,85 +146,42 @@ module Geonames
|
|
119
146
|
exit
|
120
147
|
end
|
121
148
|
|
122
|
-
#
|
123
|
-
# Require georuby optionally
|
124
|
-
#
|
125
|
-
require 'geo_ruby' if Opt[:mapping] && Opt[:mapping][:geom]
|
126
|
-
|
127
|
-
#
|
128
149
|
# Load config if we got til here
|
129
|
-
#
|
130
150
|
load_config
|
131
151
|
|
132
|
-
#
|
133
152
|
# Export Data as CSV or JSON
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
load_adapter(Opt[:store])
|
143
|
-
info "Using adapter #{Opt[:store]}.."
|
144
|
-
|
145
|
-
# Nations
|
146
|
-
if Opt[:nations].empty? || argv[0] =~ /coun|nati/
|
147
|
-
info "\nPopulating 'nations' database..."
|
148
|
-
dump = Geonames::Dump.new(:all, :dump)
|
149
|
-
info "\n---\nTotal #{dump.data.length} parsed."
|
150
|
-
|
151
|
-
info 'Writing to nations DB'
|
152
|
-
Geonames::Models::MongoWrapper.nations dump.data, Opt[:clean]
|
153
|
-
|
154
|
-
# Regions, Cities....
|
155
|
-
else
|
156
|
-
zip = Geonames::Dump.new(Opt[:nations], :zip).data
|
157
|
-
dump = Geonames::Dump.new(Opt[:nations], :dump).data
|
158
|
-
info "\n---\nTotal #{dump.size} parsed. #{zip.size} zips."
|
159
|
-
|
160
|
-
info 'Join dump << zip'
|
161
|
-
dump = unify!(dump, zip).group_by(&:kind)
|
162
|
-
|
163
|
-
info 'Writing to DB...'
|
164
|
-
Geonames::Models::MongoWrapper.batch dump, Opt[:clean]
|
165
|
-
# info "Writing cities..."
|
166
|
-
# Geonames::Models::City.from_batch dump[:city]
|
167
|
-
end
|
168
|
-
end
|
153
|
+
return Geonames::Export.new(Nation.all).to_csv if argv[0] =~ /csv|json/
|
154
|
+
|
155
|
+
# Do the magic! Import Geonames Data
|
156
|
+
load_adapter(Opt[:store])
|
157
|
+
info "Using adapter #{Opt[:store]}.."
|
158
|
+
wrapper.clean if Opt[:clean]
|
159
|
+
Benchmark.measure { work_nations } unless wrapper.nations_populated?
|
160
|
+
Benchmark.measure { work_spots }
|
169
161
|
end
|
170
162
|
|
171
163
|
def load_adapter(name)
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
stop!
|
177
|
-
end
|
164
|
+
require_relative "models/#{name}"
|
165
|
+
rescue LoadError => e
|
166
|
+
info "Can't find adapter for #{name} #{e}"
|
167
|
+
stop!
|
178
168
|
end
|
179
169
|
|
180
170
|
def unify!(dump, zip)
|
181
171
|
start = Time.now
|
182
172
|
dump.map! do |spot|
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
else
|
187
|
-
spot
|
188
|
-
end
|
173
|
+
next spot unless (other = zip.find { |z| z.code == spot.code })
|
174
|
+
spot.zip = other.zip
|
175
|
+
spot
|
189
176
|
end
|
190
177
|
info "Done. #{(Time.now - start).to_i}s"
|
191
178
|
dump
|
192
179
|
end
|
193
180
|
|
194
181
|
def stop!
|
195
|
-
|
182
|
+
info 'Closing Geonames...'
|
196
183
|
exit
|
197
184
|
end
|
198
|
-
|
199
185
|
end # class < self
|
200
|
-
|
201
186
|
end # CLI
|
202
|
-
|
203
187
|
end # Geonames
|
@@ -1,18 +1,18 @@
|
|
1
1
|
module Geonames
|
2
2
|
class Dump
|
3
|
+
attr_reader :data
|
4
|
+
|
3
5
|
# Geonames base URL
|
4
|
-
URL =
|
6
|
+
URL = 'http://download.geonames.org/export/'
|
5
7
|
# Work temporary files
|
6
|
-
TMP =
|
8
|
+
TMP = '/tmp/geonames/'
|
7
9
|
|
8
10
|
def initialize(target, kind)
|
9
11
|
@kind = kind
|
10
12
|
@data = []
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
nations
|
15
|
-
end
|
13
|
+
|
14
|
+
target.each { |n| work(n) } if target.respond_to? :each
|
15
|
+
nations if target == :all
|
16
16
|
end
|
17
17
|
|
18
18
|
def nations
|
@@ -22,7 +22,7 @@ module Geonames
|
|
22
22
|
parse file
|
23
23
|
end
|
24
24
|
|
25
|
-
def work
|
25
|
+
def work(nation)
|
26
26
|
info "\nWorking on #{@kind} for #{nation}"
|
27
27
|
file = get_file(nation)
|
28
28
|
download file
|
@@ -30,19 +30,15 @@ module Geonames
|
|
30
30
|
parse file
|
31
31
|
end
|
32
32
|
|
33
|
-
def data
|
34
|
-
@data
|
35
|
-
end
|
36
|
-
|
37
33
|
def get_file(nation)
|
38
|
-
nation ==
|
34
|
+
nation == 'nation' ? 'countryInfo.txt' : "#{nation.upcase}.zip"
|
39
35
|
end
|
40
36
|
|
41
37
|
def download(file)
|
42
|
-
Dir.mkdir(TMP) unless File.
|
43
|
-
Dir.mkdir(TMP + @kind.to_s) unless File.
|
38
|
+
Dir.mkdir(TMP) unless File.exist?(TMP)
|
39
|
+
Dir.mkdir(TMP + @kind.to_s) unless File.exist?(TMP + @kind.to_s)
|
44
40
|
fname = TMP + "#{@kind}/#{file}"
|
45
|
-
return if File.
|
41
|
+
return if File.exist?(fname)
|
46
42
|
`curl #{URL}/#{@kind}/#{file} -o #{fname}`
|
47
43
|
end
|
48
44
|
|
@@ -54,34 +50,27 @@ module Geonames
|
|
54
50
|
def parse_line(l)
|
55
51
|
return if l =~ /^#|^iso/i
|
56
52
|
if @kind == :dump
|
57
|
-
if l =~ /^\D/
|
58
|
-
|
59
|
-
|
60
|
-
if Opt[:level] != "all"
|
61
|
-
return unless l =~ /ADM\d/ # ADM2 => cities
|
62
|
-
end
|
53
|
+
return l if l =~ /^\D/
|
54
|
+
if Opt[:level] != 'all'
|
55
|
+
return unless l =~ /ADM\d/ # ADM2 => cities
|
63
56
|
end
|
64
57
|
end
|
65
58
|
Spot.new(l, @kind)
|
66
59
|
end
|
67
60
|
|
68
61
|
def parse(file)
|
69
|
-
red = 0
|
70
62
|
start = Time.now
|
71
|
-
File.open("/tmp/geonames/#{@kind}/#{file.gsub(
|
63
|
+
File.open("/tmp/geonames/#{@kind}/#{file.gsub('zip', 'txt')}") do |f|
|
72
64
|
while line = f.gets
|
73
65
|
if record = parse_line(line)
|
74
66
|
@data << record
|
75
|
-
red += 1
|
76
67
|
end
|
77
68
|
end
|
78
69
|
total = Time.now - start
|
79
|
-
info "#{
|
70
|
+
info "#{@data.size} #{@kind} spots parsed #{total}s (#{(@data.size / total).to_i}/s)"
|
80
71
|
end
|
81
|
-
|
72
|
+
rescue Errno::ENOENT => e
|
82
73
|
info "Failed to download #{file}, skipping. #{e}"
|
83
74
|
end
|
84
|
-
|
85
75
|
end
|
86
|
-
|
87
76
|
end
|
@@ -2,20 +2,19 @@ require 'csv'
|
|
2
2
|
|
3
3
|
module Geonames
|
4
4
|
class Export
|
5
|
-
|
6
5
|
def initialize(data)
|
7
|
-
info
|
6
|
+
info 'Starting export..'
|
8
7
|
@data = data
|
9
8
|
end
|
10
9
|
|
11
10
|
def to_csv
|
12
|
-
file =
|
11
|
+
file = 'export.csv'
|
13
12
|
info "Writing #{file} (#{@data.length} objects)"
|
14
|
-
CSV.open(
|
13
|
+
CSV.open('export.csv', 'w') do |csv|
|
15
14
|
csv << @data[0].export_header
|
16
15
|
@data.each { |o| csv << o.export }
|
17
16
|
end
|
18
|
-
info
|
17
|
+
info 'Export done.'
|
19
18
|
end
|
20
19
|
end
|
21
20
|
end
|
@@ -4,10 +4,9 @@
|
|
4
4
|
|
5
5
|
module Geonames
|
6
6
|
class SHP
|
7
|
-
|
8
7
|
def initialize(file)
|
9
8
|
@file = file
|
10
|
-
@fname = file.split(
|
9
|
+
@fname = file.split('/')[-1] rescue nil
|
11
10
|
@type = Object.module_eval("::#{Opt[:type].capitalize}", __FILE__, __LINE__)
|
12
11
|
# @ic = Iconv.new('UTF-8//IGNORE', 'UTF-8')
|
13
12
|
@sample = nil
|
@@ -17,7 +16,7 @@ module Geonames
|
|
17
16
|
end
|
18
17
|
|
19
18
|
def shp2pg
|
20
|
-
info
|
19
|
+
info 'Converting SRID'
|
21
20
|
`shp2pgsql -D -as 4326 #{@file} nil > /tmp/#{@fname}.dump`
|
22
21
|
end
|
23
22
|
|
@@ -32,7 +31,7 @@ module Geonames
|
|
32
31
|
end
|
33
32
|
|
34
33
|
def parse
|
35
|
-
info
|
34
|
+
info 'Parsing dump'
|
36
35
|
start = Time.now
|
37
36
|
red = 0
|
38
37
|
File.open("/tmp/#{@fname}.dump") do |f|
|
@@ -44,15 +43,15 @@ module Geonames
|
|
44
43
|
end
|
45
44
|
end
|
46
45
|
end
|
47
|
-
info "#{red} parsed. #{Time.now-start}s"
|
46
|
+
info "#{red} parsed. #{Time.now - start}s"
|
48
47
|
end
|
49
48
|
|
50
49
|
def reduce!
|
51
|
-
hsh = Cache[:roads].group_by
|
50
|
+
hsh = Cache[:roads].group_by(&:name)
|
52
51
|
arr = []
|
53
|
-
hsh.map do |
|
52
|
+
hsh.map do |_key, vals|
|
54
53
|
first = vals.delete_at(0)
|
55
|
-
# p vals[0].geom.geometries.concat(vals[1].geom.geometries)
|
54
|
+
# p vals[0].geom.geometries.concat(vals[1].geom.geometries)
|
56
55
|
vals.map(&:geom).each do |g|
|
57
56
|
first.geom.geometries.concat g.geometries
|
58
57
|
end
|
@@ -1,10 +1,10 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
#
|
2
|
+
# Road/Street/Via/Autoban
|
3
|
+
#
|
4
4
|
class Road
|
5
5
|
attr_reader :city, :region, :nation, :zone, :name, :geom, :kind, :table
|
6
6
|
|
7
|
-
def initialize(keys, vals, nation=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|
|
@@ -12,7 +12,7 @@ class Road
|
|
12
12
|
end
|
13
13
|
@name = r[:name]
|
14
14
|
@zone = r[:zone]
|
15
|
-
kind = r[:kind] || @name.split(
|
15
|
+
kind = r[:kind] || @name.split(' ')[0]
|
16
16
|
@geom = parse_geom(r[:geom])
|
17
17
|
@kind = parse_kind(kind)
|
18
18
|
@city = city
|
@@ -22,14 +22,12 @@ class Road
|
|
22
22
|
|
23
23
|
def parse_geom(hex)
|
24
24
|
if hex =~ /^SRID/ # PG 8.3 support
|
25
|
-
hex = hex.split(
|
25
|
+
hex = hex.split(';')[1]
|
26
26
|
end
|
27
27
|
GeoRuby::SimpleFeatures::Geometry.from_hex_ewkb(hex)
|
28
28
|
end
|
29
29
|
|
30
|
-
|
31
|
-
@geom = g
|
32
|
-
end
|
30
|
+
attr_writer :geom
|
33
31
|
|
34
32
|
def parse_kind(k)
|
35
33
|
case k
|
@@ -40,5 +38,4 @@ class Road
|
|
40
38
|
else :unknown
|
41
39
|
end
|
42
40
|
end
|
43
|
-
|
44
41
|
end
|
@@ -5,10 +5,10 @@ module Geonames
|
|
5
5
|
class Spot
|
6
6
|
attr_accessor :gid, :name, :ascii, :lat, :lon, :nation, :kind,
|
7
7
|
:code, :pop, :tz, :geom, :region, :zip, :abbr, :id
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
8
|
+
alias_method :x, :lon
|
9
|
+
alias_method :y, :lat
|
10
|
+
alias_method :geoname_id, :gid
|
11
|
+
alias_method :table, :kind
|
12
12
|
|
13
13
|
#
|
14
14
|
# = Geonames Spot
|
@@ -97,7 +97,6 @@ module Geonames
|
|
97
97
|
end
|
98
98
|
|
99
99
|
class << self
|
100
|
-
|
101
100
|
attr_accessor :collection
|
102
101
|
|
103
102
|
def nearest(x, y)
|
@@ -110,9 +109,6 @@ module Geonames
|
|
110
109
|
spot.geom = GeoRuby::SimpleFeatures::Point.from_x_y(*spot.geom)
|
111
110
|
spot
|
112
111
|
end
|
113
|
-
|
114
112
|
end
|
115
|
-
|
116
113
|
end
|
117
|
-
|
118
114
|
end
|
@@ -1,10 +1,10 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
#
|
2
|
+
# A polygon basically.
|
3
|
+
#
|
4
4
|
class Zone
|
5
5
|
attr_reader :city, :name, :geom, :kind
|
6
6
|
|
7
|
-
def initialize(keys, vals, city=nil)
|
7
|
+
def initialize(keys, vals, city = nil)
|
8
8
|
s = vals.split("\t")
|
9
9
|
r = {}
|
10
10
|
keys.each_with_index do |k, i|
|
@@ -20,5 +20,4 @@ class Zone
|
|
20
20
|
def parse_geom(hex)
|
21
21
|
@geom = GeoRuby::SimpleFeatures::Geometry.from_hex_ewkb(hex)
|
22
22
|
end
|
23
|
-
|
24
23
|
end
|