geonames_local 0.3.1 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +26 -11
- data/Rakefile +5 -12
- data/VERSION +1 -1
- data/bin/geonames +1 -1
- data/geonames.yml +3 -2
- data/geonames_local.gemspec +65 -68
- data/lib/geonames_ar.rb +2 -4
- data/lib/geonames_cli.rb +2 -1
- data/lib/geonames_local/adapters/mongodb.rb +2 -1
- data/lib/geonames_local/adapters/postgres.rb +28 -13
- data/lib/geonames_local/cli.rb +6 -56
- data/lib/geonames_local/data/dump.rb +4 -4
- data/lib/geonames_local/data/sync.rb +74 -0
- data/lib/geonames_local/features/spot.rb +9 -4
- data/lib/geonames_local/models/ar.rb +50 -0
- data/lib/geonames_local/{model/country.rb → models/mongo.rb} +27 -26
- data/lib/geonames_local/{mongo/country.rb → models/tokyo.rb} +23 -1
- data/lib/geonames_local.rb +1 -3
- data/spec/geonames_local/adapters/mongodb_spec.rb +14 -9
- data/spec/geonames_local/cli_spec.rb +5 -0
- data/spec/geonames_local/data/dump_spec.rb +4 -0
- data/spec/geonames_local/features/spot_spec.rb +146 -66
- data/spec/geonames_local/models/ar_spec.rb +73 -0
- data/spec/geonames_local/{mongo/city_spec.rb → models/mongo_spec.rb} +7 -8
- data/spec/spec_ar_helper.rb +78 -37
- data/spec/spec_helper.rb +4 -3
- data/spec/spec_mongo_helper.rb +3 -2
- metadata +24 -26
- data/.gitignore +0 -28
- data/lib/geonames_local/model/city.rb +0 -26
- data/lib/geonames_local/model/province.rb +0 -18
- data/lib/geonames_local/mongo/city.rb +0 -7
- data/lib/geonames_local/mongo/province.rb +0 -18
- data/spec/geonames_local/model/ar_spec.rb +0 -19
- data/spec/spec.opts +0 -1
@@ -0,0 +1,73 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_ar_helper')
|
3
|
+
module Geonames
|
4
|
+
module Models
|
5
|
+
module AR
|
6
|
+
class User < ActiveRecord::Base
|
7
|
+
belongs_to :city
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def brasil
|
14
|
+
Models::AR::Country.find_or_create_by_name(:name => "Brazil", :abbr => "BR")
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "Country" do
|
18
|
+
|
19
|
+
it "should create countries" do
|
20
|
+
Models::AR::Country.create(:name => "Chad", :abbr => "TD").should be_valid
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should write to db" do
|
24
|
+
lambda do
|
25
|
+
Models::AR::Country.create(:name => "Itália", :abbr => "IT")
|
26
|
+
end.should change(Models::AR::Country, :count).by(1)
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "Province" do
|
32
|
+
|
33
|
+
it "should be a class instance of ar" do
|
34
|
+
Models::AR::Province.new.should be_instance_of Models::AR::Province
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should create" do
|
38
|
+
Models::AR::Province.create(:name => "Chadland", :country => brasil).should be_valid
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "City" do
|
43
|
+
|
44
|
+
it "should be a class instance of ar" do
|
45
|
+
Models::AR::City.new.should be_instance_of Models::AR::City
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should create" do
|
49
|
+
Models::AR::City.create(:name => "Chadland", :country => brasil).should be_valid
|
50
|
+
end
|
51
|
+
end
|
52
|
+
# DatabaseCleaner.clean
|
53
|
+
|
54
|
+
describe "Active Record Stuff" do
|
55
|
+
|
56
|
+
before do
|
57
|
+
# DatabaseCleaner.clean
|
58
|
+
@br ||= brasil
|
59
|
+
Models::AR::City.create!("name" => "São Tomé", "geom" => [15,15], :country => @br)
|
60
|
+
Models::AR::City.create!("name" => "Rock CIty", "geom" => [18,16], :country => @br)
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should record" do
|
64
|
+
Models::AR::City.count.should eql(2)
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should create" do
|
68
|
+
user = Models::AR::User.new(:name => "Defendor")
|
69
|
+
user.city = Models::AR::City.first
|
70
|
+
user.save
|
71
|
+
Models::AR::User.first.city.name.should eql("São Tomé")
|
72
|
+
end
|
73
|
+
end
|
@@ -1,9 +1,8 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
2
|
require File.expand_path(File.dirname(__FILE__) + '/../../spec_mongo_helper')
|
3
3
|
require "geo_ruby"
|
4
|
-
include Geonames::Mongo
|
5
4
|
|
6
|
-
describe City do
|
5
|
+
describe "City" do
|
7
6
|
|
8
7
|
before(:all) do
|
9
8
|
Adapter.insert("cities", {"id" => 9, "name" => "Sao Paulo", "geom" => [15,15]})
|
@@ -15,26 +14,26 @@ describe City do
|
|
15
14
|
end
|
16
15
|
|
17
16
|
it "should set a collection name" do
|
18
|
-
City.collection.should eql("cities")
|
17
|
+
Models::Mongo::City.collection.should eql("cities")
|
19
18
|
end
|
20
19
|
|
21
20
|
it "should find all cities" do
|
22
|
-
City.all.should_not be_empty
|
21
|
+
Models::Mongo::City.all.should_not be_empty
|
23
22
|
end
|
24
23
|
|
25
24
|
it "should be a city instance" do
|
26
|
-
City.nearest(1,1).should be_instance_of(City)
|
25
|
+
Models::Mongo::City.nearest(1,1).should be_instance_of(Models::Mongo::City)
|
27
26
|
end
|
28
27
|
|
29
28
|
it "should find city nearest point" do
|
30
|
-
City.nearest(1,1).name.should eql("Rock City")
|
29
|
+
Models::Mongo::City.nearest(1,1).name.should eql("Rock City")
|
31
30
|
end
|
32
31
|
|
33
32
|
it "should find by name" do
|
34
|
-
City.find_by_name("Rock")[0].name.should eql("Rock City")
|
33
|
+
Models::Mongo::City.find_by_name("Rock")[0].name.should eql("Rock City")
|
35
34
|
end
|
36
35
|
|
37
36
|
it "should find by name" do
|
38
|
-
City.find_by_name("rock")[0].name.should eql("Rock City")
|
37
|
+
Models::Mongo::City.find_by_name("rock")[0].name.should eql("Rock City")
|
39
38
|
end
|
40
39
|
end
|
data/spec/spec_ar_helper.rb
CHANGED
@@ -1,19 +1,38 @@
|
|
1
1
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
2
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
-
require '
|
4
|
-
require '
|
3
|
+
require 'rspec'
|
4
|
+
require 'rspec/autorun'
|
5
5
|
require 'active_record'
|
6
6
|
require 'postgis_adapter'
|
7
|
+
require 'database_cleaner'
|
8
|
+
Opt[:db] = { :adapter => "postgresql",
|
9
|
+
:database => "geonames_ar",
|
10
|
+
:username => "postgres",
|
11
|
+
:password => "" }
|
7
12
|
require 'geonames_ar'
|
13
|
+
include Geonames::Models
|
14
|
+
|
15
|
+
# DatabaseCleaner.strategy = :truncation
|
8
16
|
|
9
17
|
ActiveRecord::Base.logger = $logger
|
10
|
-
ActiveRecord::Base.establish_connection({ :adapter => "postgresql",
|
11
|
-
:database => "geonames_ar",
|
12
|
-
:username => "postgres",
|
13
|
-
:password => "" })
|
14
18
|
|
15
|
-
|
19
|
+
begin
|
20
|
+
ActiveRecord::Base.establish_connection(Opt[:db])
|
21
|
+
ActiveRecord::Migration.verbose = false
|
22
|
+
PG_VERSION = ActiveRecord::Base.connection.select_value("SELECT version()").scan(/PostgreSQL ([\d\.]*)/)[0][0]
|
16
23
|
|
24
|
+
puts "Running against PostgreSQL #{PG_VERSION}"
|
25
|
+
|
26
|
+
rescue PGError
|
27
|
+
puts "Test DB not found, creating one for you..."
|
28
|
+
`createdb -U postgres geonames_ar -T template_postgis`
|
29
|
+
puts "Done. Please run spec again."
|
30
|
+
exit
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
ActiveRecord::Schema.define() do
|
17
36
|
|
18
37
|
create_table :users, :force => true do |t|
|
19
38
|
t.string :name, :nick, :limit => 100
|
@@ -21,35 +40,57 @@ ActiveRecord::Schema.define() do
|
|
21
40
|
t.integer :code
|
22
41
|
t.references :city
|
23
42
|
end
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
43
|
+
|
44
|
+
create_table :cities, :force => true do |t|
|
45
|
+
t.references :country, :null => false
|
46
|
+
t.references :province
|
47
|
+
t.string :name, :null => false
|
48
|
+
t.point :geom, :srid => 4326
|
49
|
+
t.integer :gid, :zip
|
50
|
+
end
|
51
|
+
|
52
|
+
create_table :provinces, :force => true do |t|
|
53
|
+
t.references :country, :null => false
|
54
|
+
t.string :name, :null => false
|
55
|
+
t.string :abbr, :limit => 3
|
56
|
+
t.integer :gid
|
57
|
+
end
|
58
|
+
|
59
|
+
create_table :countries, :force => true do |t|
|
60
|
+
t.string :name, :limit => 30, :null => false
|
61
|
+
t.string :abbr, :limit => 2, :null => false
|
62
|
+
end
|
63
|
+
|
64
|
+
add_index :cities, :name
|
65
|
+
add_index :cities, :gid
|
66
|
+
add_index :cities, :zip
|
67
|
+
add_index :cities, :country_id
|
68
|
+
add_index :cities, :province_id
|
69
|
+
add_index :cities, :geom, :spatial => true
|
70
|
+
add_index :provinces, :name
|
71
|
+
add_index :provinces, :abbr
|
72
|
+
add_index :provinces, :gid
|
73
|
+
add_index :provinces, :country_id
|
74
|
+
add_index :countries, :abbr, :unique => true
|
75
|
+
add_index :countries, :name, :unique => true
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
RSpec.configure do |c|
|
80
|
+
|
81
|
+
c.before(:suite) do
|
82
|
+
DatabaseCleaner.strategy = :transaction
|
83
|
+
DatabaseCleaner.clean_with(:truncation,
|
84
|
+
{:except => %w[geography_columns schema_migrations spatial_ref_sys geometry_columns]})
|
85
|
+
end
|
86
|
+
|
87
|
+
c.before(:each) do
|
88
|
+
DatabaseCleaner.start
|
89
|
+
end
|
90
|
+
|
91
|
+
c.after(:each) do
|
92
|
+
DatabaseCleaner.clean
|
93
|
+
end
|
94
|
+
|
54
95
|
|
55
96
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
2
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
3
|
require 'geonames_cli'
|
4
|
-
require '
|
5
|
-
require '
|
4
|
+
require 'geonames_local'
|
5
|
+
require 'rspec'
|
6
|
+
require 'rspec/autorun'
|
6
7
|
include Geonames
|
7
8
|
|
8
|
-
|
9
|
+
RSpec.configure do |config|
|
9
10
|
|
10
11
|
end
|
data/spec/spec_mongo_helper.rb
CHANGED
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
|
+
- 5
|
8
|
+
- 0
|
9
|
+
version: 0.5.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Marcos Piccinini
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date:
|
17
|
+
date: 2011-04-24 00:00:00 -03:00
|
18
18
|
default_executable: geonames
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -58,7 +58,6 @@ extra_rdoc_files:
|
|
58
58
|
- README.rdoc
|
59
59
|
files:
|
60
60
|
- .document
|
61
|
-
- .gitignore
|
62
61
|
- LICENSE
|
63
62
|
- README.rdoc
|
64
63
|
- Rakefile
|
@@ -79,29 +78,27 @@ files:
|
|
79
78
|
- lib/geonames_local/data/dump.rb
|
80
79
|
- lib/geonames_local/data/export.rb
|
81
80
|
- lib/geonames_local/data/shp.rb
|
81
|
+
- lib/geonames_local/data/sync.rb
|
82
82
|
- lib/geonames_local/features/road.rb
|
83
83
|
- lib/geonames_local/features/spot.rb
|
84
84
|
- lib/geonames_local/features/zone.rb
|
85
85
|
- lib/geonames_local/geonames.rb
|
86
|
-
- lib/geonames_local/
|
87
|
-
- lib/geonames_local/
|
88
|
-
- lib/geonames_local/
|
89
|
-
- lib/geonames_local/mongo/city.rb
|
90
|
-
- lib/geonames_local/mongo/country.rb
|
91
|
-
- lib/geonames_local/mongo/province.rb
|
86
|
+
- lib/geonames_local/models/ar.rb
|
87
|
+
- lib/geonames_local/models/mongo.rb
|
88
|
+
- lib/geonames_local/models/tokyo.rb
|
92
89
|
- spec/geonames_local/adapters/mongodb_spec.rb
|
93
90
|
- spec/geonames_local/adapters/postgres_spec.rb
|
94
91
|
- spec/geonames_local/adapters/tokyo_spec.rb
|
95
92
|
- spec/geonames_local/cli_spec.rb
|
96
93
|
- spec/geonames_local/data/cache_spec.rb
|
94
|
+
- spec/geonames_local/data/dump_spec.rb
|
97
95
|
- spec/geonames_local/data/shp_spec.rb
|
98
96
|
- spec/geonames_local/features/road_spec.rb
|
99
97
|
- spec/geonames_local/features/spot_spec.rb
|
100
98
|
- spec/geonames_local/features/zone_spec.rb
|
101
|
-
- spec/geonames_local/
|
102
|
-
- spec/geonames_local/
|
99
|
+
- spec/geonames_local/models/ar_spec.rb
|
100
|
+
- spec/geonames_local/models/mongo_spec.rb
|
103
101
|
- spec/geonames_local_spec.rb
|
104
|
-
- spec/spec.opts
|
105
102
|
- spec/spec_ar_helper.rb
|
106
103
|
- spec/spec_helper.rb
|
107
104
|
- spec/spec_mongo_helper.rb
|
@@ -123,8 +120,8 @@ post_install_message: "\n\
|
|
123
120
|
MongoDB 2D support is new, only mongo >= 1.3.3 mongodb gem >= 0.19.2\n\
|
124
121
|
http://github.com/mongodb/mongo-ruby-driver\n\n\
|
125
122
|
Have fun because:\n"
|
126
|
-
rdoc_options:
|
127
|
-
|
123
|
+
rdoc_options: []
|
124
|
+
|
128
125
|
require_paths:
|
129
126
|
- lib
|
130
127
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -151,18 +148,19 @@ signing_key:
|
|
151
148
|
specification_version: 3
|
152
149
|
summary: Dump and feed a tokyo local geonames db
|
153
150
|
test_files:
|
154
|
-
- spec/
|
155
|
-
- spec/spec_helper.rb
|
156
|
-
- spec/geonames_local/data/shp_spec.rb
|
157
|
-
- spec/geonames_local/data/cache_spec.rb
|
158
|
-
- spec/geonames_local/model/ar_spec.rb
|
159
|
-
- spec/geonames_local/mongo/city_spec.rb
|
160
|
-
- spec/geonames_local/cli_spec.rb
|
151
|
+
- spec/geonames_local/adapters/mongodb_spec.rb
|
161
152
|
- spec/geonames_local/adapters/postgres_spec.rb
|
162
153
|
- spec/geonames_local/adapters/tokyo_spec.rb
|
163
|
-
- spec/geonames_local/
|
154
|
+
- spec/geonames_local/cli_spec.rb
|
155
|
+
- spec/geonames_local/data/cache_spec.rb
|
156
|
+
- spec/geonames_local/data/dump_spec.rb
|
157
|
+
- spec/geonames_local/data/shp_spec.rb
|
158
|
+
- spec/geonames_local/features/road_spec.rb
|
164
159
|
- spec/geonames_local/features/spot_spec.rb
|
165
160
|
- spec/geonames_local/features/zone_spec.rb
|
166
|
-
- spec/geonames_local/
|
167
|
-
- spec/
|
161
|
+
- spec/geonames_local/models/ar_spec.rb
|
162
|
+
- spec/geonames_local/models/mongo_spec.rb
|
168
163
|
- spec/geonames_local_spec.rb
|
164
|
+
- spec/spec_ar_helper.rb
|
165
|
+
- spec/spec_helper.rb
|
166
|
+
- spec/spec_mongo_helper.rb
|
data/.gitignore
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
## MAC OS
|
2
|
-
.DS_Store
|
3
|
-
|
4
|
-
## TEXTMATE
|
5
|
-
*.tmproj
|
6
|
-
tmtags
|
7
|
-
|
8
|
-
## EMACS
|
9
|
-
*~
|
10
|
-
\#*
|
11
|
-
.\#*
|
12
|
-
*flymake.rb
|
13
|
-
|
14
|
-
## VIM
|
15
|
-
*.swp
|
16
|
-
|
17
|
-
## PROJECT::GENERAL
|
18
|
-
coverage
|
19
|
-
rdoc
|
20
|
-
pkg
|
21
|
-
|
22
|
-
## PROJECT::SPECIFIC
|
23
|
-
*.tct
|
24
|
-
*.pid
|
25
|
-
*.log
|
26
|
-
*.qgr
|
27
|
-
*.lex
|
28
|
-
shps
|
@@ -1,26 +0,0 @@
|
|
1
|
-
module Geonames
|
2
|
-
module Model
|
3
|
-
class City < ActiveRecord::Base
|
4
|
-
attr_accessor :x, :y, :z
|
5
|
-
|
6
|
-
belongs_to :province
|
7
|
-
belongs_to :country
|
8
|
-
|
9
|
-
validates_presence_of :country
|
10
|
-
validates_presence_of :name
|
11
|
-
|
12
|
-
def abbr
|
13
|
-
province.try(:abbr) || country.abbr
|
14
|
-
end
|
15
|
-
|
16
|
-
# Instantiate self.geom as a Point
|
17
|
-
def validation
|
18
|
-
self.country ||= province.country
|
19
|
-
unless !@x || !@y || @x == "" || @y == ""
|
20
|
-
self.geom = Point.from_x_y(@x.to_f, @y.to_f)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
@@ -1,18 +0,0 @@
|
|
1
|
-
module Geonames
|
2
|
-
class Province
|
3
|
-
attr_accessor :code, :name, :gid
|
4
|
-
|
5
|
-
def self.all
|
6
|
-
Tokyo.new.all({ :kind => "province" }).map do |c|
|
7
|
-
new(c)
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
def initialize(params)
|
12
|
-
@code = params["code"]
|
13
|
-
@name = params["name"]
|
14
|
-
@gid = params["gid"]
|
15
|
-
end
|
16
|
-
|
17
|
-
end
|
18
|
-
end
|