geonames 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Binary file
File without changes
@@ -3,11 +3,11 @@ require 'date'
3
3
 
4
4
  SPEC = Gem::Specification.new do |s|
5
5
  s.name = "geonames"
6
- s.version = "0.1.0"
6
+ s.version = "0.2.0"
7
7
  s.author = "Adam Wisniewski"
8
8
  s.email = "adamw@tbcn.ca"
9
9
  s.date = s.date = Date.today.to_s
10
- s.homepage = "http://www.tbcn.ca/geonames"
10
+ s.homepage = "http://www.tbcn.ca/ruby_geonames"
11
11
  s.platform = Gem::Platform::RUBY
12
12
  s.summary = "Ruby library for Geonames Web Services (http://www.geonames.org/export/)"
13
13
  candidates = Dir.glob("{bin,docs,lib,test}/**/*")
@@ -28,6 +28,7 @@ require 'postal_code'
28
28
  require 'postal_code_search_criteria'
29
29
  require 'timezone'
30
30
  require 'country_subdivision'
31
+ require 'wikipedia_article'
31
32
 
32
33
  module Geonames
33
34
 
@@ -17,7 +17,17 @@
17
17
  #=============================================================================
18
18
 
19
19
  require 'geonames'
20
+ require 'pp'
20
21
 
22
+ # get wikipedia articles by lat / long
23
+ # articles_nearby = Geonames::WebService.findNearbyWikipedia :lat => 43.900120387, :long => -78.882869834
24
+ # p articles_nearby
25
+
26
+ # get wikipedia articles by bounding box
27
+ articles_nearby = Geonames::WebService.findBoundingBoxWikipedia :north => 43.900120387, :east => -78.882869834, :south => 43.82, :west => 79.0
28
+ p articles_nearby
29
+
30
+ Process.exit
21
31
  # get list of places near by longitude/longitude location
22
32
  places_nearby = Geonames::WebService.find_nearby_place_name 43.900120387, -78.882869834
23
33
  p places_nearby
@@ -1,12 +1,12 @@
1
1
  #=============================================================================
2
2
  #
3
- # Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
3
+ # Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
4
4
  #
5
5
  # Licensed under the Apache License, Version 2.0 (the "License"); you may not
6
6
  # use this file except in compliance with the License. You may obtain a copy of
7
- # the License at
7
+ # the License at
8
8
  #
9
- # http://www.apache.org/licenses/LICENSE-2.0
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
10
  #
11
11
  # Unless required by applicable law or agreed to in writing, software
12
12
  # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
@@ -18,313 +18,417 @@
18
18
 
19
19
 
20
20
  module Geonames
21
- class WebService
22
- def WebService.get_element_child_text( element, child )
23
- if !element.elements[child].nil?
24
- element.elements[child][0].to_s
25
- end
26
- end
27
-
28
- def WebService.get_element_child_float( element, child )
29
- if !element.elements[child].nil?
30
- element.elements[child][0].to_s.to_f
31
- end
32
- end
21
+ class WebService
22
+ def WebService.get_element_child_text( element, child )
23
+ if !element.elements[child].nil?
24
+ element.elements[child][0].to_s
25
+ end
26
+ end
33
27
 
34
- def WebService.get_element_child_int( element, child )
35
- if !element.elements[child].nil?
36
- element.elements[child][0].to_s.to_i
37
- end
38
- end
39
-
40
- def WebService.element_to_postal_code ( element )
41
- postal_code = PostalCode.new
42
-
43
- postal_code.admin_code_1 = WebService::get_element_child_text( element, 'adminCode1' )
44
- postal_code.admin_code_2 = WebService::get_element_child_text( element, 'adminCode2' )
45
- postal_code.admin_name_1 = WebService::get_element_child_text( element, 'adminName1' )
46
- postal_code.admin_name_2 = WebService::get_element_child_text( element, 'adminName2' )
47
- postal_code.country_code = WebService::get_element_child_text( element, 'countryCode' )
48
- postal_code.distance = WebService::get_element_child_float( element, 'distance' )
49
- postal_code.longitude = WebService::get_element_child_float( element, 'lat' )
50
- postal_code.latitude = WebService::get_element_child_float( element, 'lng' )
51
- postal_code.place_name = WebService::get_element_child_text( element, 'name' )
52
- postal_code.postal_code = WebService::get_element_child_text( element, 'postalcode' )
53
-
54
- return postal_code
28
+ def WebService.get_element_child_float( element, child )
29
+ if !element.elements[child].nil?
30
+ element.elements[child][0].to_s.to_f
31
+ end
32
+ end
55
33
 
56
- end
34
+ def WebService.get_element_child_int( element, child )
35
+ if !element.elements[child].nil?
36
+ element.elements[child][0].to_s.to_i
37
+ end
38
+ end
57
39
 
58
- def WebService.element_to_toponym ( element )
59
- toponym = Toponym.new
60
-
61
- toponym.name = WebService::get_element_child_text( element, 'name' )
62
- toponym.alternate_names = WebService::get_element_child_text( element, 'alternateNames' )
63
- toponym.latitude = WebService::get_element_child_float( element, 'lat' )
64
- toponym.longitude = WebService::get_element_child_float( element, 'lng' )
65
- toponym.geoname_id = WebService::get_element_child_text( element, 'geonameId' )
66
- toponym.country_code = WebService::get_element_child_text( element, 'countryCode' )
67
- toponym.country_name = WebService::get_element_child_text( element, 'countryName' )
68
- toponym.feature_class = WebService::get_element_child_text( element, 'fcl' )
69
- toponym.feature_code = WebService::get_element_child_text( element, 'fcode' )
70
- toponym.feature_class_name = WebService::get_element_child_text( element, 'fclName' )
71
- toponym.feature_code_name = WebService::get_element_child_text( element, 'fCodeName' )
72
- toponym.population = WebService::get_element_child_int( element, 'population' )
73
- toponym.elevation = WebService::get_element_child_text( element, 'elevation' )
74
- toponym.distance = WebService::get_element_child_float( element, 'distance' )
75
-
76
- return toponym
40
+ def WebService.element_to_postal_code ( element )
41
+ postal_code = PostalCode.new
77
42
 
78
- end
79
-
80
- def WebService.postal_code_search( postal_code, place_name, country_code )
81
- postal_code_sc = PostalCodeSearchCriteria.new
82
- postal_code_sc.postal_code = postal_code
83
- postal_code_sc.place_name = place_name
84
- postal_code_sc.coutry_code = country_code
85
-
86
- WebService.postal_code_search postal_code_sc
87
- end
88
-
89
- def WebService.postal_code_search( search_criteria )
90
- # postal codes to reutrn
91
- postal_codes = Array.new
92
-
93
- url = Geonames::GEONAMES_SERVER + "/postalCodeSearch?a=a"
94
- url = url + search_criteria.to_query_params_string
95
-
96
- uri = URI.parse(url)
97
-
98
- req = Net::HTTP::Get.new(uri.path + '?' + uri.query)
99
-
100
- res = Net::HTTP.start( uri.host, uri.port ) { |http|
101
- http.request( req )
102
- }
103
-
104
- doc = REXML::Document.new res.body
105
-
106
- doc.elements.each("geonames/code") do |element|
107
- postal_codes << WebService::element_to_postal_code( element )
108
- end
109
-
110
- postal_codes
111
-
112
- end
43
+ postal_code.admin_code_1 = WebService::get_element_child_text( element, 'adminCode1' )
44
+ postal_code.admin_code_2 = WebService::get_element_child_text( element, 'adminCode2' )
45
+ postal_code.admin_name_1 = WebService::get_element_child_text( element, 'adminName1' )
46
+ postal_code.admin_name_2 = WebService::get_element_child_text( element, 'adminName2' )
47
+ postal_code.country_code = WebService::get_element_child_text( element, 'countryCode' )
48
+ postal_code.distance = WebService::get_element_child_float( element, 'distance' )
49
+ postal_code.longitude = WebService::get_element_child_float( element, 'lat' )
50
+ postal_code.latitude = WebService::get_element_child_float( element, 'lng' )
51
+ postal_code.place_name = WebService::get_element_child_text( element, 'name' )
52
+ postal_code.postal_code = WebService::get_element_child_text( element, 'postalcode' )
113
53
 
114
- def WebService.find_nearby_postal_codes( search_criteria )
115
- # postal codes to reutrn
116
- postal_codes = Array.new
117
-
118
- url = Geonames::GEONAMES_SERVER + "/findNearbyPostalCodes?a=a"
119
- url = url + search_criteria.to_query_params_string
120
-
121
- uri = URI.parse(url)
122
-
123
- req = Net::HTTP::Get.new(uri.path + '?' + uri.query)
124
-
125
- res = Net::HTTP.start( uri.host, uri.port ) { |http|
126
- http.request( req )
127
- }
128
-
129
- doc = REXML::Document.new res.body
130
-
131
- doc.elements.each("geonames/code") do |element|
132
- postal_codes << WebService::element_to_postal_code( element )
133
- end
134
-
135
- postal_codes
136
-
137
- end
138
-
139
- def WebService.find_nearby_place_name( lat, long )
140
- places = Array.new
141
-
142
- url = Geonames::GEONAMES_SERVER + "/findNearbyPlaceName?a=a"
143
-
144
- url = url + "&lat=" + lat.to_s
145
- url = url + "&lng=" + long.to_s
54
+ return postal_code
146
55
 
147
- uri = URI.parse(url)
56
+ end
148
57
 
149
- req = Net::HTTP::Get.new(uri.path + '?' + uri.query)
58
+ def WebService.element_to_wikipedia_article ( element )
59
+ article = WikipediaArticle.new
60
+
61
+ article.language = WebService::get_element_child_text( element, 'lang' )
62
+ article.title = WebService::get_element_child_text( element, 'title' )
63
+ article.summary = WebService::get_element_child_text( element, 'summary' )
64
+ article.wikipedia_url = WebService::get_element_child_text( element, 'wikipediaUrl' )
65
+ article.feature = WebService::get_element_child_text( element, 'feature' )
66
+ article.population = WebService::get_element_child_text( element, 'population' )
67
+ article.elevation = WebService::get_element_child_text( element, 'elevation' )
68
+ article.latitude = WebService::get_element_child_float( element, 'lat' )
69
+ article.longitude = WebService::get_element_child_float( element, 'lng' )
70
+ article.thumbnail_img = WebService::get_element_child_text( element, 'thumbnailImg' )
71
+ article.distance = WebService::get_element_child_float( element, 'distance' )
72
+
73
+ return article
150
74
 
151
- res = Net::HTTP.start( uri.host, uri.port ) { |http|
152
- http.request( req )
153
- }
75
+ end
76
+
77
+ def WebService.element_to_toponym ( element )
78
+ toponym = Toponym.new
79
+
80
+ toponym.name = WebService::get_element_child_text( element, 'name' )
81
+ toponym.alternate_names = WebService::get_element_child_text( element, 'alternateNames' )
82
+ toponym.latitude = WebService::get_element_child_float( element, 'lat' )
83
+ toponym.longitude = WebService::get_element_child_float( element, 'lng' )
84
+ toponym.geoname_id = WebService::get_element_child_text( element, 'geonameId' )
85
+ toponym.country_code = WebService::get_element_child_text( element, 'countryCode' )
86
+ toponym.country_name = WebService::get_element_child_text( element, 'countryName' )
87
+ toponym.feature_class = WebService::get_element_child_text( element, 'fcl' )
88
+ toponym.feature_code = WebService::get_element_child_text( element, 'fcode' )
89
+ toponym.feature_class_name = WebService::get_element_child_text( element, 'fclName' )
90
+ toponym.feature_code_name = WebService::get_element_child_text( element, 'fCodeName' )
91
+ toponym.population = WebService::get_element_child_int( element, 'population' )
92
+ toponym.elevation = WebService::get_element_child_text( element, 'elevation' )
93
+ toponym.distance = WebService::get_element_child_float( element, 'distance' )
94
+
95
+ return toponym
154
96
 
155
- doc = REXML::Document.new res.body
156
-
157
- doc.elements.each("geonames/geoname") do |element|
97
+ end
158
98
 
159
- places << WebService::element_to_toponym( element )
160
-
161
- end
99
+ def WebService.postal_code_search( postal_code, place_name, country_code )
100
+ postal_code_sc = PostalCodeSearchCriteria.new
101
+ postal_code_sc.postal_code = postal_code
102
+ postal_code_sc.place_name = place_name
103
+ postal_code_sc.coutry_code = country_code
162
104
 
163
- return places
164
-
165
- end
166
-
167
- def WebService.timezone( lat, long )
168
- timezone = Timezone.new
169
-
170
- url = Geonames::GEONAMES_SERVER + "/timezone?a=a"
171
-
172
- url = url + "&lat=" + lat.to_s
173
- url = url + "&lng=" + long.to_s
174
-
175
- uri = URI.parse(url)
176
-
177
- req = Net::HTTP::Get.new(uri.path + '?' + uri.query)
178
-
179
- res = Net::HTTP.start( uri.host, uri.port ) { |http|
180
- http.request( req )
181
- }
182
-
183
- doc = REXML::Document.new res.body
184
-
185
- doc.elements.each("geonames/timezone") do |element|
186
- timezone.timezone_id = WebService::get_element_child_text( element, 'timezoneId' )
187
- timezone.gmt_offset = WebService::get_element_child_float( element, 'gmtOffset' )
188
- timezone.dst_offset = WebService::get_element_child_float( element, 'dstOffset' )
189
- end
190
-
191
- return timezone
192
-
193
- end
105
+ WebService.postal_code_search postal_code_sc
106
+ end
194
107
 
195
- def WebService.country_subdivision ( lat, long )
196
- country_subdivision = CountrySubdivision.new
197
-
198
- url = Geonames::GEONAMES_SERVER + "/countrySubdivision?a=a"
199
-
200
- url = url + "&lat=" + lat.to_s
201
- url = url + "&lng=" + long.to_s
108
+ def WebService.postal_code_search( search_criteria )
109
+ # postal codes to reutrn
110
+ postal_codes = Array.new
202
111
 
203
- uri = URI.parse(url)
112
+ url = Geonames::GEONAMES_SERVER + "/postalCodeSearch?a=a"
113
+ url = url + search_criteria.to_query_params_string
204
114
 
205
- req = Net::HTTP::Get.new(uri.path + '?' + uri.query)
115
+ uri = URI.parse(url)
206
116
 
207
- res = Net::HTTP.start( uri.host, uri.port ) { |http|
208
- http.request( req )
209
- }
117
+ req = Net::HTTP::Get.new(uri.path + '?' + uri.query)
210
118
 
211
- doc = REXML::Document.new res.body
119
+ res = Net::HTTP.start( uri.host, uri.port ) { |http|
120
+ http.request( req )
121
+ }
212
122
 
213
- doc.elements.each("geonames/countrySubdivision") do |element|
214
- country_subdivision.country_code = WebService::get_element_child_text( element, 'countryCode' )
215
- country_subdivision.country_name = WebService::get_element_child_text( element, 'countryName' )
216
- country_subdivision.admin_code_1 = WebService::get_element_child_text( element, 'adminCode1' )
217
- country_subdivision.admin_name_1 = WebService::get_element_child_text( element, 'adminName1' )
218
- end
123
+ doc = REXML::Document.new res.body
219
124
 
220
- return country_subdivision
221
-
222
- end
125
+ doc.elements.each("geonames/code") do |element|
126
+ postal_codes << WebService::element_to_postal_code( element )
127
+ end
128
+
129
+ postal_codes
223
130
 
224
- def WebService.country_code ( lat, long )
225
-
226
- url = Geonames::GEONAMES_SERVER + "/countrycode?a=a"
227
-
228
- url = url + "&lat=" + lat.to_s
229
- url = url + "&lng=" + long.to_s
131
+ end
230
132
 
231
- uri = URI.parse(url)
133
+ def WebService.find_nearby_postal_codes( search_criteria )
134
+ # postal codes to reutrn
135
+ postal_codes = Array.new
232
136
 
233
- req = Net::HTTP::Get.new(uri.path + '?' + uri.query)
137
+ url = Geonames::GEONAMES_SERVER + "/findNearbyPostalCodes?a=a"
138
+ url = url + search_criteria.to_query_params_string
234
139
 
235
- res = Net::HTTP.start( uri.host, uri.port ) { |http|
236
- http.request( req )
237
- }
140
+ uri = URI.parse(url)
238
141
 
239
- doc = REXML::Document.new res.body
142
+ req = Net::HTTP::Get.new(uri.path + '?' + uri.query)
240
143
 
241
- return res.body.strip
242
-
243
- end
244
-
245
- def WebService.search( search_criteria )
246
- #toponym search results to return
247
- toponym_sr = ToponymSearchResult.new
248
-
249
- url = Geonames::GEONAMES_SERVER + "/search?a=a"
250
-
251
- if !search_criteria.q.nil?
252
- url = url + "&q=" + CGI::escape( search_criteria.q )
253
- end
254
-
255
- if !search_criteria.name_equals.nil?
256
- url = url + "&name_equals=" + CGI::escape( search_criteria.name_equals )
257
- end
258
-
259
- if !search_criteria.name_starts_with.nil?
260
- url = url + "&name_startsWith=" + CGI::escape( search_criteria.name_starts_with )
261
- end
262
-
263
- if !search_criteria.name.nil?
264
- url = url + "&name=" + CGI::escape( search_criteria.name )
265
- end
266
-
267
- if !search_criteria.tag.nil?
268
- url = url + "&tag=" + CGI::escape( search_criteria.tag )
269
- end
270
-
271
- if !search_criteria.country_code.nil?
272
- url = url + "&country=" + CGI::escape( search_criteria.country_code )
273
- end
274
-
275
- if !search_criteria.admin_code_1.nil?
276
- url = url + "&adminCode1=" + CGI::escape( search_criteria.admin_code_1 )
277
- end
278
-
279
- if !search_criteria.language.nil?
280
- url = url + "&lang=" + CGI::escape( search_criteria.language )
281
- end
282
-
283
- if !search_criteria.feature_class.nil?
284
- url = url + "&featureClass=" + CGI::escape( search_criteria.feature_class )
285
- end
286
-
287
- if !search_criteria.feature_codes.nil?
288
- for feature_code in search_criteria.feature_codes
289
- url = url + "&fcode=" + CGI::escape( feature_code )
290
- end
291
- end
292
-
293
- if !search_criteria.max_rows.nil?
294
- url = url + "&maxRows=" + CGI::escape( search_criteria.max_rows )
295
- end
296
-
297
- if !search_criteria.start_row.nil?
298
- url = url + "&startRow=" + CGI::escape( search_criteria.start_row )
299
- end
300
-
301
- if !search_criteria.style.nil?
302
- url = url + "&style=" + CGI::escape( search_criteria.style )
303
- end
304
-
305
- uri = URI.parse(url)
306
-
307
- req = Net::HTTP::Get.new(uri.path + '?' + uri.query)
308
-
309
- res = Net::HTTP.start( uri.host, uri.port ) { |http|
310
- http.request( req )
311
- }
312
-
313
- doc = REXML::Document.new res.body
314
-
315
- toponym_sr.total_results_count = doc.elements["geonames/totalResultsCount"].text
316
-
317
- doc.elements.each("geonames/geoname") do |element|
318
-
319
- toponym_sr.toponyms << WebService::element_to_toponym( element )
320
-
321
- end
322
-
323
- return toponym_sr
324
- end
144
+ res = Net::HTTP.start( uri.host, uri.port ) { |http|
145
+ http.request( req )
146
+ }
147
+
148
+ doc = REXML::Document.new res.body
149
+
150
+ doc.elements.each("geonames/code") do |element|
151
+ postal_codes << WebService::element_to_postal_code( element )
152
+ end
153
+
154
+ postal_codes
155
+
156
+ end
157
+
158
+ def WebService.find_nearby_place_name( lat, long )
159
+ places = Array.new
160
+
161
+ url = Geonames::GEONAMES_SERVER + "/findNearbyPlaceName?a=a"
162
+
163
+ url = url + "&lat=" + lat.to_s
164
+ url = url + "&lng=" + long.to_s
165
+
166
+ uri = URI.parse(url)
167
+
168
+ req = Net::HTTP::Get.new(uri.path + '?' + uri.query)
169
+
170
+ res = Net::HTTP.start( uri.host, uri.port ) { |http|
171
+ http.request( req )
172
+ }
173
+
174
+ doc = REXML::Document.new res.body
175
+
176
+ doc.elements.each("geonames/geoname") do |element|
177
+
178
+ places << WebService::element_to_toponym( element )
179
+
180
+ end
181
+
182
+ return places
183
+
184
+ end
185
+
186
+ def WebService.timezone( lat, long )
187
+ timezone = Timezone.new
188
+
189
+ url = Geonames::GEONAMES_SERVER + "/timezone?a=a"
190
+
191
+ url = url + "&lat=" + lat.to_s
192
+ url = url + "&lng=" + long.to_s
193
+
194
+ uri = URI.parse(url)
195
+
196
+ req = Net::HTTP::Get.new(uri.path + '?' + uri.query)
197
+
198
+ res = Net::HTTP.start( uri.host, uri.port ) { |http|
199
+ http.request( req )
200
+ }
201
+
202
+ doc = REXML::Document.new res.body
203
+
204
+ doc.elements.each("geonames/timezone") do |element|
205
+ timezone.timezone_id = WebService::get_element_child_text( element, 'timezoneId' )
206
+ timezone.gmt_offset = WebService::get_element_child_float( element, 'gmtOffset' )
207
+ timezone.dst_offset = WebService::get_element_child_float( element, 'dstOffset' )
208
+ end
209
+
210
+ return timezone
211
+
212
+ end
213
+
214
+ def WebService.findNearbyWikipedia( hashes )
215
+ articles = Array.new
216
+
217
+ lat = hashes[:lat]
218
+ long = hashes[:long]
219
+ lang = hashes[:lang]
220
+ radius = hashes[:radius]
221
+ max_rows = hashes[:max_rows]
222
+ country = hashes[:country]
223
+ postalcode = hashes[:postalcode]
224
+ q = hashes[:q]
225
+
226
+ url = Geonames::GEONAMES_SERVER + "/findNearbyWikipedia?a=a"
227
+
228
+ if !lat.nil? && !long.nil?
229
+ url = url + "&lat=" + lat.to_s
230
+ url = url + "&lng=" + long.to_s
231
+ url = url + "&radius=" + radius.to_s unless radius.nil?
232
+ url = url + "&max_rows=" + max_rows.to_s unless max_rows.nil?
233
+
234
+ elsif !q.nil?
235
+ url = url + "&q=" + q
236
+ url = url + "&radius=" + radius.to_s unless radius.nil?
237
+ url = url + "&max_rows=" + max_rows.to_s unless max_rows.nil?
238
+ end
239
+
240
+ uri = URI.parse(url)
241
+
242
+ req = Net::HTTP::Get.new(uri.path + '?' + uri.query)
243
+
244
+ res = Net::HTTP.start( uri.host, uri.port ) { |http|
245
+ http.request( req )
246
+ }
247
+
248
+ doc = REXML::Document.new res.body
249
+
250
+ doc.elements.each("geonames/entry") do |element|
251
+ articles << WebService::element_to_wikipedia_article( element )
252
+ end
253
+
254
+ return articles
255
+
256
+ end
325
257
 
258
+ def WebService.findBoundingBoxWikipedia( hashes )
259
+ articles = Array.new
260
+
261
+ north = hashes[:north]
262
+ east = hashes[:east]
263
+ south = hashes[:south]
264
+ west = hashes[:west]
265
+ lang = hashes[:lang]
266
+ radius = hashes[:radius]
267
+ max_rows = hashes[:max_rows]
268
+ country = hashes[:country]
269
+ postalcode = hashes[:postalcode]
270
+ q = hashes[:q]
271
+
272
+ url = Geonames::GEONAMES_SERVER + "/wikipediaBoundingBox?a=a"
273
+
274
+ url = url + "&north=" + north.to_s
275
+ url = url + "&east=" + east.to_s
276
+ url = url + "&south=" + south.to_s
277
+ url = url + "&west=" + west.to_s
278
+ url = url + "&radius=" + radius.to_s unless radius.nil?
279
+ url = url + "&max_rows=" + max_rows.to_s unless max_rows.nil?
280
+
281
+ uri = URI.parse(url)
282
+
283
+ req = Net::HTTP::Get.new(uri.path + '?' + uri.query)
284
+
285
+ res = Net::HTTP.start( uri.host, uri.port ) { |http|
286
+ http.request( req )
287
+ }
288
+
289
+ doc = REXML::Document.new res.body
290
+
291
+ doc.elements.each("geonames/entry") do |element|
292
+ articles << WebService::element_to_wikipedia_article( element )
293
+ end
294
+
295
+ return articles
296
+
297
+ end
298
+
299
+ def WebService.country_subdivision ( lat, long )
300
+ country_subdivision = CountrySubdivision.new
301
+
302
+ url = Geonames::GEONAMES_SERVER + "/countrySubdivision?a=a"
303
+
304
+ url = url + "&lat=" + lat.to_s
305
+ url = url + "&lng=" + long.to_s
306
+
307
+ uri = URI.parse(url)
308
+
309
+ req = Net::HTTP::Get.new(uri.path + '?' + uri.query)
310
+
311
+ res = Net::HTTP.start( uri.host, uri.port ) { |http|
312
+ http.request( req )
313
+ }
314
+
315
+ doc = REXML::Document.new res.body
316
+
317
+ doc.elements.each("geonames/countrySubdivision") do |element|
318
+ country_subdivision.country_code = WebService::get_element_child_text( element, 'countryCode' )
319
+ country_subdivision.country_name = WebService::get_element_child_text( element, 'countryName' )
320
+ country_subdivision.admin_code_1 = WebService::get_element_child_text( element, 'adminCode1' )
321
+ country_subdivision.admin_name_1 = WebService::get_element_child_text( element, 'adminName1' )
322
+ end
323
+
324
+ return country_subdivision
326
325
 
327
326
  end
327
+
328
+ def WebService.country_code ( lat, long )
329
+
330
+ url = Geonames::GEONAMES_SERVER + "/countrycode?a=a"
331
+
332
+ url = url + "&lat=" + lat.to_s
333
+ url = url + "&lng=" + long.to_s
334
+
335
+ uri = URI.parse(url)
336
+
337
+ req = Net::HTTP::Get.new(uri.path + '?' + uri.query)
338
+
339
+ res = Net::HTTP.start( uri.host, uri.port ) { |http|
340
+ http.request( req )
341
+ }
342
+
343
+ doc = REXML::Document.new res.body
344
+
345
+ return res.body.strip
346
+
347
+ end
348
+
349
+ def WebService.search( search_criteria )
350
+ #toponym search results to return
351
+ toponym_sr = ToponymSearchResult.new
352
+
353
+ url = Geonames::GEONAMES_SERVER + "/search?a=a"
354
+
355
+ if !search_criteria.q.nil?
356
+ url = url + "&q=" + CGI::escape( search_criteria.q )
357
+ end
358
+
359
+ if !search_criteria.name_equals.nil?
360
+ url = url + "&name_equals=" + CGI::escape( search_criteria.name_equals )
361
+ end
362
+
363
+ if !search_criteria.name_starts_with.nil?
364
+ url = url + "&name_startsWith=" + CGI::escape( search_criteria.name_starts_with )
365
+ end
366
+
367
+ if !search_criteria.name.nil?
368
+ url = url + "&name=" + CGI::escape( search_criteria.name )
369
+ end
370
+
371
+ if !search_criteria.tag.nil?
372
+ url = url + "&tag=" + CGI::escape( search_criteria.tag )
373
+ end
374
+
375
+ if !search_criteria.country_code.nil?
376
+ url = url + "&country=" + CGI::escape( search_criteria.country_code )
377
+ end
378
+
379
+ if !search_criteria.admin_code_1.nil?
380
+ url = url + "&adminCode1=" + CGI::escape( search_criteria.admin_code_1 )
381
+ end
382
+
383
+ if !search_criteria.language.nil?
384
+ url = url + "&lang=" + CGI::escape( search_criteria.language )
385
+ end
386
+
387
+ if !search_criteria.feature_class.nil?
388
+ url = url + "&featureClass=" + CGI::escape( search_criteria.feature_class )
389
+ end
390
+
391
+ if !search_criteria.feature_codes.nil?
392
+ for feature_code in search_criteria.feature_codes
393
+ url = url + "&fcode=" + CGI::escape( feature_code )
394
+ end
395
+ end
396
+
397
+ if !search_criteria.max_rows.nil?
398
+ url = url + "&maxRows=" + CGI::escape( search_criteria.max_rows )
399
+ end
400
+
401
+ if !search_criteria.start_row.nil?
402
+ url = url + "&startRow=" + CGI::escape( search_criteria.start_row )
403
+ end
404
+
405
+ if !search_criteria.style.nil?
406
+ url = url + "&style=" + CGI::escape( search_criteria.style )
407
+ end
408
+
409
+ uri = URI.parse(url)
410
+
411
+ req = Net::HTTP::Get.new(uri.path + '?' + uri.query)
412
+
413
+ res = Net::HTTP.start( uri.host, uri.port ) { |http|
414
+ http.request( req )
415
+ }
416
+
417
+ doc = REXML::Document.new res.body
418
+
419
+ toponym_sr.total_results_count = doc.elements["geonames/totalResultsCount"].text
420
+
421
+ doc.elements.each("geonames/geoname") do |element|
422
+
423
+ toponym_sr.toponyms << WebService::element_to_toponym( element )
424
+
425
+ end
426
+
427
+ return toponym_sr
428
+ end
429
+
430
+
431
+ end
328
432
  end
329
433
 
330
434
 
@@ -1,12 +1,12 @@
1
1
  #=============================================================================
2
2
  #
3
- # Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
3
+ # Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
4
4
  #
5
5
  # Licensed under the Apache License, Version 2.0 (the "License"); you may not
6
6
  # use this file except in compliance with the License. You may obtain a copy of
7
- # the License at
7
+ # the License at
8
8
  #
9
- # http://www.apache.org/licenses/LICENSE-2.0
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
10
  #
11
11
  # Unless required by applicable law or agreed to in writing, software
12
12
  # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
@@ -17,9 +17,26 @@
17
17
  #=============================================================================
18
18
 
19
19
  module Geonames
20
- class WikipediaArticle
21
-
22
- end
20
+ class WikipediaArticle
21
+
22
+ attr :language
23
+ attr :title
24
+ attr :summary
25
+ attr :wikipedia_url
26
+ attr :feature
27
+ attr :population
28
+ attr :elevation
29
+ attr :latitude
30
+ attr :longitude
31
+ attr :thumbnail_img
32
+ attr :distance
33
+
34
+ attr_writer :language, :title, :summary
35
+ attr_writer :wikipedia_url, :feature, :population
36
+ attr_writer :elevation, :latitude, :longitude
37
+ attr_writer :thumbnail_img, :distance
38
+
39
+ end
23
40
  end
24
41
 
25
42
 
metadata CHANGED
@@ -3,13 +3,13 @@ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: geonames
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.0
7
- date: 2007-02-25 00:00:00 -05:00
6
+ version: 0.2.0
7
+ date: 2007-04-01 00:00:00 -04:00
8
8
  summary: Ruby library for Geonames Web Services (http://www.geonames.org/export/)
9
9
  require_paths:
10
10
  - lib
11
11
  email: adamw@tbcn.ca
12
- homepage: http://www.tbcn.ca/geonames
12
+ homepage: http://www.tbcn.ca/ruby_geonames
13
13
  rubyforge_project:
14
14
  description:
15
15
  autorequire:
@@ -29,6 +29,8 @@ post_install_message:
29
29
  authors:
30
30
  - Adam Wisniewski
31
31
  files:
32
+ - geonames-0.1.0.gem
33
+ - geonames-0.2.0.gem
32
34
  - geonames.gemspec
33
35
  - lib
34
36
  - nbproject