geonames_local 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,6 +1,6 @@
1
1
  = Geonames Local
2
2
 
3
- Download and store in tokyo or postgresql Geonames.org data.
3
+ Download and store Geonames.org data.
4
4
  Making every Geoname API operation possible on your servers.
5
5
  No hit limit, fast as possible.
6
6
 
@@ -85,7 +85,7 @@ Default PG migration:
85
85
  add_index :cities, :province_id
86
86
  add_index :cities, :geom, :spatial => true
87
87
  add_index :provinces, :name
88
- add_index :provinces, :abbrg
88
+ add_index :provinces, :abbr
89
89
  add_index :provinces, :gid
90
90
  add_index :provinces, :country_id
91
91
 
data/Rakefile CHANGED
@@ -10,8 +10,10 @@ begin
10
10
  gem.email = "x@nofxx.com"
11
11
  gem.homepage = "http://github.com/nofxx/geonames_local"
12
12
  gem.authors = ["Marcos Piccinini"]
13
+
14
+ gem.add_dependency "nofxx-georuby", ">= 1.7.1"
13
15
  gem.add_development_dependency "rspec", ">= 1.2.9"
14
- gem.add_dependency "nofxx-georuby", ">= 1.7.1"
16
+
15
17
  gem.post_install_message = <<-POST_INSTALL_MESSAGE
16
18
 
17
19
  Geonames Local
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.3.0
data/geonames.yml ADDED
@@ -0,0 +1,14 @@
1
+ #
2
+ # Geonames Local Config Example
3
+ #
4
+ :store: mongodb
5
+ :codes: [br]
6
+ :level: city
7
+ :mapping:
8
+ :name: name
9
+ :geom: true
10
+ :db:
11
+ :host: localhost
12
+ :dbname: rtrac_development
13
+ :user: postgres
14
+ :password:
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{geonames_local}
8
- s.version = "0.2.1"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Marcos Piccinini"]
12
- s.date = %q{2010-03-19}
12
+ s.date = %q{2010-03-20}
13
13
  s.default_executable = %q{geonames}
14
14
  s.description = %q{Dump and feed a tokyo cabinet for local geonames search}
15
15
  s.email = %q{x@nofxx.com}
@@ -26,35 +26,41 @@ Gem::Specification.new do |s|
26
26
  "Rakefile",
27
27
  "VERSION",
28
28
  "bin/geonames",
29
+ "geonames.yml",
29
30
  "geonames_local.gemspec",
31
+ "lib/geonames_ext.rb",
30
32
  "lib/geonames_local.rb",
31
33
  "lib/geonames_local/adapters/mongodb.rb",
32
34
  "lib/geonames_local/adapters/postgres.rb",
33
35
  "lib/geonames_local/adapters/tokyo.rb",
34
36
  "lib/geonames_local/cli.rb",
35
37
  "lib/geonames_local/config/codes.yml",
38
+ "lib/geonames_local/config/geonames.sql",
36
39
  "lib/geonames_local/config/geonames.yml",
37
- "lib/geonames_local/data/cache.rb",
38
- "lib/geonames_local/data/geoquery.rb",
40
+ "lib/geonames_local/data/dump.rb",
41
+ "lib/geonames_local/data/export.rb",
39
42
  "lib/geonames_local/data/shp.rb",
40
- "lib/geonames_local/dump.rb",
41
- "lib/geonames_local/export.rb",
42
- "lib/geonames_local/features/city.rb",
43
- "lib/geonames_local/features/country.rb",
44
- "lib/geonames_local/features/province.rb",
45
43
  "lib/geonames_local/features/road.rb",
46
44
  "lib/geonames_local/features/spot.rb",
47
45
  "lib/geonames_local/features/zone.rb",
46
+ "lib/geonames_local/geonames.rb",
47
+ "lib/geonames_local/model/city.rb",
48
+ "lib/geonames_local/model/conn.rb",
49
+ "lib/geonames_local/model/country.rb",
50
+ "lib/geonames_local/model/province.rb",
51
+ "spec/geonames_local/adapters/mongodb_spec.rb",
52
+ "spec/geonames_local/adapters/postgres_spec.rb",
53
+ "spec/geonames_local/adapters/tokyo_spec.rb",
48
54
  "spec/geonames_local/cli_spec.rb",
49
55
  "spec/geonames_local/data/cache_spec.rb",
50
- "spec/geonames_local/data/postgres_spec.rb",
51
56
  "spec/geonames_local/data/shp_spec.rb",
52
- "spec/geonames_local/data/tokyo_spec.rb",
53
57
  "spec/geonames_local/features/road_spec.rb",
54
58
  "spec/geonames_local/features/spot_spec.rb",
55
59
  "spec/geonames_local/features/zone_spec.rb",
60
+ "spec/geonames_local/model/city_spec.rb",
56
61
  "spec/geonames_local_spec.rb",
57
62
  "spec/spec.opts",
63
+ "spec/spec_ext_helper.rb",
58
64
  "spec/spec_helper.rb",
59
65
  "task/benchmark.rb",
60
66
  "task/benchmark_cabinet.rb"
@@ -90,14 +96,17 @@ Have fun because:
90
96
  s.test_files = [
91
97
  "spec/spec_helper.rb",
92
98
  "spec/geonames_local/data/shp_spec.rb",
93
- "spec/geonames_local/data/postgres_spec.rb",
94
- "spec/geonames_local/data/tokyo_spec.rb",
95
99
  "spec/geonames_local/data/cache_spec.rb",
100
+ "spec/geonames_local/model/city_spec.rb",
96
101
  "spec/geonames_local/cli_spec.rb",
102
+ "spec/geonames_local/adapters/postgres_spec.rb",
103
+ "spec/geonames_local/adapters/tokyo_spec.rb",
104
+ "spec/geonames_local/adapters/mongodb_spec.rb",
97
105
  "spec/geonames_local/features/spot_spec.rb",
98
106
  "spec/geonames_local/features/zone_spec.rb",
99
107
  "spec/geonames_local/features/road_spec.rb",
100
- "spec/geonames_local_spec.rb"
108
+ "spec/geonames_local_spec.rb",
109
+ "spec/spec_ext_helper.rb"
101
110
  ]
102
111
 
103
112
  if s.respond_to? :specification_version then
@@ -105,15 +114,15 @@ Have fun because:
105
114
  s.specification_version = 3
106
115
 
107
116
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
108
- s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
109
117
  s.add_runtime_dependency(%q<nofxx-georuby>, [">= 1.7.1"])
118
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
110
119
  else
111
- s.add_dependency(%q<rspec>, [">= 1.2.9"])
112
120
  s.add_dependency(%q<nofxx-georuby>, [">= 1.7.1"])
121
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
113
122
  end
114
123
  else
115
- s.add_dependency(%q<rspec>, [">= 1.2.9"])
116
124
  s.add_dependency(%q<nofxx-georuby>, [">= 1.7.1"])
125
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
117
126
  end
118
127
  end
119
128
 
@@ -0,0 +1,15 @@
1
+ #
2
+ # Geonames Extension
3
+ #
4
+ $:.unshift(File.dirname(__FILE__)) unless
5
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
6
+
7
+ # Require Libs
8
+ require 'geonames_local/geonames'
9
+
10
+ require 'geonames_local/model/conn'
11
+ require 'geonames_local/model/country'
12
+ require 'geonames_local/model/province'
13
+ require 'geonames_local/model/city'
14
+ #require 'geonames_local/data/shp'
15
+
@@ -5,28 +5,77 @@ module Geonames
5
5
 
6
6
  RESOURCES = ["countries", "provinces", "cities"]
7
7
 
8
- def initialize(params)
9
- port = params[:port] || 27017
10
- @conn = Mongo::Connection.new(params[:host], port)
11
- @db = @conn.db(params[:dbname])
12
- purge
8
+ def initialize(params={})
9
+ host, port = params[:host] || "localhost", params[:port] || 27017
10
+ @conn = Mongo::Connection.new(host, port)
11
+ @db = @conn.db(params[:dbname] || "geonames")
12
+ #purge
13
13
  setup
14
14
  end
15
15
 
16
16
  def setup
17
17
  for re in RESOURCES
18
18
  coll = @db.collection(re)
19
- coll.create_index(["id", Mongo::ASCENDING])
20
- coll.create_index(["geom", Mongo::GEO2D], :min => 0, :max => 180)
19
+ coll.create_index(["id", Mongo::ASCENDING], ["gid", Mongo::ASCENDING])
20
+
21
+ # Geometric index, more info:
22
+ # http://www.mongodb.org/display/DOCS/Geospatial+Indexing
23
+ coll.create_index([["geom", Mongo::GEO2D]], :min => -180, :max => 180)
21
24
  end
22
25
  end
23
26
 
27
+ def all(resource, limit=nil, skip=0)
28
+ @db.collection(resource.to_s).find().to_a
29
+ end
30
+
24
31
  def find(resource, id, name=nil)
25
- @db.collection(resource.to_s).find("id" => id).to_a.first
32
+ @db.collection(resource.to_s).find_one("id" => id)
33
+ end
34
+
35
+ def find_by_name(resource, name)
36
+ do_find(resource, "name" => /#{name}/)
37
+ end
38
+
39
+ def find_by_zip(resource, zip)
40
+ do_find(resource, "zip" => /#{zip}/)
41
+ end
42
+
43
+ def do_find(resource, hsh)
44
+ @db.collection(resource.to_s).find(hsh).to_a
26
45
  end
27
46
 
28
47
  def insert(resource, spot)
29
- @db.collection(resource.to_s).insert(spot.to_hash)
48
+ hsh = spot.to_hash
49
+ hsh["geom"][0] = sin_proj(hsh["geom"])[0] if hsh["geom"]
50
+ @db.collection(resource.to_s).insert(hsh)
51
+ end
52
+
53
+ def count(resource)
54
+ @db.collection(resource).count
55
+ end
56
+
57
+ def find_near(resource, x, y, limit=nil, skip=0)
58
+ coll = @db.collection(resource.to_s).find("geom" => { "$near" => { "x" => x, "y" => y }}).skip(skip)
59
+ coll.limit(limit) if limit
60
+ coll.to_a
61
+ end
62
+
63
+ # +1.3.4
64
+ def find_within(resource, geom, limit=nil)
65
+ op = geom[1].kind_of?(Numeric) ? "$center" : "$box"
66
+ coll = @db.collection(resource.to_s).find("geom" => { "$within" => { op => geom }})
67
+ coll.limit(limit) if limit
68
+ coll.to_a
69
+ end
70
+
71
+ # getNear command returns distance too
72
+ # <1.9 needs OrderedHash
73
+ def near(resource, x, y, limit=nil)
74
+ cmd = OrderedHash.new
75
+ cmd["geoNear"] = resource
76
+ cmd["near"] = sin_proj(x,y)
77
+ cmd["num"] = limit if limit
78
+ @db.command(cmd)["results"].to_a
30
79
  end
31
80
 
32
81
  def purge
@@ -35,5 +84,15 @@ module Geonames
35
84
  end
36
85
  end
37
86
 
87
+ def index_info(resource)
88
+ @db.collection(resource).index_information
89
+ end
90
+
91
+ private
92
+
93
+ def sin_proj(x,y=nil)
94
+ x,y = x unless y
95
+ [x * Math.cos(y * Math::PI/180), y]
96
+ end
38
97
  end
39
98
  end
@@ -83,3 +83,27 @@ module Geonames
83
83
  end
84
84
 
85
85
  end
86
+
87
+ # def self.point(tdb, x, y)
88
+ # qry = TDBQRY::new(tdb)
89
+ # qry.addcond("x", TDBQRY::QCNUMGE, minx.to_s())
90
+ # qry.addcond("x", TDBQRY::QCNUMLE, maxx.to_s())
91
+ # qry.addcond("y", TDBQRY::QCNUMGE, miny.to_s())
92
+ # qry.addcond("y", TDBQRY::QCNUMLE, maxy.to_s())
93
+ # qry.setorder("x", TDBQRY::QONUMASC)
94
+ # qry.setlimit(80)
95
+ # end
96
+
97
+
98
+ # def self.area(tdb, minx, maxx, miny, maxy)
99
+ # qry = TDBQRY::new(tdb)
100
+ # qry.addcond("x", TDBQRY::QCNUMGE, minx.to_s())
101
+ # qry.addcond("x", TDBQRY::QCNUMLE, maxx.to_s())
102
+ # qry.addcond("y", TDBQRY::QCNUMGE, miny.to_s())
103
+ # qry.addcond("y", TDBQRY::QCNUMLE, maxy.to_s())
104
+ # qry.setorder("x", TDBQRY::QONUMASC)
105
+
106
+ # res = qry.search
107
+ # info res.length # number of results found
108
+ # return res
109
+ # end
@@ -0,0 +1,331 @@
1
+ --
2
+ -- PostgreSQL database dump
3
+ --
4
+
5
+ -- Started on 2010-03-19 05:44:38 BRT
6
+
7
+ SET statement_timeout = 0;
8
+ SET client_encoding = 'UTF8';
9
+ SET standard_conforming_strings = off;
10
+ SET check_function_bodies = false;
11
+ SET client_min_messages = warning;
12
+ SET escape_string_warning = off;
13
+
14
+ SET search_path = public, pg_catalog;
15
+
16
+ SET default_tablespace = '';
17
+
18
+ SET default_with_oids = false;
19
+
20
+ --
21
+ -- TOC entry 2245 (class 1259 OID 21665)
22
+ -- Dependencies: 2544 2545 2546 3 992
23
+ -- Name: cities; Type: TABLE; Schema: public; Owner: -; Tablespace:
24
+ --
25
+
26
+ CREATE TABLE cities (
27
+ id integer NOT NULL,
28
+ country_id integer NOT NULL,
29
+ province_id integer,
30
+ name character varying(255) NOT NULL,
31
+ gid integer,
32
+ zip integer,
33
+ geom geometry,
34
+ CONSTRAINT enforce_dims_geom CHECK ((st_ndims(geom) = 2)),
35
+ CONSTRAINT enforce_geotype_geom CHECK (((geometrytype(geom) = 'POINT'::text) OR (geom IS NULL))),
36
+ CONSTRAINT enforce_srid_geom CHECK ((st_srid(geom) = 4326))
37
+ );
38
+
39
+
40
+ --
41
+ -- TOC entry 2244 (class 1259 OID 21663)
42
+ -- Dependencies: 3 2245
43
+ -- Name: cities_id_seq; Type: SEQUENCE; Schema: public; Owner: -
44
+ --
45
+
46
+ CREATE SEQUENCE cities_id_seq
47
+ START WITH 1
48
+ INCREMENT BY 1
49
+ NO MAXVALUE
50
+ NO MINVALUE
51
+ CACHE 1;
52
+
53
+
54
+ --
55
+ -- TOC entry 2557 (class 0 OID 0)
56
+ -- Dependencies: 2244
57
+ -- Name: cities_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
58
+ --
59
+
60
+ ALTER SEQUENCE cities_id_seq OWNED BY cities.id;
61
+
62
+
63
+ --
64
+ -- TOC entry 2543 (class 2604 OID 21668)
65
+ -- Dependencies: 2245 2244 2245
66
+ -- Name: id; Type: DEFAULT; Schema: public; Owner: -
67
+ --
68
+
69
+ ALTER TABLE cities ALTER COLUMN id SET DEFAULT nextval('cities_id_seq'::regclass);
70
+
71
+
72
+ --
73
+ -- TOC entry 2548 (class 2606 OID 21670)
74
+ -- Dependencies: 2245 2245
75
+ -- Name: cities_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace:
76
+ --
77
+
78
+ ALTER TABLE ONLY cities
79
+ ADD CONSTRAINT cities_pkey PRIMARY KEY (id);
80
+
81
+
82
+ --
83
+ -- TOC entry 2549 (class 1259 OID 21697)
84
+ -- Dependencies: 2245
85
+ -- Name: index_cities_on_country_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
86
+ --
87
+
88
+ CREATE INDEX index_cities_on_country_id ON cities USING btree (country_id);
89
+
90
+
91
+ --
92
+ -- TOC entry 2550 (class 1259 OID 21699)
93
+ -- Dependencies: 2245 1869
94
+ -- Name: index_cities_on_geom; Type: INDEX; Schema: public; Owner: -; Tablespace:
95
+ --
96
+
97
+ CREATE INDEX index_cities_on_geom ON cities USING gist (geom);
98
+
99
+
100
+ --
101
+ -- TOC entry 2551 (class 1259 OID 21695)
102
+ -- Dependencies: 2245
103
+ -- Name: index_cities_on_gid; Type: INDEX; Schema: public; Owner: -; Tablespace:
104
+ --
105
+
106
+ CREATE INDEX index_cities_on_gid ON cities USING btree (gid);
107
+
108
+
109
+ --
110
+ -- TOC entry 2552 (class 1259 OID 21694)
111
+ -- Dependencies: 2245
112
+ -- Name: index_cities_on_name; Type: INDEX; Schema: public; Owner: -; Tablespace:
113
+ --
114
+
115
+ CREATE INDEX index_cities_on_name ON cities USING btree (name);
116
+
117
+
118
+ --
119
+ -- TOC entry 2553 (class 1259 OID 21698)
120
+ -- Dependencies: 2245
121
+ -- Name: index_cities_on_province_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
122
+ --
123
+
124
+ CREATE INDEX index_cities_on_province_id ON cities USING btree (province_id);
125
+
126
+
127
+ --
128
+ -- TOC entry 2554 (class 1259 OID 21696)
129
+ -- Dependencies: 2245
130
+ -- Name: index_cities_on_zip; Type: INDEX; Schema: public; Owner: -; Tablespace:
131
+ --
132
+
133
+ CREATE INDEX index_cities_on_zip ON cities USING btree (zip);
134
+
135
+
136
+ --
137
+ -- PostgreSQL database dump
138
+ --
139
+
140
+ -- Started on 2010-03-19 05:45:05 BRT
141
+
142
+ SET statement_timeout = 0;
143
+ SET client_encoding = 'UTF8';
144
+ SET standard_conforming_strings = off;
145
+ SET check_function_bodies = false;
146
+ SET client_min_messages = warning;
147
+ SET escape_string_warning = off;
148
+
149
+ SET search_path = public, pg_catalog;
150
+
151
+ SET default_tablespace = '';
152
+
153
+ SET default_with_oids = false;
154
+
155
+ --
156
+ -- TOC entry 2249 (class 1259 OID 21688)
157
+ -- Dependencies: 3
158
+ -- Name: countries; Type: TABLE; Schema: public; Owner: -; Tablespace:
159
+ --
160
+
161
+ CREATE TABLE countries (
162
+ id integer NOT NULL,
163
+ name character varying(30) NOT NULL,
164
+ abbr character varying(2) NOT NULL
165
+ );
166
+
167
+
168
+ --
169
+ -- TOC entry 2248 (class 1259 OID 21686)
170
+ -- Dependencies: 3 2249
171
+ -- Name: countries_id_seq; Type: SEQUENCE; Schema: public; Owner: -
172
+ --
173
+
174
+ CREATE SEQUENCE countries_id_seq
175
+ START WITH 1
176
+ INCREMENT BY 1
177
+ NO MAXVALUE
178
+ NO MINVALUE
179
+ CACHE 1;
180
+
181
+
182
+ --
183
+ -- TOC entry 2548 (class 0 OID 0)
184
+ -- Dependencies: 2248
185
+ -- Name: countries_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
186
+ --
187
+
188
+ ALTER SEQUENCE countries_id_seq OWNED BY countries.id;
189
+
190
+
191
+ --
192
+ -- TOC entry 2543 (class 2604 OID 21691)
193
+ -- Dependencies: 2249 2248 2249
194
+ -- Name: id; Type: DEFAULT; Schema: public; Owner: -
195
+ --
196
+
197
+ ALTER TABLE countries ALTER COLUMN id SET DEFAULT nextval('countries_id_seq'::regclass);
198
+
199
+
200
+ --
201
+ -- TOC entry 2545 (class 2606 OID 21693)
202
+ -- Dependencies: 2249 2249
203
+ -- Name: countries_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace:
204
+ --
205
+
206
+ ALTER TABLE ONLY countries
207
+ ADD CONSTRAINT countries_pkey PRIMARY KEY (id);
208
+
209
+
210
+ --
211
+ -- PostgreSQL database dump
212
+ --
213
+
214
+ -- Started on 2010-03-19 05:45:19 BRT
215
+
216
+ SET statement_timeout = 0;
217
+ SET client_encoding = 'UTF8';
218
+ SET standard_conforming_strings = off;
219
+ SET check_function_bodies = false;
220
+ SET client_min_messages = warning;
221
+ SET escape_string_warning = off;
222
+
223
+ SET search_path = public, pg_catalog;
224
+
225
+ SET default_tablespace = '';
226
+
227
+ SET default_with_oids = false;
228
+
229
+ --
230
+ -- TOC entry 2247 (class 1259 OID 21680)
231
+ -- Dependencies: 3
232
+ -- Name: provinces; Type: TABLE; Schema: public; Owner: -; Tablespace:
233
+ --
234
+
235
+ CREATE TABLE provinces (
236
+ id integer NOT NULL,
237
+ country_id integer NOT NULL,
238
+ name character varying(255) NOT NULL,
239
+ abbr character varying(2) NOT NULL,
240
+ gid integer
241
+ );
242
+
243
+
244
+ --
245
+ -- TOC entry 2246 (class 1259 OID 21678)
246
+ -- Dependencies: 3 2247
247
+ -- Name: provinces_id_seq; Type: SEQUENCE; Schema: public; Owner: -
248
+ --
249
+
250
+ CREATE SEQUENCE provinces_id_seq
251
+ START WITH 1
252
+ INCREMENT BY 1
253
+ NO MAXVALUE
254
+ NO MINVALUE
255
+ CACHE 1;
256
+
257
+
258
+ --
259
+ -- TOC entry 2552 (class 0 OID 0)
260
+ -- Dependencies: 2246
261
+ -- Name: provinces_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
262
+ --
263
+
264
+ ALTER SEQUENCE provinces_id_seq OWNED BY provinces.id;
265
+
266
+
267
+ --
268
+ -- TOC entry 2543 (class 2604 OID 21683)
269
+ -- Dependencies: 2246 2247 2247
270
+ -- Name: id; Type: DEFAULT; Schema: public; Owner: -
271
+ --
272
+
273
+ ALTER TABLE provinces ALTER COLUMN id SET DEFAULT nextval('provinces_id_seq'::regclass);
274
+
275
+
276
+ --
277
+ -- TOC entry 2549 (class 2606 OID 21685)
278
+ -- Dependencies: 2247 2247
279
+ -- Name: provinces_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace:
280
+ --
281
+
282
+ ALTER TABLE ONLY provinces
283
+ ADD CONSTRAINT provinces_pkey PRIMARY KEY (id);
284
+
285
+
286
+ --
287
+ -- TOC entry 2544 (class 1259 OID 21701)
288
+ -- Dependencies: 2247
289
+ -- Name: index_provinces_on_abbr; Type: INDEX; Schema: public; Owner: -; Tablespace:
290
+ --
291
+
292
+ CREATE INDEX index_provinces_on_abbr ON provinces USING btree (abbr);
293
+
294
+
295
+ --
296
+ -- TOC entry 2545 (class 1259 OID 21703)
297
+ -- Dependencies: 2247
298
+ -- Name: index_provinces_on_country_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
299
+ --
300
+
301
+ CREATE INDEX index_provinces_on_country_id ON provinces USING btree (country_id);
302
+
303
+
304
+ --
305
+ -- TOC entry 2546 (class 1259 OID 21702)
306
+ -- Dependencies: 2247
307
+ -- Name: index_provinces_on_gid; Type: INDEX; Schema: public; Owner: -; Tablespace:
308
+ --
309
+
310
+ CREATE INDEX index_provinces_on_gid ON provinces USING btree (gid);
311
+
312
+
313
+ --
314
+ -- TOC entry 2547 (class 1259 OID 21700)
315
+ -- Dependencies: 2247
316
+ -- Name: index_provinces_on_name; Type: INDEX; Schema: public; Owner: -; Tablespace:
317
+ --
318
+
319
+ CREATE INDEX index_provinces_on_name ON provinces USING btree (name);
320
+
321
+
322
+ -- Completed on 2010-03-19 05:45:19 BRT
323
+
324
+ --
325
+ -- PostgreSQL database dump complete
326
+ --
327
+
328
+
329
+
330
+
331
+
File without changes
File without changes
@@ -1,7 +1,7 @@
1
1
  module Geonames
2
2
  class Spot
3
3
  attr_accessor :gid, :name, :ascii, :lat, :lon, :country, :kind,
4
- :code, :pop, :tz, :geom, :province, :zip, :abbr
4
+ :code, :pop, :tz, :geom, :province, :zip, :abbr, :id
5
5
  alias :x :lon
6
6
  alias :y :lat
7
7
  alias :geoname_id :gid
@@ -11,10 +11,10 @@ module Geonames
11
11
  # = Geonames Spot
12
12
  #
13
13
  # Every geoname type will be parsed as a spot
14
- def initialize(params, k)
14
+ def initialize(params=nil, k=nil)
15
15
  return unless params.instance_of? String
16
16
  k == :zip ? parse_zip(params) : parse(params)
17
- if @kind == :provinces
17
+ if @kind == :province
18
18
  @name.gsub!(/Estado d\w\s/, "")
19
19
  @abbr = get_abbr
20
20
  end
@@ -75,16 +75,49 @@ module Geonames
75
75
  def to_hash
76
76
  { "id" => @geoname_id, "gid" => @geoname_id.to_s, "kind" => @kind.to_s,
77
77
  "name" => @name, "ascii" => @ascii, "country" => @country,
78
- "geom" => {"y" => @lat.to_s, "x" => @lon.to_s}, "tz" => @tz }
78
+ "geom" => [@geom.x, @geom.y], "tz" => @tz }
79
79
  end
80
80
 
81
-
82
81
  def human_code(code)
83
82
  case code
84
- when 'ADM1' then :provinces
85
- when 'ADM2' then :cities
83
+ when 'ADM1' then :province
84
+ when 'ADM2' then :city
86
85
  else :other
87
86
  end
88
87
  end
88
+
89
+ class << self
90
+
91
+ def all
92
+ Adapter.all(@coll)
93
+ end
94
+
95
+ def find(id)
96
+ Adapter.find(@coll, id)
97
+ end
98
+
99
+ def find_by_name(name)
100
+ Adapter.find_by_name(@coll, name).map { |hsh| from_hash(hsh) }
101
+ end
102
+
103
+ def nearest(x,y)
104
+ from_hash(Adapter.find_near(@coll, x, y, 1)[0])
105
+ end
106
+
107
+ def from_hash(hsh)
108
+ spot = self.new
109
+ hsh.each { |key, val| spot.instance_variable_set("@"+key, val) }
110
+ spot.geom = GeoRuby::SimpleFeatures::Point.from_x_y(*spot.geom)
111
+ spot
112
+ end
113
+
114
+ def set_coll(name)
115
+ @coll = name
116
+ end
117
+
118
+ def collection
119
+ @coll
120
+ end
121
+ end
89
122
  end
90
123
  end
@@ -0,0 +1,20 @@
1
+ require 'rubygems'
2
+ require 'logger'
3
+ require 'yaml'
4
+
5
+ require 'geonames_local/features/spot'
6
+ require 'geonames_local/features/road'
7
+ require 'geonames_local/features/zone'
8
+
9
+ module Geonames
10
+ Opt = {}
11
+ Cache = {:dump => [], :zip => [], :roads => [], :zones => []}
12
+ Codes = YAML.load(File.read(File.join(File.dirname(__FILE__), 'config', 'codes.yml')))
13
+ VERSION = File.read(File.join(File.dirname(__FILE__), '..', '..', 'VERSION'))
14
+
15
+ def info(txt)
16
+ if Opt[:verbose]
17
+ puts(txt)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,7 @@
1
+ class City < Geonames::Spot
2
+
3
+ set_coll "cities"
4
+
5
+
6
+
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'geonames_local/adapters/mongodb'
2
+
3
+ module Geonames
4
+
5
+ Adapter = Geonames::Mongodb.new
6
+
7
+ end
File without changes
File without changes
@@ -1,28 +1,13 @@
1
+ #
2
+ # Geonames Local
3
+ #
1
4
  $:.unshift(File.dirname(__FILE__)) unless
2
5
  $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
6
 
4
- require 'rubygems'
5
- require 'logger'
6
- require 'yaml'
7
- require 'geonames_local/features/spot'
8
- require 'geonames_local/features/country'
9
- require 'geonames_local/features/city'
10
- require 'geonames_local/features/road'
11
- require 'geonames_local/features/zone'
12
- require 'geonames_local/data/shp'
7
+ # Require CLI Stuff
8
+ require 'geonames_local/geonames'
13
9
  require 'geonames_local/cli'
14
- require 'geonames_local/dump'
15
- require 'geonames_local/export'
16
-
17
- module Geonames
18
- Opt = {}
19
- Cache = {:dump => [], :zip => [], :roads => [], :zones => []}
20
- Codes = YAML.load(File.read(File.join(File.dirname(__FILE__), 'geonames_local', 'config', 'codes.yml')))
21
- VERSION = File.read(File.join(File.dirname(__FILE__), '..', 'VERSION'))
10
+ require 'geonames_local/data/shp'
11
+ require 'geonames_local/data/dump'
12
+ require 'geonames_local/data/export'
22
13
 
23
- def info(txt)
24
- if Opt[:verbose]
25
- puts(txt)
26
- end
27
- end
28
- end
@@ -0,0 +1,105 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+ require File.expand_path(File.dirname(__FILE__) + '/../../../lib/geonames_local/adapters/mongodb')
3
+
4
+ describe Mongodb do
5
+
6
+ SPECDB = "geonames_spec"
7
+
8
+ before(:all) do
9
+ Mongodb.new({:dbname => SPECDB}).purge
10
+ @mong = Mongodb.new({:dbname => SPECDB})
11
+ end
12
+
13
+ def mock_spot(name)
14
+ Spot.new("1\t#{name}\t#{name}\t\t-5.46874226086957\t-35.3565714695652\tA\tADM2\tBR\t22\t2407500\t6593\t\t12\t\t\t\tAmerica/Recife\t2006-12-17", :dump)
15
+ end
16
+
17
+ describe "Parsing Dump" do
18
+ before do
19
+ @mock_spot = mock("Spot")
20
+ end
21
+
22
+ it "should find all" do
23
+ @mong.all("cities").each { |c| p c["geom"]} #should eql([])
24
+ end
25
+
26
+ it "should store something" do
27
+ @mock_spot.should_receive(:to_hash).and_return({"id" => 7, "name" => "Sao Tome", "geom" => [5,5]})
28
+ @mong.insert("cities", @mock_spot)
29
+ @mong.count("cities").should eql(1)
30
+ end
31
+
32
+ it "should store a spot" do
33
+ @mong.insert("cities", mock_spot("Loco"))
34
+ @mong.find("cities", 1)["name"].should eql("Loco")
35
+ end
36
+
37
+ it "should store geom with sinusoidal projection" do
38
+ @mock_spot.should_receive(:to_hash).and_return({"id" => 8, "name" => "Sao Tome", "geom" => [5,8]})
39
+ @mong.insert("cities", @mock_spot)
40
+ @mong.find("cities", 8)["geom"][0].should be_close(4.95, 0.1)
41
+ @mong.find("cities", 8)["geom"][1].should eql(8)
42
+ end
43
+
44
+ it "should have some indexes" do
45
+ @mong.index_info("cities").to_a.length.should eql(3)
46
+ end
47
+
48
+ describe "Finds" do
49
+
50
+ before(:all) do
51
+ @mong.insert("cities", {"id" => 9, "name" => "Sao Paulo", "geom" => [15,15]})
52
+ @mong.insert("cities", {"id" => 10, "name" => "Sao Tome", "geom" => [-7,-34]})
53
+ @mong.insert("cities", {"id" => 11, "name" => "Sao Benedito", "geom" => [-9,-39]})
54
+ end
55
+
56
+ it "should make sure it's on the collection" do
57
+ @mong.count("cities").should eql(3)
58
+ end
59
+
60
+ it "should find geo" do
61
+ @mong.find_near("cities", -5, -35).first["name"].should eql("Sao Tome")
62
+ @mong.find_near("cities", -5, -35).first["geom"][0].should be_close(-5.80, 0.1)
63
+ @mong.find_near("cities", -5, -35).first["geom"][1].should eql(-34)
64
+ end
65
+
66
+ it "should find geo limited" do
67
+ @mong.find_near("cities", -5, -35, 1).length.should eql(1)
68
+ end
69
+
70
+ it "should find within box" do
71
+ @mong.find_within("cities", [[10, 10],[20, 20]]).length.should eql(1)
72
+ @mong.find_within("cities", [[10, 10],[20, 20]]).first["name"].should eql("Sao Paulo")
73
+ end
74
+
75
+ it "should find within radius" do
76
+ @mong.find_within("cities", [[-6, -36], 2]).length.should eql(1)
77
+ end
78
+
79
+ it "should find within wider radius" do
80
+ @mong.find_within("cities", [[-6, -36], 5]).length.should eql(2)
81
+ end
82
+
83
+ it "should find within wider radius limited" do
84
+ @mong.find_within("cities", [[-6, -36], 5], 1).length.should eql(1)
85
+ end
86
+
87
+ it "should find geoNear" do
88
+ @mong.near("cities", -5, -35).first["dis"].should be_close(1.97, 0.01)
89
+ @mong.near("cities", -5, -35).first["obj"]["name"].should eql("Sao Tome")
90
+ end
91
+
92
+ it "should find geoNear" do
93
+ @mong.near("cities", -5, -35).first["dis"].should be_close(1.97, 0.01)
94
+ @mong.near("cities", -5, -35).first["obj"]["name"].should eql("Sao Tome")
95
+ end
96
+
97
+ it "should find geoNear limited" do
98
+ @mong.near("cities", -5, -35, 1).length.should eql(1)
99
+ end
100
+
101
+ end
102
+
103
+ end
104
+
105
+ end
@@ -1,4 +1,5 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+ require File.expand_path(File.dirname(__FILE__) + '/../../../lib/geonames_local/adapters/postgres')
2
3
 
3
4
  describe Postgres do
4
5
 
@@ -10,6 +10,7 @@ describe SHP do
10
10
  Opt[:type] = "road"
11
11
 
12
12
  it "should merge two records linestrings" do
13
+ pending
13
14
  @s = SHP.new(nil)
14
15
  r = @s.reduce!
15
16
  r.length.should eql(1)
@@ -35,7 +35,7 @@ describe Spot do
35
35
  end
36
36
 
37
37
  it "should parse spot kind" do
38
- @spot.kind.should eql(:cities)
38
+ @spot.kind.should eql(:city)
39
39
  end
40
40
 
41
41
  it "should parse spot country" do
@@ -97,7 +97,7 @@ describe Spot do
97
97
  end
98
98
 
99
99
  it "should be kind of province" do
100
- @spot.kind.should eql(:provinces)
100
+ @spot.kind.should eql(:province)
101
101
  end
102
102
 
103
103
  it "should parse geoid" do
@@ -132,7 +132,7 @@ describe Spot do
132
132
  describe "Parsing Zip" do
133
133
 
134
134
  before do
135
- @spot = Geonames::Spot.new("BR\t76375-000\tHidrolina\tGoias\t29\t\t5209804\t\t-14.7574\t-49.3596\t\n", :zip)
135
+ @spot = Geonames::Spot.new("BR\t76375-000\tHidrolina\tGoias\t29\t\t5209804\t\t-14.7574\t-49.3596\t\n", :zip)
136
136
  end
137
137
 
138
138
  it "should parse zip oO" do
@@ -163,6 +163,39 @@ describe Spot do
163
163
 
164
164
  end
165
165
 
166
+ describe "From Hash" do
167
+
168
+ before do
169
+ @spot = Spot.from_hash({"id" => 9, "name" => "Sao Rock", "geom" => [15,15], "kind" => "cities", "country" => "BR", "gid" => 13232, "tz" => "America/Foo", "ascii" => "Rock"})
170
+ end
171
+
172
+ it "should be an spot" do
173
+ @spot.should be_instance_of Spot
174
+ end
175
+
176
+ it "should set the name" do
177
+ @spot.name.should eql("Sao Rock")
178
+ end
179
+
180
+ it "should set the geom" do
181
+ @spot.geom.should be_instance_of(GeoRuby::SimpleFeatures::Point)
182
+ @spot.geom.x.should eql(15)
183
+ end
184
+
185
+ it "should set the tz" do
186
+ @spot.tz.should eql("America/Foo")
187
+ end
188
+
189
+ it "should set the ascii" do
190
+ @spot.ascii.should eql("Rock")
191
+ end
192
+
193
+ it "should set the country abbr" do
194
+ @spot.country.should eql("BR")
195
+ end
196
+
197
+ end
198
+
166
199
  end
167
200
 
168
201
  # 6319037 Maxaranguape Maxaranguape -5.46874226086957 -35.3565714695652 A ADM2 BR 22 2407500 6593 12 America/Recife 2006-12-17
@@ -189,3 +222,4 @@ end
189
222
  # "3165361\tToscana\tToscana\tTaskana,Toscan-a,Toscana,Toscane,Toscann-a,Toskana,Toskania,Toskanio,Toskansko,Toskánsko,Toskāna,Toszkana,Toszkána,Tuscany,Tuschena,Tuschèna,Tuscia,toseukana ju,tosukana zhou,tuo si ka na,twsqnh,Таскана,Тоскана,טוסקנה,تسکانہ,ტოსკანა,トスカーナ州,托斯卡纳,토스카나 주\t43.4166667\t11.0\tA\tADM1\tIT\t\t16\t\t\t\t3718210\t\t249\tEurope/Rome\t2010-01-17\n"
190
223
  # "3169778\tRegione Puglia\tRegione Puglia\tApulia,Apulie,Apulien,Apulië,Pouilles,Puglia\t41.25\t16.25\tA\tADM1\tIT\t\t13\t\t\t\t4021957\t\t95\tEurope/Rome\t2009-03-11\n"
191
224
  # "3170831\tRegione Piemonte\tRegione Piemonte\tPedemons,Pedemontium,Piamonte,Piedmont,Piemont,Piemonte,Piémont,Piëmont,Regione Piemonte\t45.0\t8.0\tA\tADM1\tIT\t\t12\t\t\t\t4294081\t\t185\tEurope/Rome\t2008-08-18\n"
225
+
@@ -0,0 +1,35 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_ext_helper')
3
+ require "geo_ruby"
4
+
5
+ describe City do
6
+
7
+ before(:all) do
8
+ Adapter.insert("cities", {"id" => 9, "name" => "Sao Paulo", "geom" => [15,15]})
9
+ Adapter.insert("cities", {"id" => 66, "name" => "Rock City", "geom" => [2,3]})
10
+ end
11
+
12
+ after(:all) do
13
+ Mongodb.new({:dbname => "geonames"}).purge
14
+ end
15
+
16
+ it "should set a collection name" do
17
+ City.collection.should eql("cities")
18
+ end
19
+
20
+ it "should find all cities" do
21
+ City.all.should_not be_empty
22
+ end
23
+
24
+ it "should be a city instance" do
25
+ City.nearest(1,1).should be_instance_of(City)
26
+ end
27
+
28
+ it "should find city nearest point" do
29
+ City.nearest(1,1).name.should eql("Rock City")
30
+ end
31
+
32
+ it "should find by name" do
33
+ City.find_by_name("Rock")[0].name.should eql("Rock City")
34
+ end
35
+ end
@@ -0,0 +1,5 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'geonames_ext'
4
+ require 'spec'
5
+ require 'spec/autorun'
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 2
8
- - 1
9
- version: 0.2.1
7
+ - 3
8
+ - 0
9
+ version: 0.3.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Marcos Piccinini
@@ -14,11 +14,11 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-19 00:00:00 -03:00
17
+ date: 2010-03-20 00:00:00 -03:00
18
18
  default_executable: geonames
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
- name: rspec
21
+ name: nofxx-georuby
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
24
  requirements:
@@ -26,13 +26,13 @@ dependencies:
26
26
  - !ruby/object:Gem::Version
27
27
  segments:
28
28
  - 1
29
- - 2
30
- - 9
31
- version: 1.2.9
32
- type: :development
29
+ - 7
30
+ - 1
31
+ version: 1.7.1
32
+ type: :runtime
33
33
  version_requirements: *id001
34
34
  - !ruby/object:Gem::Dependency
35
- name: nofxx-georuby
35
+ name: rspec
36
36
  prerelease: false
37
37
  requirement: &id002 !ruby/object:Gem::Requirement
38
38
  requirements:
@@ -40,10 +40,10 @@ dependencies:
40
40
  - !ruby/object:Gem::Version
41
41
  segments:
42
42
  - 1
43
- - 7
44
- - 1
45
- version: 1.7.1
46
- type: :runtime
43
+ - 2
44
+ - 9
45
+ version: 1.2.9
46
+ type: :development
47
47
  version_requirements: *id002
48
48
  description: Dump and feed a tokyo cabinet for local geonames search
49
49
  email: x@nofxx.com
@@ -62,35 +62,41 @@ files:
62
62
  - Rakefile
63
63
  - VERSION
64
64
  - bin/geonames
65
+ - geonames.yml
65
66
  - geonames_local.gemspec
67
+ - lib/geonames_ext.rb
66
68
  - lib/geonames_local.rb
67
69
  - lib/geonames_local/adapters/mongodb.rb
68
70
  - lib/geonames_local/adapters/postgres.rb
69
71
  - lib/geonames_local/adapters/tokyo.rb
70
72
  - lib/geonames_local/cli.rb
71
73
  - lib/geonames_local/config/codes.yml
74
+ - lib/geonames_local/config/geonames.sql
72
75
  - lib/geonames_local/config/geonames.yml
73
- - lib/geonames_local/data/cache.rb
74
- - lib/geonames_local/data/geoquery.rb
76
+ - lib/geonames_local/data/dump.rb
77
+ - lib/geonames_local/data/export.rb
75
78
  - lib/geonames_local/data/shp.rb
76
- - lib/geonames_local/dump.rb
77
- - lib/geonames_local/export.rb
78
- - lib/geonames_local/features/city.rb
79
- - lib/geonames_local/features/country.rb
80
- - lib/geonames_local/features/province.rb
81
79
  - lib/geonames_local/features/road.rb
82
80
  - lib/geonames_local/features/spot.rb
83
81
  - lib/geonames_local/features/zone.rb
82
+ - lib/geonames_local/geonames.rb
83
+ - lib/geonames_local/model/city.rb
84
+ - lib/geonames_local/model/conn.rb
85
+ - lib/geonames_local/model/country.rb
86
+ - lib/geonames_local/model/province.rb
87
+ - spec/geonames_local/adapters/mongodb_spec.rb
88
+ - spec/geonames_local/adapters/postgres_spec.rb
89
+ - spec/geonames_local/adapters/tokyo_spec.rb
84
90
  - spec/geonames_local/cli_spec.rb
85
91
  - spec/geonames_local/data/cache_spec.rb
86
- - spec/geonames_local/data/postgres_spec.rb
87
92
  - spec/geonames_local/data/shp_spec.rb
88
- - spec/geonames_local/data/tokyo_spec.rb
89
93
  - spec/geonames_local/features/road_spec.rb
90
94
  - spec/geonames_local/features/spot_spec.rb
91
95
  - spec/geonames_local/features/zone_spec.rb
96
+ - spec/geonames_local/model/city_spec.rb
92
97
  - spec/geonames_local_spec.rb
93
98
  - spec/spec.opts
99
+ - spec/spec_ext_helper.rb
94
100
  - spec/spec_helper.rb
95
101
  - task/benchmark.rb
96
102
  - task/benchmark_cabinet.rb
@@ -150,11 +156,14 @@ summary: Dump and feed a tokyo local geonames db
150
156
  test_files:
151
157
  - spec/spec_helper.rb
152
158
  - spec/geonames_local/data/shp_spec.rb
153
- - spec/geonames_local/data/postgres_spec.rb
154
- - spec/geonames_local/data/tokyo_spec.rb
155
159
  - spec/geonames_local/data/cache_spec.rb
160
+ - spec/geonames_local/model/city_spec.rb
156
161
  - spec/geonames_local/cli_spec.rb
162
+ - spec/geonames_local/adapters/postgres_spec.rb
163
+ - spec/geonames_local/adapters/tokyo_spec.rb
164
+ - spec/geonames_local/adapters/mongodb_spec.rb
157
165
  - spec/geonames_local/features/spot_spec.rb
158
166
  - spec/geonames_local/features/zone_spec.rb
159
167
  - spec/geonames_local/features/road_spec.rb
160
168
  - spec/geonames_local_spec.rb
169
+ - spec/spec_ext_helper.rb
@@ -1,5 +0,0 @@
1
- # module Geonames
2
- # class Cache
3
-
4
- # end
5
- # end
@@ -1,31 +0,0 @@
1
- module Geonames
2
- class Geoquery
3
- R = 1
4
-
5
- def self.point(tdb, x, y)
6
- qry = TDBQRY::new(tdb)
7
- qry.addcond("x", TDBQRY::QCNUMGE, minx.to_s())
8
- qry.addcond("x", TDBQRY::QCNUMLE, maxx.to_s())
9
- qry.addcond("y", TDBQRY::QCNUMGE, miny.to_s())
10
- qry.addcond("y", TDBQRY::QCNUMLE, maxy.to_s())
11
- qry.setorder("x", TDBQRY::QONUMASC)
12
- qry.setlimit(80)
13
- end
14
-
15
-
16
- def self.area(tdb, minx, maxx, miny, maxy)
17
- qry = TDBQRY::new(tdb)
18
- qry.addcond("x", TDBQRY::QCNUMGE, minx.to_s())
19
- qry.addcond("x", TDBQRY::QCNUMLE, maxx.to_s())
20
- qry.addcond("y", TDBQRY::QCNUMGE, miny.to_s())
21
- qry.addcond("y", TDBQRY::QCNUMLE, maxy.to_s())
22
- qry.setorder("x", TDBQRY::QONUMASC)
23
-
24
- res = qry.search
25
- info res.length # number of results found
26
- return res
27
- end
28
-
29
-
30
- end
31
- end
@@ -1,9 +0,0 @@
1
- class City
2
- attr_accessor :country, :province, :name
3
-
4
- def self.all
5
- qry.addcond(QCSTREQ, 'city')
6
- end
7
-
8
-
9
- end