marco-ruby-geonames 0.2.7

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.
@@ -0,0 +1,56 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
+
3
+ require 'geonames'
4
+ require 'test/unit'
5
+
6
+ class TestCountryInfo < Test::Unit::TestCase
7
+ def setup
8
+ @test_code = "TH"
9
+
10
+ @expected =
11
+ {:country_code => 'TH', :country_name => 'Thailand', :iso_numeric => 764,
12
+ :iso_alpha_3 => 'THA', :fips_code => 'TH', :continent => 'AS',
13
+ :capital => 'Bangkok', :area_sq_km => 514000.0, :population => 65493000,
14
+ :currency_code => 'THB', :geonameId => 1605651,
15
+ :languages => ['th', 'en'],
16
+ :box_north => 20.4631977081299, :box_south => 5.60999917984009,
17
+ :box_east => 105.63939666748, :box_west => 97.3456268310547}
18
+ end
19
+
20
+ def teardown
21
+ #nothing here really
22
+ end
23
+
24
+ def test_simple_values
25
+ info = Geonames::WebService.country_info(@test_code)
26
+
27
+ assert_equal(@expected[:country_code], info.country_code)
28
+ assert_equal(@expected[:country_name], info.country_name)
29
+ assert_equal(@expected[:iso_numeric], info.iso_numeric)
30
+ assert_equal(@expected[:iso_alpha_3], info.iso_alpha_3)
31
+ assert_equal(@expected[:fips_code], info.fips_code)
32
+ assert_equal(@expected[:continent], info.continent)
33
+ assert_equal(@expected[:capital], info.capital)
34
+ assert_equal(@expected[:area_sq_km], info.area_sq_km)
35
+ assert_equal(@expected[:population], info.population)
36
+ assert_equal(@expected[:currency_code], info.currency_code)
37
+ assert_equal(@expected[:geonameId], info.geoname_id )
38
+ end
39
+
40
+ def test_languages
41
+ info = Geonames::WebService.country_info(@test_code)
42
+
43
+ assert_equal(2, info.languages.size)
44
+ assert_equal(@expected[:languages][0], info.languages[0])
45
+ assert_equal(@expected[:languages][1], info.languages[1])
46
+ end
47
+
48
+ def test_bounding_box
49
+ info = Geonames::WebService.country_info(@test_code)
50
+
51
+ assert_equal(@expected[:box_north], info.bounding_box.north_point)
52
+ assert_equal(@expected[:box_south], info.bounding_box.south_point)
53
+ assert_equal(@expected[:box_east], info.bounding_box.east_point)
54
+ assert_equal(@expected[:box_west], info.bounding_box.west_point)
55
+ end
56
+ end
@@ -0,0 +1,33 @@
1
+ #=============================================================================
2
+ #
3
+ # Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may not
6
+ # use this file except in compliance with the License. You may obtain a copy of
7
+ # the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
+ # License for the specific language governing permissions and limitations under
15
+ # the License.
16
+ #
17
+ #=============================================================================
18
+
19
+ module Geonames
20
+ class Timezone
21
+ attr :timezone_id
22
+ attr :gmt_offset
23
+ attr :dst_offset
24
+
25
+ attr_writer :timezone_id, :gmt_offset, :dst_offset
26
+
27
+ def tzinfo
28
+ TZInfo::Timezone.get(timezone_id)
29
+ end
30
+ end
31
+ end
32
+
33
+
@@ -0,0 +1,50 @@
1
+ #=============================================================================
2
+ #
3
+ # Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may not
6
+ # use this file except in compliance with the License. You may obtain a copy of
7
+ # the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
+ # License for the specific language governing permissions and limitations under
15
+ # the License.
16
+ #
17
+ #=============================================================================
18
+
19
+ module Geonames
20
+ class Toponym
21
+
22
+ attr :geoname_id
23
+ attr :name
24
+ attr :alternate_names
25
+ attr :country_code
26
+ attr :country_name
27
+ attr :population
28
+ attr :elevation
29
+ attr :feature_class
30
+ attr :feature_class_name
31
+ attr :feature_code
32
+ attr :feature_code_name
33
+ attr :latitude
34
+ attr :longitude
35
+ attr :distance
36
+ attr :admin_code_1
37
+ attr :admin_code_2
38
+ attr :admin_name_1
39
+ attr :admin_name_2
40
+
41
+ attr_writer :geoname_id, :name, :alternate_names, :country_code
42
+ attr_writer :country_name, :population, :elevation, :feature_class
43
+ attr_writer :feature_class_name, :feature_code,:feature_code_name
44
+ attr_writer :latitude, :longitude, :distance
45
+ attr_writer :admin_code_1, :admin_code_2, :admin_name_1, :admin_name_2
46
+
47
+ end
48
+ end
49
+
50
+
@@ -0,0 +1,44 @@
1
+ #=============================================================================
2
+ #
3
+ # Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may not
6
+ # use this file except in compliance with the License. You may obtain a copy of
7
+ # the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
+ # License for the specific language governing permissions and limitations under
15
+ # the License.
16
+ #
17
+ #=============================================================================
18
+
19
+ module Geonames
20
+ class ToponymSearchCriteria
21
+
22
+ attr :q
23
+ attr :country_code
24
+ attr :name
25
+ attr :name_equals
26
+ attr :name_starts_with
27
+ attr :tag
28
+ attr :language
29
+ attr :style
30
+ attr :feature_class
31
+ attr :feature_codes
32
+ attr :admin_code_1
33
+ attr :max_rows
34
+ attr :start_row
35
+
36
+ attr_writer :q, :country_code, :name, :name_equals
37
+ attr_writer :name_starts_with, :tag, :language, :style
38
+ attr_writer :feature_class, :feature_codes, :admin_code_1
39
+ attr_writer :max_rows, :start_row
40
+
41
+ end
42
+ end
43
+
44
+
@@ -0,0 +1,33 @@
1
+ #=============================================================================
2
+ #
3
+ # Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may not
6
+ # use this file except in compliance with the License. You may obtain a copy of
7
+ # the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
+ # License for the specific language governing permissions and limitations under
15
+ # the License.
16
+ #
17
+ #=============================================================================
18
+
19
+ module Geonames
20
+ class ToponymSearchResult
21
+
22
+ attr :total_results_count
23
+ attr :toponyms
24
+
25
+ attr_writer :total_results_count, :toponyms
26
+
27
+ def initialize
28
+ @toponyms = Array.new
29
+ end
30
+ end
31
+ end
32
+
33
+
@@ -0,0 +1,513 @@
1
+ #=============================================================================
2
+ #
3
+ # Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
4
+ # Contributions by Andrew Turner, High Earth Orbit
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may not
7
+ # use this file except in compliance with the License. You may obtain a copy of
8
+ # the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
+ # License for the specific language governing permissions and limitations under
16
+ # the License.
17
+ #
18
+ #=============================================================================
19
+
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
33
+
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, 'lng' )
50
+ postal_code.latitude = WebService::get_element_child_float( element, 'lat' )
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
55
+
56
+ end
57
+
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
74
+
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
+ toponym.admin_code_1 = WebService::get_element_child_text( element, 'adminCode1' )
95
+ toponym.admin_code_2 = WebService::get_element_child_text( element, 'adminCode2' )
96
+ toponym.admin_name_1 = WebService::get_element_child_text( element, 'adminName1' )
97
+ toponym.admin_name_2 = WebService::get_element_child_text( element, 'adminName2' )
98
+
99
+ return toponym
100
+
101
+ end
102
+
103
+ def WebService.element_to_intersection ( element )
104
+ intersection = Intersection.new
105
+
106
+ intersection.street_1 = WebService::get_element_child_text( element, 'street1' )
107
+ intersection.street_2 = WebService::get_element_child_text( element, 'street2' )
108
+ intersection.admin_code_1 = WebService::get_element_child_text( element, 'adminCode1' )
109
+ intersection.admin_code_1 = WebService::get_element_child_text( element, 'adminCode1' )
110
+ intersection.admin_code_2 = WebService::get_element_child_text( element, 'adminCode2' )
111
+ intersection.admin_name_1 = WebService::get_element_child_text( element, 'adminName1' )
112
+ intersection.admin_name_2 = WebService::get_element_child_text( element, 'adminName2' )
113
+ intersection.country_code = WebService::get_element_child_text( element, 'countryCode' )
114
+ intersection.distance = WebService::get_element_child_float( element, 'distance' )
115
+ intersection.longitude = WebService::get_element_child_float( element, 'lat' )
116
+ intersection.latitude = WebService::get_element_child_float( element, 'lng' )
117
+ intersection.place_name = WebService::get_element_child_text( element, 'name' )
118
+ intersection.postal_code = WebService::get_element_child_text( element, 'postalcode' )
119
+
120
+ return intersection
121
+
122
+ end
123
+
124
+ def WebService.element_to_country_info(element)
125
+ country_info = Geonames::CountryInfo.new
126
+ country_info.country_code = WebService.get_element_child_text(element, 'countryCode')
127
+ country_info.country_name = WebService.get_element_child_text(element, 'countryName')
128
+ country_info.iso_numeric = WebService.get_element_child_int(element, 'isoNumeric')
129
+ country_info.iso_alpha_3 = WebService.get_element_child_text(element, 'isoAlpha3')
130
+ country_info.fips_code = WebService.get_element_child_text(element, 'fipsCode')
131
+ country_info.continent = WebService.get_element_child_text(element, 'continent')
132
+ country_info.capital = WebService.get_element_child_text(element, 'capital')
133
+ country_info.area_sq_km = WebService.get_element_child_float(element, 'areaInSqKm')
134
+ country_info.population = WebService.get_element_child_int(element, 'population')
135
+ country_info.currency_code = WebService.get_element_child_text(element, 'currencyCode')
136
+ #actually an array of the available languages
137
+ country_info.languages = WebService.get_element_child_text(element, 'languages').split(",")
138
+ country_info.geoname_id = WebService.get_element_child_int(element, 'geonameId')
139
+
140
+ north = WebService.get_element_child_float(element, 'bBoxNorth')
141
+ south = WebService.get_element_child_float(element, 'bBoxSouth')
142
+ east = WebService.get_element_child_float(element, 'bBoxEast')
143
+ west = WebService.get_element_child_float(element, 'bBoxWest')
144
+
145
+ country_info.set_bounding_box(north, south, east, west)
146
+
147
+ return country_info
148
+ end
149
+
150
+ def WebService.postal_code_search( postal_code, place_name, country_code,*args )
151
+ postal_code_sc = PostalCodeSearchCriteria.new
152
+ postal_code_sc.postal_code = postal_code
153
+ postal_code_sc.place_name = place_name
154
+ postal_code_sc.country_code = country_code
155
+
156
+ WebService.postal_code_search postal_code_sc, args
157
+ end
158
+
159
+ def WebService.postal_code_search( search_criteria, *args )
160
+ # postal codes to reutrn
161
+ postal_codes = Array.new
162
+
163
+ url = "/postalCodeSearch?a=a"
164
+ url = url + search_criteria.to_query_params_string
165
+
166
+ res = make_request(url,args)
167
+
168
+ doc = REXML::Document.new res.body
169
+
170
+ doc.elements.each("geonames/code") do |element|
171
+ postal_codes << WebService::element_to_postal_code( element )
172
+ end
173
+
174
+ postal_codes
175
+
176
+ end
177
+
178
+ def WebService.find_nearby_postal_codes( search_criteria )
179
+ # postal codes to reutrn
180
+ postal_codes = Array.new
181
+
182
+ url = "/findNearbyPostalCodes?a=a"
183
+ url = url + search_criteria.to_query_params_string
184
+
185
+ res = make_request(url)
186
+
187
+ doc = REXML::Document.new res.body
188
+
189
+ doc.elements.each("geonames/code") do |element|
190
+ postal_codes << WebService::element_to_postal_code( element )
191
+ end
192
+
193
+ postal_codes
194
+
195
+ end
196
+
197
+ def WebService.find_nearby_place_name( lat, long )
198
+ places = Array.new
199
+
200
+ url = "/findNearbyPlaceName?a=a"
201
+
202
+ url = url + "&lat=" + lat.to_s
203
+ url = url + "&lng=" + long.to_s
204
+
205
+ res = make_request(url)
206
+
207
+ doc = REXML::Document.new res.body
208
+
209
+ doc.elements.each("geonames/geoname") do |element|
210
+
211
+ places << WebService::element_to_toponym( element )
212
+
213
+ end
214
+
215
+ return places
216
+
217
+ end
218
+
219
+ def WebService.find_nearest_intersection( lat, long )
220
+
221
+ url = "/findNearestIntersection?a=a"
222
+
223
+ url = url + "&lat=" + lat.to_s
224
+ url = url + "&lng=" + long.to_s
225
+
226
+ res = make_request(url)
227
+
228
+ doc = REXML::Document.new res.body
229
+
230
+ intersection = []
231
+ doc.elements.each("geonames/intersection") do |element|
232
+
233
+ intersection = WebService::element_to_intersection( element )
234
+
235
+ end
236
+
237
+ return intersection
238
+
239
+ end
240
+
241
+ def WebService.timezone( lat, long, *args )
242
+ res = make_request("/timezone?lat=#{lat.to_s}&lng=#{long.to_s}", args)
243
+ doc = REXML::Document.new res.body
244
+ timezone = Timezone.new
245
+ doc.elements.each("geonames/timezone") do |element|
246
+ timezone.timezone_id = WebService::get_element_child_text( element, 'timezoneId' )
247
+ timezone.gmt_offset = WebService::get_element_child_float( element, 'gmtOffset' )
248
+ timezone.dst_offset = WebService::get_element_child_float( element, 'dstOffset' )
249
+ end
250
+ timezone
251
+ end
252
+
253
+ def WebService.make_request(path_and_query, *args)
254
+ url = Geonames.base_url + path_and_query
255
+ url += "&username=#{Geonames.username}" if Geonames.username
256
+ url += "&lang=#{Geonames.lang}"
257
+ options = {
258
+ :open_timeout => 60,
259
+ :read_timeout => 60
260
+ }
261
+ options.update(args.last.is_a?(::Hash) ? args.pop : {})
262
+ uri = URI.parse(url)
263
+ req = Net::HTTP::Get.new(uri.path + '?' + uri.query)
264
+ Net::HTTP.start(uri.host, uri.port) { |http|
265
+ http.read_timeout = options[:read_timeout]
266
+ http.open_timeout = options[:open_timeout]
267
+ http.request(req)
268
+ }
269
+ end
270
+
271
+ def WebService.findNearbyWikipedia( hashes )
272
+ # here for backwards compatibility
273
+ WebService.find_nearby_wikipedia( hashes )
274
+ end
275
+
276
+ def WebService.find_nearby_wikipedia( hashes )
277
+ articles = Array.new
278
+
279
+ lat = hashes[:lat]
280
+ long = hashes[:long]
281
+ lang = hashes[:lang]
282
+ radius = hashes[:radius]
283
+ max_rows = hashes[:max_rows]
284
+ country = hashes[:country]
285
+ postalcode = hashes[:postalcode]
286
+ q = hashes[:q]
287
+
288
+ url = "/findNearbyWikipedia?a=a"
289
+
290
+ if !lat.nil? && !long.nil?
291
+ url = url + "&lat=" + lat.to_s
292
+ url = url + "&lng=" + long.to_s
293
+ url = url + "&radius=" + radius.to_s unless radius.nil?
294
+ url = url + "&max_rows=" + max_rows.to_s unless max_rows.nil?
295
+
296
+ elsif !q.nil?
297
+ url = url + "&q=" + q
298
+ url = url + "&radius=" + radius.to_s unless radius.nil?
299
+ url = url + "&max_rows=" + max_rows.to_s unless max_rows.nil?
300
+ end
301
+
302
+ res = make_request(url)
303
+
304
+ doc = REXML::Document.new res.body
305
+
306
+ doc.elements.each("geonames/entry") do |element|
307
+ articles << WebService::element_to_wikipedia_article( element )
308
+ end
309
+
310
+ return articles
311
+
312
+ end
313
+
314
+ def WebService.findBoundingBoxWikipedia( hashes )
315
+ # here for backwards compatibility
316
+ WebService.find_bounding_box_wikipedia( hashes )
317
+ end
318
+
319
+ def WebService.find_bounding_box_wikipedia( hashes )
320
+ articles = Array.new
321
+
322
+ north = hashes[:north]
323
+ east = hashes[:east]
324
+ south = hashes[:south]
325
+ west = hashes[:west]
326
+ lang = hashes[:lang]
327
+ radius = hashes[:radius]
328
+ max_rows = hashes[:max_rows]
329
+ country = hashes[:country]
330
+ postalcode = hashes[:postalcode]
331
+ q = hashes[:q]
332
+
333
+ url = "/wikipediaBoundingBox?a=a"
334
+
335
+ url = url + "&north=" + north.to_s
336
+ url = url + "&east=" + east.to_s
337
+ url = url + "&south=" + south.to_s
338
+ url = url + "&west=" + west.to_s
339
+ url = url + "&radius=" + radius.to_s unless radius.nil?
340
+ url = url + "&max_rows=" + max_rows.to_s unless max_rows.nil?
341
+
342
+ res = make_request(url)
343
+
344
+ doc = REXML::Document.new res.body
345
+
346
+ doc.elements.each("geonames/entry") do |element|
347
+ articles << WebService::element_to_wikipedia_article( element )
348
+ end
349
+
350
+ return articles
351
+
352
+ end
353
+
354
+ def WebService.country_subdivision ( lat, long, radius = 0, maxRows = 1 )
355
+
356
+ country_subdivisions = Array.new
357
+
358
+ # maxRows is only implemented in the xml version:
359
+ # http://groups.google.com/group/geonames/browse_thread/thread/f7f1bb2504ed216e
360
+ # Therefore 'type=xml' is added:
361
+ url = "/countrySubdivision?a=a&type=xml"
362
+
363
+ url = url + "&lat=" + lat.to_s
364
+ url = url + "&lng=" + long.to_s
365
+ url = url + "&maxRows=" + maxRows.to_s
366
+ url = url + "&radius=" + radius.to_s
367
+
368
+ res = make_request(url)
369
+
370
+ doc = REXML::Document.new res.body
371
+
372
+ doc.elements.each("geonames/countrySubdivision") do |element|
373
+
374
+ country_subdivision = CountrySubdivision.new
375
+
376
+ country_subdivision.country_code = WebService::get_element_child_text( element, 'countryCode' )
377
+ country_subdivision.country_name = WebService::get_element_child_text( element, 'countryName' )
378
+ country_subdivision.admin_code_1 = WebService::get_element_child_text( element, 'adminCode1' )
379
+ country_subdivision.admin_name_1 = WebService::get_element_child_text( element, 'adminName1' )
380
+ country_subdivision.code_fips = WebService::get_element_child_text( element, 'code[@type="FIPS10-4"]')
381
+ country_subdivision.code_iso = WebService::get_element_child_text( element, 'code[@type="ISO3166-2"]')
382
+
383
+ country_subdivisions << country_subdivision
384
+
385
+ end
386
+
387
+ return country_subdivisions
388
+
389
+ end
390
+
391
+ def WebService.country_info(country_code = false)
392
+ url = "/countryInfo?a=a"
393
+
394
+ url += "&country=#{country_code.to_s}" if country_code
395
+ res = make_request(url)
396
+
397
+ doc = REXML::Document.new res.body
398
+
399
+ countries = Array.new
400
+
401
+ doc.elements.each("geonames/country") do |element|
402
+ countries << WebService::element_to_country_info( element )
403
+ end
404
+ countries.size > 1 ? countries : countries[0]
405
+ end
406
+
407
+ def WebService.country_code ( lat, long, radius = 0, maxRows = 1 )
408
+ # maxRows is only implemented in the xml version:
409
+ # http://groups.google.com/group/geonames/browse_thread/thread/f7f1bb2504ed216e
410
+ # Therefore 'type=xml' is added:
411
+ url = "/countrycode?a=a&type=xml"
412
+
413
+ countries = Array.new
414
+
415
+ url = url + "&lat=" + lat.to_s
416
+ url = url + "&lng=" + long.to_s
417
+ url = url + "&maxRows=" + maxRows.to_s
418
+ url = url + "&radius=" + radius.to_s
419
+
420
+ res = make_request(url)
421
+
422
+ doc = REXML::Document.new res.body
423
+
424
+ doc.elements.each("geonames/country") do |element|
425
+
426
+ countries << WebService::element_to_toponym( element )
427
+
428
+ end
429
+
430
+ return countries
431
+
432
+ end
433
+
434
+ def WebService.search( search_criteria )
435
+ #toponym search results to return
436
+ toponym_sr = ToponymSearchResult.new
437
+
438
+ url = "/search?a=a"
439
+
440
+ if !search_criteria.q.nil?
441
+ url = url + "&q=" + CGI::escape( search_criteria.q )
442
+ end
443
+
444
+ if !search_criteria.name_equals.nil?
445
+ url = url + "&name_equals=" + CGI::escape( search_criteria.name_equals )
446
+ end
447
+
448
+ if !search_criteria.name_starts_with.nil?
449
+ url = url + "&name_startsWith=" + CGI::escape( search_criteria.name_starts_with )
450
+ end
451
+
452
+ if !search_criteria.name.nil?
453
+ url = url + "&name=" + CGI::escape( search_criteria.name )
454
+ end
455
+
456
+ if !search_criteria.tag.nil?
457
+ url = url + "&tag=" + CGI::escape( search_criteria.tag )
458
+ end
459
+
460
+ if !search_criteria.country_code.nil?
461
+ url = url + "&country=" + CGI::escape( search_criteria.country_code )
462
+ end
463
+
464
+ if !search_criteria.admin_code_1.nil?
465
+ url = url + "&adminCode1=" + CGI::escape( search_criteria.admin_code_1 )
466
+ end
467
+
468
+ if !search_criteria.language.nil?
469
+ url = url + "&lang=" + CGI::escape( search_criteria.language )
470
+ end
471
+
472
+ if !search_criteria.feature_class.nil?
473
+ url = url + "&featureClass=" + CGI::escape( search_criteria.feature_class )
474
+ end
475
+
476
+ if !search_criteria.feature_codes.nil?
477
+ for feature_code in search_criteria.feature_codes
478
+ url = url + "&fcode=" + CGI::escape( feature_code )
479
+ end
480
+ end
481
+
482
+ if !search_criteria.max_rows.nil?
483
+ url = url + "&maxRows=" + CGI::escape( search_criteria.max_rows )
484
+ end
485
+
486
+ if !search_criteria.start_row.nil?
487
+ url = url + "&startRow=" + CGI::escape( search_criteria.start_row )
488
+ end
489
+
490
+ if !search_criteria.style.nil?
491
+ url = url + "&style=" + CGI::escape( search_criteria.style )
492
+ end
493
+
494
+ res = make_request(url)
495
+
496
+ doc = REXML::Document.new res.body
497
+
498
+ toponym_sr.total_results_count = doc.elements["geonames/totalResultsCount"].text
499
+
500
+ doc.elements.each("geonames/geoname") do |element|
501
+
502
+ toponym_sr.toponyms << WebService::element_to_toponym( element )
503
+
504
+ end
505
+
506
+ return toponym_sr
507
+ end
508
+
509
+ end
510
+ end
511
+
512
+ #alias WebService.find_nearby_wikipedia findNearbyWikipedia
513
+ #alias find_bounding_box_wikipedia findBoundingBoxWikipedia